File Coverage

blib/lib/DBIx/Class/DeploymentHandler/VersionHandler/ExplicitVersions.pm
Criterion Covered Total %
statement 28 28 100.0
branch 8 8 100.0
condition n/a
subroutine 9 9 100.0
pod 0 2 0.0
total 45 47 95.7


line stmt bran cond sub pod time code
1             package DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions;
2             $DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions::VERSION = '0.002231';
3 1     1   170586 use Moose;
  1         462268  
  1         8  
4              
5             # ABSTRACT: Define your own list of versions to use for migrations
6              
7 1     1   7638 use Carp 'croak';
  1         3  
  1         739  
8              
9             with 'DBIx::Class::DeploymentHandler::HandlesVersioning';
10              
11             has schema_version => (
12             is => 'ro',
13             required => 1,
14             );
15              
16             has initial_version => (
17             isa => 'Str',
18             is => 'ro',
19             required => 1,
20             );
21              
22             has to_version => (
23             is => 'ro',
24             isa => 'Str',
25             lazy_build => 1,
26             );
27              
28 2     2   90 sub _build_to_version { $_[0]->schema_version }
29              
30             has ordered_versions => (
31             is => 'ro',
32             isa => 'ArrayRef',
33             required => 1,
34             );
35              
36             has _index_of_versions => (
37             is => 'ro',
38             isa => 'HashRef',
39             lazy_build => 1,
40             );
41              
42             sub _build__index_of_versions {
43 7     7   14 my %ret;
44 7         13 my $i = 0;
45 7         14 for (@{ $_[0]->ordered_versions }) {
  7         309  
46 707         1521 $ret{$_} = $i++;
47             }
48 7         382 \%ret;
49             }
50              
51             has _version_idx => (
52             is => 'rw',
53             isa => 'Int',
54             lazy_build => 1,
55             );
56              
57 7     7   300 sub _build__version_idx { $_[0]->_index_of_versions->{$_[0]->initial_version} }
58              
59 5     5   209 sub _inc_version_idx { $_[0]->_version_idx($_[0]->_version_idx + 1 ) }
60 4     4   166 sub _dec_version_idx { $_[0]->_version_idx($_[0]->_version_idx - 1 ) }
61              
62             # provide backwards compatibility for initial_version/database_version
63             around BUILDARGS => sub {
64             my $orig = shift;
65             my $class = shift;
66              
67             my $args = $class->$orig(@_);
68             $args->{initial_version} = $args->{database_version}
69             if exists $args->{database_version} && !exists $args->{initial_version};
70             return $args;
71             };
72              
73             sub next_version_set {
74 11     11 0 3817 my $self = shift;
75 11 100       555 if (
    100          
76             $self->_index_of_versions->{$self->to_version} <
77             $self->_version_idx
78             ) {
79 1         216 croak "you are trying to upgrade and your current version is greater\n".
80             "than the version you are trying to upgrade to. Either downgrade\n".
81             "or update your schema"
82             } elsif ( $self->_version_idx == $self->_index_of_versions->{$self->to_version}) {
83             return undef
84 4         28 } else {
85 5         16 my $next_idx = $self->_inc_version_idx;
86             return [
87 5         227 $self->ordered_versions->[$next_idx - 1],
88             $self->ordered_versions->[$next_idx ],
89             ];
90             }
91             }
92              
93             sub previous_version_set {
94 7     7 0 2299 my $self = shift;
95 7 100       355 if (
    100          
96             $self->_index_of_versions->{$self->to_version} >
97             $self->_version_idx
98             ) {
99 1         95 croak "you are trying to downgrade and your current version is less\n".
100             "than the version you are trying to downgrade to. Either upgrade\n".
101             "or update your schema"
102             } elsif ( $self->_version_idx == $self->_index_of_versions->{$self->to_version}) {
103             return undef
104 2         13 } else {
105 4         15 my $next_idx = $self->_dec_version_idx;
106             return [
107 4         174 $self->ordered_versions->[$next_idx + 1],
108             $self->ordered_versions->[$next_idx ],
109             ];
110             }
111             }
112              
113             __PACKAGE__->meta->make_immutable;
114              
115             1;
116              
117             # vim: ts=2 sw=2 expandtab
118              
119             __END__
120              
121             =pod
122              
123             =head1 NAME
124              
125             DBIx::Class::DeploymentHandler::VersionHandler::ExplicitVersions - Define your own list of versions to use for migrations
126              
127             =head1 SEE ALSO
128              
129             This class is an implementation of
130             L<DBIx::Class::DeploymentHandler::HandlesVersioning>. Pretty much all the
131             documentation is there.
132              
133             =head1 AUTHOR
134              
135             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
136              
137             =head1 COPYRIGHT AND LICENSE
138              
139             This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
140              
141             This is free software; you can redistribute it and/or modify it under
142             the same terms as the Perl 5 programming language system itself.
143              
144             =cut