Commit | Line | Data |
c703d15d |
1 | package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions; |
e70a1600 |
2 | use Moose; |
3 | use Method::Signatures::Simple; |
2c627d9e |
4 | use Carp 'croak'; |
e70a1600 |
5 | |
24794769 |
6 | with 'DBIx::Class::DeploymentHandler::HandlesVersioning'; |
2c627d9e |
7 | |
b539a216 |
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 | |
2c627d9e |
28 | has 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 | |
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 { |
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 |
66 | sub 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; |
70 | my $next_idx = $self->_inc_version_idx; |
71 | if ( $next_idx <= $#{ $self->ordered_versions }) { |
72 | return [ |
73 | $self->ordered_versions->[$next_idx - 1], |
74 | $self->ordered_versions->[$next_idx ], |
75 | ] |
76 | } else { |
77 | return undef |
78 | } |
79 | } |
e70a1600 |
80 | |
81 | __PACKAGE__->meta->make_immutable; |
82 | |
83 | 1; |
84 | |
85 | __END__ |
86 | |
87 | vim: ts=2 sw=2 expandtab |