blib/lib/ORLite/Migrate.pm

filter: Statementstmt 88.3% 106 / 120 Branchbran 55.0% 33 / 60 Conditioncond 40.0% 12 / 30 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 2 totaltotal 68.4% 167 / 244 CC 43 SCAR 42.9
CC
43
Cov
72%
import
111.2
patches
6.2
plan
2.0
CRAP 73.1
↑ Summary blib/lib/ORLite/Migrate/Patch.pm »
1 package ORLite::Migrate;
2  
3 # See POD at end of file for documentation
4  
5 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::Spec 3.2701 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::Path 2.04 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Params::Util 0.37 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use DBI 1.58 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use DBD::SQLite 1.21 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use ORLite 1.28 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 $VERSION = '1.10';
19 4 @ISA = 'ORLite';
20 }
21  
22 2 ▶sub import {
Subroutine import: called CC=37 SCAR=47.1
23 2 ▶ my $class = ref $_[0] || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
24  
25 # Check for debug mode
26 2 my $DEBUG = 0;
27 2 ▶ if ( defined Params::Util::_STRING($_[-1]) and $_[-1] eq '-DEBUG' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Params::Util::_STRING($_[-1]) and $_[-1] eq "-DEBUG") 0 2 2
Condition33%1/3
defined &Params::Util::_STRING($_[-1]) and $_[-1] eq "-DEBUG"
!ll&&!rl&&r
200
MC/DC0%0/2
defined &Params::Util::_STRING($_[-1]) and $_[-1] eq "-DEBUG"
defined &Params::Util::_STRING($_[-1]) $_[-1] eq "-DEBUG"
Truth table33%1/3
A and B
A= defined &Params::Util::_STRING($_[-1])B= $_[-1] eq "-DEBUG"
ABresult
0X0
100
111
28 0 $DEBUG = 1;
29 0 pop @_;
30 }
31  
32 # Check params and apply defaults
33 2 my %params;
34 2 ▶ if ( defined Params::Util::_STRING($_[1]) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined &Params::Util::_STRING($_[1])) { } 0 2 2
elsif (&Params::Util::_HASH($_[1])) { } 2 0 2
35 # Migrate needs at least two params
36 0 Carp::croak("ORLite::Migrate must be invoked in HASH form");
37 } elsif ( Params::Util::_HASH($_[1]) ) {
38 2 %params = %{ $_[1] };
39 } else {
40 0 Carp::croak("Missing, empty or invalid params HASH");
41 }
42 2 ▶ if ( $params{timeline} and not defined $params{create} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"timeline"} and not defined $params{"create"}) 1 1 2
Condition66%2/3
$params{"timeline"} and not defined $params{"create"}
!ll&&!rl&&r
011
MC/DC50%1/2
$params{"timeline"} and not defined $params{"create"}
$params{"timeline"} not defined $params{"create"}
Truth table66%2/3
A and not(B)
A= $params{"timeline"}B= not defined $params{"create"}
ABresult
0X0
100
111
43 1 $params{create} = 1;
44 }
45 2 ▶ $params{create} = $params{create} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$params{'create'} ? : 2 0 2
46 2 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Params::Util::_STRING($params{"file"}) and $params{"create"} || -f $params{"file"}) 0 2 2
Condition33%2/6
defined &Params::Util::_STRING($params{"file"}) and $params{"create"} || -f $params{"file"}
!ll&&!rl&&r
002
$params{"create"} or -f $params{"file"}
l!l&&r!l&&!r
200
MC/DC0%0/3
defined &Params::Util::_STRING($params{"file"}) and $params{"create"} || -f $params{"file"}
defined &Params::Util::_STRING($params{"file"}) $params{"create"} -f $params{"file"}
Truth table25%1/4
A and B or C
A= defined &Params::Util::_STRING($params{"file"})B= $params{"create"}C= -f $params{"file"}
ABCresult
0XX0
1000
11X1
1011
47 defined Params::Util::_STRING($params{file})
48 and (
49 $params{create}
50 or
51 -f $params{file}
52 )
53 ) {
54 0 Carp::croak("Missing or invalid file param");
55 }
56 2 ▶ unless ( defined $params{readonly} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"readonly"}) 2 0 2
57 2 ▶ $params{readonly} = $params{create} ? 0 : ! -w $params{file};
Branch: 1 branch
BranchTrueFalseTotal
$params{'create'} ? : 2 0 2
58 }
59 2 ▶ unless ( defined $params{tables} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"tables"}) 2 0 2
60 2 $params{tables} = 1;
61 }
62 2 ▶ unless ( defined $params{package} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"package"}) 2 0 2
63 2 $params{package} = scalar caller;
64 }
65 2 ▶ unless ( Params::Util::_CLASS($params{package}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_CLASS($params{"package"})) 0 2 2
66 0 Carp::croak("Missing or invalid package class");
67 }
68  
69 2 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_DRIVER($params{"timeline"}, "ORLite::Migrate::Timeline") or $params{"timeline"} and -d $params{"timeline"} and -r $params{"timeline"}) 0 2 2
Condition44%4/9
&Params::Util::_DRIVER($params{"timeline"}, "ORLite::Migrate::Timeline") or $params{"timeline"} and -d $params{"timeline"} and -r $params{"timeline"}
l!l&&r!l&&!r
110
$params{"timeline"} and -d $params{"timeline"} and -r $params{"timeline"}
!ll&&!rl&&r
001
$params{"timeline"} and -d $params{"timeline"}
!ll&&!rl&&r
001
MC/DC0%0/4
&Params::Util::_DRIVER($params{"timeline"}, "ORLite::Migrate::Timeline") or $params{"timeline"} and -d $params{"timeline"} and -r $params{"timeline"}
&Params::Util::_DRIVER($params{"timeline"}, "ORLite::Migrate::Timeline") $params{"timeline"} -d $params{"timeline"} -r $params{"timeline"}
Truth table40%2/5
A or B and C and D
A= &Params::Util::_DRIVER($params{"timeline"}, "ORLite::Migrate::Timeline")B= $params{"timeline"}C= -d $params{"timeline"}D= -r $params{"timeline"}
ABCDresult
1XXX1
01111
00XX0
010X0
01100
70 Params::Util::_DRIVER($params{timeline}, 'ORLite::Migrate::Timeline')
71 or
72 ($params{timeline} and -d $params{timeline} and -r $params{timeline})
73 ) {
74 0 Carp::croak("Missing or invalid timeline");
75 }
76  
77 # We don't support readonly databases
78 2 ▶ if ( $params{readonly} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"readonly"}) 0 2 2
79 0 Carp::croak("ORLite::Migrate does not support readonly databases");
80 }
81  
82 # Get the schema version
83 2 my $file = File::Spec->rel2abs($params{file});
84 2 my $created = ! -f $params{file};
85 2 ▶ if ( $created ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created) 2 0 2
86 # Create the parent directory
87 2 my $dir = File::Basename::dirname($file);
88 2 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 2 2
89 0 my @dirs = File::Path::mkpath( $dir, { verbose => 0 } );
90 0 ▶ $class->prune(@dirs) if $params{prune};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"prune"} 0 0 0
91 }
92 2 ▶ $class->prune($file) if $params{prune};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"prune"} 2 0 2
93 }
94 2 my $dsn = "dbi:SQLite:$file";
95 2 my $dbh = DBI->connect($dsn);
96 2 my $version = $dbh->selectrow_arrayref('pragma user_version')->[0];
97 2 $dbh->disconnect;
98  
99 # We're done with the prune setting now
100 2 $params{prune} = 0;
101  
102 # Handle the migration class
103 2 ▶ if ( Params::Util::_DRIVER($params{timeline}, 'ORLite::Migrate::Timeline') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Params::Util::_DRIVER($params{'timeline'}, 'ORLite::Migrate::Timeline')) { } 1 1 2
104 my $timeline = $params{timeline}->new(
105 1 dbh => DBI->connect($dsn),
106 );
107 1 $timeline->upgrade( $params{user_version} );
108  
109 } else {
110 1 my $timeline = File::Spec->rel2abs($params{timeline});
111 1 my @plan = plan( $params{timeline}, $version );
112  
113 # Execute the migration plan
114 1 ▶ if ( @plan ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@plan) 1 0 1
115 # Does the migration plan reach the required destination
116 1 my $destination = $version + scalar(@plan);
117 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"user_version"} and $destination != $params{"user_version"}) 0 1 1
Condition33%1/3
exists $params{"user_version"} and $destination != $params{"user_version"}
!ll&&!rl&&r
010
MC/DC0%0/2
exists $params{"user_version"} and $destination != $params{"user_version"}
exists $params{"user_version"} $destination != $params{"user_version"}
Truth table33%1/3
A and B
A= exists $params{"user_version"}B= $destination != $params{"user_version"}
ABresult
0X0
100
111
118 exists $params{user_version}
119 and
120 $destination != $params{user_version}
121 ) {
122 0 die "Schema migration destination user_version mismatch (got $destination, wanted $params{user_version})";
123 }
124  
125 # Load the modules needed for the migration
126 1 require Probe::Perl;
127 1 require File::pushd;
128 1 require IPC::Run3;
129  
130 # Locate our Perl interpreter
131 1 my $perl = Probe::Perl->find_perl_interpreter;
132 1 ▶ unless ( $perl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($perl) 0 1 1
133 0 Carp::croak("Unable to locate your perl executable");
134 }
135  
136 # Execute each script
137 1 my $pushd = File::pushd::pushd($timeline);
138 1 foreach my $patch ( @plan ) {
139 3 my $stdin = "$file\n";
140 3 ▶ if ( $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 3 3
141 0 print STDERR "Applying schema patch $patch...\n";
142 }
143 3 ▶ my $ok = IPC::Run3::run3( [ $perl, $patch ], \$stdin, \undef, $DEBUG ? undef : \undef );
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG ? : 0 3 3
144 3 ▶ if ( ! $ok or $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok or $? != 0) 0 3 3
Condition33%1/3
not $ok or $? != 0
l!l&&r!l&&!r
003
MC/DC0%0/2
not $ok or $? != 0
not $ok $? != 0
Truth table33%1/3
not(A) or B
A= not $okB= $? != 0
ABresult
1X1
011
000
145 0 Carp::croak("Migration patch $patch failed, database in unknown state");
146 }
147 }
148  
149 # Migration complete, set user_version to new state
150 1 $dbh = DBI->connect($dsn);
151 1 $dbh->do("pragma user_version = $destination");
152 1 $dbh->disconnect;
153 }
154 }
155  
156 # Hand off to the regular constructor
157 2 ▶ $class->SUPER::import(
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG ? : 0 2 2
158 \%params,
159 $DEBUG ? '-DEBUG' : ()
160 );
161 }
162  
163  
164  
165  
166  
167 #####################################################################
168 # Simple Methods
169  
170 3 ▶sub patches {
Subroutine patches: called CC=6 SCAR=18.2
Pod undocumented
171 3 my $dir = shift;
172  
173 # Find all files in a directory
174 3 local *DIR;
175 3 ▶ opendir( DIR, $dir ) or die "opendir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or die "opendir: $!" 3 0 3
176 3 ▶ my @files = readdir( DIR ) or die "readdir: $!";
Branch: 1 branch
BranchTrueFalseTotal
my(@files) = readdir DIR or die "readdir: $!" 3 0 3
177 3 ▶ closedir( DIR ) or die "closedir: $!";
Branch: 1 branch
BranchTrueFalseTotal
closedir DIR or die "closedir: $!" 3 0 3
178  
179 # Filter to get the patch set
180 3 my @patches = ();
181 3 foreach ( @files ) {
182 15 ▶ next unless /^migrate-(\d+)\.pl$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^migrate-(\d+)\.pl$/ 6 9 15
183 9 $patches["$1"] = $_;
184 }
185  
186 3 return @patches;
187 }
188  
189 2 ▶sub plan {
Subroutine plan: called CC=2 SCAR=6.9
Pod undocumented
190 2 my $directory = shift;
191 2 my $version = shift;
192  
193 # Find the list of patches
194 2 my @patches = patches( $directory );
195  
196 # Assemble the plan by integer stepping forwards
197 # until we run out of timeline hits.
198 2 my @plan = ();
199 2 ▶ while ( $patches[++$version] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($patches[++$version]) 5 2 7
200 5 push @plan, $patches[$version];
201 }
202  
203 2 return @plan;
204 }
205  
206 4 1;
207  
208 __END__
↑ Summary blib/lib/ORLite/Migrate/Patch.pm »