initial version of ExplicitVersions VH
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / ExplicitVersions.pm
CommitLineData
e70a1600 1package DBIx::Class::DeploymentHandler::ExplicitVersions;
2use Moose;
3use Method::Signatures::Simple;
2c627d9e 4use Carp 'croak';
e70a1600 5
2c627d9e 6has schema => (
7 isa => 'DBIx::Class::Schema',
8 is => 'ro',
9 required => 1,
10 handles => [qw( ddl_filename schema_version )],
11);
12
13has version_rs => (
14 isa => 'DBIx::Class::ResultSet',
15 is => 'ro',
16 lazy_build => 1,
17 handles => [qw( is_installed db_version )],
18);
19
20method _build_version_rs {
21 $self->schema->set_us_up_the_bomb;
22 $self->schema->resultset('__VERSION')
23}
24
25has to_version => (
26 is => 'ro',
27 required => 1,
28);
29
30has 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
49has _version_idx => (
50 is => 'rw',
51 isa => 'Int',
52 lazy_build => 1,
53);
54
55method _inc_version_idx { $self->_version_idx($self->_version_idx + 1 ) }
56
57method _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
68method 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}
e70a1600 81
82__PACKAGE__->meta->make_immutable;
83
841;
85
86__END__
87
88vim: ts=2 sw=2 expandtab