Commit | Line | Data |
c703d15d |
1 | package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions; |
97aa9a74 |
2 | |
e70a1600 |
3 | use Moose; |
9deabd1f |
4 | |
5 | # ABSTRACT: Define your own list of versions to use for migrations |
6 | |
2c627d9e |
7 | use Carp 'croak'; |
e70a1600 |
8 | |
24794769 |
9 | with 'DBIx::Class::DeploymentHandler::HandlesVersioning'; |
2c627d9e |
10 | |
b539a216 |
11 | has schema_version => ( |
12 | isa => 'Str', |
13 | is => 'ro', |
14 | required => 1, |
15 | ); |
16 | |
17 | has database_version => ( |
18 | isa => 'Str', |
19 | is => 'ro', |
20 | required => 1, |
21 | ); |
22 | |
5232e8d0 |
23 | has to_version => ( |
b539a216 |
24 | is => 'ro', |
e86c0c07 |
25 | isa => 'Str', |
5232e8d0 |
26 | lazy_build => 1, |
b539a216 |
27 | ); |
28 | |
29 | sub _build_to_version { $_[0]->schema_version } |
30 | |
2c627d9e |
31 | has ordered_versions => ( |
32 | is => 'ro', |
33 | isa => 'ArrayRef', |
34 | required => 1, |
2c627d9e |
35 | ); |
36 | |
8fdf6269 |
37 | has _index_of_versions => ( |
38 | is => 'ro', |
39 | isa => 'HashRef', |
40 | lazy_build => 1, |
41 | ); |
42 | |
43 | sub _build__index_of_versions { |
44 | my %ret; |
45 | my $i = 0; |
46 | for (@{ $_[0]->ordered_versions }) { |
47 | $ret{$_} = $i++; |
48 | } |
49 | \%ret; |
50 | } |
51 | |
2c627d9e |
52 | has _version_idx => ( |
53 | is => 'rw', |
54 | isa => 'Int', |
55 | lazy_build => 1, |
56 | ); |
57 | |
8fdf6269 |
58 | sub _build__version_idx { $_[0]->_index_of_versions->{$_[0]->database_version} } |
59 | |
58a5e27f |
60 | sub _inc_version_idx { $_[0]->_version_idx($_[0]->_version_idx + 1 ) } |
f344dd91 |
61 | sub _dec_version_idx { $_[0]->_version_idx($_[0]->_version_idx - 1 ) } |
2c627d9e |
62 | |
2c627d9e |
63 | |
58a5e27f |
64 | sub next_version_set { |
24794769 |
65 | my $self = shift; |
8fdf6269 |
66 | if ( |
67 | $self->_index_of_versions->{$self->to_version} < |
68 | $self->_version_idx |
69 | ) { |
70 | croak "you are trying to upgrade and your current version is greater\n". |
71 | "than the version you are trying to upgrade to. Either downgrade\n". |
72 | "or update your schema" |
73 | } elsif ( $self->_version_idx == $self->_index_of_versions->{$self->to_version}) { |
74 | return undef |
75 | } else { |
76 | my $next_idx = $self->_inc_version_idx; |
77 | return [ |
78 | $self->ordered_versions->[$next_idx - 1], |
79 | $self->ordered_versions->[$next_idx ], |
80 | ]; |
81 | } |
2c627d9e |
82 | } |
e70a1600 |
83 | |
f344dd91 |
84 | sub previous_version_set { |
85 | my $self = shift; |
8fdf6269 |
86 | if ( |
87 | $self->_index_of_versions->{$self->to_version} > |
88 | $self->_version_idx |
89 | ) { |
90 | croak "you are trying to downgrade and your current version is less\n". |
91 | "than the version you are trying to downgrade to. Either upgrade\n". |
92 | "or update your schema" |
93 | } elsif ( $self->_version_idx == $self->_index_of_versions->{$self->to_version}) { |
94 | return undef |
95 | } else { |
96 | my $next_idx = $self->_dec_version_idx; |
97 | return [ |
8fdf6269 |
98 | $self->ordered_versions->[$next_idx + 1], |
9b620a81 |
99 | $self->ordered_versions->[$next_idx ], |
8fdf6269 |
100 | ]; |
101 | } |
f344dd91 |
102 | } |
103 | |
e70a1600 |
104 | __PACKAGE__->meta->make_immutable; |
105 | |
106 | 1; |
107 | |
e52174e3 |
108 | # vim: ts=2 sw=2 expandtab |
109 | |
e70a1600 |
110 | __END__ |
111 | |
ec167a97 |
112 | =head1 SEE ALSO |
113 | |
114 | This class is an implementation of |
115 | L<DBIx::Class::DeploymentHandler::HandlesVersioning>. Pretty much all the |
116 | documentation is there. |