1 package DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator;
4 # ABSTRACT: Manage your SQL and Perl migrations in nicely laid out directories
7 use Carp qw( carp croak );
9 use Method::Signatures::Simple;
13 require SQL::Translator::Diff;
15 require DBIx::Class::Storage; # loaded for type constraint
16 use DBIx::Class::DeploymentHandler::Types;
18 use File::Path 'mkpath';
19 use File::Spec::Functions;
21 with 'DBIx::Class::DeploymentHandler::HandlesDeploy';
24 isa => 'DBIx::Class::Schema',
30 isa => 'DBIx::Class::Storage',
35 method _build_storage {
36 my $s = $self->schema->storage;
37 $s->_determine_driver;
41 has sql_translator_args => (
44 default => sub { {} },
46 has upgrade_directory => (
55 isa => 'DBIx::Class::DeploymentHandler::Databases',
57 default => sub { [qw( MySQL SQLite PostgreSQL )] },
66 has schema_version => (
71 method _build_schema_version { $self->schema->schema_version }
73 method __ddl_consume_with_prefix($type, $versions, $prefix) {
74 my $base_dir = $self->upgrade_directory;
76 my $main = catfile( $base_dir, $type );
77 my $generic = catfile( $base_dir, '_generic' );
79 catfile( $base_dir, '_common', $prefix, join q(-), @{$versions} );
83 $dir = catfile($main, $prefix, join q(-), @{$versions})
84 } elsif (-d $generic) {
85 $dir = catfile($generic, $prefix, join q(-), @{$versions});
87 croak "neither $main or $generic exist; please write/generate some SQL";
90 opendir my($dh), $dir;
91 my %files = map { $_ => "$dir/$_" } grep { /\.(?:sql|pl)$/ && -f "$dir/$_" } readdir $dh;
95 opendir my($dh), $common;
96 for my $filename (grep { /\.(?:sql|pl)$/ && -f catfile($common,$_) } readdir $dh) {
97 unless ($files{$filename}) {
98 $files{$filename} = catfile($common,$filename);
104 return [@files{sort keys %files}]
107 method _ddl_preinstall_consume_filenames($type, $version) {
108 $self->__ddl_consume_with_prefix($type, [ $version ], 'preinstall')
111 method _ddl_schema_consume_filenames($type, $version) {
112 $self->__ddl_consume_with_prefix($type, [ $version ], 'schema')
115 method _ddl_schema_produce_filename($type, $version) {
116 my $dirname = catfile( $self->upgrade_directory, $type, 'schema', $version );
117 mkpath($dirname) unless -d $dirname;
119 return catfile( $dirname, '001-auto.sql' );
122 method _ddl_schema_up_consume_filenames($type, $versions) {
123 $self->__ddl_consume_with_prefix($type, $versions, 'up')
126 method _ddl_schema_down_consume_filenames($type, $versions) {
127 $self->__ddl_consume_with_prefix($type, $versions, 'down')
130 method _ddl_schema_up_produce_filename($type, $versions) {
131 my $dir = $self->upgrade_directory;
133 my $dirname = catfile( $dir, $type, 'up', join q(-), @{$versions});
134 mkpath($dirname) unless -d $dirname;
136 return catfile( $dirname, '001-auto.sql'
140 method _ddl_schema_down_produce_filename($type, $versions, $dir) {
141 my $dirname = catfile( $dir, $type, 'down', join q(-), @{$versions} );
142 mkpath($dirname) unless -d $dirname;
144 return catfile( $dirname, '001-auto.sql');
147 method _run_sql_and_perl($filenames) {
148 my @files = @{$filenames};
149 my $storage = $self->storage;
152 my $guard = $self->schema->txn_scope_guard if $self->txn_wrap;
155 for my $filename (@files) {
156 if ($filename =~ /\.sql$/) {
157 my @sql = @{$self->_read_sql_file($filename)};
158 $sql .= join "\n", @sql;
160 foreach my $line (@sql) {
161 $storage->_query_start($line);
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) });
168 carp "$_ (running '${line}')"
170 $storage->_query_end($line);
172 } elsif ( $filename =~ /^(.+)\.pl$/ ) {
174 my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
175 # make the package name more palateable to perl
176 $package =~ s/\W/_/g;
178 no warnings 'redefine';
179 eval "package $package;\n\n$filedata";
182 if (my $fn = $package->can('run')) {
183 $fn->($self->schema);
185 carp "$filename should define a run method that takes a schema but it didn't!";
188 croak "A file ($filename) got to deploy that wasn't sql or perl!";
192 $guard->commit if $self->txn_wrap;
199 my $version = shift || $self->schema_version;
201 return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames(
202 $self->storage->sqlt_type,
207 sub preinstall_scripts {
209 my $version = shift || $self->schema_version;
211 my @files = @{$self->_ddl_preinstall_consume_filenames(
212 $self->storage->sqlt_type,
216 for my $filename (@files) {
217 # We ignore sql for now (till I figure out what to do with it)
218 if ( $filename =~ /^(.+)\.pl$/ ) {
220 my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
221 # make the package name more palateable to perl
222 $package =~ s/\W/_/g;
224 no warnings 'redefine';
225 eval "package $package;\n\n$filedata";
228 carp "$filename failed to compile: $@";
229 } elsif (my $fn = $package->can('run')) {
232 carp "$filename should define a run sub but it didn't!";
235 croak "A file ($filename) got to preinstall_scripts that wasn't sql or perl!";
240 sub _prepare_install {
242 my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} };
244 my $schema = $self->schema;
245 my $databases = $self->databases;
246 my $dir = $self->upgrade_directory;
247 my $version = $self->schema_version;
249 my $sqlt = SQL::Translator->new({
251 ignore_constraint_names => 1,
252 ignore_index_names => 1,
253 parser => 'SQL::Translator::Parser::DBIx::Class',
257 my $sqlt_schema = $sqlt->translate( data => $schema )
258 or croak($sqlt->error);
260 foreach my $db (@$databases) {
262 $sqlt->{schema} = $sqlt_schema;
263 $sqlt->producer($db);
265 my $filename = $self->$to_file($db, $version, $dir);
267 carp "Overwriting existing DDL file - $filename";
271 my $output = $sqlt->translate;
273 carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")");
276 open my $file, q(>), $filename;
277 print {$file} $output;
282 sub _resultsource_install_filename {
283 my ($self, $source_name) = @_;
285 my ($self, $type, $version) = @_;
286 my $dirname = catfile( $self->upgrade_directory, $type, 'schema', $version );
287 mkpath($dirname) unless -d $dirname;
289 return catfile( $dirname, "001-auto-$source_name.sql" );
293 sub install_resultsource {
294 my ($self, $source, $version) = @_;
296 my $rs_install_file =
297 $self->_resultsource_install_filename($source->source_name);
300 $self->$rs_install_file(
301 $self->storage->sqlt_type,
305 $self->_run_sql_and_perl($files);
308 sub prepare_resultsource_install {
312 my $filename = $self->_resultsource_install_filename($source->source_name);
313 $self->_prepare_install({
314 parser_args => { sources => [$source->source_name], }
320 $self->_prepare_install({}, '_ddl_schema_produce_filename');
323 sub prepare_upgrade {
324 my ($self, $from_version, $to_version, $version_set) = @_;
325 $self->_prepare_changegrade($from_version, $to_version, $version_set, 'up');
328 sub prepare_downgrade {
329 my ($self, $from_version, $to_version, $version_set) = @_;
331 $self->_prepare_changegrade($from_version, $to_version, $version_set, 'down');
334 method _prepare_changegrade($from_version, $to_version, $version_set, $direction) {
335 my $schema = $self->schema;
336 my $databases = $self->databases;
337 my $dir = $self->upgrade_directory;
338 my $sqltargs = $self->sql_translator_args;
340 my $schema_version = $self->schema_version;
344 ignore_constraint_names => 1,
345 ignore_index_names => 1,
349 my $sqlt = SQL::Translator->new( $sqltargs );
351 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
352 my $sqlt_schema = $sqlt->translate( data => $schema )
353 or croak($sqlt->error);
355 foreach my $db (@$databases) {
357 $sqlt->{schema} = $sqlt_schema;
358 $sqlt->producer($db);
360 my $prefilename = $self->_ddl_schema_produce_filename($db, $from_version, $dir);
361 unless(-e $prefilename) {
362 carp("No previous schema file found ($prefilename)");
365 my $diff_file_method = "_ddl_schema_${direction}_produce_filename";
366 my $diff_file = $self->$diff_file_method($db, $version_set, $dir );
368 carp("Overwriting existing $direction-diff file - $diff_file");
374 my $t = SQL::Translator->new({
380 $t->parser( $db ) # could this really throw an exception?
383 my $out = $t->translate( $prefilename )
386 $source_schema = $t->schema;
388 $source_schema->name( $prefilename )
389 unless $source_schema->name;
392 # The "new" style of producers have sane normalization and can support
393 # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't
394 # And we have to diff parsed SQL against parsed SQL.
395 my $dest_schema = $sqlt_schema;
397 unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) {
398 my $t = SQL::Translator->new({
404 $t->parser( $db ) # could this really throw an exception?
407 my $filename = $self->_ddl_schema_produce_filename($db, $to_version, $dir);
408 my $out = $t->translate( $filename )
411 $dest_schema = $t->schema;
413 $dest_schema->name( $filename )
414 unless $dest_schema->name;
417 my $diff = SQL::Translator::Diff::schema_diff(
422 open my $file, q(>), $diff_file;
428 method _read_sql_file($file) {
431 open my $fh, '<', $file;
432 my @data = split /;\n/, join '', <$fh>;
436 $_ && # remove blank lines
437 !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's
439 s/^\s+//; s/\s+$//; # trim whitespace
440 join '', grep { !/^--/ } split /\n/ # remove comments
446 sub downgrade_single_step {
448 my $version_set = shift @_;
450 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_down_consume_filenames(
451 $self->storage->sqlt_type,
458 sub upgrade_single_step {
460 my $version_set = shift @_;
462 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_up_consume_filenames(
463 $self->storage->sqlt_type,
469 __PACKAGE__->meta->make_immutable;
473 # vim: ts=2 sw=2 expandtab
479 This class is the meat of L<DBIx::Class::DeploymentHandler>. It takes care of
480 generating sql files representing schemata as well as sql files to move from
481 one version of a schema to the rest. One of the hallmark features of this
482 class is that it allows for multiple sql files for deploy and upgrade, allowing
483 developers to fine tune deployment. In addition it also allows for perl files
484 to be run at any stage of the process.
486 For basic usage see L<DBIx::Class::DeploymentHandler::HandlesDeploy>. What's
487 documented here is extra fun stuff or private methods.
489 =head1 DIRECTORY LAYOUT
491 Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>. It's
492 heavily based upon L<DBIx::Migration::Directories>, but has some extensions and
493 modifications, so even if you are familiar with it, please read this. I feel
494 like the best way to describe the layout is with the following example:
512 | | `- 002-remove-customers.pl
515 | `- 002-generate-customers.pl
526 | `- 002-create-stored-procedures.sql
538 So basically, the code
542 on an C<SQLite> database that would simply run
543 C<$sql_migration_dir/SQLite/schema/1/001-auto.sql>. Next,
545 $dm->upgrade_single_step([1,2])
547 would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql> followed by
548 C<$sql_migration_dir/_common/up/1-2/002-generate-customers.pl>.
550 Now, a C<.pl> file doesn't have to be in the C<_common> directory, but most of
551 the time it probably should be, since perl scripts will mostly be database
554 C<_generic> exists for when you for some reason are sure that your SQL is
555 generic enough to run on all databases. Good luck with that one.
559 A perl script for this tool is very simple. It merely needs to contain a
560 sub called C<run> that takes a L<DBIx::Class::Schema> as it's only argument.
561 A very basic perl script might look like:
571 $schema->resultset('Users')->create({
579 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
580 and generate the DDL.
584 The L<DBIx::Class::Storage> that is I<actually> used to talk to the database
585 and generate the DDL. This is automatically created with L</_build_storage>.
587 =attr sql_translator_args
589 The arguments that get passed to L<SQL::Translator> when it's used.
591 =attr upgrade_directory
593 The directory (default C<'sql'>) that upgrades are stored in
597 The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to
602 Set to true (which is the default) to wrap all upgrades and deploys in a single
607 The version the schema on your harddrive is at. Defaults to
608 C<< $self->schema->schema_version >>.
610 =method __ddl_consume_with_prefix
612 $dm->__ddl_consume_with_prefix( 'SQLite', [qw( 1.00 1.01 )], 'up' )
614 This is the meat of the multi-file upgrade/deploy stuff. It returns a list of
615 files in the order that they should be run for a generic "type" of upgrade.
616 You should not be calling this in user code.
618 =method _ddl_schema_consume_filenames
620 $dm->__ddl_schema_consume_filenames( 'SQLite', [qw( 1.00 )] )
622 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
625 =method _ddl_schema_produce_filename
627 $dm->__ddl_schema_produce_filename( 'SQLite', [qw( 1.00 )] )
629 Returns a single file in which an initial schema will be stored.
631 =method _ddl_schema_up_consume_filenames
633 $dm->_ddl_schema_up_consume_filenames( 'SQLite', [qw( 1.00 )] )
635 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
638 =method _ddl_schema_down_consume_filenames
640 $dm->_ddl_schema_down_consume_filenames( 'SQLite', [qw( 1.00 )] )
642 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for a
645 =method _ddl_schema_up_produce_filenames
647 $dm->_ddl_schema_up_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
649 Returns a single file in which the sql to upgrade from one schema to another
652 =method _ddl_schema_down_produce_filename
654 $dm->_ddl_schema_down_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
656 Returns a single file in which the sql to downgrade from one schema to another
659 =method _resultsource_install_filename
661 my $filename_fn = $dm->_resultsource_install_filename('User');
662 $dm->$filename_fn('SQLite', '1.00')
664 Returns a function which in turn returns a single filename used to install a
665 single resultsource. Weird interface is convenient for me. Deal with it.
667 =method _run_sql_and_perl
669 $dm->_run_sql_and_perl([qw( list of filenames )])
671 Simply put, this runs the list of files passed to it. If the file ends in
672 C<.sql> it runs it as sql and if it ends in C<.pl> it runs it as a perl file.
674 Depending on L</txn_wrap> all of the files run will be wrapped in a single
677 =method _prepare_install
679 $dm->_prepare_install({ add_drop_table => 0 }, sub { 'file_to_create' })
681 Generates the sql file for installing the database. First arg is simply
682 L<SQL::Translator> args and the second is a coderef that returns the filename
685 =method _prepare_changegrade
687 $dm->_prepare_changegrade('1.00', '1.01', [qw( 1.00 1.01)], 'up')
689 Generates the sql file for migrating from one schema version to another. First
690 arg is the version to start from, second is the version to go to, third is the
691 L<version set|DBIx::Class::DeploymentHandler/VERSION SET>, and last is the
692 direction of the changegrade, be it 'up' or 'down'.
694 =method _read_sql_file
696 $dm->_read_sql_file('foo.sql')
698 Reads a sql file and returns lines in an C<ArrayRef>. Strips out comments,
699 transactions, and blank lines.