DeploymentHandler/Dad.pm

filter: Statementstmt 84.4% 49 / 58 Branchbran 71.4% 10 / 14 Conditioncond 80.0% 4 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 63.1% 12 / 19 Podpod 100% 4 / 4 totaltotal 77.4% 79 / 102 CC 10 SCAR 7.1
CC
10
Cov
82%
install
4.0
__ANON__
3.0
_build_to_version
2.1
CRAP 2.0
« DeploymentHandler/VersionHandler/Monotonic.pm ↑ Summary DeploymentHandler/VersionStorage/Standard/VersionResult.pm »
1 package DBIx::Class::DeploymentHandler::Dad;
2 12 $DBIx::Class::DeploymentHandler::Dad::VERSION = '0.002233';
3 # ABSTRACT: Parent class for DeploymentHandlers
4  
5 12 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 require DBIx::Class::Schema; # loaded for type constraint
7 12 ▶use Carp::Clan '^DBIx::Class::DeploymentHandler';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use DBIx::Class::DeploymentHandler::LogImporter ':log';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use DBIx::Class::DeploymentHandler::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 has schema => (
12 is => 'ro',
13 required => 1,
14 );
15  
16 12 has backup_directory => (
17 isa => 'Str',
18 is => 'ro',
19 predicate => 'has_backup_directory',
20 );
21  
22 12 has to_version => (
23 is => 'ro',
24 isa => 'Str',
25 lazy_build => 1,
26 );
27  
28 sub _build_to_version {
29 31 ▶ my $version = $_[0]->schema_version;
Subroutine _build_to_version: called CC=2 SCAR=7.2
30 31 ▶ ref($version) ? $version->numify : $version;
Branch: 1 branch
BranchTrueFalseTotal
ref $version ? : 0 31 31
31 }
32  
33 12 has schema_version => (
34 is => 'ro',
35 lazy_build => 1,
36 );
37  
38 36 ▶sub _build_schema_version { $_[0]->schema->schema_version }
Subroutine _build_schema_version: called CC=1 SCAR=0.0
39  
40 sub install {
41 19 ▶ my $self = shift;
Subroutine install: called CC=4 SCAR=14.0
Pod documented
42  
43 19 ▶ my $version = (shift @_ || {})->{version} || $self->to_version;
Condition80%4/5
+(shift @_ || {})->{'version'} || $self->to_version
l!l&&r!l&&!r
7120
shift @_ || {}
l!l
712
MC/DC0%0/2
+(shift @_ || {})->{'version'} || $self->to_version
shift @_ $self->to_version
Truth table66%2/3
A || B
A= shift @_B= $self->to_version
ABresult
1X1
011
000
44 19 ▶ log_info { "installing version $version" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 19 ▶ croak 'Install not possible as versions table already exists in database'
Branch: 1 branch
BranchTrueFalseTotal
if $self->version_storage_is_installed 7 12 19
46 if $self->version_storage_is_installed;
47  
48 $self->txn_do(sub {
49 12 ▶ my $ddl = $self->deploy({ version=> $version });
Subroutine __ANON__: called CC=1 SCAR=0.0
50  
51 12 $self->add_database_version({
52 version => $version,
53 ddl => $ddl,
54 });
55 12 });
56 }
57  
58 sub upgrade {
59 0 ▶ log_info { 'upgrading' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine upgrade: called CC=2 SCAR=7.1
Pod documented
60 15 my $self = shift;
61 15 my $ran_once = 0;
62 $self->txn_do(sub {
63 15 ▶ while ( my $version_list = $self->next_version_set ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $version_list = $self->next_version_set) 15 15 30
64 15 $ran_once = 1;
65 my ($ddl, $upgrade_sql) = @{
66 15 ▶ $self->upgrade_single_step({ version_set => $version_list })
Branch: 1 branch
BranchTrueFalseTotal
$self->upgrade_single_step({"version_set", $version_list}) or [] 0 15 15
67 ||[]};
68  
69 15 $self->add_database_version({
70 version => $version_list->[-1],
71 ddl => $ddl,
72 upgrade_sql => $upgrade_sql,
73 });
74 }
75 15 });
76  
77 15 ▶ log_warn { 'no need to run upgrade' } unless $ran_once;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless $ran_once 0 15 15
78 }
79  
80 sub downgrade {
81 0 ▶ log_info { 'downgrading' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine downgrade: called CC=2 SCAR=7.1
Pod documented
82 7 my $self = shift;
83 7 my $ran_once = 0;
84 $self->txn_do(sub {
85 7 ▶ while ( my $version_list = $self->previous_version_set ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $version_list = $self->previous_version_set) 7 7 14
86 7 $ran_once = 1;
87 7 $self->downgrade_single_step({ version_set => $version_list });
88  
89 # do we just delete a row here? I think so but not sure
90 7 $self->delete_database_version({ version => $version_list->[0] });
91 }
92 7 });
93 7 ▶ log_warn { 'no version to run downgrade' } unless $ran_once;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless $ran_once 0 7 7
94 }
95  
96 sub backup {
97 0 ▶ my $self = shift;
Subroutine backup: not called CC=1 SCAR=6.9
Pod documented
98 0 ▶ log_info { 'backing up' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 $self->schema->storage->backup($self->backup_directory)
100 }
101  
102 12 __PACKAGE__->meta->make_immutable;
103  
104 12 1;
105  
106 # vim: ts=2 sw=2 expandtab
107  
108 __END__
109  
110 =pod
111  
112 =head1 NAME
113  
114 DBIx::Class::DeploymentHandler::Dad - Parent class for DeploymentHandlers
115  
116 =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
117  
118 =head2 deploy
119  
120 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/deploy>.
121  
122 =head2 version_storage_is_installed
123  
124 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/version_storage_is_installed>.
125  
126 =head2 add_database_version
127  
128 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/add_database_version>.
129  
130 =head2 delete_database_version
131  
132 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/delete_database_version>.
133  
134 =head2 next_version_set
135  
136 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/next_version_set>.
137  
138 =head2 previous_version_set
139  
140 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/previous_version_set>.
141  
142 =head2 upgrade_single_step
143  
144 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/upgrade_single_step>.
145  
146 =head2 downgrade_single_step
147  
148 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/downgrade_single_step>.
149  
150 =head2 txn_do
151  
152 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/txn_do>.
153  
154 =head1 ORTHODOX METHODS
155  
156 These methods are not actually B<required> as things will probably still work
157 if you don't implement them, but if you want your subclass to get along with
158 other subclasses (or more likely, tools made to use another subclass), you
159 should probably implement these too, even if they are no-ops.
160  
161 =head2 database_version
162  
163 see L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/database_version>
164  
165 =head2 prepare_deploy
166  
167 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_deploy>
168  
169 =head2 prepare_resultsource_install
170  
171 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_resultsource_install>
172  
173 =head2 install_resultsource
174  
175 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/install_resultsource>
176  
177 =head2 prepare_upgrade
178  
179 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_upgrade>
180  
181 =head2 prepare_downgrade
182  
183 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_downgrade>
184  
185 =head2 SUBCLASSING
186  
187 All of the methods mentioned in L</METHODS THAT ARE REQUIRED IN SUBCLASSES> and
188 L</ORTHODOX METHODS> can be implemented in any fashion you choose. In the
189 spirit of code reuse I have used roles to implement them in my two subclasses,
190 L<DBIx::Class::DeploymentHandler> and
191 L<DBIx::Class::DeploymentHandler::Deprecated>, but you are free to implement
192 them entirely in a subclass if you so choose to.
193  
194 For in-depth documentation on how methods are supposed to work, see the roles
195 L<DBIx::Class::DeploymentHandler::HandlesDeploy>,
196 L<DBIx::Class::DeploymentHandler::HandlesVersioning>, and
197 L<DBIx::Class::DeploymentHandler::HandlesVersionStorage>.
198  
199 =head1 ATTRIBUTES
200  
201 =head2 schema
202  
203 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
204 and generate the DDL.
205  
206 =head2 schema_version
207  
208 The version that the schema is currently at. Defaults to
209 C<< $self->schema->schema_version >>.
210  
211 =head2 backup_directory
212  
213 The directory where backups are stored
214  
215 =head2 to_version
216  
217 The version (defaults to schema's version) to migrate the database to
218  
219 =head1 METHODS
220  
221 =head2 install
222  
223 $dh->install
224  
225 or
226  
227 $dh->install({ version => 1 })
228  
229 Deploys the requested version into the database Version defaults to
230 L</schema_version>. Populates C<version_storage> with C<version> and C<ddl>.
231  
232 B<Note>: you typically need to call C<< $dh->prepare_deploy >> before you call
233 this method.
234  
235 B<Note>: you cannot install on top of an already installed database
236  
237 =head2 upgrade
238  
239 $dh->upgrade
240  
241 Upgrades the database one step at a time till L</next_version_set>
242 returns C<undef>. Each upgrade step will add a C<version>, C<ddl>, and
243 C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are
244 returned from L</upgrade_single_step>.
245  
246 =head2 downgrade
247  
248 $dh->downgrade
249  
250 Downgrades the database one step at a time till L</previous_version_set>
251 returns C<undef>. Each downgrade step will delete a C<version> from the
252 version storage.
253  
254 =head2 backup
255  
256 $dh->backup
257  
258 Simply calls backup on the C<< $schema->storage >>, passing in
259 C<< $self->backup_directory >> as an argument. Please test yourself before
260 assuming it will work.
261  
262 =head1 AUTHOR
263  
264 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
265  
266 =head1 COPYRIGHT AND LICENSE
267  
268 This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
269  
270 This is free software; you can redistribute it and/or modify it under
271 the same terms as the Perl 5 programming language system itself.
272  
273 =cut
« DeploymentHandler/VersionHandler/Monotonic.pm ↑ Summary DeploymentHandler/VersionStorage/Standard/VersionResult.pm »