blib/lib/DBIx/Schema/Migration.pm

filter: Statementstmt 87.0% 128 / 147 Branchbran 59.5% 25 / 42 Conditioncond 27.5% 8 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 95.8% 23 / 24 Podpod 66.6% 2 / 3 totaltotal 71.2% 186 / 261 SCAR 39.6
CC
33
Cov
74%
up
10.8
down
7.6
__ANON__
5.2
CRAP 52.5
↑ Summary
1 package DBIx::Schema::Migration;
2  
3 1 ▶use 5.24.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Term::ANSIColor 'colored';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Slurper 'read_text';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 use constant {
19 1 UP => 'up',
20 DOWN => 'down',
21 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 our $VERSION = '1.00';
24  
25 has dbh => (
26 is => 'ro',
27 requires => 1,
28 isa => sub {
29 1 ▶ if ( not blessed $_[0]
Subroutine __ANON__: called CC=3 SCAR=16.5
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($_[0]) || $_[0]->isa("DBI::db")) 0 1 1
Condition33%1/3
builtin::blessed($_[0]) || $_[0]->isa('DBI::db')
l!l&&r!l&&!r
100
MC/DC0%0/2
builtin::blessed($_[0]) || $_[0]->isa('DBI::db')
builtin::blessed($_[0]) $_[0]->isa('DBI::db')
Truth table33%1/3
A || B
A= builtin::blessed($_[0])B= $_[0]->isa('DBI::db')
ABresult
1X1
011
000
30 and not $_[0]->isa('DBI::db') )
31 {
32 0 say colored( "$_[0] is not DBI::db", 'red' );
33 0 exit;
34 }
35 },
36 1 );
37  
38 1 has dir => (
39 is => 'ro',
40 required => 1,
41 );
42  
43 sub init {
44 1 ▶ my ($self) = @_;
Subroutine init: called CC=4 SCAR=16.3
Pod undocumented
45  
46 1 my @sql = <DATA>;
47 1 my $sql = join '', @sql;
48  
49 1 ▶ if ( $self->_is_applied_migrations_table_exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_applied_migrations_table_exists) { } 0 1 1
50 0 say colored( 'Table applied_migrations already exists', 'yellow' );
51 0 return 1;
52 }
53  
54 else {
55 1 ▶ $self->dbh->do($sql)
Branch: 1 branch
BranchTrueFalseTotal
$self->dbh->do($sql) or say colored($self->dbh->errstr, "red") and exit 0 1 1
Condition25%1/4
$self->dbh->do($sql) || say(colored($self->dbh->errstr, 'red')) && exit
l!l
10
say colored($self->dbh->errstr, "red") and exit
!ll
00
MC/DC0%0/2
$self->dbh->do($sql) || say(colored($self->dbh->errstr, 'red')) && exit
$self->dbh->do($sql) say colored($self->dbh->errstr, "red")
Truth table33%1/3
A || B
A= $self->dbh->do($sql)B= say colored($self->dbh->errstr, "red")
ABresult
1X1
011
000
56 or say colored( $self->dbh->errstr, 'red' )
57 and exit;
58  
59 1 say colored(
60 'Table applied_migrations successfully created',
61 'green'
62 );
63  
64 1 return 1;
65 }
66 }
67  
68 sub up {
69 1 ▶ my ( $self, $num ) = @_;
Subroutine up: called CC=8 SCAR=23.8
Pod documented
70  
71 1 ▶ if ( not $self->_is_applied_migrations_table_exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_applied_migrations_table_exists) 0 1 1
72 0 say $self->_applied_migrations_not_exist_phrase();
73 0 exit;
74 }
75  
76 1 $self->dbh->{AutoCommit} = 0;
77  
78 1 my $dir = $self->_detect_dir;
79 1 my @dirs = sort $self->_dir_listing($dir);
80  
81 1 ▶ $num = $num || @dirs;
Condition33%1/3
$num || @dirs
l!l&&r!l&&!r
010
MC/DC0%0/2
$num || @dirs
$num @dirs
Truth table33%1/3
A || B
A= $numB= @dirs
ABresult
1X1
011
000
82 1 my $completed = 0;
83  
84 1 for (@dirs) {
85 2 ▶ if ( not $num ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $num) { } 0 2 2
elsif (not $self->_is_migration_applied($_)) { } 2 0 2
86 0 last;
87 }
88  
89 elsif ( not $self->_is_migration_applied($_) ) {
90 2 $self->_run_migration( $dir, $_, UP );
91 2 $completed++;
92 2 $num--;
93 }
94 }
95  
96 1 my $rows = $self->dbh->commit;
97  
98 1 ▶ if ( $rows < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows < 0) 0 1 1
99 0 ▶ say colored( 'Could not run migrations', 'red' ) and exit;
Branch: 1 branch
BranchTrueFalseTotal
say colored("Could not run migrations", "red") and exit 0 0 0
100 }
101  
102 1 $self->dbh->{AutoCommit} = 1;
103  
104 1 say colored( "Migration up:$completed", 'green' );
105  
106 1 return 1;
107 }
108  
109 sub down {
110 2 ▶ my ( $self, $num ) = @_;
Subroutine down: called CC=7 SCAR=20.3
Pod documented
111  
112 2 ▶ if ( not $self->_is_applied_migrations_table_exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_applied_migrations_table_exists) 0 2 2
113 0 say $self->_applied_migrations_not_exist_phrase();
114 0 exit;
115 }
116  
117 2 $self->dbh->{AutoCommit} = 0;
118  
119 2 my $dir = $self->_detect_dir;
120 2 my @dirs = sort { $b cmp $a } $self->_dir_listing($dir);
121  
122 2 ▶ $num = $num || @dirs;
Condition33%1/3
$num || @dirs
l!l&&r!l&&!r
200
MC/DC0%0/2
$num || @dirs
$num @dirs
Truth table33%1/3
A || B
A= $numB= @dirs
ABresult
1X1
011
000
123 2 my $completed = 0;
124  
125 2 for (@dirs) {
126 4 ▶ if ( not $num ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $num) { } 1 3 4
elsif ($self->_is_migration_applied($_)) { } 2 1 3
127 1 last;
128 }
129  
130 elsif ( $self->_is_migration_applied($_) ) {
131 2 $self->_run_migration( $dir, $_, DOWN );
132 2 $completed++;
133 2 $num--;
134 }
135 }
136  
137 2 my $rows = $self->dbh->commit;
138 2 ▶ if ( $rows < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows < 0) 0 2 2
139 0 say colored( 'Could not rollback migrations', 'red' );
140 0 exit;
141 }
142  
143 2 $self->dbh->{AutoCommit} = 1;
144  
145 2 say colored( "Migration down:$completed", 'green' );
146  
147 2 return 1;
148 }
149  
150 sub _is_applied_migrations_table_exists {
151 4 ▶ my ($self) = @_;
Subroutine _is_applied_migrations_table_exists: called CC=2 SCAR=6.9
152  
153 4 my $sth =
154 $self->dbh->table_info( '%', '%', 'applied_migrations', 'TABLE' );
155 4 my @row = $sth->fetchrow_array;
156  
157 4 $sth->finish;
158  
159 4 ▶ return @row ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@row ? : 3 1 4
160 }
161  
162 sub _applied_migrations_not_exist_phrase {
163 0 ▶ return colored(
Subroutine _applied_migrations_not_exist_phrase: not called CC=1 SCAR=6.9
164 'Table applied_migrations does not exists. You should run init first',
165 'red'
166 );
167 }
168  
169 sub _detect_dir {
170 3 ▶ my ($self) = @_;
Subroutine _detect_dir: called CC=3 SCAR=11.3
171  
172 my @dirs = (
173 $self->dir,
174 $ENV{PWD} . $self->dir,
175 $ENV{PWD} . '/' . $self->dir,
176 3 $ENV{PWD} . '/' . dirname($PROGRAM_NAME) . '/' . $self->dir,
177 );
178  
179 3 for (@dirs) {
180 12 ▶ if ( -d $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_) 3 9 12
181 3 return $_;
182 }
183 }
184  
185 0 say colored(
186 "Dir $self->{dir} does not exists, try to specify full path",
187 'red'
188 );
189 0 exit;
190 }
191  
192 sub _dir_listing {
193 3 ▶ my ( $self, $dir ) = @_;
Subroutine _dir_listing: called CC=3 SCAR=12.0
194  
195 3 ▶ opendir my $dh, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or say colored("Couldn't open dir '${dir}': $DBIx::Schema::Migration::ERRNO", "red") and exit 0 3 3
Condition25%1/4
opendir(my $dh, $dir) || say(colored("Couldn't open dir '${dir}': $DBIx::Schema::Migration::ERRNO", 'red')) && exit
l!l
30
say colored("Couldn't open dir '${dir}': $DBIx::Schema::Migration::ERRNO", "red") and exit
!ll
00
MC/DC0%0/2
opendir(my $dh, $dir) || say(colored("Couldn't open dir '${dir}': $DBIx::Schema::Migration::ERRNO", 'red')) && exit
opendir(my $dh, $dir) say colored("Couldn't open dir '${dir}': $DBIx::Schema::Migration::ERRNO", "red")
Truth table33%1/3
A || B
A= opendir(my $dh, $dir)B= say colored("Couldn't open dir '${dir}': $DBIx::Schema::Migration::ERRNO", "red")
ABresult
1X1
011
000
196 or say colored( "Couldn't open dir '$dir': $ERRNO", 'red' )
197 and exit;
198 3 my @dirs = readdir $dh;
199 3 closedir $dh;
200  
201 3 return grep { !/^\.|\.{2}$/m } @dirs;
202 }
203  
204 sub _is_migration_applied {
205 5 ▶ my ( $self, $migration ) = @_;
Subroutine _is_migration_applied: called CC=4 SCAR=16.0
206  
207 5 my $sql = 'SELECT migration FROM applied_migrations WHERE migration = ?';
208 5 ▶ my $sth = $self->dbh->prepare($sql)
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->dbh->prepare($sql) or say colored($self->dbh->errstr, "red") and exit 0 5 5
Condition25%1/4
(my $sth = $self->dbh->prepare($sql)) || say(colored($self->dbh->errstr, 'red')) && exit
l!l
50
say colored($self->dbh->errstr, "red") and exit
!ll
00
MC/DC0%0/2
(my $sth = $self->dbh->prepare($sql)) || say(colored($self->dbh->errstr, 'red')) && exit
(my $sth = $self->dbh->prepare($sql)) say colored($self->dbh->errstr, "red")
Truth table33%1/3
A || B
A= (my $sth = $self->dbh->prepare($sql))B= say colored($self->dbh->errstr, "red")
ABresult
1X1
011
000
209 or say colored( $self->dbh->errstr, 'red' )
210 and exit;
211 5 my $rv = $sth->execute($migration);
212 5 my @row = $sth->fetchrow_array;
213  
214 5 $sth->finish;
215  
216 5 ▶ if ( $rv < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv < 0) 0 5 5
217 0 say colored( $sth->errstr );
218 0 exit;
219 }
220  
221 5 return @row;
222 }
223  
224 sub _run_migration {
225 4 ▶ my ( $self, $dir, $migration, $type ) = @_;
Subroutine _run_migration: called CC=3 SCAR=11.2
226  
227 4 my $filename = "${migration}_$type.sql";
228 4 my $sql = read_text "$dir/$migration/$filename";
229 4 my $rows = $self->dbh->do($sql);
230  
231 4 ▶ if ( $rows < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows < 0) 0 4 4
232 0 say colored( $self->db->errstr, 'red' );
233 0 exit;
234 }
235  
236 4 ▶ if ( $type eq UP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'up') { } 2 2 4
237 2 $self->_save_migration($migration);
238 }
239 else {
240 2 $self->_delete_migration($migration);
241 }
242  
243 4 return 1;
244 }
245  
246 sub _save_migration {
247 2 ▶ my ( $self, $migration ) = @_;
Subroutine _save_migration: called CC=3 SCAR=12.0
248  
249 2 my $sql = 'INSERT INTO applied_migrations VALUES(?)';
250 2 ▶ my $sth = $self->dbh->prepare($sql)
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->dbh->prepare($sql) or say colored($self->dbh->errstr, "red") and exit 0 2 2
Condition25%1/4
(my $sth = $self->dbh->prepare($sql)) || say(colored($self->dbh->errstr, 'red')) && exit
l!l
20
say colored($self->dbh->errstr, "red") and exit
!ll
00
MC/DC0%0/2
(my $sth = $self->dbh->prepare($sql)) || say(colored($self->dbh->errstr, 'red')) && exit
(my $sth = $self->dbh->prepare($sql)) say colored($self->dbh->errstr, "red")
Truth table33%1/3
A || B
A= (my $sth = $self->dbh->prepare($sql))B= say colored($self->dbh->errstr, "red")
ABresult
1X1
011
000
251 or say colored( $self->dbh->errstr, 'red' )
252 and exit;
253 2 my $rv = $sth->execute($migration);
254  
255 2 $sth->finish;
256  
257 2 return $rv ne '0E0';
258 }
259  
260 sub _delete_migration {
261 2 ▶ my ( $self, $migration ) = @_;
Subroutine _delete_migration: called CC=3 SCAR=12.0
262  
263 2 my $sql = 'DELETE FROM applied_migrations WHERE migration = ?';
264 2 ▶ my $sth = $self->dbh->prepare($sql)
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->dbh->prepare($sql) or say colored($self->dbh->errstr, "red") and exit 0 2 2
Condition25%1/4
(my $sth = $self->dbh->prepare($sql)) || say(colored($self->dbh->errstr, 'red')) && exit
l!l
20
say colored($self->dbh->errstr, "red") and exit
!ll
00
MC/DC0%0/2
(my $sth = $self->dbh->prepare($sql)) || say(colored($self->dbh->errstr, 'red')) && exit
(my $sth = $self->dbh->prepare($sql)) say colored($self->dbh->errstr, "red")
Truth table33%1/3
A || B
A= (my $sth = $self->dbh->prepare($sql))B= say colored($self->dbh->errstr, "red")
ABresult
1X1
011
000
265 or say colored( $self->dbh->errstr, 'red' )
266 and exit;
267 2 my $rv = $sth->execute($migration);
268  
269 2 $sth->finish;
270  
271 2 return $rv ne '0E0';
272 }
273  
274 1 1;
275  
276 __DATA__
↑ Summary