working downgrades!
[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
19has to_version => ( # configuration
20 is => 'ro',
21 lazy_build => 1, # builder comes from another role...
22 # which is... probably not how we want it
23);
24
25sub _build_to_version { $_[0]->schema_version }
26
2c627d9e 27has ordered_versions => (
28 is => 'ro',
29 isa => 'ArrayRef',
30 required => 1,
31 trigger => sub {
32 my $to_version = $_[0]->to_version;
fb105cfa 33 my $db_version = $_[0]->database_version;
2c627d9e 34
35 croak 'to_version not in ordered_versions'
36 unless grep { $to_version eq $_ } @{ $_[1] };
37
58a5e27f 38 croak 'database_version not in ordered_versions'
39 unless grep { $db_version eq $_ } @{ $_[1] };
40
2c627d9e 41 for (@{ $_[1] }) {
42 return if $_ eq $db_version;
43 croak 'to_version is before database version in ordered_versions'
44 if $_ eq $to_version;
45 }
46 },
47);
48
49has _version_idx => (
50 is => 'rw',
51 isa => 'Int',
52 lazy_build => 1,
53);
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
58a5e27f 58sub _build__version_idx {
59 my $self = shift;
fb105cfa 60 my $start = $self->database_version;
2c627d9e 61 my $idx = 0;
62 for (@{$self->ordered_versions}) {
63 return $idx
fb105cfa 64 if $_ eq $self->database_version;
2c627d9e 65 $idx++;
66 }
2c627d9e 67}
68
58a5e27f 69sub next_version_set {
24794769 70 my $self = shift;
2c627d9e 71 return undef
72 if $self->ordered_versions->[$self->_version_idx] eq $self->to_version;
a3bc8ff9 73
58a5e27f 74 # this should never get in infinite loops because we ensure
75 # that the database version is in the list in the version_idx
76 # builder
2c627d9e 77 my $next_idx = $self->_inc_version_idx;
a3bc8ff9 78 return [
79 $self->ordered_versions->[$next_idx - 1],
80 $self->ordered_versions->[$next_idx ],
58a5e27f 81 ];
2c627d9e 82}
e70a1600 83
f344dd91 84sub previous_version_set {
85 my $self = shift;
86 return undef
87 if $self->ordered_versions->[$self->_version_idx] eq $self->database_version;
88
89 # this should never get in infinite loops because we ensure
90 # that the database version is in the list in the version_idx
91 # builder
92 my $next_idx = $self->_dec_version_idx;
93 return [
94 $self->ordered_versions->[$next_idx - 1],
95 $self->ordered_versions->[$next_idx ],
96 ];
97}
98
e70a1600 99__PACKAGE__->meta->make_immutable;
100
1011;
102
103__END__
104
105vim: ts=2 sw=2 expandtab