use VersionStorage in the rest of our stuff
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / VersionHandler / ExplicitVersions.pm
1 package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions;
2 use Moose;
3 use Method::Signatures::Simple;
4 use Carp 'croak';
5
6 with 'DBIx::Class::DeploymentHandler::HandlesVersioning';
7
8 has ordered_versions => (
9   is       => 'ro',
10   isa      => 'ArrayRef',
11   required => 1,
12   trigger  => sub {
13     my $to_version = $_[0]->to_version;
14     my $db_version = $_[0]->database_version;
15
16     croak 'to_version not in ordered_versions'
17       unless grep { $to_version eq $_ } @{ $_[1] };
18
19     for (@{ $_[1] }) {
20       return if $_ eq $db_version;
21       croak 'to_version is before database version in ordered_versions'
22         if $_ eq $to_version;
23     }
24   },
25 );
26
27 has _version_idx => (
28   is         => 'rw',
29   isa        => 'Int',
30   lazy_build => 1,
31 );
32
33 method _inc_version_idx { $self->_version_idx($self->_version_idx + 1 ) }
34
35 method _build__version_idx {
36   my $start = $self->database_version;
37   my $idx = 0;
38   for (@{$self->ordered_versions}) {
39     return $idx
40       if $_ eq $self->database_version;
41     $idx++;
42   }
43   croak 'database version not found in ordered_versions!';
44 }
45
46 sub next_version_set { # sub instead of method because of when roles get composed
47   my $self = shift;
48   return undef
49     if $self->ordered_versions->[$self->_version_idx] eq $self->to_version;
50   my $next_idx = $self->_inc_version_idx;
51   if ( $next_idx <= $#{ $self->ordered_versions }) {
52     return [
53       $self->ordered_versions->[$next_idx - 1],
54       $self->ordered_versions->[$next_idx    ],
55     ]
56   } else {
57     return undef
58   }
59 }
60
61 __PACKAGE__->meta->make_immutable;
62
63 1;
64
65 __END__
66
67 vim: ts=2 sw=2 expandtab