more tests for ExplicitVersions
[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 schema_version => (
9   isa      => 'Str',
10   is       => 'ro',
11   required => 1,
12 );
13
14 has database_version => (
15   isa      => 'Str',
16   is       => 'ro',
17   required => 1,
18 );
19
20 has to_version => ( # configuration
21   is         => 'ro',
22   lazy_build => 1, # builder comes from another role...
23                    # which is... probably not how we want it
24 );
25
26 sub _build_to_version { $_[0]->schema_version }
27
28 has ordered_versions => (
29   is       => 'ro',
30   isa      => 'ArrayRef',
31   required => 1,
32   trigger  => sub {
33     my $to_version = $_[0]->to_version;
34     my $db_version = $_[0]->database_version;
35
36     croak 'to_version not in ordered_versions'
37       unless grep { $to_version eq $_ } @{ $_[1] };
38
39     for (@{ $_[1] }) {
40       return if $_ eq $db_version;
41       croak 'to_version is before database version in ordered_versions'
42         if $_ eq $to_version;
43     }
44   },
45 );
46
47 has _version_idx => (
48   is         => 'rw',
49   isa        => 'Int',
50   lazy_build => 1,
51 );
52
53 method _inc_version_idx { $self->_version_idx($self->_version_idx + 1 ) }
54
55 method _build__version_idx {
56   my $start = $self->database_version;
57   my $idx = 0;
58   for (@{$self->ordered_versions}) {
59     return $idx
60       if $_ eq $self->database_version;
61     $idx++;
62   }
63   croak 'database version not found in ordered_versions!';
64 }
65
66 sub next_version_set { # sub instead of method because of when roles get composed
67   my $self = shift;
68   return undef
69     if $self->ordered_versions->[$self->_version_idx] eq $self->to_version;
70
71   my $next_idx = $self->_inc_version_idx;
72   return [
73     $self->ordered_versions->[$next_idx - 1],
74     $self->ordered_versions->[$next_idx    ],
75   ] if $next_idx <= $#{ $self->ordered_versions };
76
77   croak 'this should never happen';
78 }
79
80 __PACKAGE__->meta->make_immutable;
81
82 1;
83
84 __END__
85
86 vim: ts=2 sw=2 expandtab