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 script_directory => (
55 isa => 'DBIx::Class::DeploymentHandler::Databases',
57 default => sub { [qw( MySQL SQLite PostgreSQL )] },
66 has schema_version => (
72 method _build_schema_version { $self->schema->schema_version }
74 method __ddl_consume_with_prefix($type, $versions, $prefix) {
75 my $base_dir = $self->script_directory;
77 my $main = catfile( $base_dir, $type );
78 my $generic = catfile( $base_dir, '_generic' );
80 catfile( $base_dir, '_common', $prefix, join q(-), @{$versions} );
84 $dir = catfile($main, $prefix, join q(-), @{$versions})
85 } elsif (-d $generic) {
86 $dir = catfile($generic, $prefix, join q(-), @{$versions});
88 croak "neither $main or $generic exist; please write/generate some SQL";
91 opendir my($dh), $dir;
92 my %files = map { $_ => "$dir/$_" } grep { /\.(?:sql|pl)$/ && -f "$dir/$_" } readdir $dh;
96 opendir my($dh), $common;
97 for my $filename (grep { /\.(?:sql|pl)$/ && -f catfile($common,$_) } readdir $dh) {
98 unless ($files{$filename}) {
99 $files{$filename} = catfile($common,$filename);
105 return [@files{sort keys %files}]
108 method _ddl_preinstall_consume_filenames($type, $version) {
109 $self->__ddl_consume_with_prefix($type, [ $version ], 'preinstall')
112 method _ddl_schema_consume_filenames($type, $version) {
113 $self->__ddl_consume_with_prefix($type, [ $version ], 'schema')
116 method _ddl_schema_produce_filename($type, $version) {
117 my $dirname = catfile( $self->script_directory, $type, 'schema', $version );
118 mkpath($dirname) unless -d $dirname;
120 return catfile( $dirname, '001-auto.sql' );
123 method _ddl_schema_up_consume_filenames($type, $versions) {
124 $self->__ddl_consume_with_prefix($type, $versions, 'up')
127 method _ddl_schema_down_consume_filenames($type, $versions) {
128 $self->__ddl_consume_with_prefix($type, $versions, 'down')
131 method _ddl_schema_up_produce_filename($type, $versions) {
132 my $dir = $self->script_directory;
134 my $dirname = catfile( $dir, $type, 'up', join q(-), @{$versions});
135 mkpath($dirname) unless -d $dirname;
137 return catfile( $dirname, '001-auto.sql'
141 method _ddl_schema_down_produce_filename($type, $versions, $dir) {
142 my $dirname = catfile( $dir, $type, 'down', join q(-), @{$versions} );
143 mkpath($dirname) unless -d $dirname;
145 return catfile( $dirname, '001-auto.sql');
148 method _run_sql_and_perl($filenames) {
149 my @files = @{$filenames};
150 my $storage = $self->storage;
153 my $guard = $self->schema->txn_scope_guard if $self->txn_wrap;
156 for my $filename (@files) {
157 if ($filename =~ /\.sql$/) {
158 my @sql = @{$self->_read_sql_file($filename)};
159 $sql .= join "\n", @sql;
161 foreach my $line (@sql) {
162 $storage->_query_start($line);
164 # do a dbh_do cycle here, as we need some error checking in
165 # place (even though we will ignore errors)
166 $storage->dbh_do (sub { $_[1]->do($line) });
169 carp "$_ (running '${line}')"
171 $storage->_query_end($line);
173 } elsif ( $filename =~ /^(.+)\.pl$/ ) {
174 my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
176 no warnings 'redefine';
177 my $fn = eval "$filedata";
181 carp "$filename failed to compile: $@";
182 } elsif (ref $fn eq 'CODE') {
185 carp "$filename should define an anonymouse sub 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 @_ || {})->{version} || $self->schema_version;
201 return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames(
202 $self->storage->sqlt_type,
210 my $version = $args->{version} || $self->schema_version;
211 my $storage_type = $args->{storage_type} || $self->storage->sqlt_type;
213 my @files = @{$self->_ddl_preinstall_consume_filenames(
218 for my $filename (@files) {
219 # We ignore sql for now (till I figure out what to do with it)
220 if ( $filename =~ /^(.+)\.pl$/ ) {
221 my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
223 no warnings 'redefine';
224 my $fn = eval "$filedata";
228 carp "$filename failed to compile: $@";
229 } elsif (ref $fn eq 'CODE') {
232 carp "$filename should define an anonymous 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->script_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->script_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, $args) = @_;
295 my $source = $args->{result_source};
296 my $version = $args->{version};
297 my $rs_install_file =
298 $self->_resultsource_install_filename($source->source_name);
301 $self->$rs_install_file(
302 $self->storage->sqlt_type,
306 $self->_run_sql_and_perl($files);
309 sub prepare_resultsource_install {
311 my $source = (shift @_)->{result_source};
313 my $filename = $self->_resultsource_install_filename($source->source_name);
314 $self->_prepare_install({
315 parser_args => { sources => [$source->source_name], }
321 $self->_prepare_install({}, '_ddl_schema_produce_filename');
324 sub prepare_upgrade {
325 my ($self, $args) = @_;
326 $self->_prepare_changegrade(
327 $args->{from_version}, $args->{to_version}, $args->{version_set}, 'up'
331 sub prepare_downgrade {
332 my ($self, $args) = @_;
333 $self->_prepare_changegrade(
334 $args->{from_version}, $args->{to_version}, $args->{version_set}, 'down'
338 method _prepare_changegrade($from_version, $to_version, $version_set, $direction) {
339 my $schema = $self->schema;
340 my $databases = $self->databases;
341 my $dir = $self->script_directory;
342 my $sqltargs = $self->sql_translator_args;
344 my $schema_version = $self->schema_version;
348 ignore_constraint_names => 1,
349 ignore_index_names => 1,
353 my $sqlt = SQL::Translator->new( $sqltargs );
355 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
356 my $sqlt_schema = $sqlt->translate( data => $schema )
357 or croak($sqlt->error);
359 foreach my $db (@$databases) {
361 $sqlt->{schema} = $sqlt_schema;
362 $sqlt->producer($db);
364 my $prefilename = $self->_ddl_schema_produce_filename($db, $from_version, $dir);
365 unless(-e $prefilename) {
366 carp("No previous schema file found ($prefilename)");
369 my $diff_file_method = "_ddl_schema_${direction}_produce_filename";
370 my $diff_file = $self->$diff_file_method($db, $version_set, $dir );
372 carp("Overwriting existing $direction-diff file - $diff_file");
378 my $t = SQL::Translator->new({
384 $t->parser( $db ) # could this really throw an exception?
387 my $out = $t->translate( $prefilename )
390 $source_schema = $t->schema;
392 $source_schema->name( $prefilename )
393 unless $source_schema->name;
396 # The "new" style of producers have sane normalization and can support
397 # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't
398 # And we have to diff parsed SQL against parsed SQL.
399 my $dest_schema = $sqlt_schema;
401 unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) {
402 my $t = SQL::Translator->new({
408 $t->parser( $db ) # could this really throw an exception?
411 my $filename = $self->_ddl_schema_produce_filename($db, $to_version, $dir);
412 my $out = $t->translate( $filename )
415 $dest_schema = $t->schema;
417 $dest_schema->name( $filename )
418 unless $dest_schema->name;
421 my $diff = SQL::Translator::Diff::schema_diff(
426 open my $file, q(>), $diff_file;
432 method _read_sql_file($file) {
435 open my $fh, '<', $file;
436 my @data = split /;\n/, join '', <$fh>;
440 $_ && # remove blank lines
441 !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's
443 s/^\s+//; s/\s+$//; # trim whitespace
444 join '', grep { !/^--/ } split /\n/ # remove comments
450 sub downgrade_single_step {
452 my $version_set = (shift @_)->{version_set};
454 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_down_consume_filenames(
455 $self->storage->sqlt_type,
462 sub upgrade_single_step {
464 my $version_set = (shift @_)->{version_set};
466 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_up_consume_filenames(
467 $self->storage->sqlt_type,
473 __PACKAGE__->meta->make_immutable;
477 # vim: ts=2 sw=2 expandtab
483 This class is the meat of L<DBIx::Class::DeploymentHandler>. It takes care of
484 generating sql files representing schemata as well as sql files to move from
485 one version of a schema to the rest. One of the hallmark features of this
486 class is that it allows for multiple sql files for deploy and upgrade, allowing
487 developers to fine tune deployment. In addition it also allows for perl files
488 to be run at any stage of the process.
490 For basic usage see L<DBIx::Class::DeploymentHandler::HandlesDeploy>. What's
491 documented here is extra fun stuff or private methods.
493 =head1 DIRECTORY LAYOUT
495 Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>. It's
496 heavily based upon L<DBIx::Migration::Directories>, but has some extensions and
497 modifications, so even if you are familiar with it, please read this. I feel
498 like the best way to describe the layout is with the following example:
516 | | `- 002-remove-customers.pl
519 | `- 002-generate-customers.pl
530 | `- 002-create-stored-procedures.sql
537 | |- 001-create_database.pl
538 | `- 002-create_users_and_permissions.pl
546 So basically, the code
550 on an C<SQLite> database that would simply run
551 C<$sql_migration_dir/SQLite/schema/1/001-auto.sql>. Next,
553 $dm->upgrade_single_step([1,2])
555 would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql> followed by
556 C<$sql_migration_dir/_common/up/1-2/002-generate-customers.pl>.
558 Now, a C<.pl> file doesn't have to be in the C<_common> directory, but most of
559 the time it probably should be, since perl scripts will mostly be database
562 C<_generic> exists for when you for some reason are sure that your SQL is
563 generic enough to run on all databases. Good luck with that one.
565 Note that unlike most steps in the process, C<preinstall> will not run SQL, as
566 there may not even be an database at preinstall time. It will run perl scripts
567 just like the other steps in the process, but nothing is passed to them.
568 Until people have used this more it will remain freeform, but a recommended use
569 of preinstall is to have it prompt for username and password, and then call the
570 appropriate C<< CREATE DATABASE >> commands etc.
574 A perl script for this tool is very simple. It merely needs to contain an
575 anonymous sub that takes a L<DBIx::Class::Schema> as it's only argument.
576 A very basic perl script might look like:
586 $schema->resultset('Users')->create({
594 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
595 and generate the DDL.
599 The L<DBIx::Class::Storage> that is I<actually> used to talk to the database
600 and generate the DDL. This is automatically created with L</_build_storage>.
602 =attr sql_translator_args
604 The arguments that get passed to L<SQL::Translator> when it's used.
606 =attr script_directory
608 The directory (default C<'sql'>) that scripts are stored in
612 The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to
617 Set to true (which is the default) to wrap all upgrades and deploys in a single
622 The version the schema on your harddrive is at. Defaults to
623 C<< $self->schema->schema_version >>.
627 =head2 __ddl_consume_with_prefix
629 $dm->__ddl_consume_with_prefix( 'SQLite', [qw( 1.00 1.01 )], 'up' )
631 This is the meat of the multi-file upgrade/deploy stuff. It returns a list of
632 files in the order that they should be run for a generic "type" of upgrade.
633 You should not be calling this in user code.
635 =head2 _ddl_schema_consume_filenames
637 $dm->__ddl_schema_consume_filenames( 'SQLite', [qw( 1.00 )] )
639 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
642 =head2 _ddl_schema_produce_filename
644 $dm->__ddl_schema_produce_filename( 'SQLite', [qw( 1.00 )] )
646 Returns a single file in which an initial schema will be stored.
648 =head2 _ddl_schema_up_consume_filenames
650 $dm->_ddl_schema_up_consume_filenames( 'SQLite', [qw( 1.00 )] )
652 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
655 =head2 _ddl_schema_down_consume_filenames
657 $dm->_ddl_schema_down_consume_filenames( 'SQLite', [qw( 1.00 )] )
659 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for a
662 =head2 _ddl_schema_up_produce_filenames
664 $dm->_ddl_schema_up_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
666 Returns a single file in which the sql to upgrade from one schema to another
669 =head2 _ddl_schema_down_produce_filename
671 $dm->_ddl_schema_down_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
673 Returns a single file in which the sql to downgrade from one schema to another
676 =head2 _resultsource_install_filename
678 my $filename_fn = $dm->_resultsource_install_filename('User');
679 $dm->$filename_fn('SQLite', '1.00')
681 Returns a function which in turn returns a single filename used to install a
682 single resultsource. Weird interface is convenient for me. Deal with it.
684 =head2 _run_sql_and_perl
686 $dm->_run_sql_and_perl([qw( list of filenames )])
688 Simply put, this runs the list of files passed to it. If the file ends in
689 C<.sql> it runs it as sql and if it ends in C<.pl> it runs it as a perl file.
691 Depending on L</txn_wrap> all of the files run will be wrapped in a single
694 =head2 _prepare_install
696 $dm->_prepare_install({ add_drop_table => 0 }, sub { 'file_to_create' })
698 Generates the sql file for installing the database. First arg is simply
699 L<SQL::Translator> args and the second is a coderef that returns the filename
702 =head2 _prepare_changegrade
704 $dm->_prepare_changegrade('1.00', '1.01', [qw( 1.00 1.01)], 'up')
706 Generates the sql file for migrating from one schema version to another. First
707 arg is the version to start from, second is the version to go to, third is the
708 L<version set|DBIx::Class::DeploymentHandler/VERSION SET>, and last is the
709 direction of the changegrade, be it 'up' or 'down'.
711 =head2 _read_sql_file
713 $dm->_read_sql_file('foo.sql')
715 Reads a sql file and returns lines in an C<ArrayRef>. Strips out comments,
716 transactions, and blank lines.