Commit | Line | Data |
45d0d9d5 |
1 | package DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator; |
334bced5 |
2 | use Moose; |
9af9d0b2 |
3 | |
9a3a62f1 |
4 | # ABSTRACT: Manage your SQL and Perl migrations in nicely laid out directories |
5 | |
9af9d0b2 |
6 | use autodie; |
7 | use Carp qw( carp croak ); |
56203087 |
8 | use DBIx::Class::DeploymentHandler::LogImporter qw(:log :dlog); |
1fcd4fe6 |
9 | use Context::Preserve; |
9af9d0b2 |
10 | |
7f50d101 |
11 | use Try::Tiny; |
9af9d0b2 |
12 | |
d23c7c77 |
13 | use SQL::Translator; |
14 | require SQL::Translator::Diff; |
9af9d0b2 |
15 | |
d23c7c77 |
16 | require DBIx::Class::Storage; # loaded for type constraint |
41863428 |
17 | use DBIx::Class::DeploymentHandler::Types; |
18 | |
297e662f |
19 | use Path::Class qw(file dir); |
2e68a8e1 |
20 | |
7521a845 |
21 | with 'DBIx::Class::DeploymentHandler::HandlesDeploy'; |
3c1b5ee8 |
22 | |
93460690 |
23 | has ignore_ddl => ( |
24 | isa => 'Bool', |
25 | is => 'ro', |
26 | default => undef, |
27 | ); |
28 | |
92624ee5 |
29 | has force_overwrite => ( |
30 | isa => 'Bool', |
31 | is => 'ro', |
32 | default => undef, |
33 | ); |
34 | |
d54b8d69 |
35 | has schema => ( |
d54b8d69 |
36 | is => 'ro', |
37 | required => 1, |
d54b8d69 |
38 | ); |
39 | |
334bced5 |
40 | has storage => ( |
41 | isa => 'DBIx::Class::Storage', |
42 | is => 'ro', |
43 | lazy_build => 1, |
44 | ); |
45 | |
6e2665d3 |
46 | sub _build_storage { |
47 | my $self = shift; |
2eaf903b |
48 | my $s = $self->schema->storage; |
49 | $s->_determine_driver; |
50 | $s |
51 | } |
52 | |
02a7b8ac |
53 | has sql_translator_args => ( |
334bced5 |
54 | isa => 'HashRef', |
55 | is => 'ro', |
56 | default => sub { {} }, |
57 | ); |
91adde75 |
58 | has script_directory => ( |
334bced5 |
59 | isa => 'Str', |
60 | is => 'ro', |
61 | required => 1, |
62 | default => 'sql', |
63 | ); |
64 | |
334bced5 |
65 | has databases => ( |
66 | coerce => 1, |
67 | isa => 'DBIx::Class::DeploymentHandler::Databases', |
68 | is => 'ro', |
69 | default => sub { [qw( MySQL SQLite PostgreSQL )] }, |
70 | ); |
71 | |
a7d53deb |
72 | has txn_wrap => ( |
73 | is => 'ro', |
74 | isa => 'Bool', |
75 | default => 1, |
76 | ); |
77 | |
73caa630 |
78 | has schema_version => ( |
79 | is => 'ro', |
e86c0c07 |
80 | isa => 'Str', |
73caa630 |
81 | lazy_build => 1, |
82 | ); |
83 | |
6df6dcb9 |
84 | # this will probably never get called as the DBICDH |
85 | # will be passing down a schema_version normally, which |
86 | # is built the same way, but we leave this in place |
115c68ce |
87 | sub _build_schema_version { |
6e2665d3 |
88 | my $self = shift; |
115c68ce |
89 | $self->schema->schema_version |
6e2665d3 |
90 | } |
73caa630 |
91 | |
6e2665d3 |
92 | sub __ddl_consume_with_prefix { |
93 | my ($self, $type, $versions, $prefix) = @_; |
91adde75 |
94 | my $base_dir = $self->script_directory; |
262166c1 |
95 | |
297e662f |
96 | my $main = dir( $base_dir, $type ); |
76d08d08 |
97 | my $common = |
297e662f |
98 | dir( $base_dir, '_common', $prefix, join q(-), @{$versions} ); |
262166c1 |
99 | |
25c3bec3 |
100 | my $common_any = |
297e662f |
101 | dir( $base_dir, '_common', $prefix, '_any' ); |
25c3bec3 |
102 | |
262166c1 |
103 | my $dir; |
104 | if (-d $main) { |
297e662f |
105 | $dir = dir($main, $prefix, join q(-), @{$versions}) |
262166c1 |
106 | } else { |
38857f30 |
107 | if ($self->ignore_ddl) { |
108 | return [] |
109 | } else { |
110 | croak "$main does not exist; please write/generate some SQL" |
111 | } |
262166c1 |
112 | } |
297e662f |
113 | my $dir_any = dir($main, $prefix, '_any'); |
262166c1 |
114 | |
ef44838b |
115 | my %files; |
116 | try { |
117 | opendir my($dh), $dir; |
118 | %files = |
119 | map { $_ => "$dir/$_" } |
120 | grep { /\.(?:sql|pl|sql-\w+)$/ && -f "$dir/$_" } |
121 | readdir $dh; |
122 | closedir $dh; |
123 | } catch { |
124 | die $_ unless $self->ignore_ddl; |
125 | }; |
25c3bec3 |
126 | for my $dirname (grep { -d $_ } $common, $common_any, $dir_any) { |
127 | opendir my($dh), $dirname; |
297e662f |
128 | for my $filename (grep { /\.(?:sql|pl)$/ && -f file($dirname,$_) } readdir $dh) { |
262166c1 |
129 | unless ($files{$filename}) { |
297e662f |
130 | $files{$filename} = file($dirname,$filename); |
262166c1 |
131 | } |
132 | } |
133 | closedir $dh; |
134 | } |
135 | |
136 | return [@files{sort keys %files}] |
137 | } |
3c1b5ee8 |
138 | |
6e2665d3 |
139 | sub _ddl_initialize_consume_filenames { |
140 | my ($self, $type, $version) = @_; |
ff40cb1f |
141 | $self->__ddl_consume_with_prefix($type, [ $version ], 'initialize') |
fc4b7602 |
142 | } |
143 | |
6e2665d3 |
144 | sub _ddl_schema_consume_filenames { |
145 | my ($self, $type, $version) = @_; |
58eb99c3 |
146 | $self->__ddl_consume_with_prefix($type, [ $version ], 'deploy') |
3c1b5ee8 |
147 | } |
148 | |
6e2665d3 |
149 | sub _ddl_protoschema_deploy_consume_filenames { |
150 | my ($self, $version) = @_; |
c72832d7 |
151 | my $base_dir = $self->script_directory; |
152 | |
297e662f |
153 | my $dir = dir( $base_dir, '_source', 'deploy', $version); |
c72832d7 |
154 | return [] unless -d $dir; |
155 | |
156 | opendir my($dh), $dir; |
157 | my %files = map { $_ => "$dir/$_" } grep { /\.yml$/ && -f "$dir/$_" } readdir $dh; |
158 | closedir $dh; |
159 | |
160 | return [@files{sort keys %files}] |
161 | } |
162 | |
6e2665d3 |
163 | sub _ddl_protoschema_upgrade_consume_filenames { |
164 | my ($self, $versions) = @_; |
f9c6ab50 |
165 | my $base_dir = $self->script_directory; |
166 | |
297e662f |
167 | my $dir = dir( $base_dir, '_preprocess_schema', 'upgrade', join q(-), @{$versions}); |
f9c6ab50 |
168 | |
169 | return [] unless -d $dir; |
170 | |
171 | opendir my($dh), $dir; |
172 | my %files = map { $_ => "$dir/$_" } grep { /\.pl$/ && -f "$dir/$_" } readdir $dh; |
173 | closedir $dh; |
174 | |
175 | return [@files{sort keys %files}] |
176 | } |
177 | |
6e2665d3 |
178 | sub _ddl_protoschema_downgrade_consume_filenames { |
179 | my ($self, $versions) = @_; |
f9c6ab50 |
180 | my $base_dir = $self->script_directory; |
181 | |
297e662f |
182 | my $dir = dir( $base_dir, '_preprocess_schema', 'downgrade', join q(-), @{$versions}); |
f9c6ab50 |
183 | |
184 | return [] unless -d $dir; |
185 | |
186 | opendir my($dh), $dir; |
187 | my %files = map { $_ => "$dir/$_" } grep { /\.pl$/ && -f "$dir/$_" } readdir $dh; |
188 | closedir $dh; |
189 | |
190 | return [@files{sort keys %files}] |
191 | } |
192 | |
6e2665d3 |
193 | sub _ddl_protoschema_produce_filename { |
194 | my ($self, $version) = @_; |
297e662f |
195 | my $dirname = dir( $self->script_directory, '_source', 'deploy', $version ); |
196 | $dirname->mkpath unless -d $dirname; |
7e08eddd |
197 | |
297e662f |
198 | return "" . file( $dirname, '001-auto.yml' ); |
7e08eddd |
199 | } |
200 | |
6e2665d3 |
201 | sub _ddl_schema_produce_filename { |
202 | my ($self, $type, $version) = @_; |
297e662f |
203 | my $dirname = dir( $self->script_directory, $type, 'deploy', $version ); |
204 | $dirname->mkpath unless -d $dirname; |
d54b8d69 |
205 | |
297e662f |
206 | return "" . file( $dirname, '001-auto.sql' ); |
d54b8d69 |
207 | } |
208 | |
6e2665d3 |
209 | sub _ddl_schema_upgrade_consume_filenames { |
210 | my ($self, $type, $versions) = @_; |
58eb99c3 |
211 | $self->__ddl_consume_with_prefix($type, $versions, 'upgrade') |
3c1b5ee8 |
212 | } |
213 | |
6e2665d3 |
214 | sub _ddl_schema_downgrade_consume_filenames { |
215 | my ($self, $type, $versions) = @_; |
58eb99c3 |
216 | $self->__ddl_consume_with_prefix($type, $versions, 'downgrade') |
a41a04e5 |
217 | } |
218 | |
6e2665d3 |
219 | sub _ddl_schema_upgrade_produce_filename { |
220 | my ($self, $type, $versions) = @_; |
91adde75 |
221 | my $dir = $self->script_directory; |
76d311e7 |
222 | |
297e662f |
223 | my $dirname = dir( $dir, $type, 'upgrade', join q(-), @{$versions}); |
224 | $dirname->mkpath unless -d $dirname; |
a41a04e5 |
225 | |
297e662f |
226 | return "" . file( $dirname, '001-auto.sql' ); |
a41a04e5 |
227 | } |
228 | |
6e2665d3 |
229 | sub _ddl_schema_downgrade_produce_filename { |
230 | my ($self, $type, $versions, $dir) = @_; |
297e662f |
231 | my $dirname = dir( $dir, $type, 'downgrade', join q(-), @{$versions} ); |
232 | $dirname->mkpath unless -d $dirname; |
24f4524b |
233 | |
297e662f |
234 | return "" . file( $dirname, '001-auto.sql'); |
24f4524b |
235 | } |
236 | |
6e2665d3 |
237 | sub _run_sql_array { |
238 | my ($self, $sql) = @_; |
41219a5d |
239 | my $storage = $self->storage; |
5d7b27cf |
240 | |
115c68ce |
241 | $sql = [ _split_sql_chunk( @$sql ) ]; |
1f0d0633 |
242 | |
f4075791 |
243 | Dlog_trace { "Running SQL $_" } $sql; |
f36afe83 |
244 | foreach my $line (@{$sql}) { |
5d7b27cf |
245 | $storage->_query_start($line); |
10a62c3d |
246 | # the whole reason we do this is so that we can see the line that was run |
5d7b27cf |
247 | try { |
5d7b27cf |
248 | $storage->dbh_do (sub { $_[1]->do($line) }); |
249 | } |
250 | catch { |
10a62c3d |
251 | die "$_ (running line '$line')" |
60e09fce |
252 | }; |
5d7b27cf |
253 | $storage->_query_end($line); |
254 | } |
4d09f712 |
255 | return join "\n", @$sql |
f36afe83 |
256 | } |
257 | |
115c68ce |
258 | # split a chunk o' SQL into statements |
259 | sub _split_sql_chunk { |
260 | my @sql = map { split /;\n/, $_ } @_; |
261 | |
262 | for ( @sql ) { |
263 | # strip transactions |
264 | s/^(?:BEGIN|BEGIN TRANSACTION|COMMIT).*//mgi; |
265 | |
266 | # trim whitespaces |
55e13b19 |
267 | s/^\s+//gm; |
268 | s/\s+$//gm; |
115c68ce |
269 | |
270 | # remove comments |
271 | s/^--.*//gm; |
272 | |
273 | # remove blank lines |
55e13b19 |
274 | s/^\n//gm; |
115c68ce |
275 | |
276 | # put on single line |
277 | s/\n/ /g; |
278 | } |
279 | |
55e13b19 |
280 | return grep $_, @sql; |
115c68ce |
281 | } |
282 | |
6e2665d3 |
283 | sub _run_sql { |
284 | my ($self, $filename) = @_; |
f4075791 |
285 | log_debug { "Running SQL from $filename" }; |
f36afe83 |
286 | return $self->_run_sql_array($self->_read_sql_file($filename)); |
5d7b27cf |
287 | } |
2e68a8e1 |
288 | |
a89cd737 |
289 | sub _load_sandbox { |
290 | my $_file = shift; |
291 | |
292 | my $_package = $_file; |
32bd7c45 |
293 | $_package =~ s/([^A-Za-z0-9_])/sprintf("_%2x", ord($1))/eg; |
a89cd737 |
294 | |
32bd7c45 |
295 | my $fn = eval sprintf <<'END_EVAL', $_package; |
a89cd737 |
296 | package DBICDH::Sandbox::%s; |
297 | { |
32bd7c45 |
298 | our $app; |
299 | $app ||= require $_file; |
a89cd737 |
300 | if ( !$app && ( my $error = $@ || $! )) { die $error; } |
301 | $app; |
302 | } |
303 | END_EVAL |
32bd7c45 |
304 | |
305 | croak $@ if $@; |
306 | |
80dcc097 |
307 | croak "$_file should define an anonymous sub that takes a schema but it didn't!" |
32bd7c45 |
308 | unless ref $fn && ref $fn eq 'CODE'; |
309 | |
310 | return $fn; |
a89cd737 |
311 | } |
312 | |
6e2665d3 |
313 | sub _run_perl { |
314 | my ($self, $filename, $versions) = @_; |
f4075791 |
315 | log_debug { "Running Perl from $filename" }; |
c8a2f7bd |
316 | |
a89cd737 |
317 | my $fn = _load_sandbox($filename); |
318 | |
f4075791 |
319 | Dlog_trace { "Running Perl $_" } $fn; |
5d7b27cf |
320 | |
32bd7c45 |
321 | $fn->($self->schema, $versions) |
5d7b27cf |
322 | } |
5d7b27cf |
323 | |
1fcd4fe6 |
324 | sub txn_do { |
325 | my ( $self, $code ) = @_; |
326 | return $code->() unless $self->txn_wrap; |
327 | |
328 | my $guard = $self->schema->txn_scope_guard; |
329 | |
330 | return preserve_context { $code->() } after => sub { $guard->commit }; |
331 | } |
332 | |
6e2665d3 |
333 | sub _run_sql_and_perl { |
334 | my ($self, $filenames, $sql_to_run, $versions) = @_; |
5d7b27cf |
335 | my @files = @{$filenames}; |
1fcd4fe6 |
336 | $self->txn_do(sub { |
337 | $self->_run_sql_array($sql_to_run) if $self->ignore_ddl; |
338 | |
339 | my $sql = ($sql_to_run)?join ";\n", @$sql_to_run:''; |
340 | FILENAME: |
297e662f |
341 | for my $filename (map file($_), @files) { |
48c4a2bb |
342 | if ($self->ignore_ddl && $filename->basename =~ /^[^-]*-auto.*\.sql$/) { |
1fcd4fe6 |
343 | next FILENAME |
344 | } elsif ($filename =~ /\.sql$/) { |
345 | $sql .= $self->_run_sql($filename) |
346 | } elsif ( $filename =~ /\.pl$/ ) { |
347 | $self->_run_perl($filename, $versions) |
348 | } else { |
349 | croak "A file ($filename) got to deploy that wasn't sql or perl!"; |
350 | } |
351 | } |
352 | |
353 | return $sql; |
354 | }); |
41219a5d |
355 | } |
356 | |
357 | sub deploy { |
358 | my $self = shift; |
be140a5f |
359 | my $version = (shift @_ || {})->{version} || $self->schema_version; |
f4075791 |
360 | log_info { "deploying version $version" }; |
ef44838b |
361 | my $sqlt_type = $self->storage->sqlt_type; |
362 | my $sql; |
b6bd39e5 |
363 | my $sqltargs = $self->sql_translator_args; |
ef44838b |
364 | if ($self->ignore_ddl) { |
b6bd39e5 |
365 | $sql = $self->_sql_from_yaml($sqltargs, |
4ea5caf5 |
366 | '_ddl_protoschema_deploy_consume_filenames', $sqlt_type |
ef44838b |
367 | ); |
368 | } |
369 | return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames( |
370 | $sqlt_type, |
371 | $version, |
25c3bec3 |
372 | ), $sql, [$version]); |
2e68a8e1 |
373 | } |
374 | |
ff40cb1f |
375 | sub initialize { |
9faec51a |
376 | my $self = shift; |
377 | my $args = shift; |
378 | my $version = $args->{version} || $self->schema_version; |
ff40cb1f |
379 | log_info { "initializing version $version" }; |
9faec51a |
380 | my $storage_type = $args->{storage_type} || $self->storage->sqlt_type; |
fc4b7602 |
381 | |
ff40cb1f |
382 | my @files = @{$self->_ddl_initialize_consume_filenames( |
9faec51a |
383 | $storage_type, |
fc4b7602 |
384 | $version, |
385 | )}; |
386 | |
387 | for my $filename (@files) { |
388 | # We ignore sql for now (till I figure out what to do with it) |
389 | if ( $filename =~ /^(.+)\.pl$/ ) { |
fc4b7602 |
390 | my $filedata = do { local( @ARGV, $/ ) = $filename; <> }; |
fc4b7602 |
391 | |
9faec51a |
392 | no warnings 'redefine'; |
5b5defbc |
393 | my $fn = eval "$filedata"; |
fc4b7602 |
394 | use warnings; |
5b5defbc |
395 | |
9faec51a |
396 | if ($@) { |
6e9a733d |
397 | croak "$filename failed to compile: $@"; |
9faec51a |
398 | } elsif (ref $fn eq 'CODE') { |
fc4b7602 |
399 | $fn->() |
400 | } else { |
6e9a733d |
401 | croak "$filename should define an anonymous sub but it didn't!"; |
fc4b7602 |
402 | } |
403 | } else { |
ff40cb1f |
404 | croak "A file ($filename) got to initialize_scripts that wasn't sql or perl!"; |
fc4b7602 |
405 | } |
406 | } |
407 | } |
408 | |
6e2665d3 |
409 | sub _sqldiff_from_yaml { |
410 | my ($self, $from_version, $to_version, $db, $direction) = @_; |
91adde75 |
411 | my $dir = $self->script_directory; |
28563f97 |
412 | my $sqltargs = { |
413 | add_drop_table => 1, |
414 | ignore_constraint_names => 1, |
415 | ignore_index_names => 1, |
416 | %{$self->sql_translator_args} |
417 | }; |
d54b8d69 |
418 | |
28563f97 |
419 | my $source_schema; |
420 | { |
421 | my $prefilename = $self->_ddl_protoschema_produce_filename($from_version, $dir); |
422 | |
423 | # should probably be a croak |
424 | carp("No previous schema file found ($prefilename)") |
425 | unless -e $prefilename; |
426 | |
427 | my $t = SQL::Translator->new({ |
428 | %{$sqltargs}, |
429 | debug => 0, |
430 | trace => 0, |
431 | parser => 'SQL::Translator::Parser::YAML', |
432 | }); |
433 | |
434 | my $out = $t->translate( $prefilename ) |
435 | or croak($t->error); |
436 | |
437 | $source_schema = $t->schema; |
438 | |
439 | $source_schema->name( $prefilename ) |
440 | unless $source_schema->name; |
441 | } |
442 | |
443 | my $dest_schema; |
444 | { |
445 | my $filename = $self->_ddl_protoschema_produce_filename($to_version, $dir); |
446 | |
447 | # should probably be a croak |
448 | carp("No next schema file found ($filename)") |
449 | unless -e $filename; |
450 | |
451 | my $t = SQL::Translator->new({ |
452 | %{$sqltargs}, |
453 | debug => 0, |
454 | trace => 0, |
455 | parser => 'SQL::Translator::Parser::YAML', |
456 | }); |
457 | |
458 | my $out = $t->translate( $filename ) |
459 | or croak($t->error); |
460 | |
461 | $dest_schema = $t->schema; |
462 | |
463 | $dest_schema->name( $filename ) |
464 | unless $dest_schema->name; |
465 | } |
f9c6ab50 |
466 | |
467 | my $transform_files_method = "_ddl_protoschema_${direction}_consume_filenames"; |
468 | my $transforms = $self->_coderefs_per_files( |
469 | $self->$transform_files_method([$from_version, $to_version]) |
470 | ); |
471 | $_->($source_schema, $dest_schema) for @$transforms; |
472 | |
28563f97 |
473 | return [SQL::Translator::Diff::schema_diff( |
474 | $source_schema, $db, |
475 | $dest_schema, $db, |
476 | $sqltargs |
477 | )]; |
478 | } |
479 | |
6e2665d3 |
480 | sub _sql_from_yaml { |
481 | my ($self, $sqltargs, $from_file, $db) = @_; |
28563f97 |
482 | my $schema = $self->schema; |
483 | my $version = $self->schema_version; |
93460690 |
484 | |
4ea5caf5 |
485 | my @sql; |
2e68a8e1 |
486 | |
4ea5caf5 |
487 | my $actual_file = $self->$from_file($version); |
f08935ea |
488 | for my $yaml_filename (@{( |
4ea5caf5 |
489 | DlogS_trace { "generating SQL from Serialized SQL Files: $_" } |
490 | (ref $actual_file?$actual_file:[$actual_file]) |
f08935ea |
491 | )}) { |
4ea5caf5 |
492 | my $sqlt = SQL::Translator->new({ |
493 | add_drop_table => 0, |
494 | parser => 'SQL::Translator::Parser::YAML', |
495 | %{$sqltargs}, |
496 | producer => $db, |
497 | }); |
498 | |
499 | push @sql, $sqlt->translate($yaml_filename); |
500 | if(!@sql) { |
501 | carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); |
502 | return undef; |
503 | } |
28563f97 |
504 | } |
505 | return \@sql; |
506 | } |
507 | |
508 | sub _prepare_install { |
509 | my $self = shift; |
510 | my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; |
511 | my $from_file = shift; |
512 | my $to_file = shift; |
513 | my $dir = $self->script_directory; |
514 | my $databases = $self->databases; |
515 | my $version = $self->schema_version; |
516 | |
2e68a8e1 |
517 | foreach my $db (@$databases) { |
28563f97 |
518 | my $sql = $self->_sql_from_yaml($sqltargs, $from_file, $db ) or next; |
2e68a8e1 |
519 | |
c8a2f7bd |
520 | my $filename = $self->$to_file($db, $version, $dir); |
9600776d |
521 | if (-e $filename ) { |
92624ee5 |
522 | if ($self->force_overwrite) { |
523 | carp "Overwriting existing DDL file - $filename"; |
524 | unlink $filename; |
525 | } else { |
526 | die "Cannot overwrite '$filename', either enable force_overwrite or delete it" |
527 | } |
2e68a8e1 |
528 | } |
387b11d2 |
529 | open my $file, q(>), $filename; |
870526b1 |
530 | print {$file} join ";\n", @$sql, ''; |
2e68a8e1 |
531 | close $file; |
532 | } |
533 | } |
534 | |
c8a2f7bd |
535 | sub _resultsource_install_filename { |
536 | my ($self, $source_name) = @_; |
537 | return sub { |
538 | my ($self, $type, $version) = @_; |
297e662f |
539 | my $dirname = dir( $self->script_directory, $type, 'deploy', $version ); |
540 | $dirname->mkpath unless -d $dirname; |
c8a2f7bd |
541 | |
297e662f |
542 | return "" . file( $dirname, "001-auto-$source_name.sql" ); |
c8a2f7bd |
543 | } |
544 | } |
545 | |
e62add58 |
546 | sub _resultsource_protoschema_filename { |
547 | my ($self, $source_name) = @_; |
548 | return sub { |
549 | my ($self, $version) = @_; |
297e662f |
550 | my $dirname = dir( $self->script_directory, '_source', 'deploy', $version ); |
551 | $dirname->mkpath unless -d $dirname; |
e62add58 |
552 | |
297e662f |
553 | return "" . file( $dirname, "001-auto-$source_name.yml" ); |
e62add58 |
554 | } |
555 | } |
556 | |
c8a2f7bd |
557 | sub install_resultsource { |
be140a5f |
558 | my ($self, $args) = @_; |
ba99ba44 |
559 | my $source = $args->{result_source} |
560 | or die 'result_source must be passed to install_resultsource'; |
561 | my $version = $args->{version} |
562 | or die 'version must be passed to install_resultsource'; |
f4075791 |
563 | log_info { 'installing_resultsource ' . $source->source_name . ", version $version" }; |
c8a2f7bd |
564 | my $rs_install_file = |
565 | $self->_resultsource_install_filename($source->source_name); |
566 | |
567 | my $files = [ |
568 | $self->$rs_install_file( |
569 | $self->storage->sqlt_type, |
570 | $version, |
571 | ) |
572 | ]; |
25c3bec3 |
573 | $self->_run_sql_and_perl($files, '', [$version]); |
c8a2f7bd |
574 | } |
575 | |
576 | sub prepare_resultsource_install { |
577 | my $self = shift; |
be140a5f |
578 | my $source = (shift @_)->{result_source}; |
f4075791 |
579 | log_info { 'preparing install for resultsource ' . $source->source_name }; |
c8a2f7bd |
580 | |
e62add58 |
581 | my $install_filename = $self->_resultsource_install_filename($source->source_name); |
582 | my $proto_filename = $self->_resultsource_protoschema_filename($source->source_name); |
6cae2f56 |
583 | $self->prepare_protoschema({ |
c8a2f7bd |
584 | parser_args => { sources => [$source->source_name], } |
e62add58 |
585 | }, $proto_filename); |
586 | $self->_prepare_install({}, $proto_filename, $install_filename); |
c8a2f7bd |
587 | } |
588 | |
91557c90 |
589 | sub prepare_deploy { |
f4075791 |
590 | log_info { 'preparing deploy' }; |
c8a2f7bd |
591 | my $self = shift; |
6776a6d4 |
592 | $self->prepare_protoschema({ |
593 | # Exclude __VERSION so that it gets installed separately |
cd41f44f |
594 | parser_args => { |
595 | sources => [ |
596 | sort { $a cmp $b } |
597 | grep { $_ ne '__VERSION' } |
598 | $self->schema->sources |
599 | ], |
600 | } |
6776a6d4 |
601 | }, '_ddl_protoschema_produce_filename'); |
e62add58 |
602 | $self->_prepare_install({}, '_ddl_protoschema_produce_filename', '_ddl_schema_produce_filename'); |
c8a2f7bd |
603 | } |
604 | |
a41a04e5 |
605 | sub prepare_upgrade { |
be140a5f |
606 | my ($self, $args) = @_; |
0df68524 |
607 | log_info { |
f4075791 |
608 | "preparing upgrade from $args->{from_version} to $args->{to_version}" |
0df68524 |
609 | }; |
be140a5f |
610 | $self->_prepare_changegrade( |
58eb99c3 |
611 | $args->{from_version}, $args->{to_version}, $args->{version_set}, 'upgrade' |
be140a5f |
612 | ); |
76d311e7 |
613 | } |
614 | |
615 | sub prepare_downgrade { |
be140a5f |
616 | my ($self, $args) = @_; |
0df68524 |
617 | log_info { |
f4075791 |
618 | "preparing downgrade from $args->{from_version} to $args->{to_version}" |
0df68524 |
619 | }; |
be140a5f |
620 | $self->_prepare_changegrade( |
58eb99c3 |
621 | $args->{from_version}, $args->{to_version}, $args->{version_set}, 'downgrade' |
be140a5f |
622 | ); |
76d311e7 |
623 | } |
624 | |
6e2665d3 |
625 | sub _coderefs_per_files { |
626 | my ($self, $files) = @_; |
f9c6ab50 |
627 | no warnings 'redefine'; |
628 | [map eval do { local( @ARGV, $/ ) = $_; <> }, @$files] |
629 | } |
630 | |
6e2665d3 |
631 | sub _prepare_changegrade { |
632 | my ($self, $from_version, $to_version, $version_set, $direction) = @_; |
2e68a8e1 |
633 | my $schema = $self->schema; |
634 | my $databases = $self->databases; |
91adde75 |
635 | my $dir = $self->script_directory; |
2e68a8e1 |
636 | |
73caa630 |
637 | my $schema_version = $self->schema_version; |
e62add58 |
638 | my $diff_file_method = "_ddl_schema_${direction}_produce_filename"; |
e62add58 |
639 | foreach my $db (@$databases) { |
640 | my $diff_file = $self->$diff_file_method($db, $version_set, $dir ); |
641 | if(-e $diff_file) { |
92624ee5 |
642 | if ($self->force_overwrite) { |
643 | carp("Overwriting existing $direction-diff file - $diff_file"); |
644 | unlink $diff_file; |
645 | } else { |
646 | die "Cannot overwrite '$diff_file', either enable force_overwrite or delete it" |
647 | } |
2e68a8e1 |
648 | } |
649 | |
387b11d2 |
650 | open my $file, q(>), $diff_file; |
f9c6ab50 |
651 | print {$file} join ";\n", @{$self->_sqldiff_from_yaml($from_version, $to_version, $db, $direction)}; |
2e68a8e1 |
652 | close $file; |
653 | } |
654 | } |
655 | |
6e2665d3 |
656 | sub _read_sql_file { |
657 | my ($self, $file) = @_; |
334bced5 |
658 | return unless $file; |
659 | |
115c68ce |
660 | local $/ = undef; #sluuuuuurp |
661 | |
aabd4237 |
662 | open my $fh, '<', $file; |
115c68ce |
663 | return [ _split_sql_chunk( <$fh> ) ]; |
1f0d0633 |
664 | } |
665 | |
7d2a6974 |
666 | sub downgrade_single_step { |
76d311e7 |
667 | my $self = shift; |
be140a5f |
668 | my $version_set = (shift @_)->{version_set}; |
f4075791 |
669 | Dlog_info { "downgrade_single_step'ing $_" } $version_set; |
41219a5d |
670 | |
ef44838b |
671 | my $sqlt_type = $self->storage->sqlt_type; |
672 | my $sql_to_run; |
673 | if ($self->ignore_ddl) { |
674 | $sql_to_run = $self->_sqldiff_from_yaml( |
58eb99c3 |
675 | $version_set->[0], $version_set->[1], $sqlt_type, 'downgrade', |
ef44838b |
676 | ); |
677 | } |
58eb99c3 |
678 | my $sql = $self->_run_sql_and_perl($self->_ddl_schema_downgrade_consume_filenames( |
ef44838b |
679 | $sqlt_type, |
627581cd |
680 | $version_set, |
25c3bec3 |
681 | ), $sql_to_run, $version_set); |
3249629f |
682 | |
41219a5d |
683 | return ['', $sql]; |
76d311e7 |
684 | } |
685 | |
7d2a6974 |
686 | sub upgrade_single_step { |
7521a845 |
687 | my $self = shift; |
be140a5f |
688 | my $version_set = (shift @_)->{version_set}; |
f4075791 |
689 | Dlog_info { "upgrade_single_step'ing $_" } $version_set; |
41219a5d |
690 | |
ef44838b |
691 | my $sqlt_type = $self->storage->sqlt_type; |
692 | my $sql_to_run; |
693 | if ($self->ignore_ddl) { |
694 | $sql_to_run = $self->_sqldiff_from_yaml( |
58eb99c3 |
695 | $version_set->[0], $version_set->[1], $sqlt_type, 'upgrade', |
ef44838b |
696 | ); |
697 | } |
58eb99c3 |
698 | my $sql = $self->_run_sql_and_perl($self->_ddl_schema_upgrade_consume_filenames( |
ef44838b |
699 | $sqlt_type, |
627581cd |
700 | $version_set, |
25c3bec3 |
701 | ), $sql_to_run, $version_set); |
41219a5d |
702 | return ['', $sql]; |
334bced5 |
703 | } |
704 | |
6cae2f56 |
705 | sub prepare_protoschema { |
7e08eddd |
706 | my $self = shift; |
e62add58 |
707 | my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; |
708 | my $to_file = shift; |
7e08eddd |
709 | my $filename |
e62add58 |
710 | = $self->$to_file($self->schema_version); |
7e08eddd |
711 | |
e62add58 |
712 | # we do this because the code that uses this sets parser args, |
713 | # so we just need to merge in the package |
7e08eddd |
714 | my $sqlt = SQL::Translator->new({ |
715 | parser => 'SQL::Translator::Parser::DBIx::Class', |
716 | producer => 'SQL::Translator::Producer::YAML', |
e62add58 |
717 | %{ $sqltargs }, |
7e08eddd |
718 | }); |
719 | |
dc223c88 |
720 | my $yml = $sqlt->translate(data => $self->schema); |
7e08eddd |
721 | |
722 | croak("Failed to translate to YAML: " . $sqlt->error) |
723 | unless $yml; |
724 | |
725 | if (-e $filename ) { |
92624ee5 |
726 | if ($self->force_overwrite) { |
727 | carp "Overwriting existing DDL-YML file - $filename"; |
728 | unlink $filename; |
729 | } else { |
730 | die "Cannot overwrite '$filename', either enable force_overwrite or delete it" |
731 | } |
7e08eddd |
732 | } |
733 | |
734 | open my $file, q(>), $filename; |
735 | print {$file} $yml; |
736 | close $file; |
737 | } |
738 | |
aabd4237 |
739 | __PACKAGE__->meta->make_immutable; |
740 | |
2e68a8e1 |
741 | 1; |
e051bb00 |
742 | |
e52174e3 |
743 | # vim: ts=2 sw=2 expandtab |
744 | |
e051bb00 |
745 | __END__ |
746 | |
bcc72297 |
747 | =head1 DESCRIPTION |
748 | |
e62add58 |
749 | This class is the meat of L<DBIx::Class::DeploymentHandler>. It takes care |
750 | of generating serialized schemata as well as sql files to move from one |
751 | version of a schema to the rest. One of the hallmark features of this class |
752 | is that it allows for multiple sql files for deploy and upgrade, allowing |
753 | developers to fine tune deployment. In addition it also allows for perl |
754 | files to be run at any stage of the process. |
bcc72297 |
755 | |
756 | For basic usage see L<DBIx::Class::DeploymentHandler::HandlesDeploy>. What's |
757 | documented here is extra fun stuff or private methods. |
758 | |
759 | =head1 DIRECTORY LAYOUT |
760 | |
39c88a9a |
761 | Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>. |
762 | It's spiritually based upon L<DBIx::Migration::Directories>, but has a |
763 | lot of extensions and modifications, so even if you are familiar with it, |
764 | please read this. I feel like the best way to describe the layout is with |
765 | the following example: |
92c34cab |
766 | |
767 | $sql_migration_dir |
58eb99c3 |
768 | |- _source |
769 | | |- deploy |
03882cab |
770 | | |- 1 |
771 | | | `- 001-auto.yml |
772 | | |- 2 |
773 | | | `- 001-auto.yml |
774 | | `- 3 |
775 | | `- 001-auto.yml |
92c34cab |
776 | |- SQLite |
58eb99c3 |
777 | | |- downgrade |
4f85efc6 |
778 | | | `- 2-1 |
e62add58 |
779 | | | `- 001-auto.sql |
58eb99c3 |
780 | | |- deploy |
92c34cab |
781 | | | `- 1 |
e62add58 |
782 | | | `- 001-auto.sql |
58eb99c3 |
783 | | `- upgrade |
92c34cab |
784 | | |- 1-2 |
e62add58 |
785 | | | `- 001-auto.sql |
92c34cab |
786 | | `- 2-3 |
e62add58 |
787 | | `- 001-auto.sql |
92c34cab |
788 | |- _common |
58eb99c3 |
789 | | |- downgrade |
4f85efc6 |
790 | | | `- 2-1 |
92c34cab |
791 | | | `- 002-remove-customers.pl |
58eb99c3 |
792 | | `- upgrade |
92c34cab |
793 | | `- 1-2 |
25c3bec3 |
794 | | | `- 002-generate-customers.pl |
795 | | `- _any |
796 | | `- 999-bump-action.pl |
92c34cab |
797 | `- MySQL |
58eb99c3 |
798 | |- downgrade |
4f85efc6 |
799 | | `- 2-1 |
e62add58 |
800 | | `- 001-auto.sql |
ff40cb1f |
801 | |- initialize |
80ff6f6d |
802 | | `- 1 |
803 | | |- 001-create_database.pl |
804 | | `- 002-create_users_and_permissions.pl |
58eb99c3 |
805 | |- deploy |
92c34cab |
806 | | `- 1 |
e62add58 |
807 | | `- 001-auto.sql |
58eb99c3 |
808 | `- upgrade |
92c34cab |
809 | `- 1-2 |
e62add58 |
810 | `- 001-auto.sql |
92c34cab |
811 | |
812 | So basically, the code |
813 | |
814 | $dm->deploy(1) |
815 | |
816 | on an C<SQLite> database that would simply run |
58eb99c3 |
817 | C<$sql_migration_dir/SQLite/deploy/1/001-auto.sql>. Next, |
92c34cab |
818 | |
819 | $dm->upgrade_single_step([1,2]) |
820 | |
58eb99c3 |
821 | would run C<$sql_migration_dir/SQLite/upgrade/1-2/001-auto.sql> followed by |
25c3bec3 |
822 | C<$sql_migration_dir/_common/upgrade/1-2/002-generate-customers.pl>, and |
823 | finally punctuated by |
824 | C<$sql_migration_dir/_common/upgrade/_any/999-bump-action.pl>. |
92c34cab |
825 | |
0824f31f |
826 | C<.pl> files don't have to be in the C<_common> directory, but most of the time |
39c88a9a |
827 | they should be, because perl scripts are generally database independent. |
92c34cab |
828 | |
ff40cb1f |
829 | Note that unlike most steps in the process, C<initialize> will not run SQL, as |
830 | there may not even be an database at initialize time. It will run perl scripts |
80ff6f6d |
831 | just like the other steps in the process, but nothing is passed to them. |
832 | Until people have used this more it will remain freeform, but a recommended use |
ff40cb1f |
833 | of initialize is to have it prompt for username and password, and then call the |
80ff6f6d |
834 | appropriate C<< CREATE DATABASE >> commands etc. |
835 | |
03882cab |
836 | =head2 Directory Specification |
837 | |
838 | The following subdirectories are recognized by this DeployMethod: |
839 | |
840 | =over 2 |
841 | |
58eb99c3 |
842 | =item C<_source> This directory can contain the following directories: |
03882cab |
843 | |
844 | =over 2 |
845 | |
39c88a9a |
846 | =item C<deploy> This directory merely contains directories named after schema |
847 | versions, which in turn contain C<yaml> files that are serialized versions |
848 | of the schema at that version. These files are not for editing by hand. |
849 | |
850 | =back |
851 | |
852 | =item C<_preprocess_schema> This directory can contain the following |
853 | directories: |
854 | |
855 | =over 2 |
856 | |
58eb99c3 |
857 | =item C<downgrade> This directory merely contains directories named after |
03882cab |
858 | migrations, which are of the form C<$from_version-$to_version>. Inside of |
859 | these directories you may put Perl scripts which are to return a subref |
860 | that takes the arguments C<< $from_schema, $to_schema >>, which are |
861 | L<SQL::Translator::Schema> objects. |
862 | |
58eb99c3 |
863 | =item C<upgrade> This directory merely contains directories named after |
03882cab |
864 | migrations, which are of the form C<$from_version-$to_version>. Inside of |
865 | these directories you may put Perl scripts which are to return a subref |
866 | that takes the arguments C<< $from_schema, $to_schema >>, which are |
867 | L<SQL::Translator::Schema> objects. |
868 | |
03882cab |
869 | =back |
870 | |
5b766a24 |
871 | =item C<$storage_type> This is a set of scripts that gets run depending on what |
872 | your storage type is. If you are not sure what your storage type is, take a |
873 | look at the producers listed for L<SQL::Translator>. Also note, C<_common> |
874 | is a special case. C<_common> will get merged into whatever other files you |
25c3bec3 |
875 | already have. This directory can contain the following directories itself: |
71d00500 |
876 | |
877 | =over 2 |
878 | |
ff40cb1f |
879 | =item C<initialize> Gets run before the C<deploy> is C<deploy>ed. Has the |
58eb99c3 |
880 | same structure as the C<deploy> subdirectory as well; that is, it has a |
881 | directory for each schema version. Unlike C<deploy>, C<upgrade>, and C<downgrade> |
71d00500 |
882 | though, it can only run C<.pl> files, and the coderef in the perl files get |
883 | no arguments passed to them. |
884 | |
58eb99c3 |
885 | =item C<deploy> Gets run when the schema is C<deploy>ed. Structure is a |
71d00500 |
886 | directory per schema version, and then files are merged with C<_common> and run |
887 | in filename order. C<.sql> files are merely run, as expected. C<.pl> files are |
888 | run according to L</PERL SCRIPTS>. |
889 | |
58eb99c3 |
890 | =item C<upgrade> Gets run when the schema is C<upgrade>d. Structure is a directory |
71d00500 |
891 | per upgrade step, (for example, C<1-2> for upgrading from version 1 to version |
892 | 2,) and then files are merged with C<_common> and run in filename order. |
893 | C<.sql> files are merely run, as expected. C<.pl> files are run according |
894 | to L</PERL SCRIPTS>. |
895 | |
58eb99c3 |
896 | =item C<downgrade> Gets run when the schema is C<downgrade>d. Structure is a directory |
71d00500 |
897 | per downgrade step, (for example, C<2-1> for downgrading from version 2 to version |
898 | 1,) and then files are merged with C<_common> and run in filename order. |
899 | C<.sql> files are merely run, as expected. C<.pl> files are run according |
900 | to L</PERL SCRIPTS>. |
901 | |
902 | |
903 | =back |
904 | |
03882cab |
905 | =back |
906 | |
25c3bec3 |
907 | Note that there can be an C<_any> in the place of any of the versions (like |
908 | C<1-2> or C<1>), which means those scripts will be run B<every> time. So if |
909 | you have an C<_any> in C<_common/upgrade>, that script will get run for every |
910 | upgrade. |
911 | |
92c34cab |
912 | =head1 PERL SCRIPTS |
913 | |
7d0b0f2b |
914 | A perl script for this tool is very simple. It merely needs to contain an |
25c3bec3 |
915 | anonymous sub that takes a L<DBIx::Class::Schema> and the version set as it's |
916 | arguments. |
917 | |
92c34cab |
918 | A very basic perl script might look like: |
919 | |
920 | #!perl |
921 | |
922 | use strict; |
923 | use warnings; |
924 | |
57a30fa8 |
925 | use DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator::ScriptHelpers |
926 | 'schema_from_schema_loader'; |
927 | |
928 | schema_from_schema_loader({ naming => 'v4' }, sub { |
92c34cab |
929 | my $schema = shift; |
930 | |
25c3bec3 |
931 | # [1] for deploy, [1,2] for upgrade or downgrade, probably used with _any |
932 | my $versions = shift; |
933 | |
92c34cab |
934 | $schema->resultset('Users')->create({ |
935 | name => 'root', |
936 | password => 'root', |
937 | }) |
57a30fa8 |
938 | }) |
939 | |
940 | Note that the above uses |
941 | L<DBIx::Class::DeploymentHanlder::DeployMethod::SQL::Translator::ScriptHelpers/schema_from_schema_loader>. |
942 | Using a raw coderef is strongly discouraged as it is likely to break as you |
943 | modify your schema. |
bcc72297 |
944 | |
39c88a9a |
945 | =attr ignore_ddl |
946 | |
947 | This attribute will, when set to true (default is false), cause the DM to use |
948 | L<SQL::Translator> to use the C<_source>'s serialized SQL::Translator::Schema |
949 | instead of any pregenerated SQL. If you have a development server this is |
950 | probably the best plan of action as you will not be putting as many generated |
951 | files in your version control. Goes well with with C<databases> of C<[]>. |
952 | |
92624ee5 |
953 | =attr force_overwrite |
954 | |
955 | When this attribute is true generated files will be overwritten when the |
956 | methods which create such files are run again. The default is false, in which |
957 | case the program will die with a message saying which file needs to be deleted. |
958 | |
eb28403b |
959 | =attr schema |
a65184c8 |
960 | |
bcc72297 |
961 | The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database |
962 | and generate the DDL. |
963 | |
eb28403b |
964 | =attr storage |
a65184c8 |
965 | |
bcc72297 |
966 | The L<DBIx::Class::Storage> that is I<actually> used to talk to the database |
967 | and generate the DDL. This is automatically created with L</_build_storage>. |
968 | |
02a7b8ac |
969 | =attr sql_translator_args |
cfc9edf9 |
970 | |
02a7b8ac |
971 | The arguments that get passed to L<SQL::Translator> when it's used. |
a65184c8 |
972 | |
91adde75 |
973 | =attr script_directory |
cfc9edf9 |
974 | |
91adde75 |
975 | The directory (default C<'sql'>) that scripts are stored in |
cfc9edf9 |
976 | |
eb28403b |
977 | =attr databases |
cfc9edf9 |
978 | |
979 | The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to |
980 | generate files for |
981 | |
eb28403b |
982 | =attr txn_wrap |
983 | |
bcc72297 |
984 | Set to true (which is the default) to wrap all upgrades and deploys in a single |
985 | transaction. |
986 | |
73caa630 |
987 | =attr schema_version |
988 | |
989 | The version the schema on your harddrive is at. Defaults to |
990 | C<< $self->schema->schema_version >>. |
ec167a97 |
991 | |
992 | =head1 SEE ALSO |
993 | |
994 | This class is an implementation of |
995 | L<DBIx::Class::DeploymentHandler::HandlesDeploy>. Pretty much all the |
996 | documentation is there. |