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$/ ) {
173 my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
175 no warnings 'redefine';
176 my $fn = eval "$filedata";
180 carp "$filename failed to compile: $@";
181 } elsif (ref $fn eq 'CODE') {
184 carp "$filename should define an anonymouse sub that takes a schema but it didn't!";
187 croak "A file ($filename) got to deploy that wasn't sql or perl!";
191 $guard->commit if $self->txn_wrap;
198 my $version = (shift @_ || {})->{version} || $self->schema_version;
200 return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames(
201 $self->storage->sqlt_type,
209 my $version = $args->{version} || $self->schema_version;
210 my $storage_type = $args->{storage_type} || $self->storage->sqlt_type;
212 my @files = @{$self->_ddl_preinstall_consume_filenames(
217 for my $filename (@files) {
218 # We ignore sql for now (till I figure out what to do with it)
219 if ( $filename =~ /^(.+)\.pl$/ ) {
220 my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
222 no warnings 'redefine';
223 my $fn = eval "$filedata";
227 carp "$filename failed to compile: $@";
228 } elsif (ref $fn eq 'CODE') {
231 carp "$filename should define an anonymous sub but it didn't!";
234 croak "A file ($filename) got to preinstall_scripts that wasn't sql or perl!";
239 sub _prepare_install {
241 my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} };
243 my $schema = $self->schema;
244 my $databases = $self->databases;
245 my $dir = $self->upgrade_directory;
246 my $version = $self->schema_version;
248 my $sqlt = SQL::Translator->new({
250 ignore_constraint_names => 1,
251 ignore_index_names => 1,
252 parser => 'SQL::Translator::Parser::DBIx::Class',
256 my $sqlt_schema = $sqlt->translate( data => $schema )
257 or croak($sqlt->error);
259 foreach my $db (@$databases) {
261 $sqlt->{schema} = $sqlt_schema;
262 $sqlt->producer($db);
264 my $filename = $self->$to_file($db, $version, $dir);
266 carp "Overwriting existing DDL file - $filename";
270 my $output = $sqlt->translate;
272 carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")");
275 open my $file, q(>), $filename;
276 print {$file} $output;
281 sub _resultsource_install_filename {
282 my ($self, $source_name) = @_;
284 my ($self, $type, $version) = @_;
285 my $dirname = catfile( $self->upgrade_directory, $type, 'schema', $version );
286 mkpath($dirname) unless -d $dirname;
288 return catfile( $dirname, "001-auto-$source_name.sql" );
292 sub install_resultsource {
293 my ($self, $args) = @_;
294 my $source = $args->{result_source};
295 my $version = $args->{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 {
310 my $source = (shift @_)->{result_source};
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, $args) = @_;
325 $self->_prepare_changegrade(
326 $args->{from_version}, $args->{to_version}, $args->{version_set}, 'up'
330 sub prepare_downgrade {
331 my ($self, $args) = @_;
332 $self->_prepare_changegrade(
333 $args->{from_version}, $args->{to_version}, $args->{version_set}, 'down'
337 method _prepare_changegrade($from_version, $to_version, $version_set, $direction) {
338 my $schema = $self->schema;
339 my $databases = $self->databases;
340 my $dir = $self->upgrade_directory;
341 my $sqltargs = $self->sql_translator_args;
343 my $schema_version = $self->schema_version;
347 ignore_constraint_names => 1,
348 ignore_index_names => 1,
352 my $sqlt = SQL::Translator->new( $sqltargs );
354 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
355 my $sqlt_schema = $sqlt->translate( data => $schema )
356 or croak($sqlt->error);
358 foreach my $db (@$databases) {
360 $sqlt->{schema} = $sqlt_schema;
361 $sqlt->producer($db);
363 my $prefilename = $self->_ddl_schema_produce_filename($db, $from_version, $dir);
364 unless(-e $prefilename) {
365 carp("No previous schema file found ($prefilename)");
368 my $diff_file_method = "_ddl_schema_${direction}_produce_filename";
369 my $diff_file = $self->$diff_file_method($db, $version_set, $dir );
371 carp("Overwriting existing $direction-diff file - $diff_file");
377 my $t = SQL::Translator->new({
383 $t->parser( $db ) # could this really throw an exception?
386 my $out = $t->translate( $prefilename )
389 $source_schema = $t->schema;
391 $source_schema->name( $prefilename )
392 unless $source_schema->name;
395 # The "new" style of producers have sane normalization and can support
396 # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't
397 # And we have to diff parsed SQL against parsed SQL.
398 my $dest_schema = $sqlt_schema;
400 unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) {
401 my $t = SQL::Translator->new({
407 $t->parser( $db ) # could this really throw an exception?
410 my $filename = $self->_ddl_schema_produce_filename($db, $to_version, $dir);
411 my $out = $t->translate( $filename )
414 $dest_schema = $t->schema;
416 $dest_schema->name( $filename )
417 unless $dest_schema->name;
420 my $diff = SQL::Translator::Diff::schema_diff(
425 open my $file, q(>), $diff_file;
431 method _read_sql_file($file) {
434 open my $fh, '<', $file;
435 my @data = split /;\n/, join '', <$fh>;
439 $_ && # remove blank lines
440 !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's
442 s/^\s+//; s/\s+$//; # trim whitespace
443 join '', grep { !/^--/ } split /\n/ # remove comments
449 sub downgrade_single_step {
451 my $version_set = (shift @_)->{version_set};
453 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_down_consume_filenames(
454 $self->storage->sqlt_type,
461 sub upgrade_single_step {
463 my $version_set = (shift @_)->{version_set};
465 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_up_consume_filenames(
466 $self->storage->sqlt_type,
472 __PACKAGE__->meta->make_immutable;
476 # vim: ts=2 sw=2 expandtab
482 This class is the meat of L<DBIx::Class::DeploymentHandler>. It takes care of
483 generating sql files representing schemata as well as sql files to move from
484 one version of a schema to the rest. One of the hallmark features of this
485 class is that it allows for multiple sql files for deploy and upgrade, allowing
486 developers to fine tune deployment. In addition it also allows for perl files
487 to be run at any stage of the process.
489 For basic usage see L<DBIx::Class::DeploymentHandler::HandlesDeploy>. What's
490 documented here is extra fun stuff or private methods.
492 =head1 DIRECTORY LAYOUT
494 Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>. It's
495 heavily based upon L<DBIx::Migration::Directories>, but has some extensions and
496 modifications, so even if you are familiar with it, please read this. I feel
497 like the best way to describe the layout is with the following example:
515 | | `- 002-remove-customers.pl
518 | `- 002-generate-customers.pl
529 | `- 002-create-stored-procedures.sql
536 | |- 001-create_database.pl
537 | `- 002-create_users_and_permissions.pl
545 So basically, the code
549 on an C<SQLite> database that would simply run
550 C<$sql_migration_dir/SQLite/schema/1/001-auto.sql>. Next,
552 $dm->upgrade_single_step([1,2])
554 would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql> followed by
555 C<$sql_migration_dir/_common/up/1-2/002-generate-customers.pl>.
557 Now, a C<.pl> file doesn't have to be in the C<_common> directory, but most of
558 the time it probably should be, since perl scripts will mostly be database
561 C<_generic> exists for when you for some reason are sure that your SQL is
562 generic enough to run on all databases. Good luck with that one.
564 Note that unlike most steps in the process, C<preinstall> will not run SQL, as
565 there may not even be an database at preinstall time. It will run perl scripts
566 just like the other steps in the process, but nothing is passed to them.
567 Until people have used this more it will remain freeform, but a recommended use
568 of preinstall is to have it prompt for username and password, and then call the
569 appropriate C<< CREATE DATABASE >> commands etc.
573 A perl script for this tool is very simple. It merely needs to contain an
574 anonymous sub that takes a L<DBIx::Class::Schema> as it's only argument.
575 A very basic perl script might look like:
585 $schema->resultset('Users')->create({
593 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
594 and generate the DDL.
598 The L<DBIx::Class::Storage> that is I<actually> used to talk to the database
599 and generate the DDL. This is automatically created with L</_build_storage>.
601 =attr sql_translator_args
603 The arguments that get passed to L<SQL::Translator> when it's used.
605 =attr upgrade_directory
607 The directory (default C<'sql'>) that upgrades are stored in
611 The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to
616 Set to true (which is the default) to wrap all upgrades and deploys in a single
621 The version the schema on your harddrive is at. Defaults to
622 C<< $self->schema->schema_version >>.
624 =method __ddl_consume_with_prefix
626 $dm->__ddl_consume_with_prefix( 'SQLite', [qw( 1.00 1.01 )], 'up' )
628 This is the meat of the multi-file upgrade/deploy stuff. It returns a list of
629 files in the order that they should be run for a generic "type" of upgrade.
630 You should not be calling this in user code.
632 =method _ddl_schema_consume_filenames
634 $dm->__ddl_schema_consume_filenames( 'SQLite', [qw( 1.00 )] )
636 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
639 =method _ddl_schema_produce_filename
641 $dm->__ddl_schema_produce_filename( 'SQLite', [qw( 1.00 )] )
643 Returns a single file in which an initial schema will be stored.
645 =method _ddl_schema_up_consume_filenames
647 $dm->_ddl_schema_up_consume_filenames( 'SQLite', [qw( 1.00 )] )
649 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
652 =method _ddl_schema_down_consume_filenames
654 $dm->_ddl_schema_down_consume_filenames( 'SQLite', [qw( 1.00 )] )
656 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for a
659 =method _ddl_schema_up_produce_filenames
661 $dm->_ddl_schema_up_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
663 Returns a single file in which the sql to upgrade from one schema to another
666 =method _ddl_schema_down_produce_filename
668 $dm->_ddl_schema_down_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
670 Returns a single file in which the sql to downgrade from one schema to another
673 =method _resultsource_install_filename
675 my $filename_fn = $dm->_resultsource_install_filename('User');
676 $dm->$filename_fn('SQLite', '1.00')
678 Returns a function which in turn returns a single filename used to install a
679 single resultsource. Weird interface is convenient for me. Deal with it.
681 =method _run_sql_and_perl
683 $dm->_run_sql_and_perl([qw( list of filenames )])
685 Simply put, this runs the list of files passed to it. If the file ends in
686 C<.sql> it runs it as sql and if it ends in C<.pl> it runs it as a perl file.
688 Depending on L</txn_wrap> all of the files run will be wrapped in a single
691 =method _prepare_install
693 $dm->_prepare_install({ add_drop_table => 0 }, sub { 'file_to_create' })
695 Generates the sql file for installing the database. First arg is simply
696 L<SQL::Translator> args and the second is a coderef that returns the filename
699 =method _prepare_changegrade
701 $dm->_prepare_changegrade('1.00', '1.01', [qw( 1.00 1.01)], 'up')
703 Generates the sql file for migrating from one schema version to another. First
704 arg is the version to start from, second is the version to go to, third is the
705 L<version set|DBIx::Class::DeploymentHandler/VERSION SET>, and last is the
706 direction of the changegrade, be it 'up' or 'down'.
708 =method _read_sql_file
710 $dm->_read_sql_file('foo.sql')
712 Reads a sql file and returns lines in an C<ArrayRef>. Strips out comments,
713 transactions, and blank lines.