X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-DeploymentHandler.git;a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FDeploymentHandler%2FDeployMethod%2FSQL%2FTranslator.pm;h=fc0aa48dfc6220cf841c7c6a564aee7ae99d999a;hp=c2bbc473a7ce10e671b559f3ce1998315f12c206;hb=28563f9710edc544df39b3828f36eb8f7db0dc21;hpb=7e08eddd832a00dbe52d8f1ba90ea486c48f7ab1 diff --git a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm index c2bbc47..fc0aa48 100644 --- a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm +++ b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm @@ -25,6 +25,12 @@ use File::Spec::Functions; with 'DBIx::Class::DeploymentHandler::HandlesDeploy'; +has ignore_ddl => ( + isa => 'Bool', + is => 'ro', + default => undef, +); + has schema => ( isa => 'DBIx::Class::Schema', is => 'ro', @@ -149,8 +155,7 @@ method _ddl_schema_up_produce_filename($type, $versions) { my $dirname = catfile( $dir, $type, 'up', join q(-), @{$versions}); mkpath($dirname) unless -d $dirname; - return catfile( $dirname, '001-auto.sql' - ); + return catfile( $dirname, '001-auto.sql' ); } method _ddl_schema_down_produce_filename($type, $versions, $dir) { @@ -208,20 +213,6 @@ method _run_perl($filename) { carp "$filename should define an anonymouse sub that takes a schema but it didn't!"; } } -{ - my $json; - - method _run_serialized_sql($filename, $type) { - if ($type eq 'json') { - require JSON; - $json ||= JSON->new->pretty; - my @sql = @{$json->decode($filename)}; - } else { - croak "A file ($filename) got to deploy that wasn't sql or perl!"; - } - } - -} method _run_sql_and_perl($filenames) { my @files = @{$filenames}; @@ -231,8 +222,6 @@ method _run_sql_and_perl($filenames) { for my $filename (@files) { if ($filename =~ /\.sql$/) { $sql .= $self->_run_sql($filename) - } elsif ( $filename =~ /\.sql-(\w+)$/ ) { - $sql .= $self->_run_serialized_sql($filename, $1) } elsif ( $filename =~ /\.pl$/ ) { $self->_run_perl($filename) } else { @@ -245,17 +234,20 @@ method _run_sql_and_perl($filenames) { return $sql; } -sub deploy { - my $self = shift; - my $version = (shift @_ || {})->{version} || $self->schema_version; - log_info { "deploying version $version" }; - +method _deploy($version) { return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames( $self->storage->sqlt_type, $version, )); } +sub deploy { + my $self = shift; + my $version = (shift @_ || {})->{version} || $self->schema_version; + log_info { "deploying version $version" }; + $self->_deploy($version); +} + sub preinstall { my $self = shift; my $args = shift; @@ -290,40 +282,109 @@ sub preinstall { } } -sub _prepare_install { - my $self = shift; - my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; - my $to_file = shift; - my $schema = $self->schema; - my $databases = $self->databases; +method _sqldiff_from_yaml($from_version, $to_version, $db) { my $dir = $self->script_directory; + my $sqltargs = { + add_drop_table => 1, + ignore_constraint_names => 1, + ignore_index_names => 1, + %{$self->sql_translator_args} + }; + + my $source_schema; + { + my $prefilename = $self->_ddl_protoschema_produce_filename($from_version, $dir); + + # should probably be a croak + carp("No previous schema file found ($prefilename)") + unless -e $prefilename; + + my $t = SQL::Translator->new({ + %{$sqltargs}, + debug => 0, + trace => 0, + parser => 'SQL::Translator::Parser::YAML', + }); + + my $out = $t->translate( $prefilename ) + or croak($t->error); + + $source_schema = $t->schema; + + $source_schema->name( $prefilename ) + unless $source_schema->name; + } + + my $dest_schema; + { + my $filename = $self->_ddl_protoschema_produce_filename($to_version, $dir); + + # should probably be a croak + carp("No next schema file found ($filename)") + unless -e $filename; + + my $t = SQL::Translator->new({ + %{$sqltargs}, + debug => 0, + trace => 0, + parser => 'SQL::Translator::Parser::YAML', + }); + + my $out = $t->translate( $filename ) + or croak($t->error); + + $dest_schema = $t->schema; + + $dest_schema->name( $filename ) + unless $dest_schema->name; + } + return [SQL::Translator::Diff::schema_diff( + $source_schema, $db, + $dest_schema, $db, + $sqltargs + )]; +} + +method _sql_from_yaml($sqltargs, $from_file, $db) { + my $schema = $self->schema; my $version = $self->schema_version; my $sqlt = SQL::Translator->new({ add_drop_table => 1, parser => 'SQL::Translator::Parser::YAML', - %{$sqltargs} + %{$sqltargs}, + producer => $db, }); - my $yaml_filename = $self->_ddl_protoschema_produce_filename($version); + my $yaml_filename = $self->$from_file($version); + + my @sql = $sqlt->translate($yaml_filename); + if(!@sql) { + carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); + return undef; + } + return \@sql; +} + +sub _prepare_install { + my $self = shift; + my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; + my $from_file = shift; + my $to_file = shift; + my $dir = $self->script_directory; + my $databases = $self->databases; + my $version = $self->schema_version; foreach my $db (@$databases) { - $sqlt->reset; - $sqlt->producer($db); + my $sql = $self->_sql_from_yaml($sqltargs, $from_file, $db ) or next; my $filename = $self->$to_file($db, $version, $dir); if (-e $filename ) { carp "Overwriting existing DDL file - $filename"; unlink $filename; } - - my $sql = $sqlt->translate($yaml_filename); - if(!$sql) { - carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); - next; - } open my $file, q(>), $filename; - print {$file} $sql; + print {$file} join ";\n", @$sql; close $file; } } @@ -339,6 +400,17 @@ sub _resultsource_install_filename { } } +sub _resultsource_protoschema_filename { + my ($self, $source_name) = @_; + return sub { + my ($self, $version) = @_; + my $dirname = catfile( $self->script_directory, '_protoschema', $version ); + mkpath($dirname) unless -d $dirname; + + return catfile( $dirname, "001-auto-$source_name.yml" ); + } +} + sub install_resultsource { my ($self, $args) = @_; my $source = $args->{result_source}; @@ -361,17 +433,19 @@ sub prepare_resultsource_install { my $source = (shift @_)->{result_source}; log_info { 'preparing install for resultsource ' . $source->source_name }; - my $filename = $self->_resultsource_install_filename($source->source_name); - $self->_prepare_install({ + my $install_filename = $self->_resultsource_install_filename($source->source_name); + my $proto_filename = $self->_resultsource_protoschema_filename($source->source_name); + $self->prepare_protoschema({ parser_args => { sources => [$source->source_name], } - }, $filename); + }, $proto_filename); + $self->_prepare_install({}, $proto_filename, $install_filename); } sub prepare_deploy { log_info { 'preparing deploy' }; my $self = shift; - $self->_generate_protoschema; - $self->_prepare_install({}, '_ddl_schema_produce_filename'); + $self->prepare_protoschema({}, '_ddl_protoschema_produce_filename'); + $self->_prepare_install({}, '_ddl_protoschema_produce_filename', '_ddl_schema_produce_filename'); } sub prepare_upgrade { @@ -398,92 +472,20 @@ method _prepare_changegrade($from_version, $to_version, $version_set, $direction my $schema = $self->schema; my $databases = $self->databases; my $dir = $self->script_directory; - my $sqltargs = $self->sql_translator_args; - my $schema_version = $self->schema_version; - - $sqltargs = { - add_drop_table => 1, - ignore_constraint_names => 1, - ignore_index_names => 1, - %{$sqltargs} - }; - - my $sqlt = SQL::Translator->new( $sqltargs ); - - $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); - my $sqlt_schema = $sqlt->translate( data => $schema ) - or croak($sqlt->error); + return if $self->ignore_ddl; + my $schema_version = $self->schema_version; + my $diff_file_method = "_ddl_schema_${direction}_produce_filename"; foreach my $db (@$databases) { - $sqlt->reset; - $sqlt->{schema} = $sqlt_schema; - $sqlt->producer($db); - - my $prefilename = $self->_ddl_schema_produce_filename($db, $from_version, $dir); - unless(-e $prefilename) { - carp("No previous schema file found ($prefilename)"); - next; - } - my $diff_file_method = "_ddl_schema_${direction}_produce_filename"; my $diff_file = $self->$diff_file_method($db, $version_set, $dir ); if(-e $diff_file) { carp("Overwriting existing $direction-diff file - $diff_file"); unlink $diff_file; } - my $source_schema; - { - my $t = SQL::Translator->new({ - %{$sqltargs}, - debug => 0, - trace => 0, - }); - - $t->parser( $db ) # could this really throw an exception? - or croak($t->error); - - my $out = $t->translate( $prefilename ) - or croak($t->error); - - $source_schema = $t->schema; - - $source_schema->name( $prefilename ) - unless $source_schema->name; - } - - # The "new" style of producers have sane normalization and can support - # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't - # And we have to diff parsed SQL against parsed SQL. - my $dest_schema = $sqlt_schema; - - unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) { - my $t = SQL::Translator->new({ - %{$sqltargs}, - debug => 0, - trace => 0, - }); - - $t->parser( $db ) # could this really throw an exception? - or croak($t->error); - - my $filename = $self->_ddl_schema_produce_filename($db, $to_version, $dir); - my $out = $t->translate( $filename ) - or croak($t->error); - - $dest_schema = $t->schema; - - $dest_schema->name( $filename ) - unless $dest_schema->name; - } - - my $diff = SQL::Translator::Diff::schema_diff( - $source_schema, $db, - $dest_schema, $db, - $sqltargs - ); open my $file, q(>), $diff_file; - print {$file} $diff; + print {$file} join ";\n", @{$self->_sqldiff_from_yaml($from_version, $to_version, $db)}; close $file; } } @@ -531,16 +533,20 @@ sub upgrade_single_step { return ['', $sql]; } -sub _generate_protoschema { +sub prepare_protoschema { my $self = shift; + my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; + my $to_file = shift; my $filename - = $self->_ddl_protoschema_produce_filename($self->schema_version); + = $self->$to_file($self->schema_version); + # we do this because the code that uses this sets parser args, + # so we just need to merge in the package + $sqltargs->{parser_args}{package} = $self->schema; my $sqlt = SQL::Translator->new({ parser => 'SQL::Translator::Parser::DBIx::Class', producer => 'SQL::Translator::Producer::YAML', - parser_args => { package => $self->schema }, - %{ $self->sql_translator_args } + %{ $sqltargs }, }); my $yml = $sqlt->translate; @@ -568,13 +574,12 @@ __END__ =head1 DESCRIPTION -This class is the meat of L. It takes -care of generating serialized sql files representing schemata as well -as serialized sql files to move from one version of a schema to the rest. -One of the hallmark features of this class is that it allows for multiple sql -files for deploy and upgrade, allowing developers to fine tune deployment. -In addition it also allows for perl files to be run -at any stage of the process. +This class is the meat of L. It takes care +of generating serialized schemata as well as sql files to move from one +version of a schema to the rest. One of the hallmark features of this class +is that it allows for multiple sql files for deploy and upgrade, allowing +developers to fine tune deployment. In addition it also allows for perl +files to be run at any stage of the process. For basic usage see L. What's documented here is extra fun stuff or private methods. @@ -590,15 +595,15 @@ like the best way to describe the layout is with the following example: |- SQLite | |- down | | `- 2-1 - | | `- 001-auto.sql-json + | | `- 001-auto.sql | |- schema | | `- 1 - | | `- 001-auto.sql-json + | | `- 001-auto.sql | `- up | |- 1-2 - | | `- 001-auto.sql-json + | | `- 001-auto.sql | `- 2-3 - | `- 001-auto.sql-json + | `- 001-auto.sql |- _common | |- down | | `- 2-1 @@ -609,39 +614,39 @@ like the best way to describe the layout is with the following example: |- _generic | |- down | | `- 2-1 - | | `- 001-auto.sql-json + | | `- 001-auto.sql | |- schema | | `- 1 - | | `- 001-auto.sql-json + | | `- 001-auto.sql | `- up | `- 1-2 - | |- 001-auto.sql-json + | |- 001-auto.sql | `- 002-create-stored-procedures.sql `- MySQL |- down | `- 2-1 - | `- 001-auto.sql-json + | `- 001-auto.sql |- preinstall | `- 1 | |- 001-create_database.pl | `- 002-create_users_and_permissions.pl |- schema | `- 1 - | `- 001-auto.sql-json + | `- 001-auto.sql `- up `- 1-2 - `- 001-auto.sql-json + `- 001-auto.sql So basically, the code $dm->deploy(1) on an C database that would simply run -C<$sql_migration_dir/SQLite/schema/1/001-auto.sql-json>. Next, +C<$sql_migration_dir/SQLite/schema/1/001-auto.sql>. Next, $dm->upgrade_single_step([1,2]) -would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql-json> followed by +would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql> followed by C<$sql_migration_dir/_common/up/1-2/002-generate-customers.pl>. C<.pl> files don't have to be in the C<_common> directory, but most of the time @@ -657,22 +662,6 @@ Until people have used this more it will remain freeform, but a recommended use of preinstall is to have it prompt for username and password, and then call the appropriate C<< CREATE DATABASE >> commands etc. -=head1 SERIALIZED SQL - -The SQL that this module generates and uses is serialized into an array of -SQL statements. The reason being that some databases handle multiple -statements in a single execution differently. Generally you do not need to -worry about this as these are scripts generated for you. If you find that -you are editing them on a regular basis something is wrong and you either need -to submit a bug or consider writing extra serialized SQL or Perl scripts to run -before or after the automatically generated script. - -B Currently the SQL is serialized into JSON. I am willing to merge in -patches that will allow more serialization formats if you want that feature, -but if you do send me a patch for that realize that I do not want to add YAML -support or whatever, I would rather add a generic method of adding any -serialization format. - =head1 PERL SCRIPTS A perl script for this tool is very simple. It merely needs to contain an