Mojo/mysql/Migrations.pm

filter: Statementstmt 21.6% 21 / 97 Branchbran 0.0% 0 / 46 Conditioncond 1.6% 1 / 59 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 46.1% 6 / 13 Podpod 100% 6 / 6 totaltotal 13.3% 34 / 255 CC 48 SCAR 57.6
CC
48
Cov
11%
from_string
600.0
migrate
272.0
_active
42.0
CRAP 317.8
↑ Summary Mojo/mysql/PubSub.pm »
1 package Mojo::mysql::Migrations;
2 18 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Mojo::Loader 'data_section';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Mojo::Util 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 ▶use constant DEBUG => $ENV{MOJO_MIGRATIONS_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_MIGRATIONS_DEBUG'} || 0
l!l
018
MC/DC0%0/1
$ENV{'MOJO_MIGRATIONS_DEBUG'} || 0
$ENV{'MOJO_MIGRATIONS_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_MIGRATIONS_DEBUG'}
Aresult
11
00
10  
11 18 has name => 'migrations';
12 18 has 'mysql';
13  
14 0 ▶sub active { $_[0]->_active($_[0]->mysql->db) }
Subroutine active: not called CC=1 SCAR=6.9
Pod documented
15  
16 sub from_data {
17 0 ▶ my ($self, $class, $name) = @_;
Subroutine from_data: not called CC=3 SCAR=24.8
Pod documented
18 0 ▶ return $self->from_string(data_section($class //= caller, $name // $self->name));
Condition0%0/6
$class //= caller
l!l&&r!l&&!r
000
$name // $self->name
l!l&&r!l&&!r
000
MC/DC0%0/2
$class //= caller
$class caller
MC/DC0%0/2
$name // $self->name
$name $self->name
Truth table0%0/3
A //= B
A= $classB= caller
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $nameB= $self->name
ABresult
1X1
011
000
19 }
20  
21 0 ▶sub from_file { shift->from_string(decode 'UTF-8', Mojo::File->new(pop)->slurp) }
Subroutine from_file: not called CC=1 SCAR=6.9
Pod documented
22  
23 sub from_string {
24 0 ▶ my ($self, $sql) = @_;
Subroutine from_string: not called CC=24 SCAR=64.0
Pod documented
25 0 ▶ return $self unless defined $sql;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql 0 0 0
26 0 my ($version, $way);
27 0 my ($new, $last, $delimiter) = (1, '', ';');
28 0 my $migrations = $self->{migrations} = {up => {}, down => {}};
29  
30 0 ▶ while (length($sql) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $sql > 0) 0 0 0
31 0 my $token;
32  
33 0 ▶ if ($sql =~ /^$delimiter/x) {
Branch: 4 branches
BranchTrueFalseTotal
if ($sql =~ /^$delimiter/ux) { } 0 0 0
elsif ($sql =~ /^delimiter\s+(\S+)\s*(?:\n|\z)/ipu) { } 0 0 0
elsif ($sql =~ /^(\s+)/su or $sql =~ /^(\w+)/u) { } 0 0 0
elsif ($sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu or $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu) { } 0 0 0
34 0 ($new, $token) = (1, $delimiter);
35 }
36 elsif ($sql =~ /^delimiter\s+(\S+)\s*(?:\n|\z)/ip) {
37 0 ($new, $token, $delimiter) = (1, ${^MATCH}, $1);
38 }
39 ▶ elsif (
Condition0%0/3
$sql =~ /^(\s+)/su or $sql =~ /^(\w+)/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$sql =~ /^(\s+)/su or $sql =~ /^(\w+)/u
$sql =~ /^(\s+)/su $sql =~ /^(\w+)/u
Truth table0%0/3
A or B
A= $sql =~ /^(\s+)/suB= $sql =~ /^(\w+)/u
ABresult
1X1
011
000
40 $sql =~ /^(\s+)/s # whitespace
41 or $sql =~ /^(\w+)/ # general name
42 )
43 {
44 0 $token = $1;
45 }
46 ▶ elsif (
Condition0%0/15
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu or $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu
l!l&&r!l&&!r
000
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu
l!l&&r!l&&!r
000
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu
l!l&&r!l&&!r
000
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu
l!l&&r!l&&!r
000
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu
l!l&&r!l&&!r
000
MC/DC0%0/6
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu or $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu
$sql =~ /^--.*(?:\n|\z)/pu $sql =~ /^\#.*(?:\n|\z)/pu $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu
Truth table0%0/7
A or B or C or D or E or F
A= $sql =~ /^--.*(?:\n|\z)/puB= $sql =~ /^\#.*(?:\n|\z)/puC= $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]puD= $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/puE= $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/puF= $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
47 $sql =~ /^--.*(?:\n|\z)/p # double-dash comment
48 or $sql =~ /^\#.*(?:\n|\z)/p # hash comment
49 or $sql =~ /^\/\*(?:[^\*]|\*[^\/])*(?:\*\/|\*\z|\z)/p # C-style comment
50 or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/p # single-quoted literal text
51 or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/p # double-quoted literal text
52 or $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/p
53 )
54 { # schema-quoted literal text
55 0 $token = ${^MATCH};
56 }
57 else {
58 0 $token = substr($sql, 0, 1);
59 }
60  
61 # chew token
62 0 substr($sql, 0, length($token), '');
63  
64 0 ▶ if ($token =~ /^--\s+(\d+)\s*(up|down)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^--\s+(\d+)\s*(up|down)/iu) 0 0 0
65 0 my ($new_version, $new_way) = ($1, lc $2);
66 0 ▶ push @{$migrations->{$way}{$version} //= []}, $last if $version and $last !~ /^\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $version and not $last =~ /^\s*$/su 0 0 0
Condition0%0/5
$version and not $last =~ /^\s*$/su
!ll&&!rl&&r
000
$migrations->{$way}{$version} //= []
l!l
00
MC/DC0%0/2
$version and not $last =~ /^\s*$/su
$version not $last =~ /^\s*$/su
MC/DC0%0/1
$migrations->{$way}{$version} //= []
$migrations->{$way}{$version}
Truth table0%0/3
A and not(B)
A= $versionB= not $last =~ /^\s*$/su
ABresult
0X0
100
111
Truth table0%0/2
A
A= $migrations->{$way}{$version}
Aresult
11
00
67 0 ($version, $way) = ($new_version, $new_way);
68 0 ($new, $last, $delimiter) = (0, '', ';');
69 }
70  
71 0 ▶ if ($new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) { } 0 0 0
72 0 ▶ push @{$migrations->{$way}{$version} //= []}, $last if $version and $last !~ /^\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $version and not $last =~ /^\s*$/su 0 0 0
Condition0%0/5
$version and not $last =~ /^\s*$/su
!ll&&!rl&&r
000
$migrations->{$way}{$version} //= []
l!l
00
MC/DC0%0/2
$version and not $last =~ /^\s*$/su
$version not $last =~ /^\s*$/su
MC/DC0%0/1
$migrations->{$way}{$version} //= []
$migrations->{$way}{$version}
Truth table0%0/3
A and not(B)
A= $versionB= not $last =~ /^\s*$/su
ABresult
0X0
100
111
Truth table0%0/2
A
A= $migrations->{$way}{$version}
Aresult
11
00
73 0 ($new, $last) = (0, '');
74 }
75 else {
76 0 $last .= $token;
77 }
78 }
79 0 ▶ push @{$migrations->{$way}{$version} //= []}, $last if $version and $last !~ /^\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $version and not $last =~ /^\s*$/su 0 0 0
Condition0%0/5
$version and not $last =~ /^\s*$/su
!ll&&!rl&&r
000
$migrations->{$way}{$version} //= []
l!l
00
MC/DC0%0/2
$version and not $last =~ /^\s*$/su
$version not $last =~ /^\s*$/su
MC/DC0%0/1
$migrations->{$way}{$version} //= []
$migrations->{$way}{$version}
Truth table0%0/3
A and not(B)
A= $versionB= not $last =~ /^\s*$/su
ABresult
0X0
100
111
Truth table0%0/2
A
A= $migrations->{$way}{$version}
Aresult
11
00
80  
81 0 return $self;
82 }
83  
84 sub latest {
85 0 ▶ (sort { $a <=> $b } keys %{shift->{migrations}{up}})[-1] || 0;
Subroutine latest: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(sort {$a <=> $b;} keys %{(shift())->{"migrations"}{"up"};})[-1] or 0 0 0 0
86 }
87  
88 sub migrate {
89 0 ▶ my ($self, $target) = @_;
Subroutine migrate: not called CC=16 SCAR=56.1
Pod documented
90 0 my $latest = $self->latest;
91 0 ▶ $target //= $latest;
Condition0%0/3
$target //= $latest
l!l&&r!l&&!r
000
MC/DC0%0/2
$target //= $latest
$target $latest
Truth table0%0/3
A //= B
A= $targetB= $latest
ABresult
1X1
011
000
92  
93 # Unknown version
94 0 my ($up, $down) = @{$self->{migrations}}{qw(up down)};
95 0 ▶ croak "Version $target has no migration" if $target != 0 && !$up->{$target};
Branch: 1 branch
BranchTrueFalseTotal
if $target != 0 and not $up->{$target} 0 0 0
Condition0%0/3
$target != 0 and not $up->{$target}
!ll&&!rl&&r
000
MC/DC0%0/2
$target != 0 and not $up->{$target}
$target != 0 not $up->{$target}
Truth table0%0/3
A and not(B)
A= $target != 0B= not $up->{$target}
ABresult
0X0
100
111
96  
97 # Already the right version (make sure migrations table exists)
98 0 my $db = $self->mysql->db;
99 0 ▶ return $self if $self->_active($db, 1) == $target;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_active($db, 1) == $target 0 0 0
100  
101 # Check version again
102 0 my $tx = $db->begin;
103 0 ▶ return $self if (my $active = $self->_active($db, 1)) == $target;
Branch: 1 branch
BranchTrueFalseTotal
if (my $active = $self->_active($db, 1)) == $target 0 0 0
104  
105 # Newer version
106 0 ▶ croak "Active version $active is greater than the latest version $latest" if $active > $latest;
Branch: 1 branch
BranchTrueFalseTotal
if $active > $latest 0 0 0
107  
108 # Up
109 0 my @sql;
110 0 ▶ if ($active < $target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active < $target) { } 0 0 0
111 0 foreach (sort { $a <=> $b } keys %$up) {
112 0 ▶ push @sql, @{$up->{$_}} if $_ <= $target && $_ > $active;
Branch: 1 branch
BranchTrueFalseTotal
if $_ <= $target and $_ > $active 0 0 0
Condition0%0/3
$_ <= $target and $_ > $active
!ll&&!rl&&r
000
MC/DC0%0/2
$_ <= $target and $_ > $active
$_ <= $target $_ > $active
Truth table0%0/3
A and B
A= $_ <= $targetB= $_ > $active
ABresult
0X0
100
111
113 }
114 }
115  
116 # Down
117 else {
118 0 foreach (reverse sort { $a <=> $b } keys %$down) {
119 0 ▶ push @sql, @{$down->{$_}} if $_ > $target && $_ <= $active;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $target and $_ <= $active 0 0 0
Condition0%0/3
$_ > $target and $_ <= $active
!ll&&!rl&&r
000
MC/DC0%0/2
$_ > $target and $_ <= $active
$_ > $target $_ <= $active
Truth table0%0/3
A and B
A= $_ > $targetB= $_ <= $active
ABresult
0X0
100
111
120 }
121 }
122  
123 0 warn "-- Migrate ($active -> $target)\n", join("\n", @sql), "\n" if DEBUG;
124 0 eval {
125 0 $db->query($_) for @sql;
126 0 $db->query("update mojo_migrations set version = ? where name = ?", $target, $self->name);
127 };
128 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
129 0 undef $tx;
130 0 die $error;
131 }
132 0 $tx->commit;
133 0 return $self;
134 }
135  
136 sub _active {
137 0 ▶ my ($self, $db, $create) = @_;
Subroutine _active: not called CC=6 SCAR=37.4
138  
139 0 my $name = $self->name;
140 0 my $results = eval { $db->query('select version from mojo_migrations where name = ?', $name) };
141 0 my $error = $@;
142 0 ▶ return 0 if !$create and !$results;
Branch: 1 branch
BranchTrueFalseTotal
unless $create or $results 0 0 0
Condition0%0/3
$create or $results
l!l&&r!l&&!r
000
MC/DC0%0/2
$create or $results
$create $results
Truth table0%0/3
A or B
A= $createB= $results
ABresult
1X1
011
000
143 0 ▶ if ($results and my $next = $results->array) { return $next->[0] }
Branch: 1 branch
BranchTrueFalseTotal
if ($results and my $next = $results->array) 0 0 0
Condition0%0/3
$results and my $next = $results->array
!ll&&!rl&&r
000
MC/DC0%0/2
$results and my $next = $results->array
$results my $next = $results->array
Truth table0%0/3
A and B
A= $resultsB= my $next = $results->array
ABresult
0X0
100
111
144  
145 $db->query(
146 0 ▶ 'create table if not exists mojo_migrations (
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
147 name varchar(128) unique not null,
148 version bigint not null
149 )'
150 ) if $error;
151 0 $db->query('insert into mojo_migrations values (?, ?)', $name, 0);
152  
153 0 return 0;
154 }
155  
156 18 1;
157  
158 =encoding utf8
159  
160 =head1 NAME
161  
162 Mojo::mysql::Migrations - Migrations
163  
164 =head1 SYNOPSIS
165  
166 use Mojo::mysql::Migrations;
167  
168 my $migrations = Mojo::mysql::Migrations->new(mysql => $mysql);
169 $migrations->from_file('/home/sri/migrations.sql')->migrate;
170  
171 =head1 DESCRIPTION
172  
173 L<Mojo::mysql::Migrations> is used by L<Mojo::mysql> to allow database schemas to
174 evolve easily over time. A migration file is just a collection of sql blocks,
175 with one or more statements, separated by comments of the form
176 C<-- VERSION UP/DOWN>.
177  
178 -- 1 up
179 create table messages (message text);
180 insert into messages values ('I ♥ Mojolicious!');
181 delimiter //
182 create procedure mojo_test()
183 begin
184 select text from messages;
185 end
186 //
187 -- 1 down
188 drop table messages;
189 drop procedure mojo_test;
190  
191 -- 2 up (...you can comment freely here...)
192 create table stuff (whatever int);
193 -- 2 down
194 drop table stuff;
195  
196 The idea is to let you migrate from any version, to any version, up and down.
197 Migrations are very safe, because they are performed in transactions and only
198 one can be performed at a time. If a single statement fails, the whole
199 migration will fail and get rolled back. Every set of migrations has a
200 L</"name">, which is stored together with the currently active version in an
201 automatically created table named C<mojo_migrations>.
202  
203 =head1 ATTRIBUTES
204  
205 L<Mojo::mysql::Migrations> implements the following attributes.
206  
207 =head2 name
208  
209 my $name = $migrations->name;
210 $migrations = $migrations->name('foo');
211  
212 Name for this set of migrations, defaults to C<migrations>.
213  
214 =head2 mysql
215  
216 my $mysql = $migrations->mysql;
217 $migrations = $migrations->mysql(Mojo::mysql->new);
218  
219 L<Mojo::mysql> object these migrations belong to.
220  
221 =head1 METHODS
222  
223 L<Mojo::mysql::Migrations> inherits all methods from L<Mojo::Base> and implements
224 the following new ones.
225  
226 =head2 active
227  
228 my $version = $migrations->active;
229  
230 Currently active version.
231  
232 =head2 from_data
233  
234 $migrations = $migrations->from_data;
235 $migrations = $migrations->from_data('main');
236 $migrations = $migrations->from_data('main', 'file_name');
237  
238 Extract migrations from a file in the DATA section of a class with
239 L<Mojo::Loader/"data_section">, defaults to using the caller class and
240 L</"name">.
241  
242 __DATA__
243 @@ migrations
244 -- 1 up
245 create table messages (message text);
246 insert into messages values ('I ♥ Mojolicious!');
247 -- 1 down
248 drop table messages;
249  
250 =head2 from_file
251  
252 $migrations = $migrations->from_file('/home/sri/migrations.sql');
253  
254 Extract migrations from a file.
255  
256 =head2 from_string
257  
258 $migrations = $migrations->from_string(
259 '-- 1 up
260 create table foo (bar int);
261 -- 1 down
262 drop table foo;'
263 );
264  
265 Extract migrations from string.
266  
267 =head2 latest
268  
269 my $version = $migrations->latest;
270  
271 Latest version available.
272  
273 =head2 migrate
274  
275 $migrations = $migrations->migrate;
276 $migrations = $migrations->migrate(3);
277  
278 Migrate from L</"active"> to a different version, up or down, defaults to
279 using L</"latest">. All version numbers need to be positive, with version C<0>
280 representing an empty database.
281  
282 # Reset database
283 $migrations->migrate(0)->migrate;
284  
285 =head1 DEBUGGING
286  
287 You can set the C<MOJO_MIGRATIONS_DEBUG> environment variable to get some
288 advanced diagnostics information printed to C<STDERR>.
289  
290 MOJO_MIGRATIONS_DEBUG=1
291  
292 =head1 SEE ALSO
293  
294 L<Mojo::mysql>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
295  
296 =cut
↑ Summary Mojo/mysql/PubSub.pm »