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