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