initial version of ExplicitVersions VH
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / ExplicitVersions.pm
1 package DBIx::Class::DeploymentHandler::ExplicitVersions;
2 use Moose;
3 use Method::Signatures::Simple;
4 use Carp 'croak';
5
6 has schema => (
7   isa      => 'DBIx::Class::Schema',
8   is       => 'ro',
9   required => 1,
10   handles => [qw( ddl_filename schema_version )],
11 );
12
13 has version_rs => (
14   isa        => 'DBIx::Class::ResultSet',
15   is         => 'ro',
16   lazy_build => 1,
17   handles    => [qw( is_installed db_version )],
18 );
19
20 method _build_version_rs {
21    $self->schema->set_us_up_the_bomb;
22    $self->schema->resultset('__VERSION')
23 }
24
25 has to_version => (
26   is       => 'ro',
27   required => 1,
28 );
29
30 has ordered_versions => (
31   is       => 'ro',
32   isa      => 'ArrayRef',
33   required => 1,
34   trigger  => sub {
35     my $to_version = $_[0]->to_version;
36     my $db_version = $_[0]->db_version;
37
38     croak 'to_version not in ordered_versions'
39       unless grep { $to_version eq $_ } @{ $_[1] };
40
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
49 has _version_idx => (
50   is         => 'rw',
51   isa        => 'Int',
52   lazy_build => 1,
53 );
54
55 method _inc_version_idx { $self->_version_idx($self->_version_idx + 1 ) }
56
57 method _build__version_idx {
58   my $start = $self->version_rs->db_version;
59   my $idx = 0;
60   for (@{$self->ordered_versions}) {
61     return $idx
62       if $_ eq $self->db_version;
63     $idx++;
64   }
65   croak 'database version not found in ordered_versions!';
66 }
67
68 method next_version_set {
69   return undef
70     if $self->ordered_versions->[$self->_version_idx] eq $self->to_version;
71   my $next_idx = $self->_inc_version_idx;
72   if ( $next_idx <= $#{ $self->ordered_versions }) {
73     return [
74       $self->ordered_versions->[$next_idx - 1],
75       $self->ordered_versions->[$next_idx    ],
76     ]
77   } else {
78     return undef
79   }
80 }
81
82 __PACKAGE__->meta->make_immutable;
83
84 1;
85
86 __END__
87
88 vim: ts=2 sw=2 expandtab