From: Arthur Axel 'fREW' Schmidt Date: Sat, 27 Feb 2010 10:04:29 +0000 (-0600) Subject: simplify most of the code significantly thanks to version handling code X-Git-Tag: v0.001000_01~114 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=24f4524be2492ee3699f7ee87eb899238c6268c7;p=dbsrgits%2FDBIx-Class-DeploymentHandler.git simplify most of the code significantly thanks to version handling code --- diff --git a/lib/DBIx/Class/DeploymentHandler.pm b/lib/DBIx/Class/DeploymentHandler.pm index 06d2f9e..965ed0f 100644 --- a/lib/DBIx/Class/DeploymentHandler.pm +++ b/lib/DBIx/Class/DeploymentHandler.pm @@ -76,11 +76,11 @@ has sqltargs => ( # configuration default => sub { {} }, ); -method install($new_version) { +method install { carp 'Install not possible as versions table already exists in database' if $self->is_installed; - $new_version ||= $self->schema_version; + my $new_version = $self->to_version; if ($new_version) { $self->deploy; @@ -94,17 +94,8 @@ method install($new_version) { } method upgrade { - my $db_version = $self->db_version; - my $schema_version = $self->schema_version; - - unless ($db_version) { - # croak? - carp 'Upgrade not possible as database is unversioned. Please call install first.'; - return; - } - while ( my $version_list = $self->next_version_set ) { - $self->upgrade_single_step( $version_list->[0], $version_list->[1] ); + $self->upgrade_single_step($version_list); } } diff --git a/lib/DBIx/Class/DeploymentHandler/HandlesVersioning.pm b/lib/DBIx/Class/DeploymentHandler/HandlesVersioning.pm index 7b111a2..b1ebb78 100644 --- a/lib/DBIx/Class/DeploymentHandler/HandlesVersioning.pm +++ b/lib/DBIx/Class/DeploymentHandler/HandlesVersioning.pm @@ -7,7 +7,7 @@ has schema => ( isa => 'DBIx::Class::Schema', is => 'ro', required => 1, - handles => [qw( ddl_filename schema_version )], + handles => [qw( schema_version )], ); has version_rs => ( diff --git a/lib/DBIx/Class/DeploymentHandler/SqltDeployMethod.pm b/lib/DBIx/Class/DeploymentHandler/SqltDeployMethod.pm index 230790c..e807ace 100644 --- a/lib/DBIx/Class/DeploymentHandler/SqltDeployMethod.pm +++ b/lib/DBIx/Class/DeploymentHandler/SqltDeployMethod.pm @@ -66,7 +66,7 @@ has schema => ( isa => 'DBIx::Class::Schema', is => 'ro', required => 1, - handles => [qw( ddl_filename schema_version )], + handles => [qw( schema_version )], ); has _filedata => ( @@ -74,6 +74,17 @@ has _filedata => ( is => 'rw', ); +method ddl_filename($type, $versions, $dir) { + my $filename = ref($self->schema); + $filename =~ s/::/-/g; + + $filename = File::Spec->catfile( + $dir, "$filename-" . join( q(-), @{$versions} ) . "-$type.sql" + ); + + return $filename; +} + method deployment_statements { my $dir = $self->upgrade_directory; my $schema = $self->schema; @@ -81,7 +92,7 @@ method deployment_statements { my $sqltargs = $self->sqltargs; my $version = $self->schema_version || '1.x'; - my $filename = $self->ddl_filename($type, $version, $dir); + my $filename = $self->ddl_filename($type, [ $version ], $dir); if(-f $filename) { my $file; open $file, q(<), $filename @@ -180,7 +191,7 @@ method create_install_ddl { $sqlt->{schema} = $sqlt_schema; $sqlt->producer($db); - my $filename = $self->ddl_filename($db, $version, $dir); + my $filename = $self->ddl_filename($db, [ $version ], $dir); if (-e $filename && ($version eq $schema_version )) { # if we are dumping the current version, overwrite the DDL carp "Overwriting existing DDL file - $filename"; @@ -234,13 +245,13 @@ method create_update_ddl($version, $preversion) { $sqlt->{schema} = $sqlt_schema; $sqlt->producer($db); - my $prefilename = $self->ddl_filename($db, $preversion, $dir); + my $prefilename = $self->ddl_filename($db, [ $preversion ], $dir); unless(-e $prefilename) { carp("No previous schema file found ($prefilename)"); next; } - my $diff_file = $self->ddl_filename($db, $version, $dir, $preversion); + my $diff_file = $self->ddl_filename($db, [ $preversion, $version ], $dir ); if(-e $diff_file) { carp("Overwriting existing diff file - $diff_file"); unlink $diff_file; @@ -281,7 +292,7 @@ method create_update_ddl($version, $preversion) { $t->parser( $db ) # could this really throw an exception? or $self->throw_exception ($t->error); - my $filename = $self->ddl_filename($db, $version, $dir); + my $filename = $self->ddl_filename($db, [ $version ], $dir); my $out = $t->translate( $filename ) or $self->throw_exception ($t->error); @@ -328,24 +339,15 @@ method _read_sql_file($file) { return \@data; } -method create_upgrade_path { } - -method upgrade_single_step($db_version, $target_version) { - if ($db_version eq $target_version) { - # croak? - carp "Upgrade not necessary\n"; - return; - } - +method upgrade_single_step { + my @version_set = @{ shift @_ }; + my $db_version = $self->db_version; my $upgrade_file = $self->ddl_filename( $self->storage->sqlt_type, - $target_version, + \@version_set, $self->upgrade_directory, - $db_version, ); - $self->create_upgrade_path({ upgrade_file => $upgrade_file }); - unless (-f $upgrade_file) { # croak? carp "Upgrade not possible, no upgrade file found ($upgrade_file), please create one\n"; @@ -359,7 +361,7 @@ method upgrade_single_step($db_version, $target_version) { $self->schema->txn_do(sub { $self->do_upgrade }); $self->version_rs->create({ - version => $target_version, + version => $version_set[-1], # ddl => $ddl, # upgrade_sql => $upgrade_sql, }); diff --git a/lib/DBIx/Class/DeploymentHandler/WithDatabaseToSchemaVersions.pm b/lib/DBIx/Class/DeploymentHandler/WithDatabaseToSchemaVersions.pm index fc80e2e..9fcf439 100644 --- a/lib/DBIx/Class/DeploymentHandler/WithDatabaseToSchemaVersions.pm +++ b/lib/DBIx/Class/DeploymentHandler/WithDatabaseToSchemaVersions.pm @@ -6,17 +6,10 @@ use DBIx::Class::DeploymentHandler::DatabaseToSchemaVersions; use Carp 'carp'; has version_handler => ( - -# < mst> isa => 'DBIx::Class::DeploymentHandler::SqltDeployMethod', -# < mst> should be -# < mst> does => -# < mst> and that role should supply those methods -# < mst> then you can pass handles => as well - - does => 'DBIx::Class::DeploymentHandler::HandlesVersioning', - is => 'ro', + is => 'ro', lazy_build => 1, - handles => 'DBIx::Class::DeploymentHandler::HandlesVersioning', + does => 'DBIx::Class::DeploymentHandler::HandlesVersioning', + handles => 'DBIx::Class::DeploymentHandler::HandlesVersioning', ); sub _build_version_handler { diff --git a/t/02-instantiation.t b/t/02-instantiation.t index 8536937..73e92de 100644 --- a/t/02-instantiation.t +++ b/t/02-instantiation.t @@ -12,9 +12,9 @@ my $sql_dir = 't/sql'; unlink 'db.db' if -e 'db.db'; if (-d 't/sql') { - unlink $_ for glob('t/sql/*'); + unlink $_ for glob('t/sql/*'); } else { - mkdir 't/sql'; + mkdir 't/sql'; } VERSION1: { @@ -25,7 +25,7 @@ VERSION1: { upgrade_directory => $sql_dir, schema => $s, databases => 'SQLite', - sqltargs => { add_drop_table => 0 }, + sqltargs => { add_drop_table => 0 }, }); ok($handler, 'DBIx::Class::DeploymentHandler w/1.0 instantiates correctly'); @@ -77,7 +77,7 @@ VERSION2: { baz => 'frew', }) } 'schema not uppgrayyed'; - $handler->upgrade_single_step('1.0', '2.0'); + $handler->upgrade_single_step(['1.0', '2.0']); lives_ok { $s->resultset('Foo')->create({ bar => 'frew',