From: Arthur Axel 'fREW' Schmidt Date: Tue, 18 May 2010 01:55:27 +0000 (-0500) Subject: Convert methods to named args and Document args X-Git-Tag: v0.001000_10~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=be140a5f1f8ab40705bf4cbedb74761c4994a765;p=dbsrgits%2FDBIx-Class-DeploymentHandler.git Convert methods to named args and Document args --- diff --git a/Changes b/Changes index 750ef42..70e0d07 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for {{$dist->name}} {{$NEXT}} + - change method args to named args + - document method args - preconnect should not connect to the database 0.001000_09 2010-05-15 23:19:05 CST6CDT diff --git a/lib/DBIx/Class/DeploymentHandler/Dad.pm b/lib/DBIx/Class/DeploymentHandler/Dad.pm index 974884e..92bbb93 100644 --- a/lib/DBIx/Class/DeploymentHandler/Dad.pm +++ b/lib/DBIx/Class/DeploymentHandler/Dad.pm @@ -48,7 +48,9 @@ method install { sub upgrade { my $self = shift; while ( my $version_list = $self->next_version_set ) { - my ($ddl, $upgrade_sql) = @{$self->upgrade_single_step($version_list)||[]}; + my ($ddl, $upgrade_sql) = @{ + $self->upgrade_single_step({ version_set => $version_list }) + ||[]}; $self->add_database_version({ version => $version_list->[-1], @@ -61,7 +63,7 @@ sub upgrade { sub downgrade { my $self = shift; while ( my $version_list = $self->previous_version_set ) { - $self->downgrade_single_step($version_list); + $self->downgrade_single_step({ version_set => $version_list }); # do we just delete a row here? I think so but not sure $self->delete_database_version({ version => $version_list->[-1] }); diff --git a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm index 586eee1..0d232fd 100644 --- a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm +++ b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm @@ -195,7 +195,7 @@ method _run_sql_and_perl($filenames) { sub deploy { my $self = shift; - my $version = shift || $self->schema_version; + my $version = (shift @_ || {})->{version} || $self->schema_version; return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames( $self->storage->sqlt_type, @@ -290,8 +290,9 @@ sub _resultsource_install_filename { } sub install_resultsource { - my ($self, $source, $version) = @_; - + my ($self, $args) = @_; + my $source = $args->{result_source}; + my $version = $args->{version}; my $rs_install_file = $self->_resultsource_install_filename($source->source_name); @@ -306,7 +307,7 @@ sub install_resultsource { sub prepare_resultsource_install { my $self = shift; - my $source = shift; + my $source = (shift @_)->{result_source}; my $filename = $self->_resultsource_install_filename($source->source_name); $self->_prepare_install({ @@ -320,14 +321,17 @@ sub prepare_deploy { } sub prepare_upgrade { - my ($self, $from_version, $to_version, $version_set) = @_; - $self->_prepare_changegrade($from_version, $to_version, $version_set, 'up'); + my ($self, $args) = @_; + $self->_prepare_changegrade( + $args->{from_version}, $args->{to_version}, $args->{version_set}, 'up' + ); } sub prepare_downgrade { - my ($self, $from_version, $to_version, $version_set) = @_; - - $self->_prepare_changegrade($from_version, $to_version, $version_set, 'down'); + my ($self, $args) = @_; + $self->_prepare_changegrade( + $args->{from_version}, $args->{to_version}, $args->{version_set}, 'down' + ); } method _prepare_changegrade($from_version, $to_version, $version_set, $direction) { @@ -444,7 +448,7 @@ method _read_sql_file($file) { sub downgrade_single_step { my $self = shift; - my $version_set = shift @_; + my $version_set = (shift @_)->{version_set}; my $sql = $self->_run_sql_and_perl($self->_ddl_schema_down_consume_filenames( $self->storage->sqlt_type, @@ -456,7 +460,7 @@ sub downgrade_single_step { sub upgrade_single_step { my $self = shift; - my $version_set = shift @_; + my $version_set = (shift @_)->{version_set}; my $sql = $self->_run_sql_and_perl($self->_ddl_schema_up_consume_filenames( $self->storage->sqlt_type, diff --git a/lib/DBIx/Class/DeploymentHandler/HandlesDeploy.pm b/lib/DBIx/Class/DeploymentHandler/HandlesDeploy.pm index 089eda1..83702f1 100644 --- a/lib/DBIx/Class/DeploymentHandler/HandlesDeploy.pm +++ b/lib/DBIx/Class/DeploymentHandler/HandlesDeploy.pm @@ -25,7 +25,10 @@ __END__ =method preinstall - $dh->preinstall + $dh->preinstall({ + version => 1, + storage_type => 'SQLite' + }); Run scripts before deploying to the database @@ -37,27 +40,36 @@ Generate the needed data files to install the schema to the database. =method deploy - $dh->deploy + $dh->deploy({ version => 1 }) Deploy the schema to the database. =method prepare_resultsource_install - $dh->prepare_resultsource_install($resultset->result_source) + $dh->prepare_resultsource_install({ + result_source => $resultset->result_source, + }) Takes a L and generates a single migration file to create the resultsource's table. =method install_resultsource - $dh->prepare_resultsource_install($resultset->result_source); + $dh->install_resultsource({ + result_source => $resultset->result_source, + version => 1, + }) Takes a L and runs a single migration file to deploy the resultsource's table. =method prepare_upgrade - $dh->prepare_upgrade(1, 2, [1, 2]); + $dh->prepare_upgrade({ + from_version => 1, + to_version => 2, + version_set => [1, 2] + }); Takes two versions and a version set. This basically is supposed to generate the needed C to migrate up from the first version to the second version. @@ -65,7 +77,11 @@ The version set uniquely identifies the migration. =method prepare_downgrade - $dh->prepare_downgrade(2, 1, [1, 2]); + $dh->prepare_downgrade({ + from_version => 1, + to_version => 2, + version_set => [1, 2] + }); Takes two versions and a version set. This basically is supposed to generate the needed C to migrate down from the first version to the second version. @@ -74,7 +90,9 @@ respective upgrade version set. =method upgrade_single_step - my ($ddl, $sql) = @{$dh->upgrade_single_step($version_set)||[]} + my ($ddl, $sql) = @{ + $dh->upgrade_single_step({ version_set => $version_set }) + ||[]} Call a single upgrade migration. Takes a version set as an argument. Optionally return C<< [ $ddl, $upgrade_sql ] >> where C<$ddl> is the DDL for diff --git a/lib/DBIx/Class/DeploymentHandler/WithReasonableDefaults.pm b/lib/DBIx/Class/DeploymentHandler/WithReasonableDefaults.pm index e9b85f7..25afb2b 100644 --- a/lib/DBIx/Class/DeploymentHandler/WithReasonableDefaults.pm +++ b/lib/DBIx/Class/DeploymentHandler/WithReasonableDefaults.pm @@ -8,12 +8,13 @@ requires qw( prepare_upgrade prepare_downgrade database_version schema_version ) around prepare_upgrade => sub { my $orig = shift; my $self = shift; + my $args = shift || {}; - my $from_version = shift || $self->database_version; - my $to_version = shift || $self->schema_version; - my $version_set = shift || [$from_version, $to_version]; + $args->{from_version} ||= $self->database_version; + $args->{to_version} ||= $self->schema_version; + $args->{version_set} ||= [$args->{from_version}, $args->{to_version}]; - $self->$orig($from_version, $to_version, $version_set); + $self->$orig($args); }; @@ -21,11 +22,13 @@ around prepare_downgrade => sub { my $orig = shift; my $self = shift; - my $from_version = shift || $self->database_version; - my $to_version = shift || $self->schema_version; - my $version_set = shift || [$from_version, $to_version]; + my $args = shift || {}; - $self->$orig($from_version, $to_version, $version_set); + $args->{from_version} ||= $self->database_version; + $args->{to_version} ||= $self->schema_version; + $args->{version_set} ||= [$args->{from_version}, $args->{to_version}]; + + $self->$orig($args); }; around install_resultsource => sub { diff --git a/t/02-instantiation-wo-component.t b/t/02-instantiation-wo-component.t index f3ea2ac..063f3a7 100644 --- a/t/02-instantiation-wo-component.t +++ b/t/02-instantiation-wo-component.t @@ -66,7 +66,7 @@ VERSION2: { my $version = $s->schema_version(); $handler->prepare_deploy(); - $handler->prepare_upgrade(1, $version); + $handler->prepare_upgrade({ from_version => 1, to_version => $version }); dies_ok { $s->resultset('Foo')->create({ bar => 'frew', @@ -103,7 +103,7 @@ VERSION3: { my $version = $s->schema_version(); $handler->prepare_deploy; - $handler->prepare_upgrade( 2, $version ); + $handler->prepare_upgrade({ from_version => 2, to_version => $version }); dies_ok { $s->resultset('Foo')->create({ bar => 'frew', @@ -135,7 +135,7 @@ DOWN2: { ok($handler, 'DBIx::Class::DeploymentHandler w/2 instantiates correctly'); my $version = $s->schema_version(); - $handler->prepare_downgrade(3, $version); + $handler->prepare_downgrade({ from_version => 3, to_version => $version }); lives_ok { $s->resultset('Foo')->create({ bar => 'frew', diff --git a/t/02-instantiation.t b/t/02-instantiation.t index 00cb75b..8a0de18 100644 --- a/t/02-instantiation.t +++ b/t/02-instantiation.t @@ -66,7 +66,7 @@ VERSION2: { my $version = $s->schema_version(); $handler->prepare_deploy(); - $handler->prepare_upgrade(1, $version); + $handler->prepare_upgrade({ from_version => 1, to_version => $version} ); dies_ok { $s->resultset('Foo')->create({ bar => 'frew', @@ -103,7 +103,7 @@ VERSION3: { my $version = $s->schema_version(); $handler->prepare_deploy; - $handler->prepare_upgrade( 2, $version ); + $handler->prepare_upgrade({ from_version => 2, to_version => $version }); dies_ok { $s->resultset('Foo')->create({ bar => 'frew', @@ -135,7 +135,7 @@ DOWN2: { ok($handler, 'DBIx::Class::DeploymentHandler w/2 instantiates correctly'); my $version = $s->schema_version(); - $handler->prepare_downgrade(3, $version); + $handler->prepare_downgrade({ from_version => 3, to_version => $version }); lives_ok { $s->resultset('Foo')->create({ bar => 'frew', diff --git a/t/03-deprecated.t b/t/03-deprecated.t index f9bd021..9b83045 100644 --- a/t/03-deprecated.t +++ b/t/03-deprecated.t @@ -65,7 +65,7 @@ VERSION2: { my $version = $s->schema_version(); $handler->prepare_deploy(); - $handler->prepare_upgrade('1.0', $version); + $handler->prepare_upgrade({ from_version => '1.0', to_version => $version }); dies_ok { $s->resultset('Foo')->create({ bar => 'frew', @@ -102,7 +102,7 @@ VERSION3: { my $version = $s->schema_version(); $handler->prepare_deploy; - $handler->prepare_upgrade( '2.0', $version ); + $handler->prepare_upgrade({ from_version => '2.0', to_version => $version }); dies_ok { $s->resultset('Foo')->create({ bar => 'frew', diff --git a/t/deploy_methods/sql_translator.t b/t/deploy_methods/sql_translator.t index c7d03e0..704b998 100644 --- a/t/deploy_methods/sql_translator.t +++ b/t/deploy_methods/sql_translator.t @@ -34,7 +34,7 @@ VERSION1: { catfile(qw( t sql SQLite preinstall 1.0 003-semiautomatic.pl )); print {$prerun} "sub {use File::Touch; touch(q(foobar));}"; close $prerun; - $dm->preinstall('1.0'); + $dm->preinstall({ version => '1.0' }); ok -e 'foobar'; @@ -100,12 +100,20 @@ VERSION2: { '2.0 schema gets generated properly' ); mkpath(catfile(qw( t sql SQLite up 1.0-2.0 ))); - $dm->prepare_upgrade(qw(1.0 2.0), [qw(1.0 2.0)]); + $dm->prepare_upgrade({ + from_version => '1.0', + to_version => '2.0', + version_set => [qw(1.0 2.0)] + }); { my $warned = 0; local $SIG{__WARN__} = sub{$warned = 1}; - $dm->prepare_upgrade(qw(0.0 1.0), [qw(0.0 1.0)]); + $dm->prepare_upgrade({ + from_version => '0.0', + to_version => '1.0', + version_set => [qw(0.0 1.0)] + }); ok( $warned, 'prepare_upgrade with a bogus preversion warns' ); } ok( @@ -113,7 +121,11 @@ VERSION2: { '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', [$version, '1.0']); + $dm->prepare_downgrade({ + from_version => $version, + to_version => '1.0', + version_set => [$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' @@ -150,7 +162,7 @@ VERSION2: { |; close $common_pl; - $dm->upgrade_single_step([qw( 1.0 2.0 )]); + $dm->upgrade_single_step({ version_set => [qw( 1.0 2.0 )] }); is( $s->resultset('Foo')->search({ bar => 'hello', baz => 'world', @@ -165,14 +177,14 @@ VERSION2: { baz => 'frew', }) } 'schema is deployed'; - $dm->downgrade_single_step([qw( 2.0 1.0 )]); + $dm->downgrade_single_step({ version_set => [qw( 2.0 1.0 )] }); dies_ok { $s->resultset('Foo')->create({ bar => 'frew', baz => 'frew', }) } 'schema is downgrayyed'; - $dm->upgrade_single_step([qw( 1.0 2.0 )]); + $dm->upgrade_single_step({ version_set => [qw( 1.0 2.0 )] }); } VERSION3: { @@ -194,21 +206,37 @@ VERSION3: { -f catfile(qw( t sql SQLite schema 3.0 001-auto.sql )), '2.0 schema gets generated properly' ); - $dm->prepare_downgrade($version, '1.0', [$version, '1.0']); + $dm->prepare_downgrade({ + from_version => $version, + to_version => '1.0', + version_set => [$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' ); - $dm->prepare_upgrade( '1.0', $version, ['1.0', $version] ); + $dm->prepare_upgrade({ + from_version => '1.0', + to_version => $version, + version_set => ['1.0', $version] + }); ok( -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, ['2.0', $version]); + $dm->prepare_upgrade({ + from_version => '2.0', + to_version => $version, + version_set => ['2.0', $version] + }); { my $warned = 0; local $SIG{__WARN__} = sub{$warned = 1}; - $dm->prepare_upgrade( '2.0', $version, ['2.0', $version] ); + $dm->prepare_upgrade({ + from_version => '2.0', + to_version => $version, + version_set => ['2.0', $version] + }); ok( $warned, 'prepare_upgrade warns if you clobber an existing upgrade file' ); } ok( @@ -226,7 +254,7 @@ VERSION3: { biff => 'frew', }) } 'schema not deployed'; - $dm->upgrade_single_step([qw( 2.0 3.0 )]); + $dm->upgrade_single_step({ version_set => [qw( 2.0 3.0 )] }); lives_ok { $s->resultset('Foo')->create({ bar => 'frew', @@ -237,7 +265,7 @@ VERSION3: { rmtree(catfile(qw( t sql SQLite ))); rmtree(catfile(qw( t sql _generic ))); dies_ok { - $dm->upgrade_single_step([qw( 2.0 3.0 )]); + $dm->upgrade_single_step({ version_set => [qw( 2.0 3.0 )] }); } 'dies when sql dir does not exist'; } done_testing; diff --git a/t/deploy_methods/sql_translator_deprecated.t b/t/deploy_methods/sql_translator_deprecated.t index e3c0a91..4038abe 100644 --- a/t/deploy_methods/sql_translator_deprecated.t +++ b/t/deploy_methods/sql_translator_deprecated.t @@ -64,7 +64,11 @@ VERSION2: { $version = $s->schema_version; $dm->prepare_deploy; - $dm->prepare_upgrade('1.0', $version, ['1.0', $version]); + $dm->prepare_upgrade({ + from_version => '1.0', + to_version => $version, + version_set => ['1.0', $version] + }); dies_ok { $s->resultset('Foo')->create({ bar => 'frew', @@ -77,7 +81,7 @@ VERSION2: { baz => 'frew', }) } 'schema not uppgrayyed'; - $dm->upgrade_single_step(['1.0', $version]); + $dm->upgrade_single_step({ version_set => ['1.0', $version] }); lives_ok { $s->resultset('Foo')->create({ bar => 'frew', diff --git a/t/version_storages/standard.t b/t/version_storages/standard.t index d3d3ff7..5a36e1a 100644 --- a/t/version_storages/standard.t +++ b/t/version_storages/standard.t @@ -34,18 +34,18 @@ my $dm = Translator->new({ my $vs = Standard->new({ schema => $s }); -$dm->prepare_resultsource_install( - $vs->version_rs->result_source -); +$dm->prepare_resultsource_install({ + result_source => $vs->version_rs->result_source +}); ok( $vs, 'DBIC::DH::VersionStorage::Standard instantiates correctly' ); ok( !$vs->version_storage_is_installed, 'VersionStorage is not yet installed' ); -$dm->install_resultsource( - $vs->version_rs->result_source, - '1.0', -); +$dm->install_resultsource({ + result_source => $vs->version_rs->result_source, + version => '1.0', +}); ok( $vs->version_storage_is_installed, 'VersionStorage is now installed' );