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.002233';
3 1     1   84451 use Moose;
  1         390346  
  1         6  
4              
5             # ABSTRACT: Define your own list of versions to use for migrations
6              
7 1     1   6178 use Carp 'croak';
  1         3  
  1         571  
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   72 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   9 my %ret;
44 7         15 my $i = 0;
45 7         10 for (@{ $_[0]->ordered_versions }) {
  7         248  
46 707         1187 $ret{$_} = $i++;
47             }
48 7         260 \%ret;
49             }
50              
51             has _version_idx => (
52             is => 'rw',
53             isa => 'Int',
54             lazy_build => 1,
55             );
56              
57 7     7   238 sub _build__version_idx { $_[0]->_index_of_versions->{$_[0]->initial_version} }
58              
59 5     5   178 sub _inc_version_idx { $_[0]->_version_idx($_[0]->_version_idx + 1 ) }
60 4     4   136 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 2981 my $self = shift;
75 11 100       470 if (
    100          
76             $self->_index_of_versions->{$self->to_version} <
77             $self->_version_idx
78             ) {
79 1         178 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         22 } else {
85 5         12 my $next_idx = $self->_inc_version_idx;
86             return [
87 5         181 $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 1838 my $self = shift;
95 7 100       277 if (
    100          
96             $self->_index_of_versions->{$self->to_version} >
97             $self->_version_idx
98             ) {
99 1         77 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         8 } else {
105 4         9 my $next_idx = $self->_dec_version_idx;
106             return [
107 4         139 $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