add missing newline for no-linenumber-change dzil
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / VersionHandler / ExplicitVersions.pm
CommitLineData
c703d15d 1package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions;
97aa9a74 2
e70a1600 3use Moose;
9deabd1f 4
5# ABSTRACT: Define your own list of versions to use for migrations
6
2c627d9e 7use Carp 'croak';
e70a1600 8
24794769 9with 'DBIx::Class::DeploymentHandler::HandlesVersioning';
2c627d9e 10
b539a216 11has schema_version => (
12 isa => 'Str',
13 is => 'ro',
14 required => 1,
15);
16
17has database_version => (
18 isa => 'Str',
19 is => 'ro',
20 required => 1,
21);
22
5232e8d0 23has to_version => (
b539a216 24 is => 'ro',
e86c0c07 25 isa => 'Str',
5232e8d0 26 lazy_build => 1,
b539a216 27);
28
29sub _build_to_version { $_[0]->schema_version }
30
2c627d9e 31has ordered_versions => (
32 is => 'ro',
33 isa => 'ArrayRef',
34 required => 1,
2c627d9e 35);
36
8fdf6269 37has _index_of_versions => (
38 is => 'ro',
39 isa => 'HashRef',
40 lazy_build => 1,
41);
42
43sub _build__index_of_versions {
44 my %ret;
45 my $i = 0;
46 for (@{ $_[0]->ordered_versions }) {
47 $ret{$_} = $i++;
48 }
49 \%ret;
50}
51
2c627d9e 52has _version_idx => (
53 is => 'rw',
54 isa => 'Int',
55 lazy_build => 1,
56);
57
8fdf6269 58sub _build__version_idx { $_[0]->_index_of_versions->{$_[0]->database_version} }
59
58a5e27f 60sub _inc_version_idx { $_[0]->_version_idx($_[0]->_version_idx + 1 ) }
f344dd91 61sub _dec_version_idx { $_[0]->_version_idx($_[0]->_version_idx - 1 ) }
2c627d9e 62
2c627d9e 63
58a5e27f 64sub next_version_set {
24794769 65 my $self = shift;
8fdf6269 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 }
2c627d9e 82}
e70a1600 83
f344dd91 84sub previous_version_set {
85 my $self = shift;
8fdf6269 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 [
8fdf6269 98 $self->ordered_versions->[$next_idx + 1],
9b620a81 99 $self->ordered_versions->[$next_idx ],
8fdf6269 100 ];
101 }
f344dd91 102}
103
e70a1600 104__PACKAGE__->meta->make_immutable;
105
1061;
107
e52174e3 108# vim: ts=2 sw=2 expandtab
109
e70a1600 110__END__
111
ec167a97 112=head1 SEE ALSO
113
114This class is an implementation of
115L<DBIx::Class::DeploymentHandler::HandlesVersioning>. Pretty much all the
116documentation is there.