File Coverage

blib/lib/Mojo/Hakkefuin.pm
Criterion Covered Total %
statement 43 51 84.3
branch 10 26 38.4
condition 2 4 50.0
subroutine 9 10 90.0
pod 3 3 100.0
total 67 94 71.2


line stmt bran cond sub pod time code
1             package Mojo::Hakkefuin;
2 4     4   27 use Mojo::Base -base;
  4         10  
  4         36  
3              
4 4     4   1155 use Carp 'croak';
  4         23  
  4         238  
5 4     4   38 use Mojo::File qw(path);
  4         8  
  4         206  
6 4     4   20 use Mojo::Loader 'load_class';
  4         7  
  4         187  
7 4     4   3429 use String::Random;
  4         15180  
  4         279  
8 4     4   1953 use CellBIS::SQL::Abstract;
  4         58987  
  4         28  
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 4     4 1 9 my $self = shift;
23              
24 4         14 my $backend = $self->backend;
25 4         35 my $file_migration = $backend->file_migration;
26 4 100       24 unless (-d $self->dir) { mkdir $self->dir }
  1         75  
27 4 50       464 unless (-f $file_migration) {
28 4 50       21 path($file_migration)->spurt($backend->table_query)
29             if ($self->table_config);
30             }
31 4         32 return $self;
32             }
33              
34             sub check_migration {
35 4     4 1 9 my $self = shift;
36              
37 4         12 my $backend = $self->backend;
38 4         40 my $check = $backend->check_table;
39 4 50       857 unless ($check->{result}) {
40             croak "Can't create table database"
41 4 50       26 unless $backend->create_table->{code} == 200;
42             }
43 4         905 return $self;
44             }
45              
46             sub new {
47 4     4 1 67 my $self = shift->SUPER::new(@_);
48              
49 4   50     65 $self->{via} //= 'sqlite';
50 4   50     18 $self->{dir} //= 'migrations';
51 4 50       24 $self->via('mariadb') if $self->via eq 'mysql';
52              
53             # Params for backend
54             my @param
55             = $self->table_config
56 4 50       44 ? (dir => $self->dir, %{$self->table_config})
  0         0  
57             : (dir => $self->dir);
58 4 50       42 push @param, dsn => $self->dsn if $self->dsn;
59              
60             # Load class backend
61 4         30 my $class = 'Mojo::Hakkefuin::Backend::' . $self->via;
62 4         26 my $load = load_class $class;
63 4 0       125 croak ref $load ? $load : qq{Backend "$class" missing} if $load;
    50          
64 4         39 $self->backend($class->new(@param));
65              
66 4         55 return $self;
67             }
68              
69             sub _check_migration_file {
70 0     0     my $self = shift;
71              
72 0           my $loc_file = $self->dir . $self->backend()->file_migration;
73 0 0         if (-f $loc_file) {
74 0 0         return $self unless $self->backend()->table()->{result};
75             }
76             else {
77 0           my $content_file = $self->backend()->table_query();
78 0           path($loc_file)->spurt($content_file);
79 0 0         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
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. By defaults
114             using L as backend storage.
115              
116             =head1 ATTRIBUTES
117              
118             L inherits all attributes from
119             L 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.
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.
140              
141             =head2 dir
142              
143             $mhf->dir;
144             $mhf->dir('for/specific/dir/name');
145              
146             This attribute by default contains C
147             and is automatically created and saved to the application directory.
148             Specify the migration storage directory for L
149             configuration file. If the backend storage uses C, then the file
150             will be saved into a directory set via this attribute.
151              
152             =head1 METHODS
153              
154             L inherits all methods from
155             L 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 object either from L.
175              
176             =head1 SEE ALSO
177              
178             =over 2
179              
180             =item * L
181              
182             =item * L
183              
184             =item * L
185              
186             =item * L
187              
188             =item * L
189              
190             =item * L
191              
192             =item * L
193              
194             =back
195              
196             =cut