}
opendir my($dh), $dir;
- my %files = map { $_ => "$dir/$_" } grep { /\.sql$/ && -f "$dir/$_" } readdir $dh;
+ my %files = map { $_ => "$dir/$_" } grep { /\.(?:sql|pl)$/ && -f "$dir/$_" } readdir $dh;
closedir $dh;
if (-d $common) {
opendir my($dh), $common;
- for my $filename (grep { /\.sql$/ && -f catfile($common,$_) } readdir $dh) {
+ for my $filename (grep { /\.(?:sql|pl)$/ && -f catfile($common,$_) } readdir $dh) {
unless ($files{$filename}) {
$files{$filename} = catfile($common,$filename);
}
return catfile( $dirname, '001-auto.sql');
}
-sub deploy {
- my $self = shift;
- my $storage = $self->storage;
+method _run_sql_and_perl($filenames) {
+ my @files = @{$filenames};
+ my $storage = $self->storage;
my $guard = $self->schema->txn_scope_guard if $self->txn_wrap;
- my @sql = map @{$self->_read_sql_file($_)}, @{$self->_ddl_schema_consume_filenames(
- $self->storage->sqlt_type,
- $self->schema_version
- )};
-
- foreach my $line (@sql) {
- $storage->_query_start($line);
- try {
- # do a dbh_do cycle here, as we need some error checking in
- # place (even though we will ignore errors)
- $storage->dbh_do (sub { $_[1]->do($line) });
- }
- catch {
- carp "$_ (running '${line}')"
+ my $sql;
+ for my $filename (@files) {
+ if ($filename =~ /\.sql$/) {
+ my @sql = @{$self->_read_sql_file($filename)};
+ $sql .= join "\n", @sql;
+
+ foreach my $line (@sql) {
+ $storage->_query_start($line);
+ try {
+ # do a dbh_do cycle here, as we need some error checking in
+ # place (even though we will ignore errors)
+ $storage->dbh_do (sub { $_[1]->do($line) });
+ }
+ catch {
+ carp "$_ (running '${line}')"
+ }
+ $storage->_query_end($line);
+ }
+ } elsif ( $filename =~ /\.pl$/ ) {
+ qx( $^X $filename );
+ } else {
+ croak "A file got to deploy that wasn't sql or perl!";
}
- $storage->_query_end($line);
}
$guard->commit if $self->txn_wrap;
- return join "\n", @sql;
+
+ return $sql;
+}
+
+sub deploy {
+ my $self = shift;
+
+ return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames(
+ $self->storage->sqlt_type,
+ $self->schema_version
+ ));
}
sub prepare_install {
sub prepare_upgrade {
my ($self, $from_version, $to_version, $version_set) = @_;
-
- $from_version ||= '1.0'; #$self->database_version;
- $to_version ||= $self->schema_version;
-
# for updates prepared automatically (rob's stuff)
# one would want to explicitly set $version_set to
# [$to_version]
- $version_set ||= [$from_version, $to_version];
-
$self->_prepare_changegrade($from_version, $to_version, $version_set, 'up');
}
sub prepare_downgrade {
my ($self, $from_version, $to_version, $version_set) = @_;
- $from_version ||= $self->db_version;
- $to_version ||= $self->schema_version;
-
# for updates prepared automatically (rob's stuff)
# one would want to explicitly set $version_set to
# [$to_version]
- $version_set ||= [$from_version, $to_version];
-
$self->_prepare_changegrade($from_version, $to_version, $version_set, 'down');
}
sub downgrade_single_step {
my $self = shift;
my @version_set = @{ shift @_ };
- my @downgrade_files = @{$self->_ddl_schema_down_consume_filenames(
+
+ my $sql = $self->_run_sql_and_perl($self->_ddl_schema_down_consume_filenames(
$self->storage->sqlt_type,
\@version_set,
- )};
-
- for my $downgrade_file (@downgrade_files) {
- $self->_filedata($self->_read_sql_file($downgrade_file)); # I don't like this --fREW 2010-02-22
+ ));
- my $guard = $self->schema->txn_scope_guard if $self->txn_wrap;
- $self->_do_upgrade;
- $guard->commit if $self->txn_wrap;
- }
+ return ['', $sql];
}
sub upgrade_single_step {
my $self = shift;
my @version_set = @{ shift @_ };
- my @upgrade_files = @{$self->_ddl_schema_up_consume_filenames(
+
+ my $sql = $self->_run_sql_and_perl($self->_ddl_schema_up_consume_filenames(
$self->storage->sqlt_type,
\@version_set,
- )};
-
- my $upgrade_sql;
- for my $upgrade_file (@upgrade_files) {
- my $up = $self->_read_sql_file($upgrade_file);
- $upgrade_sql .= $up;
- $self->_filedata($up); # I don't like this --fREW 2010-02-22
- my $guard = $self->schema->txn_scope_guard if $self->txn_wrap;
- $self->_do_upgrade;
- $guard->commit if $self->txn_wrap;
- }
- return ['', $upgrade_sql];
-}
-
-method _do_upgrade { $self->_run_upgrade(qr/.*?/) }
-
-method _run_upgrade($stm) {
- my @statements = grep { $_ =~ $stm } @{$self->_filedata};
-
- for (@statements) {
- $self->storage->debugobj->query_start($_) if $self->storage->debug;
- $self->_apply_statement($_);
- $self->storage->debugobj->query_end($_) if $self->storage->debug;
- }
-}
-
-method _apply_statement($statement) {
- # croak?
- $self->storage->dbh->do($_) or carp "SQL was: $_"
+ ));
+ return ['', $sql];
}
__PACKAGE__->meta->make_immutable;
ok( $dm, 'DBIC::DH::SQL::Translator w/2.0 instantiates correctly');
$version = $s->schema_version();
- $dm->prepare_install();
+ $dm->prepare_install;
ok(
-f catfile(qw( t sql SQLite schema 2.0 001-auto.sql )),
'2.0 schema gets generated properly'
);
mkpath(catfile(qw( t sql SQLite up 1.0-2.0 )));
- $dm->prepare_upgrade;
+ $dm->prepare_upgrade(qw(1.0 2.0), [qw(1.0 2.0)]);
{
my $warned = 0;
local $SIG{__WARN__} = sub{$warned = 1};
- $dm->prepare_upgrade('0.0', '1.0');
+ $dm->prepare_upgrade(qw(0.0 1.0), [qw(0.0 1.0)]);
ok( $warned, 'prepare_upgrade with a bogus preversion warns' );
}
ok(
'1.0-2.0 diff gets generated properly and default start and end versions get set'
);
mkpath(catfile(qw( t sql SQLite down 2.0-1.0 )));
- $dm->prepare_downgrade($version, '1.0');
+ $dm->prepare_downgrade($version, '1.0', [$version, '1.0']);
ok(
-f catfile(qw( t sql SQLite down 2.0-1.0 001-auto.sql )),
'2.0-1.0 diff gets generated properly'
-f catfile(qw( t sql SQLite schema 3.0 001-auto.sql )),
'2.0 schema gets generated properly'
);
- $dm->prepare_downgrade($version, '1.0');
+ $dm->prepare_downgrade($version, '1.0', [$version, '1.0']);
ok(
-f catfile(qw( t sql SQLite down 3.0-1.0 001-auto.sql )),
'3.0-1.0 diff gets generated properly'
-f catfile(qw( t sql SQLite up 1.0-3.0 001-auto.sql )),
'1.0-3.0 diff gets generated properly'
);
- $dm->prepare_upgrade( '2.0', $version );
+ $dm->prepare_upgrade( '2.0', $version, ['2.0', $version]);
{
my $warned = 0;
local $SIG{__WARN__} = sub{$warned = 1};
- $dm->prepare_upgrade( '2.0', $version );
+ $dm->prepare_upgrade( '2.0', $version, ['2.0', $version] );
ok( $warned, 'prepare_upgrade warns if you clobber an existing upgrade file' );
}
ok(