From: Arthur Axel 'fREW' Schmidt Date: Sun, 28 Mar 2010 16:00:26 +0000 (-0500) Subject: run arbitrary perl in upgrade/downgrade/schema X-Git-Tag: v0.001000_01~34 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0841a74313b0b1f813ba0211a7e665c7fcf93a4b;p=dbsrgits%2FDBIx-Class-DeploymentHandler.git run arbitrary perl in upgrade/downgrade/schema --- diff --git a/TODO b/TODO index a80091d..4385256 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,3 @@ make recommended bundle (monotonic) -run arbitrary perl from migration scripts (tests) make deploy_version_storage pod diff --git a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm index 637a983..6d00bdd 100644 --- a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm +++ b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm @@ -162,8 +162,17 @@ method _run_sql_and_perl($filenames) { } $storage->_query_end($line); } - } elsif ( $filename =~ /\.pl$/ ) { - qx( $^X $filename ); + } elsif ( $filename =~ /^(.+)\.pl$/ ) { + my $package = $1; + my $filedata = do { local( @ARGV, $/ ) = $filename; <> }; + # make the package name more palateable to perl + $package =~ s/\W/_/g; + + no warnings 'redefine'; + eval "package $package;\n\n$filedata"; + use warnings; + + $package->can('run')->($self->schema); } else { croak "A file got to deploy that wasn't sql or perl!"; } diff --git a/t/deploy_methods/sql_translator.t b/t/deploy_methods/sql_translator.t index 17d35e9..eca08c8 100644 --- a/t/deploy_methods/sql_translator.t +++ b/t/deploy_methods/sql_translator.t @@ -78,6 +78,7 @@ VERSION2: { upgrade_directory => $sql_dir, databases => ['SQLite'], sqltargs => { add_drop_table => 0 }, + txn_wrap => 1, }); ok( $dm, 'DBIC::DH::SQL::Translator w/2.0 instantiates correctly'); @@ -126,11 +127,28 @@ VERSION2: { print {$common} qq; close $common; + open my $common_pl, '>', + catfile(qw( t sql _common up 1.0-2.0 003-semiautomatic.pl )); + print {$common_pl} q| + sub run { + my $schema = shift; + $schema->resultset('Foo')->create({ + bar => 'goodbye', + baz => 'blue skies', + }) + } + |; + close $common_pl; + $dm->upgrade_single_step([qw( 1.0 2.0 )]); is( $s->resultset('Foo')->search({ bar => 'hello', baz => 'world', })->count, 1, '_common migration got run'); + is( $s->resultset('Foo')->search({ + bar => 'goodbye', + #baz => 'blue skies', + })->count, 1, '_common perl migration got run'); lives_ok { $s->resultset('Foo')->create({ bar => 'frew',