cleanup modelines
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / VersionHandler / ExplicitVersions.pm
1 package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions;
2 use Moose;
3 use Carp 'croak';
4
5 with 'DBIx::Class::DeploymentHandler::HandlesVersioning';
6
7 has schema_version => (
8   isa      => 'Str',
9   is       => 'ro',
10   required => 1,
11 );
12
13 has database_version => (
14   isa      => 'Str',
15   is       => 'ro',
16   required => 1,
17 );
18
19 has to_version => (
20   is         => 'ro',
21   lazy_build => 1,
22 );
23
24 sub _build_to_version { $_[0]->schema_version }
25
26 has ordered_versions => (
27   is       => 'ro',
28   isa      => 'ArrayRef',
29   required => 1,
30 );
31
32 has _index_of_versions => (
33   is         => 'ro',
34   isa        => 'HashRef',
35   lazy_build => 1,
36 );
37
38 sub _build__index_of_versions {
39   my %ret;
40   my $i = 0;
41   for (@{ $_[0]->ordered_versions }) {
42     $ret{$_} = $i++;
43   }
44   \%ret;
45 }
46
47 has _version_idx => (
48   is         => 'rw',
49   isa        => 'Int',
50   lazy_build => 1,
51 );
52
53 sub _build__version_idx { $_[0]->_index_of_versions->{$_[0]->database_version} }
54
55 sub _inc_version_idx { $_[0]->_version_idx($_[0]->_version_idx + 1 ) }
56 sub _dec_version_idx { $_[0]->_version_idx($_[0]->_version_idx - 1 ) }
57
58
59 sub next_version_set {
60   my $self = shift;
61   if (
62     $self->_index_of_versions->{$self->to_version} <
63     $self->_version_idx
64   ) {
65     croak "you are trying to upgrade and your current version is greater\n".
66           "than the version you are trying to upgrade to.  Either downgrade\n".
67           "or update your schema"
68   } elsif ( $self->_version_idx == $self->_index_of_versions->{$self->to_version}) {
69     return undef
70   } else {
71     my $next_idx = $self->_inc_version_idx;
72     return [
73       $self->ordered_versions->[$next_idx - 1],
74       $self->ordered_versions->[$next_idx    ],
75     ];
76   }
77 }
78
79 sub previous_version_set {
80   my $self = shift;
81   if (
82     $self->_index_of_versions->{$self->to_version} >
83     $self->_version_idx
84   ) {
85     croak "you are trying to downgrade and your current version is less\n".
86           "than the version you are trying to downgrade to.  Either upgrade\n".
87           "or update your schema"
88   } elsif ( $self->_version_idx == $self->_index_of_versions->{$self->to_version}) {
89     return undef
90   } else {
91     my $next_idx = $self->_dec_version_idx;
92     return [
93       $self->ordered_versions->[$next_idx    ],
94       $self->ordered_versions->[$next_idx + 1],
95     ];
96   }
97 }
98
99 __PACKAGE__->meta->make_immutable;
100
101 1;
102
103 # vim: ts=2 sw=2 expandtab
104
105 __END__
106