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