| 1 | package Mojo::Hakkefuin; | ||
| 2 | use Mojo::Base -base; | ||
| 3 | |||
| 4 | use Carp 'croak'; | ||
| 5 | use Mojo::File qw(path); | ||
| 6 | use Mojo::Loader 'load_class'; | ||
| 7 | use String::Random; | ||
| 8 | use CellBIS::SQL::Abstract; | ||
| 9 | |||
| 10 | # Attributes | ||
| 11 | has random => sub { String::Random->new }; | ||
| 12 | has 'via'; | ||
| 13 | has 'dsn'; | ||
| 14 | has 'dir'; | ||
| 15 | has 'table_config'; # not implemented yet. | ||
| 16 | |||
| 17 | # Internal Attributes | ||
| 18 | has 'backend'; | ||
| 19 | has 'migration_status' => 0; | ||
| 20 | |||
| 21 | sub check_file_migration { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | my $backend = $self->backend; | ||
| 25 | my $file_migration = $backend->file_migration; | ||
| 26 | unless (-d $self->dir) { mkdir $self->dir } | ||
| 27 | unless (-f $file_migration) { | ||
| 28 | path($file_migration)->spurt($backend->table_query) | ||
| 29 | if ($self->table_config); | ||
| 30 | } | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub check_migration { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | my $backend = $self->backend; | ||
| 38 | my $check = $backend->check_table; | ||
| 39 | unless ($check->{result}) { | ||
| 40 | croak "Can't create table database" | ||
| 41 | unless $backend->create_table->{code} == 200; | ||
| 42 | } | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my $self = shift->SUPER::new(@_); | ||
| 48 | |||
| 49 | $self->{via} //= 'sqlite'; | ||
| 50 | $self->{dir} //= 'migrations'; | ||
| 51 | $self->via('mariadb') if $self->via eq 'mysql'; | ||
| 52 | |||
| 53 | # Params for backend | ||
| 54 | my @param | ||
| 55 | = $self->table_config | ||
| 56 | ? (dir => $self->dir, %{$self->table_config}) | ||
| 57 | : (dir => $self->dir); | ||
| 58 | push @param, dsn => $self->dsn if $self->dsn; | ||
| 59 | |||
| 60 | # Load class backend | ||
| 61 | my $class = 'Mojo::Hakkefuin::Backend::' . $self->via; | ||
| 62 | my $load = load_class $class; | ||
| 63 | croak ref $load ? $load : qq{Backend "$class" missing} if $load; | ||
| 64 | $self->backend($class->new(@param)); | ||
| 65 | |||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _check_migration_file { | ||
| 70 | my $self = shift; | ||
| 71 | |||
| 72 | my $loc_file = $self->dir . $self->backend()->file_migration; | ||
| 73 | if (-f $loc_file) { | ||
| 74 | return $self unless $self->backend()->table()->{result}; | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | my $content_file = $self->backend()->table_query(); | ||
| 78 | path($loc_file)->spurt($content_file); | ||
| 79 | return $self unless $self->backend()->table()->{result}; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | =encoding utf8 | ||
| 86 | |||
| 87 | =head1 NAME | ||
| 88 | |||
| 89 | Mojo::Hakkefuin - Abstraction for L<Mojolicious::Plugin::Hakkefuin> | ||
| 90 | |||
| 91 | =head1 SYNOPSIS | ||
| 92 | |||
| 93 | use Mojo::Hakkefuin; | ||
| 94 | |||
| 95 | # SQLite as backend | ||
| 96 | my $mhf = Mojo::Hakkefuin->new; | ||
| 97 | my $mhf = Mojo::Hakkefuin->new({ dir => '/path/location/migrations' }); | ||
| 98 | |||
| 99 | # MariaDB/MySQL as backend | ||
| 100 | my $mhf = Mojo::Hakkefuin->new({ | ||
| 101 | via => 'mariadb', | ||
| 102 | dsn => 'mariadb://username:password@hostname/database' | ||
| 103 | }); | ||
| 104 | |||
| 105 | # PostgreSQL as backend | ||
| 106 | my $mhf = Mojo::Hakkefuin->new({ | ||
| 107 | via => 'pg', | ||
| 108 | dsn => 'postgresql://username:password@hostname/database' | ||
| 109 | }); | ||
| 110 | |||
| 111 | =head1 DESCRIPTION | ||
| 112 | |||
| 113 | General abstraction for L<Mojolicious::Plugin:::Hakkefuin>. By defaults | ||
| 114 | using L<Mojo::SQLite> as backend storage. | ||
| 115 | |||
| 116 | =head1 ATTRIBUTES | ||
| 117 | |||
| 118 | L<Mojo::Hakkefuin> inherits all attributes from | ||
| 119 | L<Mojo::Base> and implements the following new ones. | ||
| 120 | |||
| 121 | =head2 via | ||
| 122 | |||
| 123 | $mhf->via; | ||
| 124 | $mhf->via('mariadb'); | ||
| 125 | $mhf->via('sqlite'); | ||
| 126 | $mhf->via('pg'); | ||
| 127 | |||
| 128 | Specify of backend via MariaDB/MySQL or SQLite or PostgreSQL. | ||
| 129 | This attribute by default contains C<sqlite>. | ||
| 130 | |||
| 131 | =head2 dsn | ||
| 132 | |||
| 133 | $mhf->dsn; | ||
| 134 | $mhf->dsn('mariadb://username:password@hostname/database'); | ||
| 135 | $mhf->dsn('postgresql://username:password@hostname/database'); | ||
| 136 | |||
| 137 | Determine DSN (Data Source Name) based on the DBMS that will be used. | ||
| 138 | E.g. MariaDB/MySQL or PostgreSQL. Especially for SQLite, | ||
| 139 | you don't need to use C<dsn>. | ||
| 140 | |||
| 141 | =head2 dir | ||
| 142 | |||
| 143 | $mhf->dir; | ||
| 144 | $mhf->dir('for/specific/dir/name'); | ||
| 145 | |||
| 146 | This attribute by default contains C<migrations> | ||
| 147 | and is automatically created and saved to the application directory. | ||
| 148 | Specify the migration storage directory for L<Mojo::Hakkefuin> | ||
| 149 | configuration file. If the backend storage uses C<sqlite>, then the file | ||
| 150 | will be saved into a directory set via this attribute. | ||
| 151 | |||
| 152 | =head1 METHODS | ||
| 153 | |||
| 154 | L<Mojo::Hakkefuin> inherits all methods from | ||
| 155 | L<Mojo::Base> and implements the following new ones. | ||
| 156 | |||
| 157 | =head2 check_file_migration | ||
| 158 | |||
| 159 | $mhf->check_file_migration(); | ||
| 160 | |||
| 161 | Checking file migration on your application directory. | ||
| 162 | |||
| 163 | =head2 check_migration | ||
| 164 | |||
| 165 | $mhf->check_migration(); | ||
| 166 | |||
| 167 | Checking migration database storage. | ||
| 168 | |||
| 169 | =head2 new() | ||
| 170 | |||
| 171 | my $mhf = Mojo::Hakkefuin->new; | ||
| 172 | my $mhf = Mojo::Hakkefuin->new(\%attr); | ||
| 173 | |||
| 174 | Construct a new L<Mojo::Hakkefuin> object either from L</ATTRIBUTES>. | ||
| 175 | |||
| 176 | =head1 SEE ALSO | ||
| 177 | |||
| 178 | =over 2 | ||
| 179 | |||
| 180 | =item * L<Mojolicious::Plugin::Hakkefuin> | ||
| 181 | |||
| 182 | =item * L<Mojo::Hakkefuin> | ||
| 183 | |||
| 184 | =item * L<Mojo::mysql> | ||
| 185 | |||
| 186 | =item * L<Mojo::Pg> | ||
| 187 | |||
| 188 | =item * L<Mojo::SQLite> | ||
| 189 | |||
| 190 | =item * L<Mojolicious::Guides> | ||
| 191 | |||
| 192 | =item * L<https://mojolicious.org> | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | =cut |