lib/Migraine.pm

filter: Statementstmt 15.9% 30 / 188 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 33.3% 9 / 27 Podpod 81.2% 13 / 16 totaltotal 15.5% 52 / 334 CC 48 SCAR 37.7
CC
48
Cov
11%
apply_migration
132.0
upgrade_migraine_metadata
72.0
migration_applied
42.0
CRAP 43.4
↑ Summary script/migraine.pl »
1 package Migraine;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 our $VERSION = "0.54"; # ==> ALSO update the version in the pod text below!
6  
7 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use constant MIGRATION_REGEX => qr/^(\d+)-.+\..+/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use constant MIGRAINE_META_CREATION_SQL =>
Subroutine BEGIN: called CC=1 SCAR=0.0
13 "CREATE TABLE migraine_meta (name varchar(20),
14 8 value varchar(200))";
15 8 ▶use constant MIGRAINE_META_UPGRADE_SQL =>
Subroutine BEGIN: called CC=1 SCAR=0.0
16 "ALTER TABLE migraine_meta ADD (name varchar(20),
17 8 value varchar(200))";
18 8 ▶use constant MIGRAINE_META_REMOVE_OBSOLETE_SQL =>
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 "ALTER TABLE migraine_meta DROP version";
20 8 ▶use constant MIGRAINE_MIGRATIONS_CREATION_SQL =>
Subroutine BEGIN: called CC=1 SCAR=0.0
21 "CREATE TABLE migraine_migrations (id integer,
22 8 PRIMARY KEY (id))";
23  
24 8 our $SUPPORTED_METADATA_FORMAT = 2;
25  
26 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my ($class, $dsn, %params) = @_;
28 my $dbh = DBI->connect($dsn, $params{user},
29 $params{password},
30 0 $params{dbi_options});
31 0 my $attrs = { migrations_dir => "migrations",
32 %params,
33 dbh => $dbh };
34 0 bless $attrs, $class;
35 }
36  
37 0 ▶sub dbh {
Subroutine dbh: not called CC=1 SCAR=6.9
Pod documented
38 0 my ($self) = @_;
39 0 $self->{dbh};
40 }
41  
42 0 ▶sub migraine_metadata_present {
Subroutine migraine_metadata_present: not called CC=1 SCAR=6.9
Pod documented
43 0 my ($self) = @_;
44  
45 0 return $self->_check_table_exists("migraine_meta");
46 }
47  
48 0 ▶sub migraine_metadata_usable {
Subroutine migraine_metadata_usable: not called CC=1 SCAR=6.9
Pod documented
49 0 my ($self) = @_;
50  
51 0 return ($self->migraine_metadata_version == $SUPPORTED_METADATA_FORMAT);
52 }
53  
54 0 ▶sub _check_table_exists {
Subroutine _check_table_exists: not called CC=1 SCAR=6.9
55 0 my ($self, $table_name) = @_;
56  
57 0 my @tables = $self->dbh->tables(undef, undef, $table_name);
58 0 return (scalar grep /$table_name/, @tables);
59 }
60  
61 0 ▶sub migraine_metadata_version {
Subroutine migraine_metadata_version: not called CC=5 SCAR=34.0
Pod documented
62 0 my ($self) = @_;
63  
64 0 ▶ if ($self->migraine_metadata_present) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->migraine_metadata_present) { } 0 0 0
65 my ($old_raise, $old_print) = ($self->dbh->{RaiseError},
66 0 $self->dbh->{PrintError});
67 0 $self->dbh->{RaiseError} = $self->dbh->{PrintError} = 0;
68 0 my $sth = $self->dbh->prepare("SELECT value
69 FROM migraine_meta
70 WHERE name = 'metadata_version'");
71 0 my $r = $sth->execute;
72 0 $self->dbh->{RaiseError} = $old_raise;
73 0 $self->dbh->{PrintError} = $old_print;
74 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
75 0 my $results = $sth->fetchrow_hashref;
76 0 ▶ if (defined $results->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $results->{'value'}) { } 0 0 0
77 0 return 0 + $results->{value}; # Force number
78 }
79 else {
80 0 croak "Inconsistent or unknown migraine_meta";
81 }
82 }
83 else {
84 0 my $old_raise = $self->dbh->{RaiseError};
85 0 $self->dbh->{RaiseError} = 0;
86 0 $sth = $self->dbh->prepare("SELECT version FROM migraine_meta");
87 0 $r = $sth->execute;
88 0 $self->dbh->{RaiseError} = $old_raise;
89 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
90 0 return 1;
91 }
92 else {
93 0 croak "Inconsistent or unknown migraine_meta";
94 }
95 }
96 }
97 else {
98 0 return 0;
99 }
100 }
101  
102 0 ▶sub create_migraine_metadata {
Subroutine create_migraine_metadata: not called CC=5 SCAR=34.0
Pod documented
103 0 my ($self) = @_;
104  
105 0 ▶ unless ($self->migraine_metadata_present) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->migraine_metadata_present) 0 0 0
106 0 my $sth = $self->dbh->prepare(MIGRAINE_META_CREATION_SQL);
107 0 ▶ $sth->execute ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or croak("Couldn't create migraine_meta table: $DBI::errstr\n") 0 0 0
108 croak "Couldn't create migraine_meta table: $DBI::errstr\n";
109 0 $sth = $self->dbh->prepare(MIGRAINE_MIGRATIONS_CREATION_SQL);
110 0 ▶ $sth->execute ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or croak("Couldn't create migraine_migrations table: $DBI::errstr\n") 0 0 0
111 croak "Couldn't create migraine_migrations table: $DBI::errstr\n";
112 0 $sth = $self->dbh->prepare("INSERT INTO migraine_meta (name, value)
113 VALUES (?, ?)");
114 0 ▶ $sth->execute('metadata_version', $SUPPORTED_METADATA_FORMAT) ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute("metadata_version", $Migraine::SUPPORTED_METADATA_FORMAT) or croak("Couldn't insert migraine_meta information.") 0 0 0
115 croak "Couldn't insert migraine_meta information.";
116 }
117 0 return 1;
118 }
119  
120 0 ▶sub upgrade_migraine_metadata {
Subroutine upgrade_migraine_metadata: not called CC=8 SCAR=42.8
Pod documented
121 0 my ($self) = @_;
122  
123 0 my $df = $self->migraine_metadata_version;
124 0 ▶ if ($df == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($df == 0) { } 0 0 0
elsif ($df == 1) { } 0 0 0
elsif ($df == 2) { } 0 0 0
125 0 return $self->create_migraine_metadata;
126 }
127 elsif ($df == 1) {
128 0 my $dbh = $self->dbh;
129 0 ▶ $dbh->do(MIGRAINE_META_UPGRADE_SQL) ||
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("ALTER TABLE migraine_meta ADD (name varchar(20),\n value varchar(200))") or croak("Couldn't upgrade the migraine_meta table: $DBI::errstr\n") 0 0 0
130 croak "Couldn't upgrade the migraine_meta table: $DBI::errstr\n";
131 # Store how many migrations had been applied and remove obsolete record
132 0 my $res = $dbh->selectall_arrayref("SELECT version FROM migraine_meta");
133 0 my $version = $res->[0]->[0];
134 0 $dbh->do("DELETE FROM migraine_meta"); # Remove obsolete record
135 0 ▶ $dbh->do(MIGRAINE_META_REMOVE_OBSOLETE_SQL) ||
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("ALTER TABLE migraine_meta DROP version") or croak("Couldn't remove obsolete fields from migraine_meta table: $DBI::errstr\n") 0 0 0
136 croak "Couldn't remove obsolete fields from migraine_meta table: $DBI::errstr\n";
137 0 $dbh->do("INSERT INTO migraine_meta (name, value)
138 VALUES ('metadata_version', '2')");
139 0 ▶ $dbh->do(MIGRAINE_MIGRATIONS_CREATION_SQL) ||
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("CREATE TABLE migraine_migrations (id integer,\n PRIMARY KEY (id))") or croak("Couldn't create migraine_migrations table: $DBI::errstr\n") 0 0 0
140 croak "Couldn't create migraine_migrations table: $DBI::errstr\n";
141 0 ▶ for (my $v = 1; $v <= $version; $v++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v <= $version) 0 0 0
142 0 $dbh->do("INSERT INTO migraine_migrations (id) VALUES ($v)");
143 }
144 0 return 1;
145 }
146 elsif ($df == 2) {
147 0 return 1;
148 }
149 else {
150 0 croak "Sorry, I don't know how to upgrade from version $df";
151 }
152 }
153  
154 0 ▶sub latest_version {
Subroutine latest_version: not called CC=2 SCAR=17.9
Pod documented
155 0 my ($self) = @_;
156  
157 0 opendir D, $self->{migrations_dir};
158 0 my ($higher_version) = reverse sort { $a <=> $b }
159 0 map { $_ =~ MIGRATION_REGEX;
160 0 int($1); }
161 0 grep { $_ =~ MIGRATION_REGEX }
162 readdir D;
163 0 closedir D;
164 0 ▶ $higher_version || 0;
Branch: 1 branch
BranchTrueFalseTotal
$higher_version or 0 0 0 0
165 }
166  
167 0 ▶sub current_version {
Subroutine current_version: not called CC=5 SCAR=34.0
Pod documented
168 0 my ($self) = @_;
169  
170 0 ▶ if ($self->migraine_metadata_present) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->migraine_metadata_present) { } 0 0 0
171 0 my $sth = $self->dbh->prepare("SELECT id FROM migraine_migrations
172 ORDER BY id DESC
173 LIMIT 1");
174 0 ▶ if (defined $sth && $sth->execute) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth and $sth->execute) { } 0 0 0
Condition0%0/3
defined $sth and $sth->execute
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sth and $sth->execute
defined $sth $sth->execute
Truth table0%0/3
A and B
A= defined $sthB= $sth->execute
ABresult
0X0
100
111
175 0 $sth->bind_columns(\my $version);
176 0 $sth->fetch;
177 0 ▶ return $version || 0;
Condition0%0/2
$version || 0
l!l
00
MC/DC0%0/1
$version || 0
$version
Truth table0%0/2
A
A= $version
Aresult
11
00
178 }
179 else {
180 0 croak "Can't query migraine_meta table?! ".$self->dbh->errstr;
181 }
182 }
183 else {
184 0 return 0;
185 }
186 }
187  
188 0 ▶sub migration_applied {
Subroutine migration_applied: not called CC=6 SCAR=37.4
Pod documented
189 0 my ($self, $migration_id) = @_;
190  
191 0 ▶ return unless $self->migraine_metadata_usable;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->migraine_metadata_usable 0 0 0
192 0 my $sth = $self->dbh->prepare("SELECT id FROM migraine_migrations
193 WHERE id = ?");
194 0 ▶ if (defined $sth && $sth->execute($migration_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth and $sth->execute($migration_id)) { } 0 0 0
Condition0%0/3
defined $sth and $sth->execute($migration_id)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sth and $sth->execute($migration_id)
defined $sth $sth->execute($migration_id)
Truth table0%0/3
A and B
A= defined $sthB= $sth->execute($migration_id)
ABresult
0X0
100
111
195 0 $sth->bind_columns(\my $version);
196 0 $sth->fetch;
197 0 ▶ if (defined $version && $version == $migration_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version and $version == $migration_id) 0 0 0
Condition0%0/3
defined $version and $version == $migration_id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $version and $version == $migration_id
defined $version $version == $migration_id
Truth table0%0/3
A and B
A= defined $versionB= $version == $migration_id
ABresult
0X0
100
111
198 0 return 1;
199 }
200 }
201 else {
202 0 return 0;
203 }
204 }
205  
206 0 ▶sub migrate {
Subroutine migrate: not called CC=5 SCAR=34.0
Pod documented
207 0 my ($self, %user_params) = @_;
208 0 my %params = (no_act => 0, %user_params);
209  
210 0 ▶ my $up_to_version = $params{version} || $self->latest_version;
Condition0%0/3
$params{'version'} || $self->latest_version
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{'version'} || $self->latest_version
$params{'version'} $self->latest_version
Truth table0%0/3
A || B
A= $params{'version'}B= $self->latest_version
ABresult
1X1
011
000
211  
212 # Create migraine metadata if it's not there
213 0 ▶ $self->create_migraine_metadata unless $params{no_act};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"no_act"} 0 0 0
214  
215 0 ▶ for (my $cnt = 1; $cnt <= $up_to_version; $cnt++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt <= $up_to_version) 0 0 0
216 0 ▶ next if $self->migration_applied($cnt);
Branch: 1 branch
BranchTrueFalseTotal
if $self->migration_applied($cnt) 0 0 0
217 $self->apply_migration($cnt, before_migrate => $params{before_migrate},
218 after_migrate => $params{after_migrate},
219 no_act => $params{no_act},
220 skip_missing_migrations =>
221 0 $params{skip_missing_migrations});
222 }
223 }
224  
225 0 ▶sub _mark_migration_as_applied {
Subroutine _mark_migration_as_applied: not called CC=1 SCAR=6.9
226 0 my ($self, $version) = @_;
227  
228 0 my $sth = $self->dbh->prepare("INSERT INTO migraine_migrations (id)
229 VALUES (?)");
230 0 $sth->execute($version);
231 }
232  
233 0 ▶sub apply_migration {
Subroutine apply_migration: not called CC=11 SCAR=48.8
Pod documented
234 0 my ($self, $version, %user_options) = @_;
235 0 my %options = (no_act => 0,
236 skip_missing_migrations => 0,
237 %user_options);
238  
239 0 ▶ if ($self->migration_applied($version)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->migration_applied($version)) 0 0 0
240 0 croak "Migration $version already applied";
241 }
242  
243 0 my $contents;
244 0 eval {
245 0 ▶ if ($options{before_migrate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"before_migrate"}) 0 0 0
246 0 $options{before_migrate}->($version,
247 $self->get_migration_path($version));
248 }
249 0 $contents = $self->get_migration($version);
250 };
251 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Migraine::EVAL_ERROR) 0 0 0
252 0 ▶ if ($options{skip_missing_migrations}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'skip_missing_migrations'}) { } 0 0 0
253 0 print STDERR "Skipping migration $version: $EVAL_ERROR";
254 0 return;
255 }
256 else {
257 0 die $EVAL_ERROR;
258 }
259 }
260  
261 0 ▶ if (defined $contents) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $contents) 0 0 0
262 0 ▶ unless ($options{no_act}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"no_act"}) 0 0 0
263 0 foreach my $query (split(/;\s*\n/, $contents)) {
264 0 my $sth = $self->dbh->prepare($query);
265 0 ▶ if ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth) { } 0 0 0
266 0 my $r;
267 0 eval { $r = $sth->execute };
268 0 ▶ $r || croak "Couldn't execute migration $version ($query): ".$self->dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$r or croak("Couldn't execute migration $version ($query): " . $self->dbh->errstr) 0 0 0
269 }
270 else {
271 0 croak "Can't prepare migration $version: ".$self->dbh->errstr;
272 }
273 }
274 0 $self->_mark_migration_as_applied($version);
275 }
276  
277 0 ▶ if ($options{after_migrate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"after_migrate"}) 0 0 0
278 0 $options{after_migrate}->($version,
279 $self->get_migration_path($version));
280 }
281 }
282 }
283  
284 0 ▶sub applied_migrations {
Subroutine applied_migrations: not called CC=2 SCAR=17.9
Pod documented
285 0 my ($self) = @_;
286  
287 0 ▶ if ($self->migraine_metadata_usable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->migraine_metadata_usable) { } 0 0 0
288 0 my $res = $self->dbh->selectall_arrayref("SELECT id
289 FROM migraine_migrations
290 ORDER BY id");
291 0 return map { $_->[0] } @$res;
292 }
293 else {
294 0 return;
295 }
296 }
297  
298 0 ▶sub applied_migration_ranges {
Subroutine applied_migration_ranges: not called CC=6 SCAR=37.4
Pod documented
299 0 my ($self) = @_;
300  
301 0 my @ordered_migrations = $self->applied_migrations;
302 0 ▶ return if scalar @ordered_migrations == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ordered_migrations == 0 0 0 0
303 0 my $range_first_item = $ordered_migrations[0];
304 0 my $last_item = $range_first_item;
305 0 my @r = ();
306 0 foreach my $id (@ordered_migrations) {
307 0 ▶ if ($id > $last_item + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id > $last_item + 1) 0 0 0
308 0 ▶ push @r,
Branch: 1 branch
BranchTrueFalseTotal
$last_item != $range_first_item ? : 0 0 0
309 ($last_item != $range_first_item ?
310 "$range_first_item-$last_item" :
311 "$range_first_item");
312 0 $range_first_item = $id;
313 }
314 0 $last_item = $id;
315 }
316  
317 # Take care of the last range/element
318 0 ▶ push @r,
Branch: 1 branch
BranchTrueFalseTotal
$last_item != $range_first_item ? : 0 0 0
319 ($last_item != $range_first_item ?
320 "$range_first_item-$last_item" :
321 "$range_first_item");
322  
323 0 return @r;
324 }
325  
326 0 ▶sub get_migration_path {
Subroutine get_migration_path: not called CC=3 SCAR=24.8
Pod undocumented
327 0 my ($self, $version) = @_;
328  
329 0 opendir D, $self->{migrations_dir};
330 0 my @migrations = map { "$self->{migrations_dir}/$_" }
331 grep /^0*$version-.+\..+$/,
332 readdir D;
333 0 closedir D;
334  
335 0 ▶ @migrations || die "Can't find migration $version\n";
Branch: 1 branch
BranchTrueFalseTotal
@migrations or die "Can't find migration $version\n" 0 0 0
336 0 ▶ scalar @migrations == 1 || die "More than one migration '$version'?!\n";
Branch: 1 branch
BranchTrueFalseTotal
scalar @migrations == 1 or die "More than one migration '${version}'?!\n" 0 0 0
337 0 return $migrations[0];
338 }
339  
340 0 ▶sub get_migration {
Subroutine get_migration: not called CC=1 SCAR=6.9
Pod undocumented
341 0 my ($self, $version) = @_;
342  
343 # This will throw an exception if it's not there, there is more than one or
344 # whatever. So we can assume everything was right.
345 0 my $path = $self->get_migration_path($version);
346  
347 0 open F, $path;
348 0 my $contents = join("", <F>);
349 0 close F;
350 0 return $contents;
351 }
352  
353 8 1;
354  
355 __END__
↑ Summary script/migraine.pl »