# a single with would be better, but we can't do that
# see: http://rt.cpan.org/Public/Bug/Display.html?id=46347
with 'DBIx::Class::DeploymentHandler::WithSqltDeployMethod',
- 'DBIx::Class::DeploymentHandler::WithDatabaseToSchemaVersions',
+ 'DBIx::Class::DeploymentHandler::WithMonotonicVersions',
'DBIx::Class::DeploymentHandler::WithStandardVersionStorage';
with 'DBIx::Class::DeploymentHandler::WithReasonableDefaults';
--- /dev/null
+package DBIx::Class::DeploymentHandler::WithMonotonicVersions;
+use Moose::Role;
+
+use DBIx::Class::DeploymentHandler::VersionHandler::Monotonic;
+
+has version_handler => (
+ is => 'ro',
+ lazy_build => 1,
+ does => 'DBIx::Class::DeploymentHandler::HandlesVersioning',
+ handles => 'DBIx::Class::DeploymentHandler::HandlesVersioning',
+);
+
+sub _build_version_handler {
+ my $self = shift;
+
+ my $args = {
+ database_version => $self->database_version,
+ schema_version => $self->schema_version,
+ };
+
+ $args->{to_version} = $self->to_version if $self->has_to_version;
+ DBIx::Class::DeploymentHandler::VersionHandler::Monotonic->new($args);
+}
+
+1;
+
+__END__
+
+vim: ts=2 sw=2 expandtab
requires qw( prepare_upgrade prepare_downgrade database_version schema_version );
-around qw( prepare_upgrade prepare_downgrade ) => sub {
+around prepare_upgrade => sub {
my $orig = shift;
my $self = shift;
};
+around prepare_downgrade => sub {
+ my $orig = shift;
+ my $self = shift;
+
+ my $from_version = shift || $self->database_version;
+ my $to_version = shift || $self->schema_version;
+ my $version_set = shift || [$to_version, $from_version];
+
+ $self->$orig($from_version, $to_version, $version_set);
+};
+
+
1;
__END__
#!perl
+use strict;
+use warnings;
+
use lib 't/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',
+ sqltargs => { add_drop_table => 0 },
+ });
+
+ ok($handler, 'DBIx::Class::DeploymentHandler w/1 instantiates correctly');
+
+ my $version = $s->schema_version;
+ $handler->prepare_install;
+
+ 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_install();
+ $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_install;
+ $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');
-DBICDHTest::test_bundle(DBIx::Class::DeploymentHandler);
+ 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;
use lib 't/lib';
use DBICDHTest;
-use DBIx::Class::DeploymentHandler::Deprecated;
+use aliased 'DBIx::Class::DeploymentHandler::Deprecated';
+
+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';
+
+ready;
+
+VERSION1: {
+ use_ok 'DBICVersion_v1';
+ my $s = DBICVersion::Schema->connect(@connection);
+ is $s->schema_version, '1.0', 'schema version is at 1.0';
+ ok($s, 'DBICVersion::Schema 1.0 instantiates correctly');
+ my $handler = Deprecated->new({
+ upgrade_directory => $sql_dir,
+ schema => $s,
+ databases => 'SQLite',
+ sqltargs => { add_drop_table => 0 },
+ });
+
+ ok($handler, 'DBIx::Class::DeploymentHandler w/1.0 instantiates correctly');
+
+ my $version = $s->schema_version();
+ $handler->prepare_install();
+
+ 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);
+ is $s->schema_version, '2.0', 'schema version is at 2.0';
+ ok($s, 'DBICVersion::Schema 2.0 instantiates correctly');
+ my $handler = Deprecated->new({
+ upgrade_directory => $sql_dir,
+ schema => $s,
+ databases => 'SQLite',
+ });
+
+ ok($handler, 'DBIx::Class::DeploymentHandler w/2.0 instantiates correctly');
+
+ my $version = $s->schema_version();
+ $handler->prepare_install();
+ $handler->prepare_upgrade('1.0', $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);
+ is $s->schema_version, '3.0', 'schema version is at 3.0';
+ ok($s, 'DBICVersion::Schema 3.0 instantiates correctly');
+ my $handler = Deprecated->new({
+ upgrade_directory => $sql_dir,
+ schema => $s,
+ databases => 'SQLite',
+ });
+
+ ok($handler, 'DBIx::Class::DeploymentHandler w/3.0 instantiates correctly');
-DBICDHTest::test_bundle(DBIx::Class::DeploymentHandler::Deprecated);
+ my $version = $s->schema_version();
+ $handler->prepare_install;
+ $handler->prepare_upgrade( '2.0', $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';
+}
done_testing;
}
}
-sub test_bundle {
- my $bundle = shift;
- my $db = 'dbi:SQLite:db.db';
- my @connection = ($db, '', '', { ignore_version => 1 });
- my $sql_dir = 't/sql';
-
- ready;
-
- VERSION1: {
- use_ok 'DBICVersion_v1';
- my $s = DBICVersion::Schema->connect(@connection);
- is $s->schema_version, '1.0', 'schema version is at 1.0';
- ok($s, 'DBICVersion::Schema 1.0 instantiates correctly');
- my $handler = $bundle->new({
- upgrade_directory => $sql_dir,
- schema => $s,
- databases => 'SQLite',
- sqltargs => { add_drop_table => 0 },
- });
-
- ok($handler, 'DBIx::Class::DeploymentHandler w/1.0 instantiates correctly');
-
- my $version = $s->schema_version();
- $handler->prepare_install();
-
- 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);
- is $s->schema_version, '2.0', 'schema version is at 2.0';
- ok($s, 'DBICVersion::Schema 2.0 instantiates correctly');
- my $handler = $bundle->new({
- upgrade_directory => $sql_dir,
- schema => $s,
- databases => 'SQLite',
- });
-
- ok($handler, 'DBIx::Class::DeploymentHandler w/2.0 instantiates correctly');
-
- my $version = $s->schema_version();
- $handler->prepare_install();
- $handler->prepare_upgrade('1.0', $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);
- is $s->schema_version, '3.0', 'schema version is at 3.0';
- ok($s, 'DBICVersion::Schema 3.0 instantiates correctly');
- my $handler = $bundle->new({
- upgrade_directory => $sql_dir,
- schema => $s,
- databases => 'SQLite',
- });
-
- ok($handler, 'DBIx::Class::DeploymentHandler w/3.0 instantiates correctly');
-
- my $version = $s->schema_version();
- $handler->prepare_install;
- $handler->prepare_upgrade( '2.0', $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);
- is $s->schema_version, '2.0', 'schema version is at 2.0';
- ok($s, 'DBICVersion::Schema 2.0 instantiates correctly');
- my $handler = $bundle->new({
- upgrade_directory => $sql_dir,
- schema => $s,
- databases => 'SQLite',
- });
-
- ok($handler, 'DBIx::Class::DeploymentHandler w/2.0 instantiates correctly');
-
- my $version = $s->schema_version();
- $handler->prepare_downgrade('3.0', $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';
- }
-}
-
-
1;
database_version => '1.0',
});
$vh->next_version_set;
-} 'cannot request a version before the current version';
+} 'cannot request an upgrade before the current version';
+
+dies_ok {
+ my $vh = ExplicitVersions->new({
+ ordered_versions => $versions,
+ to_version => '2.0',
+ schema_version => '1.0',
+ database_version => '1.0',
+ });
+ $vh->previous_version_set;
+} 'cannot request a downgrade after the current version';
done_testing;
#vim: ts=2 sw=2 expandtab