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