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