From: Arthur Axel 'fREW' Schmidt Date: Sat, 27 Feb 2010 09:21:56 +0000 (-0600) Subject: move some general comments to a general role, along with the interface, and initial... X-Git-Tag: v0.001000_01~116 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=24794769393e7e30c827e1010c5e23f7a9a58657;p=dbsrgits%2FDBIx-Class-DeploymentHandler.git move some general comments to a general role, along with the interface, and initial commit of db_schema_versions --- diff --git a/lib/DBIx/Class/DeploymentHandler/DatabaseToSchemaVersions.pm b/lib/DBIx/Class/DeploymentHandler/DatabaseToSchemaVersions.pm index f93c166..eb7063f 100644 --- a/lib/DBIx/Class/DeploymentHandler/DatabaseToSchemaVersions.pm +++ b/lib/DBIx/Class/DeploymentHandler/DatabaseToSchemaVersions.pm @@ -2,62 +2,25 @@ package DBIx::Class::DeploymentHandler::DatabaseToSchemaVersions; use Moose; use Method::Signatures::Simple; -# normally a VersionHandler will take -# a to_version and yeild an iterator of -# "version sets" or something like that. -# -# A "version set" is basically an arrayref -# of "version numbers" (which we already know -# is vague as is.) Typically an call to a -# VH w/ a db version of 1 and a "to_version" -# of 5 will iterate over something like this: -# [1, 2] -# [2, 3] -# [3, 4] -# [4, 5] -# -# Of course rob wants to be able to have dep -# management with his versions, so I *think* his -# would work like this: -# -# to_version = 7, db_version = 1 -# [1] -# [5] -# [7] -# -# Because 7 depended on 5, 5 was installed first; -# note that this potentially never released module -# doesn't use version pairs, instead it just yeilds -# versions. Version pairs are too much work for users -# to have to deal with in that sitation. We may -# actually switch to this for other versioners. -# -# The upshot of all this is that the DeploymentMethod -# needs to be able to take an ArrayRef[VersionNumber], -# instead of just a pair of VersionNumber. +with 'DBIx::Class::DeploymentHandler::HandlesVersioning'; -has schema => ( - isa => 'DBIx::Class::Schema', - is => 'ro', - required => 1, - handles => [qw( ddl_filename schema_version )], +has once => ( + is => 'rw', + isa => 'Bool', + default => undef, ); -has version_rs => ( - isa => 'DBIx::Class::ResultSet', - is => 'ro', - lazy_build => 1, - handles => [qw( is_installed db_version )], -); +sub next_version_set { + my $self = shift; + return undef + if $self->once; -method _build_version_rs { - $self->schema->set_us_up_the_bomb; - $self->schema->resultset('__VERSION') + $self->once(!$self->once); + return undef + if $self->db_version eq $self->to_version; + return [$self->db_version, $self->to_version]; } -method ordered_schema_versions { - ( $self->db_version, $self->schema_version) -} __PACKAGE__->meta->make_immutable; diff --git a/lib/DBIx/Class/DeploymentHandler/ExplicitVersions.pm b/lib/DBIx/Class/DeploymentHandler/ExplicitVersions.pm index 6c83d3b..f9e4f64 100644 --- a/lib/DBIx/Class/DeploymentHandler/ExplicitVersions.pm +++ b/lib/DBIx/Class/DeploymentHandler/ExplicitVersions.pm @@ -3,29 +3,7 @@ use Moose; use Method::Signatures::Simple; use Carp 'croak'; -has schema => ( - isa => 'DBIx::Class::Schema', - is => 'ro', - required => 1, - handles => [qw( ddl_filename schema_version )], -); - -has version_rs => ( - isa => 'DBIx::Class::ResultSet', - is => 'ro', - lazy_build => 1, - handles => [qw( is_installed db_version )], -); - -method _build_version_rs { - $self->schema->set_us_up_the_bomb; - $self->schema->resultset('__VERSION') -} - -has to_version => ( - is => 'ro', - required => 1, -); +with 'DBIx::Class::DeploymentHandler::HandlesVersioning'; has ordered_versions => ( is => 'ro', @@ -65,7 +43,8 @@ method _build__version_idx { croak 'database version not found in ordered_versions!'; } -method next_version_set { +sub next_version_set { # sub instead of method because of when roles get composed + my $self = shift; return undef if $self->ordered_versions->[$self->_version_idx] eq $self->to_version; my $next_idx = $self->_inc_version_idx; diff --git a/lib/DBIx/Class/DeploymentHandler/HandlesVersioning.pm b/lib/DBIx/Class/DeploymentHandler/HandlesVersioning.pm new file mode 100644 index 0000000..7b111a2 --- /dev/null +++ b/lib/DBIx/Class/DeploymentHandler/HandlesVersioning.pm @@ -0,0 +1,69 @@ +package DBIx::Class::DeploymentHandler::HandlesVersioning; +use Moose::Role; + +requires 'next_version_set'; + +has schema => ( + isa => 'DBIx::Class::Schema', + is => 'ro', + required => 1, + handles => [qw( ddl_filename schema_version )], +); + +has version_rs => ( + isa => 'DBIx::Class::ResultSet', + is => 'ro', + lazy_build => 1, + handles => [qw( is_installed db_version )], +); + +sub _build_version_rs { + $_[0]->schema->set_us_up_the_bomb; + $_[0]->schema->resultset('__VERSION') +} + +has to_version => ( + is => 'ro', + lazy_build => 1, +); + +sub _build_to_version { $_[0]->schema->schema_version } + +1; + +__END__ + +# normally a VersionHandler will take +# a to_version and yeild an iterator of +# "version sets" or something like that. +# +# A "version set" is basically an arrayref +# of "version numbers" (which we already know +# is vague as is.) Typically an call to a +# VH w/ a db version of 1 and a "to_version" +# of 5 will iterate over something like this: +# [1, 2] +# [2, 3] +# [3, 4] +# [4, 5] +# +# Of course rob wants to be able to have dep +# management with his versions, so I *think* his +# would work like this: +# +# to_version = 7, db_version = 1 +# [1] +# [5] +# [7] +# +# Because 7 depended on 5, 5 was installed first; +# note that this potentially never released module +# doesn't use version pairs, instead it just yeilds +# versions. Version pairs are too much work for users +# to have to deal with in that sitation. We may +# actually switch to this for other versioners. +# +# The upshot of all this is that the DeploymentMethod +# needs to be able to take an ArrayRef[VersionNumber], +# instead of just a pair of VersionNumber. +vim: ts=2 sw=2 expandtab diff --git a/t/04_db_schema_versions.t b/t/04_db_schema_versions.t new file mode 100644 index 0000000..87d770e --- /dev/null +++ b/t/04_db_schema_versions.t @@ -0,0 +1,84 @@ +#!perl + +use Test::More; +use Test::Exception; + +use lib 't/lib'; +use DBICTest; +use DBIx::Class::DeploymentHandler; +use DBIx::Class::DeploymentHandler::ExplicitVersions; +my $db = 'dbi:SQLite:db.db'; +my @connection = ($db, '', '', { ignore_version => 1 }); +my $sql_dir = 't/sql'; + +unlink 'db.db' if -e 'db.db'; +if (-d 't/sql') { + unlink $_ for glob('t/sql/*'); +} else { + mkdir 't/sql'; +} + +use DBICVersion_v1; +my $s = DBICVersion::Schema->connect(@connection); + +my $handler = DBIx::Class::DeploymentHandler->new({ + upgrade_directory => $sql_dir, + schema => $s, + databases => 'SQLite', + sqltargs => { add_drop_table => 0 }, +}); + +my $version = $s->schema_version(); +$handler->create_install_ddl(); + +$handler->install; +{ + my $vh = DBIx::Class::DeploymentHandler::DatabaseToSchemaVersions->new({ + schema => $s, + ordered_versions => $versions, + to_version => '5.0', + }); + + ok( $vh, 'VersionHandler gets instantiated' ); + ok( eq_array( $vh->next_version_set, [qw( 1.0 5.0 )] ), 'db version and to_version get correctly put into version set'); + ok( !$vh->next_version_set, 'next_version_set only works once'); + ok( !$vh->next_version_set, 'seriously.'); +} + +{ + my $vh = DBIx::Class::DeploymentHandler::DatabaseToSchemaVersions->new({ + schema => $s, + ordered_versions => $versions, + }); + + ok( $vh, 'VersionHandler gets instantiated' ); + ok( !$vh->next_version_set, 'VersionHandler is null when schema_version and db_verison are the same' ); +} + +{ + my $vh = DBIx::Class::DeploymentHandler::DatabaseToSchemaVersions->new({ + schema => $s, + ordered_versions => $versions, + }); + + ok( $vh, 'VersionHandler gets instantiated' ); + ok( !$vh->next_version_set, 'VersionHandler is null when schema_version and db_verison are the same' ); +} + +{ + $DBICVersion::Schema::VERSION = '10.0'; + + my $vh = DBIx::Class::DeploymentHandler::DatabaseToSchemaVersions->new({ + schema => $s, + ordered_versions => $versions, + }); + + ok( $vh, 'VersionHandler gets instantiated' ); + ok( eq_array( $vh->next_version_set, [qw( 1.0 10.0 )] ), 'db version and schema version get correctly put into version set'); + ok( !$vh->next_version_set, 'VersionHandler is null on next try' ); +} + +done_testing; +__END__ + +vim: ts=2 sw=2 expandtab