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