doc for HandlesDeploy
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / VersionHandler / ExplicitVersions.pm
CommitLineData
c703d15d 1package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions;
e70a1600 2use Moose;
2c627d9e 3use Carp 'croak';
e70a1600 4
24794769 5with 'DBIx::Class::DeploymentHandler::HandlesVersioning';
2c627d9e 6
b539a216 7has schema_version => (
8 isa => 'Str',
9 is => 'ro',
10 required => 1,
11);
12
13has database_version => (
14 isa => 'Str',
15 is => 'ro',
16 required => 1,
17);
18
5232e8d0 19has to_version => (
b539a216 20 is => 'ro',
5232e8d0 21 lazy_build => 1,
b539a216 22);
23
24sub _build_to_version { $_[0]->schema_version }
25
2c627d9e 26has ordered_versions => (
27 is => 'ro',
28 isa => 'ArrayRef',
29 required => 1,
2c627d9e 30);
31
8fdf6269 32has _index_of_versions => (
33 is => 'ro',
34 isa => 'HashRef',
35 lazy_build => 1,
36);
37
38sub _build__index_of_versions {
39 my %ret;
40 my $i = 0;
41 for (@{ $_[0]->ordered_versions }) {
42 $ret{$_} = $i++;
43 }
44 \%ret;
45}
46
2c627d9e 47has _version_idx => (
48 is => 'rw',
49 isa => 'Int',
50 lazy_build => 1,
51);
52
8fdf6269 53sub _build__version_idx { $_[0]->_index_of_versions->{$_[0]->database_version} }
54
58a5e27f 55sub _inc_version_idx { $_[0]->_version_idx($_[0]->_version_idx + 1 ) }
f344dd91 56sub _dec_version_idx { $_[0]->_version_idx($_[0]->_version_idx - 1 ) }
2c627d9e 57
2c627d9e 58
58a5e27f 59sub next_version_set {
24794769 60 my $self = shift;
8fdf6269 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 }
2c627d9e 77}
e70a1600 78
f344dd91 79sub previous_version_set {
80 my $self = shift;
8fdf6269 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 }
f344dd91 97}
98
e70a1600 99__PACKAGE__->meta->make_immutable;
100
1011;
102
103__END__
104
105vim: ts=2 sw=2 expandtab