Database/Migrator/Core.pm

filter: Statementstmt 43.3% 55 / 127 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 50.0% 12 / 24 Podpod 0.0% 0 / 2 totaltotal 31.9% 67 / 210 CC 24 SCAR 32.7
CC
24
Cov
32%
_run_one_migration
90.0
_numeric_or_alpha_sort
42.0
__ANON__
12.0
CRAP 26.3
↑ Summary Test/Database/Migrator.pm »
1 package Database::Migrator::Core;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.14';
8  
9 1 ▶use Database::Migrator::Types qw( ArrayRef Bool Dir File Maybe Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Eval::Closure qw( eval_closure );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IPC::Run3 qw( run3 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Log::Dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Moose::Util::TypeConstraints qw( duck_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use MooseX::Getopt::OptionTypeMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 with 'MooseX::Getopt::Dashes';
21  
22 1 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
23 Maybe [Str] => '=s',
24 );
25  
26 1 requires qw(
27 _create_database
28 _driver_name
29 _drop_database
30 _run_ddl
31 );
32  
33 1 has database => (
34 is => 'ro',
35 isa => Str,
36 required => 1,
37 );
38  
39 1 has [qw( username password host port )] => (
40 is => 'ro',
41 isa => Maybe [Str],
42 default => undef,
43 );
44  
45 1 has migration_table => (
46 is => 'ro',
47 isa => Str,
48 required => 1,
49 );
50  
51 1 has migrations_dir => (
52 is => 'ro',
53 isa => Dir,
54 coerce => 1,
55 required => 1,
56 );
57  
58 1 has schema_file => (
59 is => 'ro',
60 isa => File,
61 coerce => 1,
62 required => 1,
63 );
64  
65 1 has _database_exists => (
66 is => 'ro',
67 isa => Bool,
68 init_arg => undef,
69 lazy => 1,
70 builder => '_build_database_exists',
71 );
72  
73 1 has __pending_migrations => (
74 traits => ['Array'],
75 is => 'ro',
76 isa => ArrayRef [Dir],
77 init_arg => undef,
78 lazy => 1,
79 builder => '_build_pending_migrations',
80 handles => {
81 _pending_migrations => 'elements',
82 has_pending_migrations => 'count',
83 },
84 );
85  
86 1 has dbh => (
87 traits => ['NoGetopt'],
88 is => 'ro',
89 isa => 'DBI::db',
90 init_arg => undef,
91 lazy => 1,
92 builder => '_build_dbh',
93 );
94  
95 1 has logger => (
96 traits => ['NoGetopt'],
97 is => 'ro',
98 isa => duck_type( [qw( debug info )] ),
99 lazy => 1,
100 builder => '_build_logger',
101 );
102  
103 1 has verbose => (
104 is => 'ro',
105 isa => Bool,
106 default => 0,
107 );
108  
109 1 has quiet => (
110 is => 'ro',
111 isa => Bool,
112 default => 0,
113 );
114  
115 1 has dry_run => (
116 is => 'ro',
117 isa => Bool,
118 default => 0,
119 );
120  
121 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
122 0 my $orig = shift;
123 0 my $class = shift;
124  
125 0 my $p = $class->$orig(@_);
126  
127 $p->{username} = delete $p->{user}
128 0 ▶ if exists $p->{user};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{"user"} 0 0 0
129  
130 0 return $p;
131 1 };
132  
133 0 ▶sub BUILD { }
Subroutine BUILD: not called CC=1 SCAR=0.0
Pod undocumented
134 0 ▶after BUILD => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
135 0 my $self = shift;
136  
137 0 ▶ die 'Cannot be both quiet and verbose'
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet and $self->verbose 0 0 0
Condition0%0/3
$self->quiet and $self->verbose
!ll&&!rl&&r
000
MC/DC0%0/2
$self->quiet and $self->verbose
$self->quiet $self->verbose
Truth table0%0/3
A and B
A= $self->quietB= $self->verbose
ABresult
0X0
100
111
138 if $self->quiet() && $self->verbose();
139 1 };
140  
141 0 ▶sub create_or_update_database {
Subroutine create_or_update_database: not called CC=2 SCAR=17.9
Pod undocumented
142 0 my $self = shift;
143  
144 0 ▶ if ( $self->_database_exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_database_exists) { } 0 0 0
145 0 my $database = $self->database();
146 0 $self->logger()->debug("The $database database already exists");
147 }
148 else {
149 0 $self->_create_database();
150 0 $self->_run_ddl( $self->schema_file() );
151 }
152  
153 0 $self->_run_migrations();
154  
155 0 return;
156 }
157  
158 0 ▶sub _run_migrations {
Subroutine _run_migrations: not called CC=2 SCAR=17.9
159 0 my $self = shift;
160  
161 0 $self->_run_one_migration($_) for $self->_pending_migrations();
162 }
163  
164 0 ▶sub _run_one_migration {
Subroutine _run_one_migration: not called CC=9 SCAR=45.0
165 0 my $self = shift;
166 0 my $migration = shift;
167  
168 0 my $name = $migration->basename();
169  
170 0 $self->logger()->info("Running migration - $name");
171  
172 0 my @files = grep { !$_->is_dir() } $migration->children( no_hidden => 1 );
173  
174 0 for my $file ( sort _numeric_or_alpha_sort @files ) {
175 0 my $basename = $file->basename();
176 0 ▶ if ( $file =~ /\.sql/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /\.sql/) { } 0 0 0
elsif (-x $file) { } 0 0 0
177 0 $self->logger()->debug(" - running $basename as sql");
178 0 $self->_run_ddl($file);
179 }
180 elsif ( -x $file ) {
181 0 $self->logger->debug(
182 " - running $basename as a separate program");
183  
184 0 ▶ next if $self->dry_run;
Branch: 1 branch
BranchTrueFalseTotal
if $self->dry_run 0 0 0
185  
186 0 my @command = ( $file->absolute->stringify );
187 0 my $stderr = q{};
188 0 run3( \@command, \undef, \undef, \$stderr );
189 0 ▶ if ( $? != 0 || $stderr ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0 or $stderr ne "") 0 0 0
Condition0%0/3
$? != 0 or $stderr ne ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$? != 0 or $stderr ne ""
$? != 0 $stderr ne ""
Truth table0%0/3
A or B
A= $? != 0B= $stderr ne ""
ABresult
1X1
011
000
190 0 die "$file failed: $stderr";
191 }
192 }
193 else {
194 0 $self->logger()->debug(" - running $basename as perl code");
195  
196 0 my $perl = $file->slurp();
197  
198 0 my $sub = eval_closure( source => $perl );
199  
200 0 ▶ next if $self->dry_run();
Branch: 1 branch
BranchTrueFalseTotal
if $self->dry_run 0 0 0
201  
202 0 $sub->($self);
203 }
204 }
205  
206 0 ▶ return if $self->dry_run();
Branch: 1 branch
BranchTrueFalseTotal
if $self->dry_run 0 0 0
207  
208 0 my $table = $self->dbh()->quote_identifier( $self->migration_table() );
209 0 $self->dbh()
210 ->do( "INSERT INTO $table (migration) VALUES (?)", undef, $name );
211  
212 0 return;
213 }
214  
215 0 ▶sub _build_pending_migrations {
Subroutine _build_pending_migrations: not called CC=3 SCAR=24.8
216 0 my $self = shift;
217  
218 0 my $table = $self->migration_table();
219  
220 0 my %ran;
221 0 ▶ if ( grep { $_ =~ /\b\Q$table\E\b/ } $self->dbh()->tables() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ =~ /\b\Q$table\E\b/;} $self->dbh->tables) 0 0 0
222 0 my $quoted = $self->dbh()->quote_identifier($table);
223  
224 %ran
225 0 = map { $_ => 1 }
226 0 ▶ @{ $self->dbh()
Condition0%0/2
$self->dbh->selectcol_arrayref("SELECT migration FROM $quoted") || []
l!l
00
MC/DC0%0/1
$self->dbh->selectcol_arrayref("SELECT migration FROM $quoted") || []
$self->dbh->selectcol_arrayref("SELECT migration FROM $quoted")
Truth table0%0/2
A
A= $self->dbh->selectcol_arrayref("SELECT migration FROM $quoted")
Aresult
11
00
227 ->selectcol_arrayref("SELECT migration FROM $quoted") || [] };
228 }
229  
230 return [
231 0 sort _numeric_or_alpha_sort grep { !$ran{ $_->basename() } }
232 0 grep { $_->is_dir() }
233 $self->migrations_dir()->children( no_hidden => 1 )
234 ];
235 }
236  
237 0 ▶sub _build_logger {
Subroutine _build_logger: not called CC=3 SCAR=24.8
238 0 my $self = shift;
239  
240 0 ▶ my $outputs
Branch: 2 branches
BranchTrueFalseTotal
$self->quiet ? : 0 0 0
$self->verbose ? : 0 0 0
241 = $self->quiet()
242 ? [ 'Null', min_level => 'emerg' ]
243 : [
244 'Screen',
245 min_level => ( $self->verbose() ? 'debug' : 'info' ),
246 newline => 1,
247 ];
248  
249 0 return Log::Dispatch->new( outputs => [$outputs] );
250 }
251  
252 0 ▶sub _build_database_exists {
Subroutine _build_database_exists: not called CC=2 SCAR=17.9
253 0 my $self = shift;
254  
255 ## no critic (RequireBlockTermination)
256 0 ▶ return try { $self->_build_dbh(); 1 } || 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
&try(sub { $self->_build_dbh; 1; } ) || 0
l!l
00
MC/DC0%0/1
&try(sub { $self->_build_dbh; 1; } ) || 0
&try(sub { $self->_build_dbh; 1; } )
Truth table0%0/2
A
A= &try(sub { $self->_build_dbh; 1; } )
Aresult
11
00
257 }
258  
259 0 ▶sub _build_dbh {
Subroutine _build_dbh: not called CC=1 SCAR=6.9
260 0 my $self = shift;
261  
262 0 return DBI->connect(
263 'dbi:' . $self->_driver_name() . ':database=' . $self->database(),
264 $self->username(),
265 $self->password(), {
266 RaiseError => 1,
267 PrintError => 1,
268 PrintWarn => 1,
269 ShowErrorStatement => 1,
270 },
271 );
272 }
273  
274 0 ▶sub _numeric_or_alpha_sort {
Subroutine _numeric_or_alpha_sort: not called CC=6 SCAR=37.4
275 0 my ( $a_num, $a_alpha ) = $a->basename() =~ /^(\d+)(.+)/;
276 0 my ( $b_num, $b_alpha ) = $b->basename() =~ /^(\d+)(.+)/;
277  
278 0 ▶ $a_num ||= 0;
Condition0%0/2
$a_num ||= 0
l!l
00
MC/DC0%0/1
$a_num ||= 0
$a_num
Truth table0%0/2
A
A= $a_num
Aresult
11
00
279 0 ▶ $b_num ||= 0;
Condition0%0/2
$b_num ||= 0
l!l
00
MC/DC0%0/1
$b_num ||= 0
$b_num
Truth table0%0/2
A
A= $b_num
Aresult
11
00
280  
281 0 ▶ $a_alpha ||= q{};
Condition0%0/2
$a_alpha ||= ""
l!l
00
MC/DC0%0/1
$a_alpha ||= ""
$a_alpha
Truth table0%0/2
A
A= $a_alpha
Aresult
11
00
282 0 ▶ $b_alpha ||= q{};
Condition0%0/2
$b_alpha ||= ""
l!l
00
MC/DC0%0/1
$b_alpha ||= ""
$b_alpha
Truth table0%0/2
A
A= $b_alpha
Aresult
11
00
283  
284 0 ▶ return ( $a_num <=> $b_num or $a_alpha cmp $b_alpha );
Condition0%0/3
$a_num <=> $b_num || $a_alpha cmp $b_alpha
l!l&&r!l&&!r
000
MC/DC0%0/2
$a_num <=> $b_num || $a_alpha cmp $b_alpha
$a_num <=> $b_num $a_alpha cmp $b_alpha
Truth table0%0/3
A || B
A= $a_num <=> $b_numB= $a_alpha cmp $b_alpha
ABresult
1X1
011
000
285 }
286  
287 1 1;
288  
289 # ABSTRACT: Core role for Database::Migrator implementation classes
290  
291 __END__
↑ Summary Test/Database/Migrator.pm »