From: Arthur Axel 'fREW' Schmidt Date: Sun, 16 May 2010 03:33:25 +0000 (-0500) Subject: Schemata don't have to use component to use DBICDH X-Git-Tag: v0.001000_09~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=53de57ed75cc2baf105fd7013769b0c6572c6dbe;p=dbsrgits%2FDBIx-Class-DeploymentHandler.git Schemata don't have to use component to use DBICDH --- diff --git a/Changes b/Changes index ef05a08..422503e 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for {{$dist->name}} {{$NEXT}} + - Schemata is no longer required to add version checking component 0.001000_08 2010-05-11 22:42:20 CST6CDT - Add missing dep namespace::autoclean diff --git a/lib/DBIx/Class/DeploymentHandler.pm b/lib/DBIx/Class/DeploymentHandler.pm index 44cc22f..155db1f 100644 --- a/lib/DBIx/Class/DeploymentHandler.pm +++ b/lib/DBIx/Class/DeploymentHandler.pm @@ -49,6 +49,9 @@ sub prepare_install { $_[0]->prepare_version_storage_install; } +# the following is just a hack so that ->version_storage +# won't be lazy +sub BUILD { $_[0]->version_storage } __PACKAGE__->meta->make_immutable; 1; diff --git a/lib/DBIx/Class/DeploymentHandler/VersionStorage/Standard.pm b/lib/DBIx/Class/DeploymentHandler/VersionStorage/Standard.pm index 3da6506..908ba07 100644 --- a/lib/DBIx/Class/DeploymentHandler/VersionStorage/Standard.pm +++ b/lib/DBIx/Class/DeploymentHandler/VersionStorage/Standard.pm @@ -4,6 +4,7 @@ use Moose; # ABSTRACT: Version storage that does the normal stuff use Method::Signatures::Simple; +use DBIx::Class::DeploymentHandler::VersionStorage::Standard::VersionResult; has schema => ( isa => 'DBIx::Class::Schema', @@ -14,7 +15,7 @@ has schema => ( has version_rs => ( isa => 'DBIx::Class::ResultSet', is => 'ro', - lazy_build => 1, + builder => '_build_version_rs', handles => [qw( database_version version_storage_is_installed )], ); diff --git a/t/02-instantiation-wo-component.t b/t/02-instantiation-wo-component.t new file mode 100644 index 0000000..f3ea2ac --- /dev/null +++ b/t/02-instantiation-wo-component.t @@ -0,0 +1,162 @@ +#!perl + +use strict; +use warnings; + +use lib 't/no-component-lib'; +use DBICDHTest; +use DBIx::Class::DeploymentHandler; +use aliased 'DBIx::Class::DeploymentHandler', 'DH'; + +use File::Path 'remove_tree'; +use Test::More; +use Test::Exception; + +DBICDHTest::ready; + +my $db = 'dbi:SQLite:db.db'; +my @connection = ($db, '', '', { ignore_version => 1 }); +my $sql_dir = 't/sql'; + +VERSION1: { + use_ok 'DBICVersion_v1'; + my $s = DBICVersion::Schema->connect(@connection); + $DBICVersion::Schema::VERSION = 1; + ok($s, 'DBICVersion::Schema 1 instantiates correctly'); + my $handler = DH->new({ + upgrade_directory => $sql_dir, + schema => $s, + databases => 'SQLite', + sql_translator_args => { add_drop_table => 0 }, + }); + + ok($handler, 'DBIx::Class::DeploymentHandler w/1 instantiates correctly'); + + my $version = $s->schema_version; + $handler->prepare_deploy; + + dies_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + }) + } 'schema not deployed'; + $handler->install; + dies_ok { + $handler->install; + } 'cannot install twice'; + lives_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + }) + } 'schema is deployed'; +} + +VERSION2: { + use_ok 'DBICVersion_v2'; + my $s = DBICVersion::Schema->connect(@connection); + $DBICVersion::Schema::VERSION = 2; + ok($s, 'DBICVersion::Schema 2 instantiates correctly'); + my $handler = DH->new({ + upgrade_directory => $sql_dir, + schema => $s, + databases => 'SQLite', + }); + + ok($handler, 'DBIx::Class::DeploymentHandler w/2 instantiates correctly'); + + my $version = $s->schema_version(); + $handler->prepare_deploy(); + $handler->prepare_upgrade(1, $version); + dies_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + baz => 'frew', + }) + } 'schema not deployed'; + dies_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + baz => 'frew', + }) + } 'schema not uppgrayyed'; + $handler->upgrade; + lives_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + baz => 'frew', + }) + } 'schema is deployed'; +} + +VERSION3: { + use_ok 'DBICVersion_v3'; + my $s = DBICVersion::Schema->connect(@connection); + $DBICVersion::Schema::VERSION = 3; + ok($s, 'DBICVersion::Schema 3 instantiates correctly'); + my $handler = DH->new({ + upgrade_directory => $sql_dir, + schema => $s, + databases => 'SQLite', + }); + + ok($handler, 'DBIx::Class::DeploymentHandler w/3 instantiates correctly'); + + my $version = $s->schema_version(); + $handler->prepare_deploy; + $handler->prepare_upgrade( 2, $version ); + dies_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + baz => 'frew', + biff => 'frew', + }) + } 'schema not deployed'; + $handler->upgrade; + lives_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + baz => 'frew', + biff => 'frew', + }) + } 'schema is deployed'; +} + +DOWN2: { + use_ok 'DBICVersion_v4'; + my $s = DBICVersion::Schema->connect(@connection); + $DBICVersion::Schema::VERSION = 2; + ok($s, 'DBICVersion::Schema 2 instantiates correctly'); + my $handler = DH->new({ + upgrade_directory => $sql_dir, + schema => $s, + databases => 'SQLite', + }); + + ok($handler, 'DBIx::Class::DeploymentHandler w/2 instantiates correctly'); + + my $version = $s->schema_version(); + $handler->prepare_downgrade(3, $version); + lives_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + baz => 'frew', + biff => 'frew', + }) + } 'schema at version 3'; + $handler->downgrade; + dies_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + baz => 'frew', + biff => 'frew', + }) + } 'schema not at version 3'; + lives_ok { + $s->resultset('Foo')->create({ + bar => 'frew', + baz => 'frew', + }) + } 'schema is at version 2'; +} + +done_testing; diff --git a/t/no-component-lib/DBICDHTest.pm b/t/no-component-lib/DBICDHTest.pm new file mode 100644 index 0000000..7960e05 --- /dev/null +++ b/t/no-component-lib/DBICDHTest.pm @@ -0,0 +1,18 @@ +package DBICDHTest; + +use strict; +use warnings; + +use File::Path 'remove_tree'; +use Test::More; +use Test::Exception; + +sub ready { + unlink 'db.db' if -e 'db.db'; + if (-d 't/sql') { + remove_tree('t/sql'); + mkdir 't/sql'; + } +} + +1; diff --git a/t/no-component-lib/DBICVersion_v1.pm b/t/no-component-lib/DBICVersion_v1.pm new file mode 100644 index 0000000..4e3b51d --- /dev/null +++ b/t/no-component-lib/DBICVersion_v1.pm @@ -0,0 +1,31 @@ +package DBICVersion::Foo; + +use base 'DBIx::Class::Core'; +use strict; +use warnings; + +__PACKAGE__->table('Foo'); + +__PACKAGE__->add_columns( + foo => { + data_type => 'INTEGER', + is_auto_increment => 1, + }, + bar => { + data_type => 'VARCHAR', + size => '10' + }, +); + +__PACKAGE__->set_primary_key('foo'); + +package DBICVersion::Schema; +use base 'DBIx::Class::Schema'; +use strict; +use warnings; + +our $VERSION = '1.0'; + +__PACKAGE__->register_class('Foo', 'DBICVersion::Foo'); + +1; diff --git a/t/no-component-lib/DBICVersion_v2.pm b/t/no-component-lib/DBICVersion_v2.pm new file mode 100644 index 0000000..06e2c90 --- /dev/null +++ b/t/no-component-lib/DBICVersion_v2.pm @@ -0,0 +1,36 @@ +package DBICVersion::Foo; + +use base 'DBIx::Class::Core'; +use strict; +use warnings; + +__PACKAGE__->table('Foo'); + +__PACKAGE__->add_columns( + foo => { + data_type => 'INTEGER', + is_auto_increment => 1, + }, + bar => { + data_type => 'VARCHAR', + size => '10' + }, + baz => { + data_type => 'VARCHAR', + size => '10', + is_nullable => 1, + }, +); + +__PACKAGE__->set_primary_key('foo'); + +package DBICVersion::Schema; +use base 'DBIx::Class::Schema'; +use strict; +use warnings; + +our $VERSION = '2.0'; + +__PACKAGE__->register_class('Foo', 'DBICVersion::Foo'); + +1; diff --git a/t/no-component-lib/DBICVersion_v3.pm b/t/no-component-lib/DBICVersion_v3.pm new file mode 100644 index 0000000..4cd43f0 --- /dev/null +++ b/t/no-component-lib/DBICVersion_v3.pm @@ -0,0 +1,41 @@ +package DBICVersion::Foo; + +use base 'DBIx::Class::Core'; +use strict; +use warnings; + +__PACKAGE__->table('Foo'); + +__PACKAGE__->add_columns( + foo => { + data_type => 'INTEGER', + is_auto_increment => 1, + }, + bar => { + data_type => 'VARCHAR', + size => '10' + }, + baz => { + data_type => 'VARCHAR', + size => '10', + is_nullable => 1, + }, + biff => { + data_type => 'VARCHAR', + size => '10', + is_nullable => 1, + }, +); + +__PACKAGE__->set_primary_key('foo'); + +package DBICVersion::Schema; +use base 'DBIx::Class::Schema'; +use strict; +use warnings; + +our $VERSION = '3.0'; + +__PACKAGE__->register_class('Foo', 'DBICVersion::Foo'); + +1; diff --git a/t/no-component-lib/DBICVersion_v4.pm b/t/no-component-lib/DBICVersion_v4.pm new file mode 100644 index 0000000..06e2c90 --- /dev/null +++ b/t/no-component-lib/DBICVersion_v4.pm @@ -0,0 +1,36 @@ +package DBICVersion::Foo; + +use base 'DBIx::Class::Core'; +use strict; +use warnings; + +__PACKAGE__->table('Foo'); + +__PACKAGE__->add_columns( + foo => { + data_type => 'INTEGER', + is_auto_increment => 1, + }, + bar => { + data_type => 'VARCHAR', + size => '10' + }, + baz => { + data_type => 'VARCHAR', + size => '10', + is_nullable => 1, + }, +); + +__PACKAGE__->set_primary_key('foo'); + +package DBICVersion::Schema; +use base 'DBIx::Class::Schema'; +use strict; +use warnings; + +our $VERSION = '2.0'; + +__PACKAGE__->register_class('Foo', 'DBICVersion::Foo'); + +1;