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 |
267 | s/^\s+|\s+$//mg; |
268 | |
269 | # remove comments |
270 | s/^--.*//gm; |
271 | |
272 | # remove blank lines |
273 | s/^\n//mg; |
274 | |
275 | # put on single line |
276 | s/\n/ /g; |
277 | } |
278 | |
279 | return @sql; |
280 | } |
281 | |
6e2665d3 |
282 | sub _run_sql { |
283 | my ($self, $filename) = @_; |
f4075791 |
284 | log_debug { "Running SQL from $filename" }; |
f36afe83 |
285 | return $self->_run_sql_array($self->_read_sql_file($filename)); |
5d7b27cf |
286 | } |
2e68a8e1 |
287 | |
a89cd737 |
288 | sub _load_sandbox { |
289 | my $_file = shift; |
290 | |
291 | my $_package = $_file; |
32bd7c45 |
292 | $_package =~ s/([^A-Za-z0-9_])/sprintf("_%2x", ord($1))/eg; |
a89cd737 |
293 | |
32bd7c45 |
294 | my $fn = eval sprintf <<'END_EVAL', $_package; |
a89cd737 |
295 | package DBICDH::Sandbox::%s; |
296 | { |
32bd7c45 |
297 | our $app; |
298 | $app ||= require $_file; |
a89cd737 |
299 | if ( !$app && ( my $error = $@ || $! )) { die $error; } |
300 | $app; |
301 | } |
302 | END_EVAL |
32bd7c45 |
303 | |
304 | croak $@ if $@; |
305 | |
80dcc097 |
306 | croak "$_file should define an anonymous sub that takes a schema but it didn't!" |
32bd7c45 |
307 | unless ref $fn && ref $fn eq 'CODE'; |
308 | |
309 | return $fn; |
a89cd737 |
310 | } |
311 | |
6e2665d3 |
312 | sub _run_perl { |
313 | my ($self, $filename, $versions) = @_; |
f4075791 |
314 | log_debug { "Running Perl from $filename" }; |
c8a2f7bd |
315 | |
a89cd737 |
316 | my $fn = _load_sandbox($filename); |
317 | |
f4075791 |
318 | Dlog_trace { "Running Perl $_" } $fn; |
5d7b27cf |
319 | |
32bd7c45 |
320 | $fn->($self->schema, $versions) |
5d7b27cf |
321 | } |
5d7b27cf |
322 | |
1fcd4fe6 |
323 | sub txn_do { |
324 | my ( $self, $code ) = @_; |
325 | return $code->() unless $self->txn_wrap; |
326 | |
327 | my $guard = $self->schema->txn_scope_guard; |
328 | |
329 | return preserve_context { $code->() } after => sub { $guard->commit }; |
330 | } |
331 | |
6e2665d3 |
332 | sub _run_sql_and_perl { |
333 | my ($self, $filenames, $sql_to_run, $versions) = @_; |
5d7b27cf |
334 | my @files = @{$filenames}; |
1fcd4fe6 |
335 | $self->txn_do(sub { |
336 | $self->_run_sql_array($sql_to_run) if $self->ignore_ddl; |
337 | |
338 | my $sql = ($sql_to_run)?join ";\n", @$sql_to_run:''; |
339 | FILENAME: |
297e662f |
340 | for my $filename (map file($_), @files) { |
341 | if ($self->ignore_ddl && $filename->basename =~ /^[^_]*-auto.*\.sql$/) { |
1fcd4fe6 |
342 | next FILENAME |
343 | } elsif ($filename =~ /\.sql$/) { |
344 | $sql .= $self->_run_sql($filename) |
345 | } elsif ( $filename =~ /\.pl$/ ) { |
346 | $self->_run_perl($filename, $versions) |
347 | } else { |
348 | croak "A file ($filename) got to deploy that wasn't sql or perl!"; |
349 | } |
350 | } |
351 | |
352 | return $sql; |
353 | }); |
41219a5d |
354 | } |
355 | |
356 | sub deploy { |
357 | my $self = shift; |
be140a5f |
358 | my $version = (shift @_ || {})->{version} || $self->schema_version; |
f4075791 |
359 | log_info { "deploying version $version" }; |
ef44838b |
360 | my $sqlt_type = $self->storage->sqlt_type; |
361 | my $sql; |
362 | if ($self->ignore_ddl) { |
363 | $sql = $self->_sql_from_yaml({}, |
4ea5caf5 |
364 | '_ddl_protoschema_deploy_consume_filenames', $sqlt_type |
ef44838b |
365 | ); |
366 | } |
367 | return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames( |
368 | $sqlt_type, |
369 | $version, |
25c3bec3 |
370 | ), $sql, [$version]); |
2e68a8e1 |
371 | } |
372 | |
ff40cb1f |
373 | sub initialize { |
9faec51a |
374 | my $self = shift; |
375 | my $args = shift; |
376 | my $version = $args->{version} || $self->schema_version; |
ff40cb1f |
377 | log_info { "initializing version $version" }; |
9faec51a |
378 | my $storage_type = $args->{storage_type} || $self->storage->sqlt_type; |
fc4b7602 |
379 | |
ff40cb1f |
380 | my @files = @{$self->_ddl_initialize_consume_filenames( |
9faec51a |
381 | $storage_type, |
fc4b7602 |
382 | $version, |
383 | )}; |
384 | |
385 | for my $filename (@files) { |
386 | # We ignore sql for now (till I figure out what to do with it) |
387 | if ( $filename =~ /^(.+)\.pl$/ ) { |
fc4b7602 |
388 | my $filedata = do { local( @ARGV, $/ ) = $filename; <> }; |
fc4b7602 |
389 | |
9faec51a |
390 | no warnings 'redefine'; |
5b5defbc |
391 | my $fn = eval "$filedata"; |
fc4b7602 |
392 | use warnings; |
5b5defbc |
393 | |
9faec51a |
394 | if ($@) { |
6e9a733d |
395 | croak "$filename failed to compile: $@"; |
9faec51a |
396 | } elsif (ref $fn eq 'CODE') { |
fc4b7602 |
397 | $fn->() |
398 | } else { |
6e9a733d |
399 | croak "$filename should define an anonymous sub but it didn't!"; |
fc4b7602 |
400 | } |
401 | } else { |
ff40cb1f |
402 | croak "A file ($filename) got to initialize_scripts that wasn't sql or perl!"; |
fc4b7602 |
403 | } |
404 | } |
405 | } |
406 | |
6e2665d3 |
407 | sub _sqldiff_from_yaml { |
408 | my ($self, $from_version, $to_version, $db, $direction) = @_; |
91adde75 |
409 | my $dir = $self->script_directory; |
28563f97 |
410 | my $sqltargs = { |
411 | add_drop_table => 1, |
412 | ignore_constraint_names => 1, |
413 | ignore_index_names => 1, |
414 | %{$self->sql_translator_args} |
415 | }; |
d54b8d69 |
416 | |
28563f97 |
417 | my $source_schema; |
418 | { |
419 | my $prefilename = $self->_ddl_protoschema_produce_filename($from_version, $dir); |
420 | |
421 | # should probably be a croak |
422 | carp("No previous schema file found ($prefilename)") |
423 | unless -e $prefilename; |
424 | |
425 | my $t = SQL::Translator->new({ |
426 | %{$sqltargs}, |
427 | debug => 0, |
428 | trace => 0, |
429 | parser => 'SQL::Translator::Parser::YAML', |
430 | }); |
431 | |
432 | my $out = $t->translate( $prefilename ) |
433 | or croak($t->error); |
434 | |
435 | $source_schema = $t->schema; |
436 | |
437 | $source_schema->name( $prefilename ) |
438 | unless $source_schema->name; |
439 | } |
440 | |
441 | my $dest_schema; |
442 | { |
443 | my $filename = $self->_ddl_protoschema_produce_filename($to_version, $dir); |
444 | |
445 | # should probably be a croak |
446 | carp("No next schema file found ($filename)") |
447 | unless -e $filename; |
448 | |
449 | my $t = SQL::Translator->new({ |
450 | %{$sqltargs}, |
451 | debug => 0, |
452 | trace => 0, |
453 | parser => 'SQL::Translator::Parser::YAML', |
454 | }); |
455 | |
456 | my $out = $t->translate( $filename ) |
457 | or croak($t->error); |
458 | |
459 | $dest_schema = $t->schema; |
460 | |
461 | $dest_schema->name( $filename ) |
462 | unless $dest_schema->name; |
463 | } |
f9c6ab50 |
464 | |
465 | my $transform_files_method = "_ddl_protoschema_${direction}_consume_filenames"; |
466 | my $transforms = $self->_coderefs_per_files( |
467 | $self->$transform_files_method([$from_version, $to_version]) |
468 | ); |
469 | $_->($source_schema, $dest_schema) for @$transforms; |
470 | |
28563f97 |
471 | return [SQL::Translator::Diff::schema_diff( |
472 | $source_schema, $db, |
473 | $dest_schema, $db, |
474 | $sqltargs |
475 | )]; |
476 | } |
477 | |
6e2665d3 |
478 | sub _sql_from_yaml { |
479 | my ($self, $sqltargs, $from_file, $db) = @_; |
28563f97 |
480 | my $schema = $self->schema; |
481 | my $version = $self->schema_version; |
93460690 |
482 | |
4ea5caf5 |
483 | my @sql; |
2e68a8e1 |
484 | |
4ea5caf5 |
485 | my $actual_file = $self->$from_file($version); |
486 | for my $yaml_filename (@{ |
487 | DlogS_trace { "generating SQL from Serialized SQL Files: $_" } |
488 | (ref $actual_file?$actual_file:[$actual_file]) |
489 | }) { |
490 | my $sqlt = SQL::Translator->new({ |
491 | add_drop_table => 0, |
492 | parser => 'SQL::Translator::Parser::YAML', |
493 | %{$sqltargs}, |
494 | producer => $db, |
495 | }); |
496 | |
497 | push @sql, $sqlt->translate($yaml_filename); |
498 | if(!@sql) { |
499 | carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); |
500 | return undef; |
501 | } |
28563f97 |
502 | } |
503 | return \@sql; |
504 | } |
505 | |
506 | sub _prepare_install { |
507 | my $self = shift; |
508 | my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; |
509 | my $from_file = shift; |
510 | my $to_file = shift; |
511 | my $dir = $self->script_directory; |
512 | my $databases = $self->databases; |
513 | my $version = $self->schema_version; |
514 | |
2e68a8e1 |
515 | foreach my $db (@$databases) { |
28563f97 |
516 | my $sql = $self->_sql_from_yaml($sqltargs, $from_file, $db ) or next; |
2e68a8e1 |
517 | |
c8a2f7bd |
518 | my $filename = $self->$to_file($db, $version, $dir); |
9600776d |
519 | if (-e $filename ) { |
92624ee5 |
520 | if ($self->force_overwrite) { |
521 | carp "Overwriting existing DDL file - $filename"; |
522 | unlink $filename; |
523 | } else { |
524 | die "Cannot overwrite '$filename', either enable force_overwrite or delete it" |
525 | } |
2e68a8e1 |
526 | } |
387b11d2 |
527 | open my $file, q(>), $filename; |
28563f97 |
528 | print {$file} join ";\n", @$sql; |
2e68a8e1 |
529 | close $file; |
530 | } |
531 | } |
532 | |
c8a2f7bd |
533 | sub _resultsource_install_filename { |
534 | my ($self, $source_name) = @_; |
535 | return sub { |
536 | my ($self, $type, $version) = @_; |
297e662f |
537 | my $dirname = dir( $self->script_directory, $type, 'deploy', $version ); |
538 | $dirname->mkpath unless -d $dirname; |
c8a2f7bd |
539 | |
297e662f |
540 | return "" . file( $dirname, "001-auto-$source_name.sql" ); |
c8a2f7bd |
541 | } |
542 | } |
543 | |
e62add58 |
544 | sub _resultsource_protoschema_filename { |
545 | my ($self, $source_name) = @_; |
546 | return sub { |
547 | my ($self, $version) = @_; |
297e662f |
548 | my $dirname = dir( $self->script_directory, '_source', 'deploy', $version ); |
549 | $dirname->mkpath unless -d $dirname; |
e62add58 |
550 | |
297e662f |
551 | return "" . file( $dirname, "001-auto-$source_name.yml" ); |
e62add58 |
552 | } |
553 | } |
554 | |
c8a2f7bd |
555 | sub install_resultsource { |
be140a5f |
556 | my ($self, $args) = @_; |
ba99ba44 |
557 | my $source = $args->{result_source} |
558 | or die 'result_source must be passed to install_resultsource'; |
559 | my $version = $args->{version} |
560 | or die 'version must be passed to install_resultsource'; |
f4075791 |
561 | log_info { 'installing_resultsource ' . $source->source_name . ", version $version" }; |
c8a2f7bd |
562 | my $rs_install_file = |
563 | $self->_resultsource_install_filename($source->source_name); |
564 | |
565 | my $files = [ |
566 | $self->$rs_install_file( |
567 | $self->storage->sqlt_type, |
568 | $version, |
569 | ) |
570 | ]; |
25c3bec3 |
571 | $self->_run_sql_and_perl($files, '', [$version]); |
c8a2f7bd |
572 | } |
573 | |
574 | sub prepare_resultsource_install { |
575 | my $self = shift; |
be140a5f |
576 | my $source = (shift @_)->{result_source}; |
f4075791 |
577 | log_info { 'preparing install for resultsource ' . $source->source_name }; |
c8a2f7bd |
578 | |
e62add58 |
579 | my $install_filename = $self->_resultsource_install_filename($source->source_name); |
580 | my $proto_filename = $self->_resultsource_protoschema_filename($source->source_name); |
6cae2f56 |
581 | $self->prepare_protoschema({ |
c8a2f7bd |
582 | parser_args => { sources => [$source->source_name], } |
e62add58 |
583 | }, $proto_filename); |
584 | $self->_prepare_install({}, $proto_filename, $install_filename); |
c8a2f7bd |
585 | } |
586 | |
91557c90 |
587 | sub prepare_deploy { |
f4075791 |
588 | log_info { 'preparing deploy' }; |
c8a2f7bd |
589 | my $self = shift; |
6776a6d4 |
590 | $self->prepare_protoschema({ |
591 | # Exclude __VERSION so that it gets installed separately |
592 | parser_args => { sources => [grep { $_ ne '__VERSION' } $self->schema->sources], } |
593 | }, '_ddl_protoschema_produce_filename'); |
e62add58 |
594 | $self->_prepare_install({}, '_ddl_protoschema_produce_filename', '_ddl_schema_produce_filename'); |
c8a2f7bd |
595 | } |
596 | |
a41a04e5 |
597 | sub prepare_upgrade { |
be140a5f |
598 | my ($self, $args) = @_; |
0df68524 |
599 | log_info { |
f4075791 |
600 | "preparing upgrade from $args->{from_version} to $args->{to_version}" |
0df68524 |
601 | }; |
be140a5f |
602 | $self->_prepare_changegrade( |
58eb99c3 |
603 | $args->{from_version}, $args->{to_version}, $args->{version_set}, 'upgrade' |
be140a5f |
604 | ); |
76d311e7 |
605 | } |
606 | |
607 | sub prepare_downgrade { |
be140a5f |
608 | my ($self, $args) = @_; |
0df68524 |
609 | log_info { |
f4075791 |
610 | "preparing downgrade from $args->{from_version} to $args->{to_version}" |
0df68524 |
611 | }; |
be140a5f |
612 | $self->_prepare_changegrade( |
58eb99c3 |
613 | $args->{from_version}, $args->{to_version}, $args->{version_set}, 'downgrade' |
be140a5f |
614 | ); |
76d311e7 |
615 | } |
616 | |
6e2665d3 |
617 | sub _coderefs_per_files { |
618 | my ($self, $files) = @_; |
f9c6ab50 |
619 | no warnings 'redefine'; |
620 | [map eval do { local( @ARGV, $/ ) = $_; <> }, @$files] |
621 | } |
622 | |
6e2665d3 |
623 | sub _prepare_changegrade { |
624 | my ($self, $from_version, $to_version, $version_set, $direction) = @_; |
2e68a8e1 |
625 | my $schema = $self->schema; |
626 | my $databases = $self->databases; |
91adde75 |
627 | my $dir = $self->script_directory; |
2e68a8e1 |
628 | |
73caa630 |
629 | my $schema_version = $self->schema_version; |
e62add58 |
630 | my $diff_file_method = "_ddl_schema_${direction}_produce_filename"; |
e62add58 |
631 | foreach my $db (@$databases) { |
632 | my $diff_file = $self->$diff_file_method($db, $version_set, $dir ); |
633 | if(-e $diff_file) { |
92624ee5 |
634 | if ($self->force_overwrite) { |
635 | carp("Overwriting existing $direction-diff file - $diff_file"); |
636 | unlink $diff_file; |
637 | } else { |
638 | die "Cannot overwrite '$diff_file', either enable force_overwrite or delete it" |
639 | } |
2e68a8e1 |
640 | } |
641 | |
387b11d2 |
642 | open my $file, q(>), $diff_file; |
f9c6ab50 |
643 | print {$file} join ";\n", @{$self->_sqldiff_from_yaml($from_version, $to_version, $db, $direction)}; |
2e68a8e1 |
644 | close $file; |
645 | } |
646 | } |
647 | |
6e2665d3 |
648 | sub _read_sql_file { |
649 | my ($self, $file) = @_; |
334bced5 |
650 | return unless $file; |
651 | |
115c68ce |
652 | local $/ = undef; #sluuuuuurp |
653 | |
aabd4237 |
654 | open my $fh, '<', $file; |
115c68ce |
655 | return [ _split_sql_chunk( <$fh> ) ]; |
1f0d0633 |
656 | } |
657 | |
7d2a6974 |
658 | sub downgrade_single_step { |
76d311e7 |
659 | my $self = shift; |
be140a5f |
660 | my $version_set = (shift @_)->{version_set}; |
f4075791 |
661 | Dlog_info { "downgrade_single_step'ing $_" } $version_set; |
41219a5d |
662 | |
ef44838b |
663 | my $sqlt_type = $self->storage->sqlt_type; |
664 | my $sql_to_run; |
665 | if ($self->ignore_ddl) { |
666 | $sql_to_run = $self->_sqldiff_from_yaml( |
58eb99c3 |
667 | $version_set->[0], $version_set->[1], $sqlt_type, 'downgrade', |
ef44838b |
668 | ); |
669 | } |
58eb99c3 |
670 | my $sql = $self->_run_sql_and_perl($self->_ddl_schema_downgrade_consume_filenames( |
ef44838b |
671 | $sqlt_type, |
627581cd |
672 | $version_set, |
25c3bec3 |
673 | ), $sql_to_run, $version_set); |
3249629f |
674 | |
41219a5d |
675 | return ['', $sql]; |
76d311e7 |
676 | } |
677 | |
7d2a6974 |
678 | sub upgrade_single_step { |
7521a845 |
679 | my $self = shift; |
be140a5f |
680 | my $version_set = (shift @_)->{version_set}; |
f4075791 |
681 | Dlog_info { "upgrade_single_step'ing $_" } $version_set; |
41219a5d |
682 | |
ef44838b |
683 | my $sqlt_type = $self->storage->sqlt_type; |
684 | my $sql_to_run; |
685 | if ($self->ignore_ddl) { |
686 | $sql_to_run = $self->_sqldiff_from_yaml( |
58eb99c3 |
687 | $version_set->[0], $version_set->[1], $sqlt_type, 'upgrade', |
ef44838b |
688 | ); |
689 | } |
58eb99c3 |
690 | my $sql = $self->_run_sql_and_perl($self->_ddl_schema_upgrade_consume_filenames( |
ef44838b |
691 | $sqlt_type, |
627581cd |
692 | $version_set, |
25c3bec3 |
693 | ), $sql_to_run, $version_set); |
41219a5d |
694 | return ['', $sql]; |
334bced5 |
695 | } |
696 | |
6cae2f56 |
697 | sub prepare_protoschema { |
7e08eddd |
698 | my $self = shift; |
e62add58 |
699 | my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; |
700 | my $to_file = shift; |
7e08eddd |
701 | my $filename |
e62add58 |
702 | = $self->$to_file($self->schema_version); |
7e08eddd |
703 | |
e62add58 |
704 | # we do this because the code that uses this sets parser args, |
705 | # so we just need to merge in the package |
706 | $sqltargs->{parser_args}{package} = $self->schema; |
7e08eddd |
707 | my $sqlt = SQL::Translator->new({ |
708 | parser => 'SQL::Translator::Parser::DBIx::Class', |
709 | producer => 'SQL::Translator::Producer::YAML', |
e62add58 |
710 | %{ $sqltargs }, |
7e08eddd |
711 | }); |
712 | |
713 | my $yml = $sqlt->translate; |
714 | |
715 | croak("Failed to translate to YAML: " . $sqlt->error) |
716 | unless $yml; |
717 | |
718 | if (-e $filename ) { |
92624ee5 |
719 | if ($self->force_overwrite) { |
720 | carp "Overwriting existing DDL-YML file - $filename"; |
721 | unlink $filename; |
722 | } else { |
723 | die "Cannot overwrite '$filename', either enable force_overwrite or delete it" |
724 | } |
7e08eddd |
725 | } |
726 | |
727 | open my $file, q(>), $filename; |
728 | print {$file} $yml; |
729 | close $file; |
730 | } |
731 | |
aabd4237 |
732 | __PACKAGE__->meta->make_immutable; |
733 | |
2e68a8e1 |
734 | 1; |
e051bb00 |
735 | |
e52174e3 |
736 | # vim: ts=2 sw=2 expandtab |
737 | |
e051bb00 |
738 | __END__ |
739 | |
bcc72297 |
740 | =head1 DESCRIPTION |
741 | |
e62add58 |
742 | This class is the meat of L<DBIx::Class::DeploymentHandler>. It takes care |
743 | of generating serialized schemata as well as sql files to move from one |
744 | version of a schema to the rest. One of the hallmark features of this class |
745 | is that it allows for multiple sql files for deploy and upgrade, allowing |
746 | developers to fine tune deployment. In addition it also allows for perl |
747 | files to be run at any stage of the process. |
bcc72297 |
748 | |
749 | For basic usage see L<DBIx::Class::DeploymentHandler::HandlesDeploy>. What's |
750 | documented here is extra fun stuff or private methods. |
751 | |
752 | =head1 DIRECTORY LAYOUT |
753 | |
39c88a9a |
754 | Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>. |
755 | It's spiritually based upon L<DBIx::Migration::Directories>, but has a |
756 | lot of extensions and modifications, so even if you are familiar with it, |
757 | please read this. I feel like the best way to describe the layout is with |
758 | the following example: |
92c34cab |
759 | |
760 | $sql_migration_dir |
58eb99c3 |
761 | |- _source |
762 | | |- deploy |
03882cab |
763 | | |- 1 |
764 | | | `- 001-auto.yml |
765 | | |- 2 |
766 | | | `- 001-auto.yml |
767 | | `- 3 |
768 | | `- 001-auto.yml |
92c34cab |
769 | |- SQLite |
58eb99c3 |
770 | | |- downgrade |
4f85efc6 |
771 | | | `- 2-1 |
e62add58 |
772 | | | `- 001-auto.sql |
58eb99c3 |
773 | | |- deploy |
92c34cab |
774 | | | `- 1 |
e62add58 |
775 | | | `- 001-auto.sql |
58eb99c3 |
776 | | `- upgrade |
92c34cab |
777 | | |- 1-2 |
e62add58 |
778 | | | `- 001-auto.sql |
92c34cab |
779 | | `- 2-3 |
e62add58 |
780 | | `- 001-auto.sql |
92c34cab |
781 | |- _common |
58eb99c3 |
782 | | |- downgrade |
4f85efc6 |
783 | | | `- 2-1 |
92c34cab |
784 | | | `- 002-remove-customers.pl |
58eb99c3 |
785 | | `- upgrade |
92c34cab |
786 | | `- 1-2 |
25c3bec3 |
787 | | | `- 002-generate-customers.pl |
788 | | `- _any |
789 | | `- 999-bump-action.pl |
92c34cab |
790 | `- MySQL |
58eb99c3 |
791 | |- downgrade |
4f85efc6 |
792 | | `- 2-1 |
e62add58 |
793 | | `- 001-auto.sql |
ff40cb1f |
794 | |- initialize |
80ff6f6d |
795 | | `- 1 |
796 | | |- 001-create_database.pl |
797 | | `- 002-create_users_and_permissions.pl |
58eb99c3 |
798 | |- deploy |
92c34cab |
799 | | `- 1 |
e62add58 |
800 | | `- 001-auto.sql |
58eb99c3 |
801 | `- upgrade |
92c34cab |
802 | `- 1-2 |
e62add58 |
803 | `- 001-auto.sql |
92c34cab |
804 | |
805 | So basically, the code |
806 | |
807 | $dm->deploy(1) |
808 | |
809 | on an C<SQLite> database that would simply run |
58eb99c3 |
810 | C<$sql_migration_dir/SQLite/deploy/1/001-auto.sql>. Next, |
92c34cab |
811 | |
812 | $dm->upgrade_single_step([1,2]) |
813 | |
58eb99c3 |
814 | would run C<$sql_migration_dir/SQLite/upgrade/1-2/001-auto.sql> followed by |
25c3bec3 |
815 | C<$sql_migration_dir/_common/upgrade/1-2/002-generate-customers.pl>, and |
816 | finally punctuated by |
817 | C<$sql_migration_dir/_common/upgrade/_any/999-bump-action.pl>. |
92c34cab |
818 | |
0824f31f |
819 | C<.pl> files don't have to be in the C<_common> directory, but most of the time |
39c88a9a |
820 | they should be, because perl scripts are generally database independent. |
92c34cab |
821 | |
ff40cb1f |
822 | Note that unlike most steps in the process, C<initialize> will not run SQL, as |
823 | there may not even be an database at initialize time. It will run perl scripts |
80ff6f6d |
824 | just like the other steps in the process, but nothing is passed to them. |
825 | Until people have used this more it will remain freeform, but a recommended use |
ff40cb1f |
826 | of initialize is to have it prompt for username and password, and then call the |
80ff6f6d |
827 | appropriate C<< CREATE DATABASE >> commands etc. |
828 | |
03882cab |
829 | =head2 Directory Specification |
830 | |
831 | The following subdirectories are recognized by this DeployMethod: |
832 | |
833 | =over 2 |
834 | |
58eb99c3 |
835 | =item C<_source> This directory can contain the following directories: |
03882cab |
836 | |
837 | =over 2 |
838 | |
39c88a9a |
839 | =item C<deploy> This directory merely contains directories named after schema |
840 | versions, which in turn contain C<yaml> files that are serialized versions |
841 | of the schema at that version. These files are not for editing by hand. |
842 | |
843 | =back |
844 | |
845 | =item C<_preprocess_schema> This directory can contain the following |
846 | directories: |
847 | |
848 | =over 2 |
849 | |
58eb99c3 |
850 | =item C<downgrade> This directory merely contains directories named after |
03882cab |
851 | migrations, which are of the form C<$from_version-$to_version>. Inside of |
852 | these directories you may put Perl scripts which are to return a subref |
853 | that takes the arguments C<< $from_schema, $to_schema >>, which are |
854 | L<SQL::Translator::Schema> objects. |
855 | |
58eb99c3 |
856 | =item C<upgrade> This directory merely contains directories named after |
03882cab |
857 | migrations, which are of the form C<$from_version-$to_version>. Inside of |
858 | these directories you may put Perl scripts which are to return a subref |
859 | that takes the arguments C<< $from_schema, $to_schema >>, which are |
860 | L<SQL::Translator::Schema> objects. |
861 | |
03882cab |
862 | =back |
863 | |
5b766a24 |
864 | =item C<$storage_type> This is a set of scripts that gets run depending on what |
865 | your storage type is. If you are not sure what your storage type is, take a |
866 | look at the producers listed for L<SQL::Translator>. Also note, C<_common> |
867 | is a special case. C<_common> will get merged into whatever other files you |
25c3bec3 |
868 | already have. This directory can contain the following directories itself: |
71d00500 |
869 | |
870 | =over 2 |
871 | |
ff40cb1f |
872 | =item C<initialize> Gets run before the C<deploy> is C<deploy>ed. Has the |
58eb99c3 |
873 | same structure as the C<deploy> subdirectory as well; that is, it has a |
874 | directory for each schema version. Unlike C<deploy>, C<upgrade>, and C<downgrade> |
71d00500 |
875 | though, it can only run C<.pl> files, and the coderef in the perl files get |
876 | no arguments passed to them. |
877 | |
58eb99c3 |
878 | =item C<deploy> Gets run when the schema is C<deploy>ed. Structure is a |
71d00500 |
879 | directory per schema version, and then files are merged with C<_common> and run |
880 | in filename order. C<.sql> files are merely run, as expected. C<.pl> files are |
881 | run according to L</PERL SCRIPTS>. |
882 | |
58eb99c3 |
883 | =item C<upgrade> Gets run when the schema is C<upgrade>d. Structure is a directory |
71d00500 |
884 | per upgrade step, (for example, C<1-2> for upgrading from version 1 to version |
885 | 2,) and then files are merged with C<_common> and run in filename order. |
886 | C<.sql> files are merely run, as expected. C<.pl> files are run according |
887 | to L</PERL SCRIPTS>. |
888 | |
58eb99c3 |
889 | =item C<downgrade> Gets run when the schema is C<downgrade>d. Structure is a directory |
71d00500 |
890 | per downgrade step, (for example, C<2-1> for downgrading from version 2 to version |
891 | 1,) and then files are merged with C<_common> and run in filename order. |
892 | C<.sql> files are merely run, as expected. C<.pl> files are run according |
893 | to L</PERL SCRIPTS>. |
894 | |
895 | |
896 | =back |
897 | |
03882cab |
898 | =back |
899 | |
25c3bec3 |
900 | Note that there can be an C<_any> in the place of any of the versions (like |
901 | C<1-2> or C<1>), which means those scripts will be run B<every> time. So if |
902 | you have an C<_any> in C<_common/upgrade>, that script will get run for every |
903 | upgrade. |
904 | |
92c34cab |
905 | =head1 PERL SCRIPTS |
906 | |
7d0b0f2b |
907 | A perl script for this tool is very simple. It merely needs to contain an |
25c3bec3 |
908 | anonymous sub that takes a L<DBIx::Class::Schema> and the version set as it's |
909 | arguments. |
910 | |
92c34cab |
911 | A very basic perl script might look like: |
912 | |
913 | #!perl |
914 | |
915 | use strict; |
916 | use warnings; |
917 | |
57a30fa8 |
918 | use DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator::ScriptHelpers |
919 | 'schema_from_schema_loader'; |
920 | |
921 | schema_from_schema_loader({ naming => 'v4' }, sub { |
92c34cab |
922 | my $schema = shift; |
923 | |
25c3bec3 |
924 | # [1] for deploy, [1,2] for upgrade or downgrade, probably used with _any |
925 | my $versions = shift; |
926 | |
92c34cab |
927 | $schema->resultset('Users')->create({ |
928 | name => 'root', |
929 | password => 'root', |
930 | }) |
57a30fa8 |
931 | }) |
932 | |
933 | Note that the above uses |
934 | L<DBIx::Class::DeploymentHanlder::DeployMethod::SQL::Translator::ScriptHelpers/schema_from_schema_loader>. |
935 | Using a raw coderef is strongly discouraged as it is likely to break as you |
936 | modify your schema. |
bcc72297 |
937 | |
39c88a9a |
938 | =attr ignore_ddl |
939 | |
940 | This attribute will, when set to true (default is false), cause the DM to use |
941 | L<SQL::Translator> to use the C<_source>'s serialized SQL::Translator::Schema |
942 | instead of any pregenerated SQL. If you have a development server this is |
943 | probably the best plan of action as you will not be putting as many generated |
944 | files in your version control. Goes well with with C<databases> of C<[]>. |
945 | |
92624ee5 |
946 | =attr force_overwrite |
947 | |
948 | When this attribute is true generated files will be overwritten when the |
949 | methods which create such files are run again. The default is false, in which |
950 | case the program will die with a message saying which file needs to be deleted. |
951 | |
eb28403b |
952 | =attr schema |
a65184c8 |
953 | |
bcc72297 |
954 | The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database |
955 | and generate the DDL. |
956 | |
eb28403b |
957 | =attr storage |
a65184c8 |
958 | |
bcc72297 |
959 | The L<DBIx::Class::Storage> that is I<actually> used to talk to the database |
960 | and generate the DDL. This is automatically created with L</_build_storage>. |
961 | |
02a7b8ac |
962 | =attr sql_translator_args |
cfc9edf9 |
963 | |
02a7b8ac |
964 | The arguments that get passed to L<SQL::Translator> when it's used. |
a65184c8 |
965 | |
91adde75 |
966 | =attr script_directory |
cfc9edf9 |
967 | |
91adde75 |
968 | The directory (default C<'sql'>) that scripts are stored in |
cfc9edf9 |
969 | |
eb28403b |
970 | =attr databases |
cfc9edf9 |
971 | |
972 | The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to |
973 | generate files for |
974 | |
eb28403b |
975 | =attr txn_wrap |
976 | |
bcc72297 |
977 | Set to true (which is the default) to wrap all upgrades and deploys in a single |
978 | transaction. |
979 | |
73caa630 |
980 | =attr schema_version |
981 | |
982 | The version the schema on your harddrive is at. Defaults to |
983 | C<< $self->schema->schema_version >>. |
ec167a97 |
984 | |
985 | =head1 SEE ALSO |
986 | |
987 | This class is an implementation of |
988 | L<DBIx::Class::DeploymentHandler::HandlesDeploy>. Pretty much all the |
989 | documentation is there. |