1 package DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator;
5 use Carp qw( carp croak );
7 use Method::Signatures::Simple;
11 require SQL::Translator::Diff;
13 require DBIx::Class::Storage; # loaded for type constraint
14 use DBIx::Class::DeploymentHandler::Types;
16 use File::Path 'mkpath';
17 use File::Spec::Functions;
19 with 'DBIx::Class::DeploymentHandler::HandlesDeploy';
22 isa => 'DBIx::Class::Schema',
25 handles => [qw( schema_version )],
29 isa => 'DBIx::Class::Storage',
34 method _build_storage {
35 my $s = $self->schema->storage;
36 $s->_determine_driver;
43 default => sub { {} },
45 has upgrade_directory => (
54 isa => 'DBIx::Class::DeploymentHandler::Databases',
56 default => sub { [qw( MySQL SQLite PostgreSQL )] },
65 method __ddl_consume_with_prefix($type, $versions, $prefix) {
66 my $base_dir = $self->upgrade_directory;
68 my $main = catfile( $base_dir, $type );
69 my $generic = catfile( $base_dir, '_generic' );
71 catfile( $base_dir, '_common', $prefix, join q(-), @{$versions} );
75 $dir = catfile($main, $prefix, join q(-), @{$versions})
76 } elsif (-d $generic) {
77 $dir = catfile($generic, $prefix, join q(-), @{$versions});
79 croak "neither $main or $generic exist; please write/generate some SQL";
82 opendir my($dh), $dir;
83 my %files = map { $_ => "$dir/$_" } grep { /\.(?:sql|pl)$/ && -f "$dir/$_" } readdir $dh;
87 opendir my($dh), $common;
88 for my $filename (grep { /\.(?:sql|pl)$/ && -f catfile($common,$_) } readdir $dh) {
89 unless ($files{$filename}) {
90 $files{$filename} = catfile($common,$filename);
96 return [@files{sort keys %files}]
99 method _ddl_schema_consume_filenames($type, $version) {
100 $self->__ddl_consume_with_prefix($type, [ $version ], 'schema')
103 method _ddl_schema_produce_filename($type, $version) {
104 my $dirname = catfile( $self->upgrade_directory, $type, 'schema', $version );
105 mkpath($dirname) unless -d $dirname;
107 return catfile( $dirname, '001-auto.sql' );
110 method _ddl_schema_up_consume_filenames($type, $versions) {
111 $self->__ddl_consume_with_prefix($type, $versions, 'up')
114 method _ddl_schema_down_consume_filenames($type, $versions) {
115 $self->__ddl_consume_with_prefix($type, $versions, 'down')
118 method _ddl_schema_up_produce_filename($type, $versions) {
119 my $dir = $self->upgrade_directory;
121 my $dirname = catfile( $dir, $type, 'up', join q(-), @{$versions});
122 mkpath($dirname) unless -d $dirname;
124 return catfile( $dirname, '001-auto.sql'
128 method _ddl_schema_down_produce_filename($type, $versions, $dir) {
129 my $dirname = catfile( $dir, $type, 'down', join q(-), @{$versions} );
130 mkpath($dirname) unless -d $dirname;
132 return catfile( $dirname, '001-auto.sql');
135 method _run_sql_and_perl($filenames) {
136 my @files = @{$filenames};
137 my $storage = $self->storage;
140 my $guard = $self->schema->txn_scope_guard if $self->txn_wrap;
143 for my $filename (@files) {
144 if ($filename =~ /\.sql$/) {
145 my @sql = @{$self->_read_sql_file($filename)};
146 $sql .= join "\n", @sql;
148 foreach my $line (@sql) {
149 $storage->_query_start($line);
151 # do a dbh_do cycle here, as we need some error checking in
152 # place (even though we will ignore errors)
153 $storage->dbh_do (sub { $_[1]->do($line) });
156 carp "$_ (running '${line}')"
158 $storage->_query_end($line);
160 } elsif ( $filename =~ /^(.+)\.pl$/ ) {
162 my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
163 # make the package name more palateable to perl
164 $package =~ s/\W/_/g;
166 no warnings 'redefine';
167 eval "package $package;\n\n$filedata";
170 if (my $fn = $package->can('run')) {
171 $fn->($self->schema);
173 carp "$filename should define a run method that takes a schema but it didn't!";
176 croak "A file got to deploy that wasn't sql or perl!";
180 $guard->commit if $self->txn_wrap;
187 my $version = shift || $self->schema_version;
189 return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames(
190 $self->storage->sqlt_type,
195 sub _prepare_install {
197 my $sqltargs = { %{$self->sqltargs}, %{shift @_} };
199 my $schema = $self->schema;
200 my $databases = $self->databases;
201 my $dir = $self->upgrade_directory;
202 my $version = $schema->schema_version;
204 my $sqlt = SQL::Translator->new({
206 ignore_constraint_names => 1,
207 ignore_index_names => 1,
208 parser => 'SQL::Translator::Parser::DBIx::Class',
212 my $sqlt_schema = $sqlt->translate( data => $schema )
213 or croak($sqlt->error);
215 foreach my $db (@$databases) {
217 $sqlt->{schema} = $sqlt_schema;
218 $sqlt->producer($db);
220 my $filename = $self->$to_file($db, $version, $dir);
222 carp "Overwriting existing DDL file - $filename";
226 my $output = $sqlt->translate;
228 carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")");
231 open my $file, q(>), $filename;
232 print {$file} $output;
237 sub _resultsource_install_filename {
238 my ($self, $source_name) = @_;
240 my ($self, $type, $version) = @_;
241 my $dirname = catfile( $self->upgrade_directory, $type, 'schema', $version );
242 mkpath($dirname) unless -d $dirname;
244 return catfile( $dirname, "001-auto-$source_name.sql" );
248 sub install_resultsource {
249 my ($self, $source, $version) = @_;
251 my $rs_install_file =
252 $self->_resultsource_install_filename($source->source_name);
255 $self->$rs_install_file(
256 $self->storage->sqlt_type,
260 $self->_run_sql_and_perl($files);
263 sub prepare_resultsource_install {
267 my $filename = $self->_resultsource_install_filename($source->source_name);
268 $self->_prepare_install({
269 parser_args => { sources => [$source->source_name], }
273 sub prepare_install {
275 $self->_prepare_install({}, '_ddl_schema_produce_filename');
278 sub prepare_upgrade {
279 my ($self, $from_version, $to_version, $version_set) = @_;
280 $self->_prepare_changegrade($from_version, $to_version, $version_set, 'up');
283 sub prepare_downgrade {
284 my ($self, $from_version, $to_version, $version_set) = @_;
286 $self->_prepare_changegrade($from_version, $to_version, $version_set, 'down');
289 method _prepare_changegrade($from_version, $to_version, $version_set, $direction) {
290 my $schema = $self->schema;
291 my $databases = $self->databases;
292 my $dir = $self->upgrade_directory;
293 my $sqltargs = $self->sqltargs;
295 my $schema_version = $schema->schema_version;
299 ignore_constraint_names => 1,
300 ignore_index_names => 1,
304 my $sqlt = SQL::Translator->new( $sqltargs );
306 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
307 my $sqlt_schema = $sqlt->translate( data => $schema )
308 or croak($sqlt->error);
310 foreach my $db (@$databases) {
312 $sqlt->{schema} = $sqlt_schema;
313 $sqlt->producer($db);
315 my $prefilename = $self->_ddl_schema_produce_filename($db, $from_version, $dir);
316 unless(-e $prefilename) {
317 carp("No previous schema file found ($prefilename)");
320 my $diff_file_method = "_ddl_schema_${direction}_produce_filename";
321 my $diff_file = $self->$diff_file_method($db, $version_set, $dir );
323 carp("Overwriting existing $direction-diff file - $diff_file");
329 my $t = SQL::Translator->new({
335 $t->parser( $db ) # could this really throw an exception?
338 my $out = $t->translate( $prefilename )
341 $source_schema = $t->schema;
343 $source_schema->name( $prefilename )
344 unless $source_schema->name;
347 # The "new" style of producers have sane normalization and can support
348 # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't
349 # And we have to diff parsed SQL against parsed SQL.
350 my $dest_schema = $sqlt_schema;
352 unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) {
353 my $t = SQL::Translator->new({
359 $t->parser( $db ) # could this really throw an exception?
362 my $filename = $self->_ddl_schema_produce_filename($db, $to_version, $dir);
363 my $out = $t->translate( $filename )
366 $dest_schema = $t->schema;
368 $dest_schema->name( $filename )
369 unless $dest_schema->name;
372 my $diff = SQL::Translator::Diff::schema_diff(
377 open my $file, q(>), $diff_file;
383 method _read_sql_file($file) {
386 open my $fh, '<', $file;
387 my @data = split /;\n/, join '', <$fh>;
391 $_ && # remove blank lines
392 !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's
394 s/^\s+//; s/\s+$//; # trim whitespace
395 join '', grep { !/^--/ } split /\n/ # remove comments
401 sub downgrade_single_step {
403 my $version_set = shift @_;
405 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_down_consume_filenames(
406 $self->storage->sqlt_type,
413 sub upgrade_single_step {
415 my $version_set = shift @_;
417 my $sql = $self->_run_sql_and_perl($self->_ddl_schema_up_consume_filenames(
418 $self->storage->sqlt_type,
424 __PACKAGE__->meta->make_immutable;
428 # vim: ts=2 sw=2 expandtab
434 This class is the meat of L<DBIx::Class::DeploymentHandler>. It takes care of
435 generating sql files representing schemata as well as sql files to move from
436 one version of a schema to the rest. One of the hallmark features of this
437 class is that it allows for multiple sql files for deploy and upgrade, allowing
438 developers to fine tune deployment. In addition it also allows for perl files
439 to be run at any stage of the process.
441 For basic usage see L<DBIx::Class::DeploymentHandler::HandlesDeploy>. What's
442 documented here is extra fun stuff or private methods.
444 =head1 DIRECTORY LAYOUT
446 Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>. It's
447 heavily based upon L<DBIx::Migration::Directories>, but has some extensions and
448 modifications, so even if you are familiar with it, please read this. I feel
449 like the best way to describe the layout is with the following example:
467 | | `- 002-remove-customers.pl
470 | `- 002-generate-customers.pl
481 | `- 002-create-stored-procedures.sql
493 So basically, the code
497 on an C<SQLite> database that would simply run
498 C<$sql_migration_dir/SQLite/schema/1/001-auto.sql>. Next,
500 $dm->upgrade_single_step([1,2])
502 would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql> followed by
503 C<$sql_migration_dir/_common/up/1-2/002-generate-customers.pl>.
505 Now, a C<.pl> file doesn't have to be in the C<_common> directory, but most of
506 the time it probably should be, since perl scripts will mostly be database
509 C<_generic> exists for when you for some reason are sure that your SQL is
510 generic enough to run on all databases. Good luck with that one.
514 A perl script for this tool is very simple. It merely needs to contain a
515 sub called C<run> that takes a L<DBIx::Class::Schema> as it's only argument.
516 A very basic perl script might look like:
526 $schema->resultset('Users')->create({
534 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
535 and generate the DDL.
539 The L<DBIx::Class::Storage> that is I<actually> used to talk to the database
540 and generate the DDL. This is automatically created with L</_build_storage>.
546 =attr upgrade_directory
548 The directory (default C<'sql'>) that upgrades are stored in
552 The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to
557 Set to true (which is the default) to wrap all upgrades and deploys in a single
560 =method __ddl_consume_with_prefix
562 $dm->__ddl_consume_with_prefix( 'SQLite', [qw( 1.00 1.01 )], 'up' )
564 This is the meat of the multi-file upgrade/deploy stuff. It returns a list of
565 files in the order that they should be run for a generic "type" of upgrade.
566 You should not be calling this in user code.
568 =method _ddl_schema_consume_filenames
570 $dm->__ddl_schema_consume_filenames( 'SQLite', [qw( 1.00 )] )
572 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
575 =method _ddl_schema_produce_filename
577 $dm->__ddl_schema_produce_filename( 'SQLite', [qw( 1.00 )] )
579 Returns a single file in which an initial schema will be stored.
581 =method _ddl_schema_up_consume_filenames
583 $dm->_ddl_schema_up_consume_filenames( 'SQLite', [qw( 1.00 )] )
585 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an
588 =method _ddl_schema_down_consume_filenames
590 $dm->_ddl_schema_down_consume_filenames( 'SQLite', [qw( 1.00 )] )
592 Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for a
595 =method _ddl_schema_up_produce_filenames
597 $dm->_ddl_schema_up_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
599 Returns a single file in which the sql to upgrade from one schema to another
602 =method _ddl_schema_down_produce_filename
604 $dm->_ddl_schema_down_produce_filename( 'SQLite', [qw( 1.00 1.01 )] )
606 Returns a single file in which the sql to downgrade from one schema to another
609 =method _resultsource_install_filename
611 my $filename_fn = $dm->_resultsource_install_filename('User');
612 $dm->$filename_fn('SQLite', '1.00')
614 Returns a function which in turn returns a single filename used to install a
615 single resultsource. Weird interface is convenient for me. Deal with it.
617 =method _run_sql_and_perl
619 $dm->_run_sql_and_perl([qw( list of filenames )])
621 Simply put, this runs the list of files passed to it. If the file ends in
622 C<.sql> it runs it as sql and if it ends in C<.pl> it runs it as a perl file.
624 Depending on L</txn_wrap> all of the files run will be wrapped in a single
627 =method _prepare_install
629 $dm->_prepare_install({ add_drop_table => 0 }, sub { 'file_to_create' })
631 Generates the sql file for installing the database. First arg is simply
632 L<SQL::Translator> args and the second is a coderef that returns the filename
635 =method _prepare_changegrade
637 $dm->_prepare_changegrade('1.00', '1.01', [qw( 1.00 1.01)], 'up')
639 Generates the sql file for migrating from one schema version to another. First
640 arg is the version to start from, second is the version to go to, third is the
641 L<version set|DBIx::Class::DeploymentHandler/VERSION SET>, and last is the
642 direction of the changegrade, be it 'up' or 'down'.
644 =method _read_sql_file
646 $dm->_read_sql_file('foo.sql')
648 Reads a sql file and returns lines in an C<ArrayRef>. Strips out comments,
649 transactions, and blank lines.