Schemata don't have to use component to use DBICDH
Arthur Axel 'fREW' Schmidt [Sun, 16 May 2010 03:33:25 +0000 (22:33 -0500)]
Changes
lib/DBIx/Class/DeploymentHandler.pm
lib/DBIx/Class/DeploymentHandler/VersionStorage/Standard.pm
t/02-instantiation-wo-component.t [new file with mode: 0644]
t/no-component-lib/DBICDHTest.pm [new file with mode: 0644]
t/no-component-lib/DBICVersion_v1.pm [new file with mode: 0644]
t/no-component-lib/DBICVersion_v2.pm [new file with mode: 0644]
t/no-component-lib/DBICVersion_v3.pm [new file with mode: 0644]
t/no-component-lib/DBICVersion_v4.pm [new file with mode: 0644]

diff --git a/Changes b/Changes
index ef05a08..422503e 100644 (file)
--- 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
index 44cc22f..155db1f 100644 (file)
@@ -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;
index 3da6506..908ba07 100644 (file)
@@ -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 (file)
index 0000000..f3ea2ac
--- /dev/null
@@ -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 (file)
index 0000000..7960e05
--- /dev/null
@@ -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 (file)
index 0000000..4e3b51d
--- /dev/null
@@ -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 (file)
index 0000000..06e2c90
--- /dev/null
@@ -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 (file)
index 0000000..4cd43f0
--- /dev/null
@@ -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 (file)
index 0000000..06e2c90
--- /dev/null
@@ -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;