Port to Moo
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / VersionHandler / ExplicitVersions.pm
1 package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions;
2 use Moo;
3 use MooX::Types::MooseLike::Base qw(Str ArrayRef Int HashRef);
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   builder => '_build_to_version',
27   lazy => 1,
28 );
29
30 sub _build_to_version { $_[0]->schema_version }
31
32 has ordered_versions => (
33   is       => 'ro',
34   isa      => ArrayRef,
35   required => 1,
36 );
37
38 has _index_of_versions => (
39   is         => 'ro',
40   isa        => HashRef,
41   builder => '_build__index_of_versions',
42   lazy => 1,
43 );
44
45 sub _build__index_of_versions {
46   my %ret;
47   my $i = 0;
48   for (@{ $_[0]->ordered_versions }) {
49     $ret{$_} = $i++;
50   }
51   \%ret;
52 }
53
54 has _version_idx => (
55   is         => 'rw',
56   isa        => Int,
57   builder => '_build__version_idx',
58   lazy => 1,
59 );
60
61 sub _build__version_idx { $_[0]->_index_of_versions->{$_[0]->database_version} }
62
63 sub _inc_version_idx { $_[0]->_version_idx($_[0]->_version_idx + 1 ) }
64 sub _dec_version_idx { $_[0]->_version_idx($_[0]->_version_idx - 1 ) }
65
66
67 sub next_version_set {
68   my $self = shift;
69   if (
70     $self->_index_of_versions->{$self->to_version} <
71     $self->_version_idx
72   ) {
73     croak "you are trying to upgrade and your current version is greater\n".
74           "than the version you are trying to upgrade to.  Either downgrade\n".
75           "or update your schema"
76   } elsif ( $self->_version_idx == $self->_index_of_versions->{$self->to_version}) {
77     return undef
78   } else {
79     my $next_idx = $self->_inc_version_idx;
80     return [
81       $self->ordered_versions->[$next_idx - 1],
82       $self->ordered_versions->[$next_idx    ],
83     ];
84   }
85 }
86
87 sub previous_version_set {
88   my $self = shift;
89   if (
90     $self->_index_of_versions->{$self->to_version} >
91     $self->_version_idx
92   ) {
93     croak "you are trying to downgrade and your current version is less\n".
94           "than the version you are trying to downgrade to.  Either upgrade\n".
95           "or update your schema"
96   } elsif ( $self->_version_idx == $self->_index_of_versions->{$self->to_version}) {
97     return undef
98   } else {
99     my $next_idx = $self->_dec_version_idx;
100     return [
101       $self->ordered_versions->[$next_idx + 1],
102       $self->ordered_versions->[$next_idx    ],
103     ];
104   }
105 }
106
107 1;
108
109 # vim: ts=2 sw=2 expandtab
110
111 __END__
112
113 =head1 SEE ALSO
114
115 This class is an implementation of
116 L<DBIx::Class::DeploymentHandler::HandlesVersioning>.  Pretty much all the
117 documentation is there.