better logging of sql
[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 );
0df68524 8use Log::Contextual::WarnLogger;
9use Log::Contextual qw(:log :dlog), -default_logger => Log::Contextual::WarnLogger->new({
10 env_prefix => 'DBICDH'
11});
12use Data::Dumper::Concise;
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
d54b8d69 28has schema => (
29 isa => 'DBIx::Class::Schema',
30 is => 'ro',
31 required => 1,
d54b8d69 32);
33
334bced5 34has storage => (
35 isa => 'DBIx::Class::Storage',
36 is => 'ro',
37 lazy_build => 1,
38);
39
2eaf903b 40method _build_storage {
41 my $s = $self->schema->storage;
42 $s->_determine_driver;
43 $s
44}
45
02a7b8ac 46has sql_translator_args => (
334bced5 47 isa => 'HashRef',
48 is => 'ro',
49 default => sub { {} },
50);
91adde75 51has script_directory => (
334bced5 52 isa => 'Str',
53 is => 'ro',
54 required => 1,
55 default => 'sql',
56);
57
334bced5 58has databases => (
59 coerce => 1,
60 isa => 'DBIx::Class::DeploymentHandler::Databases',
61 is => 'ro',
62 default => sub { [qw( MySQL SQLite PostgreSQL )] },
63);
64
a7d53deb 65has txn_wrap => (
66 is => 'ro',
67 isa => 'Bool',
68 default => 1,
69);
70
73caa630 71has schema_version => (
72 is => 'ro',
e86c0c07 73 isa => 'Str',
73caa630 74 lazy_build => 1,
75);
76
6df6dcb9 77# this will probably never get called as the DBICDH
78# will be passing down a schema_version normally, which
79# is built the same way, but we leave this in place
73caa630 80method _build_schema_version { $self->schema->schema_version }
81
76d311e7 82method __ddl_consume_with_prefix($type, $versions, $prefix) {
91adde75 83 my $base_dir = $self->script_directory;
262166c1 84
76d08d08 85 my $main = catfile( $base_dir, $type );
86 my $generic = catfile( $base_dir, '_generic' );
87 my $common =
88 catfile( $base_dir, '_common', $prefix, join q(-), @{$versions} );
262166c1 89
90 my $dir;
91 if (-d $main) {
76d08d08 92 $dir = catfile($main, $prefix, join q(-), @{$versions})
262166c1 93 } elsif (-d $generic) {
9af9d0b2 94 $dir = catfile($generic, $prefix, join q(-), @{$versions});
262166c1 95 } else {
9af9d0b2 96 croak "neither $main or $generic exist; please write/generate some SQL";
262166c1 97 }
98
99 opendir my($dh), $dir;
f36afe83 100 my %files = map { $_ => "$dir/$_" } grep { /\.(?:sql|pl|sql-\w+)$/ && -f "$dir/$_" } readdir $dh;
262166c1 101 closedir $dh;
102
103 if (-d $common) {
104 opendir my($dh), $common;
41219a5d 105 for my $filename (grep { /\.(?:sql|pl)$/ && -f catfile($common,$_) } readdir $dh) {
262166c1 106 unless ($files{$filename}) {
9af9d0b2 107 $files{$filename} = catfile($common,$filename);
262166c1 108 }
109 }
110 closedir $dh;
111 }
112
113 return [@files{sort keys %files}]
114}
3c1b5ee8 115
fc4b7602 116method _ddl_preinstall_consume_filenames($type, $version) {
117 $self->__ddl_consume_with_prefix($type, [ $version ], 'preinstall')
118}
119
76d311e7 120method _ddl_schema_consume_filenames($type, $version) {
121 $self->__ddl_consume_with_prefix($type, [ $version ], 'schema')
3c1b5ee8 122}
123
76d311e7 124method _ddl_schema_produce_filename($type, $version) {
91adde75 125 my $dirname = catfile( $self->script_directory, $type, 'schema', $version );
76d08d08 126 mkpath($dirname) unless -d $dirname;
d54b8d69 127
76d08d08 128 return catfile( $dirname, '001-auto.sql' );
d54b8d69 129}
130
76d311e7 131method _ddl_schema_up_consume_filenames($type, $versions) {
132 $self->__ddl_consume_with_prefix($type, $versions, 'up')
3c1b5ee8 133}
134
76d311e7 135method _ddl_schema_down_consume_filenames($type, $versions) {
136 $self->__ddl_consume_with_prefix($type, $versions, 'down')
a41a04e5 137}
138
76d311e7 139method _ddl_schema_up_produce_filename($type, $versions) {
91adde75 140 my $dir = $self->script_directory;
76d311e7 141
76d08d08 142 my $dirname = catfile( $dir, $type, 'up', join q(-), @{$versions});
143 mkpath($dirname) unless -d $dirname;
a41a04e5 144
76d08d08 145 return catfile( $dirname, '001-auto.sql'
a41a04e5 146 );
147}
148
76d311e7 149method _ddl_schema_down_produce_filename($type, $versions, $dir) {
76d08d08 150 my $dirname = catfile( $dir, $type, 'down', join q(-), @{$versions} );
151 mkpath($dirname) unless -d $dirname;
24f4524b 152
76d08d08 153 return catfile( $dirname, '001-auto.sql');
24f4524b 154}
155
f36afe83 156method _run_sql_array($sql) {
41219a5d 157 my $storage = $self->storage;
5d7b27cf 158
4d09f712 159 log_trace { '[DBICDH] Running SQL ' . Dumper($sql) };
f36afe83 160 foreach my $line (@{$sql}) {
5d7b27cf 161 $storage->_query_start($line);
162 try {
163 # do a dbh_do cycle here, as we need some error checking in
164 # place (even though we will ignore errors)
165 $storage->dbh_do (sub { $_[1]->do($line) });
166 }
167 catch {
168 carp "$_ (running '${line}')"
169 }
170 $storage->_query_end($line);
171 }
4d09f712 172 return join "\n", @$sql
f36afe83 173}
174
175method _run_sql($filename) {
176 log_debug { "[DBICDH] Running SQL from $filename" };
177 return $self->_run_sql_array($self->_read_sql_file($filename));
5d7b27cf 178}
2e68a8e1 179
5d7b27cf 180method _run_perl($filename) {
181 log_debug { "[DBICDH] Running Perl from $filename" };
182 my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
c8a2f7bd 183
5d7b27cf 184 no warnings 'redefine';
185 my $fn = eval "$filedata";
186 use warnings;
187 log_trace { '[DBICDH] Running Perl ' . Dumper($fn) };
188
189 if ($@) {
190 carp "$filename failed to compile: $@";
191 } elsif (ref $fn eq 'CODE') {
192 $fn->($self->schema)
193 } else {
194 carp "$filename should define an anonymouse sub that takes a schema but it didn't!";
195 }
196}
f36afe83 197{
198 my $json;
199
200 method _run_serialized_sql($filename, $type) {
201 if ($type eq 'json') {
202 require JSON;
203 $json ||= JSON->new->pretty;
204 my @sql = @{$json->decode($filename)};
205 } else {
206 croak "A file ($filename) got to deploy that wasn't sql or perl!";
207 }
208 }
5d7b27cf 209
210}
211
212method _run_sql_and_perl($filenames) {
5d7b27cf 213 my @files = @{$filenames};
214 my $guard = $self->schema->txn_scope_guard if $self->txn_wrap;
215
216 my $sql = '';
41219a5d 217 for my $filename (@files) {
218 if ($filename =~ /\.sql$/) {
5d7b27cf 219 $sql .= $self->_run_sql($filename)
220 } elsif ( $filename =~ /\.sql-(\w+)$/ ) {
221 $sql .= $self->_run_serialized_sql($filename, $1)
398b1385 222 } elsif ( $filename =~ /\.pl$/ ) {
5d7b27cf 223 $self->_run_perl($filename)
41219a5d 224 } else {
fc4b7602 225 croak "A file ($filename) got to deploy that wasn't sql or perl!";
2e68a8e1 226 }
2e68a8e1 227 }
a7d53deb 228
229 $guard->commit if $self->txn_wrap;
41219a5d 230
231 return $sql;
232}
233
234sub deploy {
235 my $self = shift;
be140a5f 236 my $version = (shift @_ || {})->{version} || $self->schema_version;
0df68524 237 log_info { "[DBICDH] deploying version $version" };
41219a5d 238
239 return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames(
240 $self->storage->sqlt_type,
92c34cab 241 $version,
41219a5d 242 ));
2e68a8e1 243}
244
80ff6f6d 245sub preinstall {
9faec51a 246 my $self = shift;
247 my $args = shift;
248 my $version = $args->{version} || $self->schema_version;
0df68524 249 log_info { "[DBICDH] preinstalling version $version" };
9faec51a 250 my $storage_type = $args->{storage_type} || $self->storage->sqlt_type;
fc4b7602 251
252 my @files = @{$self->_ddl_preinstall_consume_filenames(
9faec51a 253 $storage_type,
fc4b7602 254 $version,
255 )};
256
257 for my $filename (@files) {
258 # We ignore sql for now (till I figure out what to do with it)
259 if ( $filename =~ /^(.+)\.pl$/ ) {
fc4b7602 260 my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
fc4b7602 261
9faec51a 262 no warnings 'redefine';
5b5defbc 263 my $fn = eval "$filedata";
fc4b7602 264 use warnings;
5b5defbc 265
9faec51a 266 if ($@) {
3fa64c79 267 carp "$filename failed to compile: $@";
9faec51a 268 } elsif (ref $fn eq 'CODE') {
fc4b7602 269 $fn->()
270 } else {
5b5defbc 271 carp "$filename should define an anonymous sub but it didn't!";
fc4b7602 272 }
273 } else {
274 croak "A file ($filename) got to preinstall_scripts that wasn't sql or perl!";
275 }
276 }
277}
278
c8a2f7bd 279sub _prepare_install {
73caa630 280 my $self = shift;
02a7b8ac 281 my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} };
c8a2f7bd 282 my $to_file = shift;
2e68a8e1 283 my $schema = $self->schema;
284 my $databases = $self->databases;
91adde75 285 my $dir = $self->script_directory;
73caa630 286 my $version = $self->schema_version;
d54b8d69 287
9600776d 288 my $sqlt = SQL::Translator->new({
d54b8d69 289 add_drop_table => 1,
2e68a8e1 290 ignore_constraint_names => 1,
d54b8d69 291 ignore_index_names => 1,
292 parser => 'SQL::Translator::Parser::DBIx::Class',
3aaf766f 293 %{$sqltargs}
9600776d 294 });
2e68a8e1 295
d53e0bfc 296 my $sqlt_schema = $sqlt->translate( data => $schema )
387b11d2 297 or croak($sqlt->error);
2e68a8e1 298
299 foreach my $db (@$databases) {
300 $sqlt->reset;
301 $sqlt->{schema} = $sqlt_schema;
302 $sqlt->producer($db);
303
c8a2f7bd 304 my $filename = $self->$to_file($db, $version, $dir);
9600776d 305 if (-e $filename ) {
2e68a8e1 306 carp "Overwriting existing DDL file - $filename";
307 unlink $filename;
308 }
309
310 my $output = $sqlt->translate;
311 if(!$output) {
312 carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")");
313 next;
314 }
387b11d2 315 open my $file, q(>), $filename;
2e68a8e1 316 print {$file} $output;
317 close $file;
318 }
319}
320
c8a2f7bd 321sub _resultsource_install_filename {
322 my ($self, $source_name) = @_;
323 return sub {
324 my ($self, $type, $version) = @_;
91adde75 325 my $dirname = catfile( $self->script_directory, $type, 'schema', $version );
c8a2f7bd 326 mkpath($dirname) unless -d $dirname;
327
328 return catfile( $dirname, "001-auto-$source_name.sql" );
329 }
330}
331
332sub install_resultsource {
be140a5f 333 my ($self, $args) = @_;
334 my $source = $args->{result_source};
335 my $version = $args->{version};
0df68524 336 log_info { '[DBICDH] installing_resultsource ' . $source->source_name . ", version $version" };
c8a2f7bd 337 my $rs_install_file =
338 $self->_resultsource_install_filename($source->source_name);
339
340 my $files = [
341 $self->$rs_install_file(
342 $self->storage->sqlt_type,
343 $version,
344 )
345 ];
346 $self->_run_sql_and_perl($files);
347}
348
349sub prepare_resultsource_install {
350 my $self = shift;
be140a5f 351 my $source = (shift @_)->{result_source};
0df68524 352 log_info { '[DBICDH] preparing install for resultsource ' . $source->source_name };
c8a2f7bd 353
354 my $filename = $self->_resultsource_install_filename($source->source_name);
355 $self->_prepare_install({
356 parser_args => { sources => [$source->source_name], }
357 }, $filename);
358}
359
91557c90 360sub prepare_deploy {
0df68524 361 log_info { '[DBICDH] preparing deploy' };
c8a2f7bd 362 my $self = shift;
363 $self->_prepare_install({}, '_ddl_schema_produce_filename');
364}
365
a41a04e5 366sub prepare_upgrade {
be140a5f 367 my ($self, $args) = @_;
0df68524 368 log_info {
369 '[DBICDH] preparing upgrade ' .
370 "from $args->{from_version} to $args->{to_version}"
371 };
be140a5f 372 $self->_prepare_changegrade(
373 $args->{from_version}, $args->{to_version}, $args->{version_set}, 'up'
374 );
76d311e7 375}
376
377sub prepare_downgrade {
be140a5f 378 my ($self, $args) = @_;
0df68524 379 log_info {
380 '[DBICDH] preparing downgrade ' .
381 "from $args->{from_version} to $args->{to_version}"
382 };
be140a5f 383 $self->_prepare_changegrade(
384 $args->{from_version}, $args->{to_version}, $args->{version_set}, 'down'
385 );
76d311e7 386}
387
388method _prepare_changegrade($from_version, $to_version, $version_set, $direction) {
2e68a8e1 389 my $schema = $self->schema;
390 my $databases = $self->databases;
91adde75 391 my $dir = $self->script_directory;
02a7b8ac 392 my $sqltargs = $self->sql_translator_args;
2e68a8e1 393
73caa630 394 my $schema_version = $self->schema_version;
2e68a8e1 395
396 $sqltargs = {
397 add_drop_table => 1,
398 ignore_constraint_names => 1,
399 ignore_index_names => 1,
400 %{$sqltargs}
401 };
402
403 my $sqlt = SQL::Translator->new( $sqltargs );
404
405 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
d53e0bfc 406 my $sqlt_schema = $sqlt->translate( data => $schema )
387b11d2 407 or croak($sqlt->error);
2e68a8e1 408
409 foreach my $db (@$databases) {
410 $sqlt->reset;
411 $sqlt->{schema} = $sqlt_schema;
412 $sqlt->producer($db);
413
76d311e7 414 my $prefilename = $self->_ddl_schema_produce_filename($db, $from_version, $dir);
2e68a8e1 415 unless(-e $prefilename) {
416 carp("No previous schema file found ($prefilename)");
417 next;
418 }
76d311e7 419 my $diff_file_method = "_ddl_schema_${direction}_produce_filename";
420 my $diff_file = $self->$diff_file_method($db, $version_set, $dir );
2e68a8e1 421 if(-e $diff_file) {
76d311e7 422 carp("Overwriting existing $direction-diff file - $diff_file");
2e68a8e1 423 unlink $diff_file;
424 }
425
426 my $source_schema;
427 {
428 my $t = SQL::Translator->new({
429 %{$sqltargs},
430 debug => 0,
431 trace => 0,
432 });
433
434 $t->parser( $db ) # could this really throw an exception?
387b11d2 435 or croak($t->error);
2e68a8e1 436
437 my $out = $t->translate( $prefilename )
387b11d2 438 or croak($t->error);
2e68a8e1 439
440 $source_schema = $t->schema;
441
442 $source_schema->name( $prefilename )
443 unless $source_schema->name;
444 }
445
446 # The "new" style of producers have sane normalization and can support
447 # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't
448 # And we have to diff parsed SQL against parsed SQL.
449 my $dest_schema = $sqlt_schema;
450
451 unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) {
452 my $t = SQL::Translator->new({
453 %{$sqltargs},
454 debug => 0,
455 trace => 0,
456 });
457
458 $t->parser( $db ) # could this really throw an exception?
387b11d2 459 or croak($t->error);
2e68a8e1 460
76d311e7 461 my $filename = $self->_ddl_schema_produce_filename($db, $to_version, $dir);
2e68a8e1 462 my $out = $t->translate( $filename )
387b11d2 463 or croak($t->error);
2e68a8e1 464
465 $dest_schema = $t->schema;
466
467 $dest_schema->name( $filename )
468 unless $dest_schema->name;
469 }
470
471 my $diff = SQL::Translator::Diff::schema_diff(
472 $source_schema, $db,
473 $dest_schema, $db,
474 $sqltargs
475 );
387b11d2 476 open my $file, q(>), $diff_file;
2e68a8e1 477 print {$file} $diff;
478 close $file;
479 }
480}
481
334bced5 482method _read_sql_file($file) {
483 return unless $file;
484
aabd4237 485 open my $fh, '<', $file;
0d19af1d 486 my @data = split /;\n/, join '', <$fh>;
334bced5 487 close $fh;
488
489 @data = grep {
0d19af1d 490 $_ && # remove blank lines
491 !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's
492 } map {
493 s/^\s+//; s/\s+$//; # trim whitespace
494 join '', grep { !/^--/ } split /\n/ # remove comments
495 } @data;
334bced5 496
497 return \@data;
498}
499
7d2a6974 500sub downgrade_single_step {
76d311e7 501 my $self = shift;
be140a5f 502 my $version_set = (shift @_)->{version_set};
0df68524 503 log_info { qq([DBICDH] downgrade_single_step'ing ) . Dumper($version_set) };
41219a5d 504
505 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_down_consume_filenames(
76d311e7 506 $self->storage->sqlt_type,
627581cd 507 $version_set,
41219a5d 508 ));
3249629f 509
41219a5d 510 return ['', $sql];
76d311e7 511}
512
7d2a6974 513sub upgrade_single_step {
7521a845 514 my $self = shift;
be140a5f 515 my $version_set = (shift @_)->{version_set};
0df68524 516 log_info { qq([DBICDH] upgrade_single_step'ing ) . Dumper($version_set) };
41219a5d 517
518 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_up_consume_filenames(
334bced5 519 $self->storage->sqlt_type,
627581cd 520 $version_set,
41219a5d 521 ));
522 return ['', $sql];
334bced5 523}
524
aabd4237 525__PACKAGE__->meta->make_immutable;
526
2e68a8e1 5271;
e051bb00 528
e52174e3 529# vim: ts=2 sw=2 expandtab
530
e051bb00 531__END__
532
bcc72297 533=head1 DESCRIPTION
534
535This class is the meat of L<DBIx::Class::DeploymentHandler>. It takes care of
536generating sql files representing schemata as well as sql files to move from
537one version of a schema to the rest. One of the hallmark features of this
538class is that it allows for multiple sql files for deploy and upgrade, allowing
539developers to fine tune deployment. In addition it also allows for perl files
540to be run at any stage of the process.
541
542For basic usage see L<DBIx::Class::DeploymentHandler::HandlesDeploy>. What's
543documented here is extra fun stuff or private methods.
544
545=head1 DIRECTORY LAYOUT
546
92c34cab 547Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>. It's
548heavily based upon L<DBIx::Migration::Directories>, but has some extensions and
549modifications, so even if you are familiar with it, please read this. I feel
550like the best way to describe the layout is with the following example:
551
552 $sql_migration_dir
553 |- SQLite
554 | |- down
4f85efc6 555 | | `- 2-1
92c34cab 556 | | `- 001-auto.sql
557 | |- schema
558 | | `- 1
559 | | `- 001-auto.sql
560 | `- up
561 | |- 1-2
562 | | `- 001-auto.sql
563 | `- 2-3
564 | `- 001-auto.sql
565 |- _common
566 | |- down
4f85efc6 567 | | `- 2-1
92c34cab 568 | | `- 002-remove-customers.pl
569 | `- up
570 | `- 1-2
571 | `- 002-generate-customers.pl
572 |- _generic
573 | |- down
4f85efc6 574 | | `- 2-1
92c34cab 575 | | `- 001-auto.sql
576 | |- schema
577 | | `- 1
578 | | `- 001-auto.sql
579 | `- up
580 | `- 1-2
581 | |- 001-auto.sql
582 | `- 002-create-stored-procedures.sql
583 `- MySQL
584 |- down
4f85efc6 585 | `- 2-1
92c34cab 586 | `- 001-auto.sql
80ff6f6d 587 |- preinstall
588 | `- 1
589 | |- 001-create_database.pl
590 | `- 002-create_users_and_permissions.pl
92c34cab 591 |- schema
592 | `- 1
593 | `- 001-auto.sql
594 `- up
595 `- 1-2
596 `- 001-auto.sql
597
598So basically, the code
599
600 $dm->deploy(1)
601
602on an C<SQLite> database that would simply run
603C<$sql_migration_dir/SQLite/schema/1/001-auto.sql>. Next,
604
605 $dm->upgrade_single_step([1,2])
606
607would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql> followed by
608C<$sql_migration_dir/_common/up/1-2/002-generate-customers.pl>.
609
610Now, a C<.pl> file doesn't have to be in the C<_common> directory, but most of
611the time it probably should be, since perl scripts will mostly be database
612independent.
613
614C<_generic> exists for when you for some reason are sure that your SQL is
615generic enough to run on all databases. Good luck with that one.
616
80ff6f6d 617Note that unlike most steps in the process, C<preinstall> will not run SQL, as
618there may not even be an database at preinstall time. It will run perl scripts
619just like the other steps in the process, but nothing is passed to them.
620Until people have used this more it will remain freeform, but a recommended use
621of preinstall is to have it prompt for username and password, and then call the
622appropriate C<< CREATE DATABASE >> commands etc.
623
92c34cab 624=head1 PERL SCRIPTS
625
7d0b0f2b 626A perl script for this tool is very simple. It merely needs to contain an
627anonymous sub that takes a L<DBIx::Class::Schema> as it's only argument.
92c34cab 628A very basic perl script might look like:
629
630 #!perl
631
632 use strict;
633 use warnings;
634
7d0b0f2b 635 sub {
92c34cab 636 my $schema = shift;
637
638 $schema->resultset('Users')->create({
639 name => 'root',
640 password => 'root',
641 })
642 }
bcc72297 643
eb28403b 644=attr schema
a65184c8 645
bcc72297 646The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
647and generate the DDL.
648
eb28403b 649=attr storage
a65184c8 650
bcc72297 651The L<DBIx::Class::Storage> that is I<actually> used to talk to the database
652and generate the DDL. This is automatically created with L</_build_storage>.
653
02a7b8ac 654=attr sql_translator_args
cfc9edf9 655
02a7b8ac 656The arguments that get passed to L<SQL::Translator> when it's used.
a65184c8 657
91adde75 658=attr script_directory
cfc9edf9 659
91adde75 660The directory (default C<'sql'>) that scripts are stored in
cfc9edf9 661
eb28403b 662=attr databases
cfc9edf9 663
664The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to
665generate files for
666
eb28403b 667=attr txn_wrap
668
bcc72297 669Set to true (which is the default) to wrap all upgrades and deploys in a single
670transaction.
671
73caa630 672=attr schema_version
673
674The version the schema on your harddrive is at. Defaults to
675C<< $self->schema->schema_version >>.
676
db223aff 677=begin comment
678
679=head2 __ddl_consume_with_prefix
a65184c8 680
bcc72297 681 $dm->__ddl_consume_with_prefix( 'SQLite', [qw( 1.00 1.01 )], 'up' )
682
683This is the meat of the multi-file upgrade/deploy stuff. It returns a list of
684files in the order that they should be run for a generic "type" of upgrade.
685You should not be calling this in user code.
686
db223aff 687=head2 _ddl_schema_consume_filenames
a65184c8 688
bcc72297 689 $dm->__ddl_schema_consume_filenames( 'SQLite', [qw( 1.00 )] )
690
691Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
692initial deploy.
693
db223aff 694=head2 _ddl_schema_produce_filename
a65184c8 695
bcc72297 696 $dm->__ddl_schema_produce_filename( 'SQLite', [qw( 1.00 )] )
697
698Returns a single file in which an initial schema will be stored.
699
db223aff 700=head2 _ddl_schema_up_consume_filenames
a65184c8 701
bcc72297 702 $dm->_ddl_schema_up_consume_filenames( 'SQLite', [qw( 1.00 )] )
703
704Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
705upgrade.
706
db223aff 707=head2 _ddl_schema_down_consume_filenames
a65184c8 708
bcc72297 709 $dm->_ddl_schema_down_consume_filenames( 'SQLite', [qw( 1.00 )] )
710
711Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for a
712downgrade.
713
db223aff 714=head2 _ddl_schema_up_produce_filenames
a65184c8 715
bcc72297 716 $dm->_ddl_schema_up_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
717
718Returns a single file in which the sql to upgrade from one schema to another
719will be stored.
720
db223aff 721=head2 _ddl_schema_down_produce_filename
bcc72297 722
723 $dm->_ddl_schema_down_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
724
725Returns a single file in which the sql to downgrade from one schema to another
726will be stored.
a65184c8 727
db223aff 728=head2 _resultsource_install_filename
a65184c8 729
bcc72297 730 my $filename_fn = $dm->_resultsource_install_filename('User');
731 $dm->$filename_fn('SQLite', '1.00')
732
733Returns a function which in turn returns a single filename used to install a
734single resultsource. Weird interface is convenient for me. Deal with it.
735
db223aff 736=head2 _run_sql_and_perl
eb28403b 737
bcc72297 738 $dm->_run_sql_and_perl([qw( list of filenames )])
a65184c8 739
bcc72297 740Simply put, this runs the list of files passed to it. If the file ends in
741C<.sql> it runs it as sql and if it ends in C<.pl> it runs it as a perl file.
a65184c8 742
bcc72297 743Depending on L</txn_wrap> all of the files run will be wrapped in a single
744transaction.
eb28403b 745
db223aff 746=head2 _prepare_install
a65184c8 747
bcc72297 748 $dm->_prepare_install({ add_drop_table => 0 }, sub { 'file_to_create' })
a65184c8 749
bcc72297 750Generates the sql file for installing the database. First arg is simply
751L<SQL::Translator> args and the second is a coderef that returns the filename
752to store the sql in.
a65184c8 753
db223aff 754=head2 _prepare_changegrade
bcc72297 755
756 $dm->_prepare_changegrade('1.00', '1.01', [qw( 1.00 1.01)], 'up')
a65184c8 757
bcc72297 758Generates the sql file for migrating from one schema version to another. First
759arg is the version to start from, second is the version to go to, third is the
760L<version set|DBIx::Class::DeploymentHandler/VERSION SET>, and last is the
761direction of the changegrade, be it 'up' or 'down'.
a65184c8 762
db223aff 763=head2 _read_sql_file
a65184c8 764
bcc72297 765 $dm->_read_sql_file('foo.sql')
a65184c8 766
bcc72297 767Reads a sql file and returns lines in an C<ArrayRef>. Strips out comments,
768transactions, and blank lines.
eb28403b 769
db223aff 770=end comment