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