my $common =
catfile( $base_dir, '_common', $prefix, join q(-), @{$versions} );
+ my $common_any =
+ catfile( $base_dir, '_common', $prefix, '_any' );
+
my $dir;
if (-d $main) {
$dir = catfile($main, $prefix, join q(-), @{$versions})
croak "$main does not exist; please write/generate some SQL"
}
}
+ my $dir_any = catfile($main, $prefix, '_any');
my %files;
try {
} catch {
die $_ unless $self->ignore_ddl;
};
- if (-d $common) {
- opendir my($dh), $common;
- for my $filename (grep { /\.(?:sql|pl)$/ && -f catfile($common,$_) } readdir $dh) {
+ for my $dirname (grep { -d $_ } $common, $common_any, $dir_any) {
+ opendir my($dh), $dirname;
+ for my $filename (grep { /\.(?:sql|pl)$/ && -f catfile($dirname,$_) } readdir $dh) {
unless ($files{$filename}) {
- $files{$filename} = catfile($common,$filename);
+ $files{$filename} = catfile($dirname,$filename);
}
}
closedir $dh;
return $self->_run_sql_array($self->_read_sql_file($filename));
}
-method _run_perl($filename) {
+method _run_perl($filename, $versions) {
log_debug { "Running Perl from $filename" };
my $filedata = do { local( @ARGV, $/ ) = $filename; <> };
if ($@) {
carp "$filename failed to compile: $@";
} elsif (ref $fn eq 'CODE') {
- $fn->($self->schema)
+ $fn->($self->schema, $versions)
} else {
carp "$filename should define an anonymouse sub that takes a schema but it didn't!";
}
}
-method _run_sql_and_perl($filenames, $sql_to_run) {
+method _run_sql_and_perl($filenames, $sql_to_run, $versions) {
my @files = @{$filenames};
my $guard = $self->schema->txn_scope_guard if $self->txn_wrap;
} elsif ($filename =~ /\.sql$/) {
$sql .= $self->_run_sql($filename)
} elsif ( $filename =~ /\.pl$/ ) {
- $self->_run_perl($filename)
+ $self->_run_perl($filename, $versions)
} else {
croak "A file ($filename) got to deploy that wasn't sql or perl!";
}
return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames(
$sqlt_type,
$version,
- ), $sql);
+ ), $sql, [$version]);
}
sub initialize {
$version,
)
];
- $self->_run_sql_and_perl($files);
+ $self->_run_sql_and_perl($files, '', [$version]);
}
sub prepare_resultsource_install {
my $sql = $self->_run_sql_and_perl($self->_ddl_schema_downgrade_consume_filenames(
$sqlt_type,
$version_set,
- ), $sql_to_run);
+ ), $sql_to_run, $version_set);
return ['', $sql];
}
my $sql = $self->_run_sql_and_perl($self->_ddl_schema_upgrade_consume_filenames(
$sqlt_type,
$version_set,
- ), $sql_to_run);
+ ), $sql_to_run, $version_set);
return ['', $sql];
}
| | `- 002-remove-customers.pl
| `- upgrade
| `- 1-2
- | `- 002-generate-customers.pl
+ | | `- 002-generate-customers.pl
+ | `- _any
+ | `- 999-bump-action.pl
`- MySQL
|- downgrade
| `- 2-1
$dm->upgrade_single_step([1,2])
would run C<$sql_migration_dir/SQLite/upgrade/1-2/001-auto.sql> followed by
-C<$sql_migration_dir/_common/upgrade/1-2/002-generate-customers.pl>.
+C<$sql_migration_dir/_common/upgrade/1-2/002-generate-customers.pl>, and
+finally punctuated by
+C<$sql_migration_dir/_common/upgrade/_any/999-bump-action.pl>.
C<.pl> files don't have to be in the C<_common> directory, but most of the time
they should be, because perl scripts are generally database independent.
your storage type is. If you are not sure what your storage type is, take a
look at the producers listed for L<SQL::Translator>. Also note, C<_common>
is a special case. C<_common> will get merged into whatever other files you
-already have. This directory can containt the following directories itself:
+already have. This directory can contain the following directories itself:
=over 2
=back
+Note that there can be an C<_any> in the place of any of the versions (like
+C<1-2> or C<1>), which means those scripts will be run B<every> time. So if
+you have an C<_any> in C<_common/upgrade>, that script will get run for every
+upgrade.
+
=head1 PERL SCRIPTS
A perl script for this tool is very simple. It merely needs to contain an
-anonymous sub that takes a L<DBIx::Class::Schema> as it's only argument.
+anonymous sub that takes a L<DBIx::Class::Schema> and the version set as it's
+arguments.
+
A very basic perl script might look like:
#!perl
sub {
my $schema = shift;
+ # [1] for deploy, [1,2] for upgrade or downgrade, probably used with _any
+ my $versions = shift;
+
$schema->resultset('Users')->create({
name => 'root',
password => 'root',
my $sql_dir = 't/sql';
DBICDHTest::ready;
+unlink 'stuffthatran';
+
+for (qw(initialize upgrade downgrade deploy)) {
+ mkpath(catfile(qw( t sql _common), $_, '_any' ));
+ open my $fh, '>',
+ catfile(qw( t sql _common), $_, qw(_any 000-win.pl ));
+ print {$fh} 'sub {open my $fh, ">>", "stuffthatran"; use Data::Dumper::Concise; print {$fh} join(",", @{$_[1]||[]}) . "\n"; }';
+ close $fh;
+}
+
+for (qw(initialize upgrade downgrade deploy)) {
+ mkpath(catfile(qw( t sql SQLite), $_, '_any' ));
+ open my $fh, '>',
+ catfile(qw( t sql SQLite), $_, qw(_any 000-win2.pl ));
+ print {$fh} 'sub {open my $fh, ">>", "stuffthatran"; use Data::Dumper::Concise; print {$fh} join(",", @{$_[1]||[]}) . "\n"; }';
+ close $fh;
+}
VERSION1: {
use_ok 'DBICVersion_v1';
close $prerun;
$dm->initialize({ version => '1.0' });
- ok -e 'foobar';
+ ok -e 'foobar', 'code got run in preinit';
dies_ok {$dm->prepare_deploy} 'prepare_deploy dies if you run it twice' ;
$dm->upgrade_single_step({ version_set => [qw( 2.0 3.0 )] });
} 'dies when sql dir does not exist';
}
+
+my $stuff_that_ran = do { local( @ARGV, $/ ) = 'stuffthatran'; <> };
+is $stuff_that_ran,
+'
+
+1.0
+1.0
+1.0,2.0
+1.0,2.0
+2.0,1.0
+2.0,1.0
+1.0,2.0
+1.0,2.0
+2.0,3.0
+2.0,3.0
+2.0,3.0
+2.0,3.0
+', '_any got ran the right amount of times with the right args';
+
done_testing;
#vim: ts=2 sw=2 expandtab