more tests for ExplicitVersions
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / VersionHandler / ExplicitVersions.pm
CommitLineData
c703d15d 1package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions;
e70a1600 2use Moose;
3use Method::Signatures::Simple;
2c627d9e 4use Carp 'croak';
e70a1600 5
24794769 6with 'DBIx::Class::DeploymentHandler::HandlesVersioning';
2c627d9e 7
b539a216 8has schema_version => (
9 isa => 'Str',
10 is => 'ro',
11 required => 1,
12);
13
14has database_version => (
15 isa => 'Str',
16 is => 'ro',
17 required => 1,
18);
19
20has 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
26sub _build_to_version { $_[0]->schema_version }
27
2c627d9e 28has ordered_versions => (
29 is => 'ro',
30 isa => 'ArrayRef',
31 required => 1,
32 trigger => sub {
33 my $to_version = $_[0]->to_version;
fb105cfa 34 my $db_version = $_[0]->database_version;
2c627d9e 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
47has _version_idx => (
48 is => 'rw',
49 isa => 'Int',
50 lazy_build => 1,
51);
52
53method _inc_version_idx { $self->_version_idx($self->_version_idx + 1 ) }
54
55method _build__version_idx {
fb105cfa 56 my $start = $self->database_version;
2c627d9e 57 my $idx = 0;
58 for (@{$self->ordered_versions}) {
59 return $idx
fb105cfa 60 if $_ eq $self->database_version;
2c627d9e 61 $idx++;
62 }
63 croak 'database version not found in ordered_versions!';
64}
65
24794769 66sub next_version_set { # sub instead of method because of when roles get composed
67 my $self = shift;
2c627d9e 68 return undef
69 if $self->ordered_versions->[$self->_version_idx] eq $self->to_version;
a3bc8ff9 70
2c627d9e 71 my $next_idx = $self->_inc_version_idx;
a3bc8ff9 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';
2c627d9e 78}
e70a1600 79
80__PACKAGE__->meta->make_immutable;
81
821;
83
84__END__
85
86vim: ts=2 sw=2 expandtab