initial monotonic commit
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / VersionHandler / Monotonic.pm
1 package DBIx::Class::DeploymentHandler::VersionHandler::Monotonic;
2 use Moose;
3 use Carp 'croak';
4
5 with 'DBIx::Class::DeploymentHandler::HandlesVersioning';
6
7 has schema_version => (
8   isa      => 'Int',
9   is       => 'ro',
10   required => 1,
11 );
12
13 has database_version => (
14   isa      => 'Int',
15   is       => 'ro',
16   required => 1,
17 );
18
19 has to_version => (
20   isa        => 'Int',
21   is         => 'ro',
22   lazy_build => 1,
23 );
24
25 sub _build_to_version { $_[0]->schema_version }
26
27 has _version => (
28   is         => 'rw',
29   isa        => 'Int',
30   lazy_build => 1,
31 );
32
33 sub BUILD {
34   croak "you are trying to upgrade and your current version is greater\n".
35         "than the version you are trying to upgrade to.  Either downgrade\n".
36         "or update your schema" if $_[0]->to_version < $_[0]->_version;
37 }
38
39 sub _inc_version { $_[0]->_version($_[0]->_version + 1 ) }
40 sub _dec_version { $_[0]->_version($_[0]->_version - 1 ) }
41
42 sub _build__version { $_[0]->database_version }
43
44 sub previous_version_set {
45   my $self = shift;
46   return undef
47     if $self->to_version == $self->_version;
48
49   $self->_dec_version;
50   return [$self->_version, $self->_version + 1];
51 }
52
53 sub next_version_set {
54   my $self = shift;
55   return undef
56     if $self->to_version == $self->_version;
57
58   $self->_inc_version;
59   return [$self->_version - 1, $self->_version];
60 }
61
62 __PACKAGE__->meta->make_immutable;
63
64 1;
65
66 __END__
67
68 vim: ts=2 sw=2 expandtab