run arbitrary perl in upgrade/downgrade/schema
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / VersionHandler / ExplicitVersions.pm
CommitLineData
c703d15d 1package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions;
e70a1600 2use Moose;
2c627d9e 3use Carp 'croak';
e70a1600 4
24794769 5with 'DBIx::Class::DeploymentHandler::HandlesVersioning';
2c627d9e 6
b539a216 7has schema_version => (
8 isa => 'Str',
9 is => 'ro',
10 required => 1,
11);
12
13has database_version => (
14 isa => 'Str',
15 is => 'ro',
16 required => 1,
17);
18
5232e8d0 19has to_version => (
b539a216 20 is => 'ro',
5232e8d0 21 lazy_build => 1,
b539a216 22);
23
24sub _build_to_version { $_[0]->schema_version }
25
2c627d9e 26has ordered_versions => (
27 is => 'ro',
28 isa => 'ArrayRef',
29 required => 1,
30 trigger => sub {
31 my $to_version = $_[0]->to_version;
fb105cfa 32 my $db_version = $_[0]->database_version;
2c627d9e 33
34 croak 'to_version not in ordered_versions'
35 unless grep { $to_version eq $_ } @{ $_[1] };
36
58a5e27f 37 croak 'database_version not in ordered_versions'
38 unless grep { $db_version eq $_ } @{ $_[1] };
39
2c627d9e 40 for (@{ $_[1] }) {
41 return if $_ eq $db_version;
42 croak 'to_version is before database version in ordered_versions'
43 if $_ eq $to_version;
44 }
45 },
46);
47
48has _version_idx => (
49 is => 'rw',
50 isa => 'Int',
51 lazy_build => 1,
52);
53
58a5e27f 54sub _inc_version_idx { $_[0]->_version_idx($_[0]->_version_idx + 1 ) }
f344dd91 55sub _dec_version_idx { $_[0]->_version_idx($_[0]->_version_idx - 1 ) }
2c627d9e 56
58a5e27f 57sub _build__version_idx {
58 my $self = shift;
fb105cfa 59 my $start = $self->database_version;
2c627d9e 60 my $idx = 0;
61 for (@{$self->ordered_versions}) {
62 return $idx
fb105cfa 63 if $_ eq $self->database_version;
2c627d9e 64 $idx++;
65 }
2c627d9e 66}
67
58a5e27f 68sub next_version_set {
24794769 69 my $self = shift;
2c627d9e 70 return undef
71 if $self->ordered_versions->[$self->_version_idx] eq $self->to_version;
a3bc8ff9 72
58a5e27f 73 # this should never get in infinite loops because we ensure
74 # that the database version is in the list in the version_idx
75 # builder
2c627d9e 76 my $next_idx = $self->_inc_version_idx;
a3bc8ff9 77 return [
78 $self->ordered_versions->[$next_idx - 1],
79 $self->ordered_versions->[$next_idx ],
58a5e27f 80 ];
2c627d9e 81}
e70a1600 82
f344dd91 83sub previous_version_set {
84 my $self = shift;
85 return undef
86 if $self->ordered_versions->[$self->_version_idx] eq $self->database_version;
87
88 # this should never get in infinite loops because we ensure
89 # that the database version is in the list in the version_idx
90 # builder
91 my $next_idx = $self->_dec_version_idx;
92 return [
93 $self->ordered_versions->[$next_idx - 1],
94 $self->ordered_versions->[$next_idx ],
95 ];
96}
97
e70a1600 98__PACKAGE__->meta->make_immutable;
99
1001;
101
102__END__
103
104vim: ts=2 sw=2 expandtab