Port to Moo
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / VersionHandler / Monotonic.pm
CommitLineData
dab1797d 1package DBIx::Class::DeploymentHandler::VersionHandler::Monotonic;
a976d6e4 2use Moo;
3use MooX::Types::MooseLike::Base qw(Int);
9deabd1f 4
5# ABSTRACT: Obvious version progressions
6
dab1797d 7use Carp 'croak';
8
9with 'DBIx::Class::DeploymentHandler::HandlesVersioning';
10
11has schema_version => (
a976d6e4 12 isa => Int,
dab1797d 13 is => 'ro',
14 required => 1,
15);
16
17has database_version => (
a976d6e4 18 isa => Int,
dab1797d 19 is => 'ro',
20 required => 1,
21);
22
23has to_version => (
a976d6e4 24 isa => Int,
dab1797d 25 is => 'ro',
a976d6e4 26 lazy => 1,
27 builder => '_build_to_version',
dab1797d 28);
29
30sub _build_to_version { $_[0]->schema_version }
31
32has _version => (
33 is => 'rw',
a976d6e4 34 isa => Int,
35 lazy => 1,
36 builder => '_build__version',
dab1797d 37);
38
dab1797d 39sub _inc_version { $_[0]->_version($_[0]->_version + 1 ) }
40sub _dec_version { $_[0]->_version($_[0]->_version - 1 ) }
41
42sub _build__version { $_[0]->database_version }
43
44sub previous_version_set {
45 my $self = shift;
df0fcae9 46 if ($self->to_version > $self->_version) {
1bf789ff 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;
48c3a562 54 return [$self->_version + 1, $self->_version];
1bf789ff 55 }
dab1797d 56}
57
58sub next_version_set {
59 my $self = shift;
df0fcae9 60 if ($self->to_version < $self->_version) {
1bf789ff 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 }
dab1797d 70}
71
dab1797d 721;
73
e52174e3 74# vim: ts=2 sw=2 expandtab
75
dab1797d 76__END__
77
ec167a97 78=head1 SEE ALSO
79
80This class is an implementation of
81L<DBIx::Class::DeploymentHandler::HandlesVersioning>. Pretty much all the
82documentation is there.