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

filter: Statementstmt 30.5% 26 / 85 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.0% 7 / 20 Podpod 100% 9 / 9 totaltotal 33.3% 42 / 126 CC 7 SCAR 20.5
CC
7
Cov
27%
run
30.0
custom_upgrade_directory
6.0
prepare_upgrade
6.0
CRAP 7.7
↑ 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.001
19  
20 =cut
21  
22 1 our $VERSION = '0.001';
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 ATTRIBUTES
64  
65 =head2 schema
66  
67 L<DBIx::Class::Schema> object. This parameter is B<required>.
68  
69 =cut
70  
71 1 has schema => (
72 is => 'ro',
73 isa => InstanceOf['DBIx::Class::Schema'],
74 required => 1,
75 );
76  
77 =head2 databases
78  
79 Array reference with database names or single database
80 name as a string. This parameter is B<required>.
81  
82 It is passed directly to L<DBIx::Class::DeploymentHandler>.
83  
84 =cut
85  
86 has databases => (
87 isa => Str | ArrayRef,
88 is => 'ro',
89 0 ▶ default => sub { [qw( MySQL SQLite PostgreSQL )] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 1 required => 1,
91 );
92  
93 =head2 sql_translator_args
94  
95 Hash reference with parameters for L<SQL::Translator>.
96  
97 Defaults to:
98  
99 { add_drop_table => 0 }
100  
101 It is passed directly to L<DBIx::Class::DeploymentHandler>.
102  
103 =cut
104  
105 has sql_translator_args => (
106 isa => HashRef,
107 is => 'ro',
108 0 ▶ default => sub { { add_drop_table => 0 } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 1 );
110  
111 =head2 args
112  
113 Array reference with commandline parameters.
114  
115 =cut
116  
117 has args => (
118 isa => ArrayRef,
119 is => 'ro',
120 0 ▶ default => sub {[]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 1 );
122  
123 =head2 run
124  
125 Determines method to be run.
126  
127 =cut
128  
129 sub run {
130 0 ▶ my $self = shift;
Subroutine run: not called CC=5 SCAR=34.0
Pod documented
131 0 my $cmd;
132  
133 # check if we have commandline arguments
134 0 ▶ if (@{$self->args}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->args;}) { } 0 0 0
elsif ($DBIx::Class::DeploymentHandler::CLI::Script =~ /^dh-(.*?)$/) { } 0 0 0
135 0 $cmd = $self->args->[0];
136 }
137 elsif ($Script =~ /^dh-(.*?)$/) {
138 0 $cmd = $1;
139 }
140  
141 0 ▶ if (defined $cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd) 0 0 0
142 0 $cmd =~ s/-/_/g;
143  
144 0 ▶ if ($self->can($cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($cmd)) 0 0 0
145 0 return $self->$cmd;
146 }
147  
148 0 die "No method for command $cmd";
149 }
150  
151 0 die "Cannot determine command from $Script.";
152 }
153  
154 =head2 version
155  
156 Prints database and schema version.
157  
158 =cut
159  
160 sub version {
161 0 ▶ my $self = shift;
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
162 0 my $database_version = $self->database_version;
163 0 my $schema_version = $self->schema_version;
164  
165 0 return qq{Database version: $database_version
166 Schema version: $schema_version};
167 }
168  
169 =head2 database_version
170  
171 Retrieves schema version from database.
172  
173 =cut
174  
175 sub database_version {
176 0 ▶ my $self = shift;
Subroutine database_version: not called CC=1 SCAR=6.9
Pod documented
177 0 my $dh = $self->_dh_object;
178  
179 0 return $dh->database_version;
180 }
181  
182 =head2 schema_version
183  
184 Retrieves schema version from schema.
185  
186 =cut
187  
188 sub schema_version {
189 0 ▶ my $self = shift;
Subroutine schema_version: not called CC=1 SCAR=6.9
Pod documented
190 0 my $dh = $self->_dh_object;
191  
192 0 return $dh->schema_version;
193 }
194  
195 =head2 custom_upgrade_directory
196  
197 Returns custom upgrade directory if possible.
198  
199 =cut
200  
201 sub custom_upgrade_directory {
202 0 ▶ my $self = shift;
Subroutine custom_upgrade_directory: not called CC=2 SCAR=17.9
Pod documented
203 0 my $dh = $self->_dh_object;
204  
205 0 my $db_version = $self->database_version;
206 0 my $schema_version = $self->schema_version;
207  
208 0 ▶ unless ($schema_version == $db_version + 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_version == $db_version + 1) 0 0 0
209 0 die "Schema version $schema_version needs to be one version ahead of database version $db_version.";
210 }
211  
212 0 return "sql/_common/upgrade/${db_version}-${schema_version}";
213 }
214  
215 =head2 prepare_version_storage
216  
217 =cut
218  
219 sub prepare_version_storage {
220 0 ▶ my $self = shift;
Subroutine prepare_version_storage: not called CC=1 SCAR=6.9
Pod documented
221  
222 0 my $dh = $self->_dh_object;
223  
224 0 $dh->prepare_version_storage_install;
225 0 $dh->prepare_deploy;
226 }
227  
228 =head2 install_version_storage
229  
230 =cut
231  
232 sub install_version_storage {
233 0 ▶ my $self = shift;
Subroutine install_version_storage: not called CC=1 SCAR=6.9
Pod documented
234  
235 0 my $dh = $self->_dh_object;
236  
237 0 $dh->install_version_storage( { version => 1 } );
238 0 $dh->add_database_version( { version => 1 } );
239 }
240  
241 =head2 prepare_upgrade
242  
243 =cut
244  
245 sub prepare_upgrade {
246 0 ▶ my $self = shift;
Subroutine prepare_upgrade: not called CC=2 SCAR=17.9
Pod documented
247  
248 0 my $dh = $self->_dh_object;
249  
250 0 my $db_version = $self->database_version;
251 0 my $schema_version = $self->schema_version;
252  
253 0 ▶ unless ($schema_version == $db_version + 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_version == $db_version + 1) 0 0 0
254 0 die "Schema version $schema_version needs to be one version ahead of database version $db_version for preparing upgrades.";
255 }
256  
257 0 $dh->prepare_deploy;
258 0 $dh->prepare_upgrade(
259 {
260 from_version => $db_version,
261 to_version => $schema_version,
262 }
263 );
264  
265 0 return;
266 }
267  
268 =head2 upgrade
269  
270 =cut
271  
272 sub upgrade {
273 0 ▶ my $self = shift;
Subroutine upgrade: not called CC=1 SCAR=6.9
Pod documented
274  
275 0 my $dh = $self->_dh_object;
276  
277 0 my $db_version = $self->database_version;
278 0 my $schema_version = $self->schema_version;
279  
280 0 $dh->upgrade(
281 {
282 from_version => $db_version,
283 to_version => $schema_version,
284 }
285 );
286  
287 0 return;
288 }
289  
290 sub _dh_object {
291 0 ▶ my $self = shift;
Subroutine _dh_object: not called CC=1 SCAR=6.9
292 0 my $dh;
293  
294 0 $dh = DBIx::Class::DeploymentHandler->new(
295 {
296 schema => $self->schema,
297 databases => $self->databases,
298 sql_translator_args => $self->sql_translator_args,
299 }
300 );
301  
302 0 return $dh;
303 }
304  
305 =head1 AUTHOR
306  
307 Stefan Hornburg (Racke), C<< <racke at linuxia.de> >>
308  
309 =head1 BUGS
310  
311 Please report any bugs or feature requests at
312 L<https://github.com/interchange/DBIx-Class-DeploymentHandler-CLI/issues>
313  
314 I will be notified, and then you'll automatically be notified of progress
315 on your bug as I make changes.
316  
317 =head1 ACKNOWLEDGEMENTS
318  
319 None so far.
320  
321 =head1 LICENSE AND COPYRIGHT
322  
323 Copyright 2016 Stefan Hornburg (Racke).
324  
325 This program is free software; you can redistribute it and/or modify it
326 under the terms of the the Artistic License (2.0). You may obtain a
327 copy of the full license at:
328  
329 L<http://www.perlfoundation.org/artistic_license_2_0>
330  
331 Any use, modification, and distribution of the Standard or Modified
332 Versions is governed by this Artistic License. By using, modifying or
333 distributing the Package, you accept this license. Do not use, modify,
334 or distribute the Package, if you do not accept this license.
335  
336 If your Modified Version has been derived from a Modified Version made
337 by someone other than you, you are nevertheless required to ensure that
338 your Modified Version complies with the requirements of this license.
339  
340 This license does not grant you the right to use any trademark, service
341 mark, tradename, or logo of the Copyright Holder.
342  
343 This license includes the non-exclusive, worldwide, free-of-charge
344 patent license to make, have made, use, offer to sell, sell, import and
345 otherwise transfer the Package with respect to any patent claims
346 licensable by the Copyright Holder that are necessarily infringed by the
347 Package. If you institute patent litigation (including a cross-claim or
348 counterclaim) against any party alleging that the Package constitutes
349 direct or contributory patent infringement, then this Artistic License
350 to you shall terminate on the date that such litigation is filed.
351  
352 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
353 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
354 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
355 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
356 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
357 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
358 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
359 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
360  
361  
362 =cut
363  
364 1 1; # End of DBIx::Class::DeploymentHandler::CLI
↑ Summary