From: Arthur Axel 'fREW' Schmidt Date: Sat, 27 Feb 2010 08:41:09 +0000 (-0600) Subject: initial version of ExplicitVersions VH X-Git-Tag: v0.001000_01~117 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2c627d9e87a083870f6e67c63000363e6726dfc9;p=dbsrgits%2FDBIx-Class-DeploymentHandler.git initial version of ExplicitVersions VH --- diff --git a/lib/DBIx/Class/DeploymentHandler/ExplicitVersions.pm b/lib/DBIx/Class/DeploymentHandler/ExplicitVersions.pm index da87886..6c83d3b 100644 --- a/lib/DBIx/Class/DeploymentHandler/ExplicitVersions.pm +++ b/lib/DBIx/Class/DeploymentHandler/ExplicitVersions.pm @@ -1,8 +1,83 @@ package DBIx::Class::DeploymentHandler::ExplicitVersions; use Moose; use Method::Signatures::Simple; +use Carp 'croak'; -method ordered_schema_versions { undef } +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, +); + +has ordered_versions => ( + is => 'ro', + isa => 'ArrayRef', + required => 1, + trigger => sub { + my $to_version = $_[0]->to_version; + my $db_version = $_[0]->db_version; + + croak 'to_version not in ordered_versions' + unless grep { $to_version eq $_ } @{ $_[1] }; + + for (@{ $_[1] }) { + return if $_ eq $db_version; + croak 'to_version is before database version in ordered_versions' + if $_ eq $to_version; + } + }, +); + +has _version_idx => ( + is => 'rw', + isa => 'Int', + lazy_build => 1, +); + +method _inc_version_idx { $self->_version_idx($self->_version_idx + 1 ) } + +method _build__version_idx { + my $start = $self->version_rs->db_version; + my $idx = 0; + for (@{$self->ordered_versions}) { + return $idx + if $_ eq $self->db_version; + $idx++; + } + croak 'database version not found in ordered_versions!'; +} + +method next_version_set { + return undef + if $self->ordered_versions->[$self->_version_idx] eq $self->to_version; + my $next_idx = $self->_inc_version_idx; + if ( $next_idx <= $#{ $self->ordered_versions }) { + return [ + $self->ordered_versions->[$next_idx - 1], + $self->ordered_versions->[$next_idx ], + ] + } else { + return undef + } +} __PACKAGE__->meta->make_immutable; diff --git a/t/03_explict_versions.t b/t/03_explict_versions.t new file mode 100644 index 0000000..1c5c05a --- /dev/null +++ b/t/03_explict_versions.t @@ -0,0 +1,77 @@ +#!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 $versions = [map "$_.0", 0..100]; + +{ + my $vh = DBIx::Class::DeploymentHandler::ExplicitVersions->new({ + schema => $s, + ordered_versions => $versions, + to_version => '1.0', + }); + + ok $vh, 'VersionHandler gets instantiated'; + + ok( !$vh->next_version_set, 'next version set returns undef if we are at the version requested' ); +} + +{ + my $vh = DBIx::Class::DeploymentHandler::ExplicitVersions->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 2.0 )]), 'first version pair works' ); + ok( eq_array($vh->next_version_set, [qw( 2.0 3.0 )]), 'second version pair works' ); + ok( eq_array($vh->next_version_set, [qw( 3.0 4.0 )]), 'third version pair works' ); + ok( eq_array($vh->next_version_set, [qw( 4.0 5.0 )]), 'fourth version pair works' ); + ok( !$vh->next_version_set, 'no more versions after final pair' ); + ok( !$vh->next_version_set, 'still no more versions after final pair' ); +} + +dies_ok { + my $vh = DBIx::Class::DeploymentHandler::ExplicitVersions->new({ + schema => $s, + ordered_versions => $versions, + to_version => '0.0', + }); +} 'cannot request a version before the current version'; + +done_testing; +__END__ + +vim: ts=2 sw=2 expandtab