X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FDeploymentHandler%2FDeployMethod%2FSQL%2FTranslator.pm;h=301bdb4b471f0b803b54876007f48332146f4884;hb=93460690e9d4a399f18345d2654d5f4c35abdb46;hp=c2bbc473a7ce10e671b559f3ce1998315f12c206;hpb=7e08eddd832a00dbe52d8f1ba90ea486c48f7ab1;p=dbsrgits%2FDBIx-Class-DeploymentHandler.git diff --git a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm index c2bbc47..301bdb4 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,15 +234,33 @@ method _run_sql_and_perl($filenames) { return $sql; } +method _deploy($version) { + if (!$self->ignore_ddl) { + return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames( + $self->storage->sqlt_type, + $version, + )); + } else { + my $sqlt = SQL::Translator->new({ + add_drop_table => 1, + parser => 'SQL::Translator::Parser::YAML', + producer => $self->storage->sqlt_type; + %{$sqltargs}, + }); + + my $yaml_filename = $self->$from_file($version); + + my @sql = $sqlt->translate($yaml_filename); + croak("Failed to translate to $db, skipping. (" . $sqlt->error . ")") + unless $sql; + } +} + sub deploy { my $self = shift; my $version = (shift @_ || {})->{version} || $self->schema_version; log_info { "deploying version $version" }; - - return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames( - $self->storage->sqlt_type, - $version, - )); + $self->_deploy($version); } sub preinstall { @@ -293,19 +300,22 @@ sub preinstall { sub _prepare_install { my $self = shift; my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; + my $from_file = shift; my $to_file = shift; my $schema = $self->schema; my $databases = $self->databases; my $dir = $self->script_directory; my $version = $self->schema_version; + return if $self->ignore_ddl; + my $sqlt = SQL::Translator->new({ add_drop_table => 1, parser => 'SQL::Translator::Parser::YAML', %{$sqltargs} }); - my $yaml_filename = $self->_ddl_protoschema_produce_filename($version); + my $yaml_filename = $self->$from_file($version); foreach my $db (@$databases) { $sqlt->reset; @@ -339,6 +349,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 +382,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 { @@ -400,6 +423,8 @@ method _prepare_changegrade($from_version, $to_version, $version_set, $direction my $dir = $self->script_directory; my $sqltargs = $self->sql_translator_args; + return if $self->ignore_ddl; + my $schema_version = $self->schema_version; $sqltargs = { @@ -409,72 +434,59 @@ method _prepare_changegrade($from_version, $to_version, $version_set, $direction %{$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); + my $diff_file_method = "_ddl_schema_${direction}_produce_filename"; + my $source_schema; + { + my $prefilename = $self->_ddl_protoschema_produce_filename($from_version, $dir); - 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; - } + # should probably be a croak + carp("No previous schema file found ($prefilename)") + unless -e $prefilename; - my $source_schema; - { - my $t = SQL::Translator->new({ - %{$sqltargs}, - debug => 0, - trace => 0, - }); + my $t = SQL::Translator->new({ + %{$sqltargs}, + debug => 0, + trace => 0, + parser => 'SQL::Translator::Parser::YAML', + }); - $t->parser( $db ) # could this really throw an exception? - or croak($t->error); + my $out = $t->translate( $prefilename ) + or croak($t->error); - my $out = $t->translate( $prefilename ) - or croak($t->error); + $source_schema = $t->schema; - $source_schema = $t->schema; - - $source_schema->name( $prefilename ) - unless $source_schema->name; - } + $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; + my $dest_schema; + { + my $filename = $self->_ddl_protoschema_produce_filename($to_version, $dir); - unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) { - my $t = SQL::Translator->new({ - %{$sqltargs}, - debug => 0, - trace => 0, - }); + # should probably be a croak + carp("No next schema file found ($filename)") + unless -e $filename; - $t->parser( $db ) # could this really throw an exception? - or croak($t->error); + my $t = SQL::Translator->new({ + %{$sqltargs}, + debug => 0, + trace => 0, + parser => 'SQL::Translator::Parser::YAML', + }); - my $filename = $self->_ddl_schema_produce_filename($db, $to_version, $dir); - my $out = $t->translate( $filename ) - or croak($t->error); + my $out = $t->translate( $filename ) + or croak($t->error); - $dest_schema = $t->schema; + $dest_schema = $t->schema; - $dest_schema->name( $filename ) - unless $dest_schema->name; + $dest_schema->name( $filename ) + unless $dest_schema->name; + } + foreach my $db (@$databases) { + 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 $diff = SQL::Translator::Diff::schema_diff( @@ -531,16 +543,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 +584,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 +605,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 +624,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 +672,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