MySQL5/Migrations.pm

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