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