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