blib/lib/DBIx/Class/DeploymentHandler/CLI.pm

filter: Statementstmt 28.2% 26 / 92 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.0% 7 / 20 Podpod 100% 9 / 9 totaltotal 30.6% 42 / 137 CC 9 SCAR 21.3
CC
9
Cov
24%
run
30.0
database_version
12.0
custom_upgrade_directory
6.0
CRAP 8.4
↑ Summary
1 package DBIx::Class::DeploymentHandler::CLI;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use FindBin qw($Script);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Types::Standard qw/ArrayRef HashRef InstanceOf Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DBIx::Class::DeploymentHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 DBIx::Class::DeploymentHandler::CLI - Command line interface for deployment handler
15  
16 =head1 VERSION
17  
18 Version 0.002
19  
20 =cut
21  
22 1 our $VERSION = '0.002';
23  
24  
25 =head1 SYNOPSIS
26  
27 This module provides an command line interface for
28 L<DBIx::Class::DeploymentHandler>.
29  
30 A sample script using this module looks like:
31  
32 #! /usr/bin/env perl
33  
34 use strict;
35 use warnings;
36  
37 use PerlDance::Schema;
38 use DBIx::Class::DeploymentHandler::CLI;
39  
40 my $schema = PerlDance::Schema->connect('perldance');
41  
42 my $dh_cli = DBIx::Class::DeploymentHandler::CLI->new(
43 schema => $schema,
44 databases => 'MySQL',
45 args => \@ARGV,
46 );
47  
48 if (my $ret = $dh_cli->run) {
49 print $ret, "\n";
50 }
51  
52 Let's assume that you name the script C<dh-cli>.
53  
54 Now you can call the module's methods through commandline
55 parameters:
56  
57 ./dh-cli prepare-version-storage
58 ./dh-cli install-version-storage
59  
60 It doesn't matter whether you use dashes (C< - >) or
61 underscores (C< _ >) in the function name.
62  
63 =head1 BEST PRACTICES
64  
65 It is often desirable to follow what DeploymentHandler is
66 doing, thus we recommendend to turn on debugging in your
67 C<dh-cli> script:
68  
69 BEGIN {
70 $ENV{DBICDH_DEBUG} = 1;
71 }
72  
73 =head1 ATTRIBUTES
74  
75 =head2 schema
76  
77 L<DBIx::Class::Schema> object. This parameter is B<required>.
78  
79 =cut
80  
81 1 has schema => (
82 is => 'ro',
83 isa => InstanceOf['DBIx::Class::Schema'],
84 required => 1,
85 );
86  
87 =head2 databases
88  
89 Array reference with database names or single database
90 name as a string. This parameter is B<required>.
91  
92 It is passed directly to L<DBIx::Class::DeploymentHandler>.
93  
94 =cut
95  
96 has databases => (
97 isa => Str | ArrayRef,
98 is => 'ro',
99 0 ▶ default => sub { [qw( MySQL SQLite PostgreSQL )] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 1 required => 1,
101 );
102  
103 =head2 sql_translator_args
104  
105 Hash reference with parameters for L<SQL::Translator>.
106  
107 Defaults to:
108  
109 {
110 add_drop_table => 0,
111 producer_args => {
112 mysql_version => 5,
113 },
114 }
115  
116 L<SQL::Translator> defaults to use data types for totally
117 outdated versions of MySQL, thus we force the major version
118 almost all running instances are using.
119  
120 This prevents you from ending up with an C<enum> MySQL data type
121 for boolean columns in the schema instead of the C<boolean> one
122 supported by MySQL 5.
123  
124 It is passed directly to L<DBIx::Class::DeploymentHandler>.
125  
126 =cut
127  
128 has sql_translator_args => (
129 isa => HashRef,
130 is => 'ro',
131 default => sub { {
132 0 ▶ add_drop_table => 0,
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 producer_args => {
134 mysql_version => 5,
135 },
136 } },
137 1 );
138  
139 =head2 args
140  
141 Array reference with commandline parameters.
142  
143 =cut
144  
145 has args => (
146 isa => ArrayRef,
147 is => 'ro',
148 0 ▶ default => sub {[]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 1 );
150  
151 =head2 run
152  
153 Determines method to be run.
154  
155 =cut
156  
157 sub run {
158 0 ▶ my $self = shift;
Subroutine run: not called CC=5 SCAR=34.0
Pod documented
159 0 my $cmd;
160  
161 # check if we have commandline arguments
162 0 ▶ if (@{$self->args}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->args;}) { } 0 0 0
elsif ($DBIx::Class::DeploymentHandler::CLI::Script =~ /^dh-(.*?)$/) { } 0 0 0
163 0 $cmd = $self->args->[0];
164 }
165 elsif ($Script =~ /^dh-(.*?)$/) {
166 0 $cmd = $1;
167 }
168  
169 0 ▶ if (defined $cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd) 0 0 0
170 0 $cmd =~ s/-/_/g;
171  
172 0 ▶ if ($self->can($cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($cmd)) 0 0 0
173 0 return $self->$cmd;
174 }
175  
176 0 die "No method for command $cmd";
177 }
178  
179 0 die "Cannot determine command from $Script.";
180 }
181  
182 =head2 version
183  
184 Prints database and schema version.
185  
186 =cut
187  
188 sub version {
189 0 ▶ my $self = shift;
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
190 0 my $database_version = $self->database_version;
191 0 my $schema_version = $self->schema_version;
192  
193 0 return qq{Database version: $database_version
194 Schema version: $schema_version};
195 }
196  
197 =head2 database_version
198  
199 Retrieves schema version from database.
200  
201 Dies if version storage is missing from database.
202  
203 Returns 0 if version storage is present, but doesn't
204 contain any records.
205  
206 =cut
207  
208 sub database_version {
209 0 ▶ my $self = shift;
Subroutine database_version: not called CC=3 SCAR=24.8
Pod documented
210 0 my $dh = $self->_dh_object;
211  
212 # check if version is present in the database
213 0 ▶ unless ($dh->version_storage_is_installed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dh->version_storage_is_installed) 0 0 0
214 0 die "Version storage isn't present in the database.";
215 }
216  
217 0 my $version = $dh->database_version;
218  
219 0 ▶ unless (defined $version) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $version) 0 0 0
220 0 $version = 0;
221 }
222  
223 0 return $version;
224 }
225  
226 =head2 schema_version
227  
228 Retrieves schema version from schema.
229  
230 =cut
231  
232 sub schema_version {
233 0 ▶ my $self = shift;
Subroutine schema_version: not called CC=1 SCAR=6.9
Pod documented
234 0 my $dh = $self->_dh_object;
235  
236 0 return $dh->schema_version;
237 }
238  
239 =head2 custom_upgrade_directory
240  
241 Returns custom upgrade directory if possible.
242  
243 =cut
244  
245 sub custom_upgrade_directory {
246 0 ▶ my $self = shift;
Subroutine custom_upgrade_directory: not called CC=2 SCAR=17.9
Pod documented
247 0 my $dh = $self->_dh_object;
248  
249 0 my $db_version = $self->database_version;
250 0 my $schema_version = $self->schema_version;
251  
252 0 ▶ unless ($schema_version == $db_version + 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_version == $db_version + 1) 0 0 0
253 0 die "Schema version $schema_version needs to be one version ahead of database version $db_version.";
254 }
255  
256 0 return "sql/_common/upgrade/${db_version}-${schema_version}";
257 }
258  
259 =head2 prepare_version_storage
260  
261 =cut
262  
263 sub prepare_version_storage {
264 0 ▶ my $self = shift;
Subroutine prepare_version_storage: not called CC=1 SCAR=6.9
Pod documented
265  
266 0 my $dh = $self->_dh_object;
267  
268 0 $dh->prepare_version_storage_install;
269 0 $dh->prepare_deploy;
270  
271 0 return;
272 }
273  
274 =head2 install_version_storage
275  
276 =cut
277  
278 sub install_version_storage {
279 0 ▶ my $self = shift;
Subroutine install_version_storage: not called CC=1 SCAR=6.9
Pod documented
280  
281 0 my $dh = $self->_dh_object;
282  
283 0 $dh->install_version_storage( { version => 1 } );
284 0 $dh->add_database_version( { version => 1 } );
285  
286 0 return;
287 }
288  
289 =head2 prepare_upgrade
290  
291 =cut
292  
293 sub prepare_upgrade {
294 0 ▶ my $self = shift;
Subroutine prepare_upgrade: not called CC=2 SCAR=17.9
Pod documented
295  
296 0 my $dh = $self->_dh_object;
297  
298 0 my $db_version = $self->database_version;
299 0 my $schema_version = $self->schema_version;
300  
301 0 ▶ unless ($schema_version == $db_version + 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_version == $db_version + 1) 0 0 0
302 0 die "Schema version $schema_version needs to be one version ahead of database version $db_version for preparing upgrades.";
303 }
304  
305 0 $dh->prepare_deploy;
306 0 $dh->prepare_upgrade(
307 {
308 from_version => $db_version,
309 to_version => $schema_version,
310 }
311 );
312  
313 0 return;
314 }
315  
316 =head2 upgrade
317  
318 =cut
319  
320 sub upgrade {
321 0 ▶ my $self = shift;
Subroutine upgrade: not called CC=1 SCAR=6.9
Pod documented
322  
323 0 my $dh = $self->_dh_object;
324  
325 0 my $db_version = $self->database_version;
326 0 my $schema_version = $self->schema_version;
327  
328 0 $dh->upgrade(
329 {
330 from_version => $db_version,
331 to_version => $schema_version,
332 }
333 );
334  
335 0 return;
336 }
337  
338 sub _dh_object {
339 0 ▶ my $self = shift;
Subroutine _dh_object: not called CC=1 SCAR=6.9
340 0 my $dh;
341  
342 0 $dh = DBIx::Class::DeploymentHandler->new(
343 {
344 schema => $self->schema,
345 databases => $self->databases,
346 sql_translator_args => $self->sql_translator_args,
347 }
348 );
349  
350 0 return $dh;
351 }
352  
353 =head1 AUTHOR
354  
355 Stefan Hornburg (Racke), C<< <racke at linuxia.de> >>
356  
357 =head1 BUGS
358  
359 Please report any bugs or feature requests at
360 L<https://github.com/interchange/DBIx-Class-DeploymentHandler-CLI/issues>
361  
362 I will be notified, and then you'll automatically be notified of progress
363 on your bug as I make changes.
364  
365 =head1 ACKNOWLEDGEMENTS
366  
367 None so far.
368  
369 =head1 LICENSE AND COPYRIGHT
370  
371 Copyright 2016-2017 Stefan Hornburg (Racke).
372  
373 This program is free software; you can redistribute it and/or modify it
374 under the terms of the the Artistic License (2.0). You may obtain a
375 copy of the full license at:
376  
377 L<http://www.perlfoundation.org/artistic_license_2_0>
378  
379 Any use, modification, and distribution of the Standard or Modified
380 Versions is governed by this Artistic License. By using, modifying or
381 distributing the Package, you accept this license. Do not use, modify,
382 or distribute the Package, if you do not accept this license.
383  
384 If your Modified Version has been derived from a Modified Version made
385 by someone other than you, you are nevertheless required to ensure that
386 your Modified Version complies with the requirements of this license.
387  
388 This license does not grant you the right to use any trademark, service
389 mark, tradename, or logo of the Copyright Holder.
390  
391 This license includes the non-exclusive, worldwide, free-of-charge
392 patent license to make, have made, use, offer to sell, sell, import and
393 otherwise transfer the Package with respect to any patent claims
394 licensable by the Copyright Holder that are necessarily infringed by the
395 Package. If you institute patent litigation (including a cross-claim or
396 counterclaim) against any party alleging that the Package constitutes
397 direct or contributory patent infringement, then this Artistic License
398 to you shall terminate on the date that such litigation is filed.
399  
400 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
401 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
402 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
403 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
404 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
405 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
406 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
407 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
408  
409  
410 =cut
411  
412 1 1; # End of DBIx::Class::DeploymentHandler::CLI
↑ Summary