DBIx/Class/Migration/Script.pm

filter: Statementstmt 77.3% 109 / 141 Branchbran 51.5% 34 / 66 Conditioncond 46.6% 7 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 76.6% 23 / 30 Podpod 33.3% 3 / 9 totaltotal 65.6% 178 / 271 CC 40 SCAR 23.4
CC
40
Cov
68%
_build_migration
28.9
run
8.1
_prepare_dbic_dh_args
7.9
CRAP 10.4
« Catalyst/TraitFor/Model/DBIC/Schema/FromMigration.pm ↑ Summary DBIx/Class/Migration/RunScript.pm »
1 package DBIx::Class::Migration::Script;
2  
3 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use MooX::Attribute::ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Pod::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Pod::Usage ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use DBIx::Class::Migration::Types -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use MooX::Options protect_argv => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub _log_die {
Subroutine _log_die: not called CC=1 SCAR=6.9
14 0 my ($self, $msg) = @_;
15 0 $self->log->error($msg);
16 0 croak $msg;
17 }
18  
19 35 ▶sub ENV_PREFIX {
Subroutine ENV_PREFIX: called CC=2 SCAR=7.6
Pod undocumented
20 $ENV{DBIC_MIGRATION_ENV_PREFIX}
21 35 ▶ || 'DBIC_MIGRATION';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DBIC_MIGRATION_ENV_PREFIX"} or "DBIC_MIGRATION" 0 35 35
22 }
23  
24 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 SANDBOX_SQLITE => 'SqliteSandbox',
26 SANDBOX_MYSQL => 'MySQLSandbox',
27 SANDBOX_POSTGRESQL => 'PostgresqlSandbox',
28 5 };
29  
30 has log => (
31 is => 'ro',
32 isa => InstanceOf['Log::Any::Proxy'],
33 6 ▶ default => sub { Log::Any->get_logger( category => 'DBIx::Class::Migration') },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 5 );
35  
36 5 option includes => (
37 is => 'ro',
38 isa => ArrayRef,
39 predicate => 'has_includes',
40 short => join('|', qw(include I lib libs)),
41 format => 's@',
42 );
43  
44 5 has schema => (
45 is=>'ro', predicate=>'has_schema',
46 );
47  
48 5 option schema_class => (
49 is => 'ro', isa => Str,
50 predicate=>'has_schema_class', env_prefix=>ENV_PREFIX,
51 short => 'S', format => 's',
52 );
53  
54 5 option target_dir => (
55 is => 'ro', isa=> Str,
56 predicate=>'has_target_dir', env_prefix=>ENV_PREFIX,
57 short => 'dir', format => 's',
58 );
59  
60 5 option sandbox_dir => (
61 is => 'ro', isa=> Str,
62 predicate=>'has_sandbox_dir', env_prefix=>ENV_PREFIX,
63 format => 's',
64 );
65  
66 5 option username => (
67 is => 'ro', isa => Str,
68 default => '', env_prefix=>ENV_PREFIX, short => 'U',
69 format => 's',
70 );
71  
72 5 option password => (
73 is => 'ro', isa => Str,
74 default => '', env_prefix=>ENV_PREFIX, short => 'P',
75 format => 's',
76 );
77  
78 5 option dsn => (
79 is => 'ro',
80 env_prefix=>ENV_PREFIX, isa => Str,
81 format => 's',
82 );
83  
84 5 option force_overwrite => (
85 is => 'ro', isa => Bool,
86 predicate=>'has_force_overwrite',
87 short => 'O',
88 );
89  
90 5 option to_version => (
91 is => 'ro', isa => Int,
92 predicate=>'has_to_version',
93 short => 'V', format => 'i',
94 );
95  
96 option sql_translator_args => (
97 is => 'ro', isa => HashRef,
98 predicate=>'has_sql_translator_args',
99 6 ▶ default => sub { +{ quote_identifiers => 1 }},
Subroutine __ANON__: called CC=1 SCAR=0.0
100 5 format => 's%',
101 );
102  
103 5 option databases => (
104 is => 'ro', isa => ArraySQLTProducers,
105 predicate=>'has_databases',
106 short => 'database', format => 's@',
107 );
108  
109 5 option sandbox_class => (
110 is => 'ro', isa => Str,
111 predicate=>'has_sandbox_class', default=>SANDBOX_SQLITE,
112 env_prefix=>ENV_PREFIX,
113 short => 'T|sb', format => 's',
114 );
115  
116 5 option dbic_fixture_class => (
117 is => 'ro', isa => Str,
118 predicate=>'has_dbic_fixture_class',
119 format => 's',
120 );
121  
122 5 option dbic_fixtures_extra_args => (
123 is => 'ro', isa => HashRef,
124 predicate=>'has_dbic_fixtures_extra_args',
125 format => 's%',
126 );
127  
128 5 option dbic_connect_attrs => (
129 is => 'ro', isa => HashRef,
130 predicate=>'has_dbic_connect_attrs',
131 format => 's%',
132 );
133  
134 5 option dbi_connect_attrs => (
135 is => 'ro', isa => HashRef,
136 predicate=>'has_dbi_connect_attrs',
137 format => 's%',
138 );
139  
140 5 option extra_schemaloader_args => (
141 is => 'ro', isa => HashRef,
142 predicate=>'has_extra_schemaloader_args',
143 format => 's%',
144 );
145  
146 option fixture_sets => (
147 is=>'ro',
148 isa=>ArrayRef,
149 6 ▶ default => sub { +['all_tables'] },
Subroutine __ANON__: called CC=1 SCAR=0.0
150 5 short => 'fixture_set', format => 's@',
151 );
152  
153 5 ▶ sub _delegated_commands {
Subroutine _delegated_commands: called CC=1 SCAR=0.0
154 5 map { 'cmd_'.$_ => $_ } qw(
155 version status prepare install upgrade
156 downgrade drop_tables delete_table_rows
157 dump_all_sets make_schema install_if_needed
158 dump diagram install_version_storage );
159 }
160  
161 5 has migration_class => (
162 is => 'ro',
163 default => 'DBIx::Class::Migration',
164 isa => LoadableClass,
165 );
166  
167 5 option migration_sandbox_builder_class => (
168 is => 'ro', isa => Str, predicate=>'has_migration_sandbox_builder_class',
169 format => 's%',
170 );
171  
172 5 has migration => (
173 is => 'lazy',
174 handles => { _delegated_commands },
175 );
176  
177 5 ▶ sub _prepare_schema_args {
Subroutine _prepare_schema_args: called CC=5 SCAR=20.4
178 5 my $self = shift;
179 5 my @schema_args;
180 5 ▶ if($self->dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dsn) { } 0 5 5
181 0 push @schema_args, ($self->dsn,
182 $self->username, $self->password);
183 } else {
184 5 warn "no --dsn argument was found, defaulting to a local SQLite database\n";
185 }
186 5 ▶ if($self->has_dbi_connect_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_dbi_connect_attrs) 0 5 5
187 0 push @schema_args, $self->dbi_connect_attrs;
188 }
189 5 ▶ if($self->has_dbic_connect_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_dbic_connect_attrs) 0 5 5
190 0 ▶ push @schema_args, {} unless $self->has_dbi_connect_attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_dbi_connect_attrs 0 0 0
191 0 push @schema_args, $self->dbic_connect_attrs;
192 }
193 5 return @schema_args;
194 }
195  
196 6 ▶ sub _prepare_dbic_dh_args {
Subroutine _prepare_dbic_dh_args: called CC=7 SCAR=20.7
197 6 my $self = shift;
198 return (
199 6 ▶ ($self->has_force_overwrite ? (force_overwrite => $self->force_overwrite) : ()),
Branch: 5 branches
BranchTrueFalseTotal
$self->has_force_overwrite ? : 0 6 6
$self->has_target_dir && $self->target_dir ? : 5 1 6
$self->has_to_version ? : 1 5 6
$self->has_databases ? : 0 6 6
$self->has_sql_translator_args ? : 6 0 6
Condition66%2/3
$self->has_target_dir and $self->target_dir
!ll&&!rl&&r
105
MC/DC50%1/2
$self->has_target_dir and $self->target_dir
$self->has_target_dir $self->target_dir
Truth table66%2/3
A and B
A= $self->has_target_dirB= $self->target_dir
ABresult
0X0
100
111
200 ($self->has_target_dir && $self->target_dir ? (script_directory=>$self->target_dir) : ()),
201 ($self->has_to_version ? (to_version=>$self->to_version) : ()),
202 ($self->has_databases ? (databases=>$self->databases) : ()),
203 ($self->has_sql_translator_args ? (sql_translator_args=>$self->sql_translator_args) : ()),
204 );
205 }
206  
207 6 ▶sub _build_migration {
Subroutine _build_migration: called CC=17 SCAR=33.6
208 6 my $self = shift;
209 6 my %dbic_dh_args = $self->_prepare_dbic_dh_args;
210 6 ▶ my %args = (%dbic_dh_args ? (dbic_dh_args => \%dbic_dh_args) : ());
Branch: 1 branch
BranchTrueFalseTotal
%dbic_dh_args ? : 6 0 6
211  
212 6 ▶ if($self->has_schema) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_schema) { } 1 5 6
elsif ($self->has_schema_class and $self->schema_class) { } 5 0 5
213 1 $args{schema} = $self->schema;
214 ▶ } elsif($self->has_schema_class && $self->schema_class) {
Condition33%1/3
$self->has_schema_class and $self->schema_class
!ll&&!rl&&r
005
MC/DC0%0/2
$self->has_schema_class and $self->schema_class
$self->has_schema_class $self->schema_class
Truth table33%1/3
A and B
A= $self->has_schema_classB= $self->schema_class
ABresult
0X0
100
111
215 5 my @schema_args = $self->_prepare_schema_args;
216 5 $args{schema_class} = $self->schema_class;
217 5 ▶ $args{schema_args} = \@schema_args if @schema_args;
Branch: 1 branch
BranchTrueFalseTotal
if @schema_args 0 5 5
218 }
219  
220 6 ▶ $args{target_dir} = $self->target_dir
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_target_dir and $self->target_dir 5 1 6
Condition66%2/3
$self->has_target_dir and $self->target_dir
!ll&&!rl&&r
105
MC/DC50%1/2
$self->has_target_dir and $self->target_dir
$self->has_target_dir $self->target_dir
Truth table66%2/3
A and B
A= $self->has_target_dirB= $self->target_dir
ABresult
0X0
100
111
221 if $self->has_target_dir && $self->target_dir;
222  
223 6 ▶ if($self->has_sandbox_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_sandbox_class) 6 0 6
224 6 my ($plus, $class) = ($self->sandbox_class=~/^(\+)*(.+)$/);
225 6 ▶ $args{db_sandbox_class} = $plus ? $class : "DBIx::Class::Migration::$class";
Branch: 1 branch
BranchTrueFalseTotal
$plus ? : 0 6 6
226 }
227  
228 6 ▶ $args{dbic_fixture_class} = $self->dbic_fixture_class
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_dbic_fixture_class 0 6 6
229 if $self->has_dbic_fixture_class;
230  
231 6 ▶ $args{dbic_fixtures_extra_args} = $self->dbic_fixtures_extra_args
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_dbic_fixtures_extra_args 0 6 6
232 if $self->has_dbic_fixtures_extra_args;
233  
234 6 ▶ $args{extra_schemaloader_args} = $self->extra_schemaloader_args
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_extra_schemaloader_args 0 6 6
235 if $self->has_extra_schemaloader_args;
236  
237 6 ▶ if($self->has_migration_sandbox_builder_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_migration_sandbox_builder_class) 0 6 6
238 0 my ($plus, $class) = ($self->migration_sandbox_builder_class=~/^(\+)*(.+)$/);
239 0 ▶ $args{db_sandbox_builder_class} = $plus ? $class : "DBIx::Class::Migration::$class";
Branch: 1 branch
BranchTrueFalseTotal
$plus ? : 0 0 0
240 }
241  
242 # Because this attribute uses the ENV thing, its always going to meet the 'has'
243 # Requirement, but it will be '' so we can also check for truthiness.
244 6 ▶ $args{db_sandbox_dir} = $self->sandbox_dir
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_sandbox_dir and $self->sandbox_dir 0 6 6
Condition33%1/3
$self->has_sandbox_dir and $self->sandbox_dir
!ll&&!rl&&r
600
MC/DC0%0/2
$self->has_sandbox_dir and $self->sandbox_dir
$self->has_sandbox_dir $self->sandbox_dir
Truth table33%1/3
A and B
A= $self->has_sandbox_dirB= $self->sandbox_dir
ABresult
0X0
100
111
245 if $self->has_sandbox_dir && $self->sandbox_dir;
246
247 6 return $self->migration_class->new(%args);
248 }
249  
250 0 ▶sub cmd_dump_named_sets {
Subroutine cmd_dump_named_sets: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my $self = shift;
252 $self->migration
253 0 ->dump_named_sets(@{$self->fixture_sets});
254 }
255  
256 0 ▶sub cmd_delete_named_sets {
Subroutine cmd_delete_named_sets: not called CC=1 SCAR=6.9
Pod undocumented
257 0 my $self = shift;
258 $self->migration
259 0 ->delete_named_sets(@{$self->fixture_sets});
260 }
261  
262 0 ▶sub cmd_populate {
Subroutine cmd_populate: not called CC=1 SCAR=6.9
Pod undocumented
263 0 my $self = shift;
264 $self->migration
265 0 ->populate(@{$self->fixture_sets});
266 }
267  
268 0 ▶sub cmd_help {
Subroutine cmd_help: not called CC=2 SCAR=17.9
Pod undocumented
269 0 my ($self, $subhelp) = @_;
270 0 ▶ my $docs = "DBIx::Class::Migration::Script::Help" . ($subhelp ? "::$subhelp" : "");
Branch: 1 branch
BranchTrueFalseTotal
$subhelp ? : 0 0 0
271 0 Pod::Usage::pod2usage(
272 -verbose => 2,
273 -exitval => 'NOEXIT',
274 -input => Pod::Find::pod_where({-inc => 1}, $docs));
275 }
276  
277 0 ▶sub _import_libs {
Subroutine _import_libs: not called CC=1 SCAR=6.9
278 0 my ($self, @libs) = @_;
279 0 require lib;
280 0 lib->import(@libs);
281 }
282  
283 4 ▶sub _defaults {
Subroutine _defaults: called CC=2 SCAR=7.2
284 4 my $class = shift;
285 4 ▶ $class->can('defaults') ? $class->defaults : ();
Branch: 1 branch
BranchTrueFalseTotal
$class->can('defaults') ? : 0 4 4
286 }
287  
288 4 ▶sub run {
Subroutine run: called CC=6 SCAR=20.9
Pod documented
289 4 my ($self) = @_;
290 4 my ($argv, @extra_argv) = @ARGV;
291  
292 0 ▶ $self->_import_libs(@{$self->includes})
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_includes 0 4 4
293 if $self->has_includes;
294  
295 4 ▶ if(!$argv || $argv eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $argv or $argv eq 'help') { } 0 4 4
Condition33%1/3
not $argv or $argv eq "help"
l!l&&r!l&&!r
004
MC/DC0%0/2
not $argv or $argv eq "help"
not $argv $argv eq "help"
Truth table33%1/3
not(A) or B
A= not $argvB= $argv eq "help"
ABresult
1X1
011
000
296 0 $self->cmd_help(@extra_argv);
297 } else {
298 4 foreach my $cmd ($argv, @extra_argv) {
299 $self->can("cmd_${cmd}") ?
300 4 ▶ $self->${\"cmd_${cmd}"} :
Branch: 1 branch
BranchTrueFalseTotal
$self->can("cmd_$cmd") ? : 4 0 4
301 $self->_log_die( "No such command ${cmd}\n" );
302 }
303  
304 }
305 }
306  
307 4 ▶sub run_with_options {
Subroutine run_with_options: called CC=1 SCAR=0.0
Pod documented
308 4 my ($class, %options) = @_;
309 4 $class->new_with_options($class->_defaults, %options)
310 ->run;
311 }
312  
313 0 ▶sub new_with_defaults {
Subroutine new_with_defaults: not called CC=1 SCAR=6.9
Pod undocumented
314 0 my $class = shift;
315 0 return $class->new_with_options($class->_defaults, @_);
316 }
317  
318 {
319 # overrides MooX::Options::Role's one to not exit() on ->new exception
320 5 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
321 7 ▶sub new_with_options {
Subroutine new_with_options: called CC=5 SCAR=19.3
322 7 my ($class, %params) = @_;
323 7 my %cmdline_params = $class->parse_options(%params);
324 7 ▶ if ($cmdline_params{h}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmdline_params{"h"}) 0 7 7
325 0 return $class->options_usage($params{h}, $cmdline_params{h});
326 }
327 7 ▶ if ($cmdline_params{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmdline_params{"help"}) 0 7 7
328 0 return $class->options_help($params{help}, $cmdline_params{help});
329 }
330 7 ▶ if ($cmdline_params{man}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmdline_params{"man"}) 0 7 7
331 0 return $class->options_man($cmdline_params{man});
332 }
333 7 ▶ if ($cmdline_params{usage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmdline_params{"usage"}) 0 7 7
334 0 return $class->options_short_usage($params{usage}, $cmdline_params{usage});
335 }
336 7 $class->new(%cmdline_params);
337 }
338 }
339  
340 5 ▶sub run_if_script {
Subroutine run_if_script: called CC=2 SCAR=7.1
Pod documented
341 5 my $class = shift;
342 5 ▶ caller(1) ? $class : $class->run_with_options(@_);
Branch: 1 branch
BranchTrueFalseTotal
&CORE::GLOBAL::caller(1) ? : 5 0 5
343 }
344  
345 5 __PACKAGE__->meta->make_immutable;
346 5 __PACKAGE__->run_if_script;
347  
348 =head1 NAME
349  
350 DBIx::Class::Migration::Script - Tools to manage database Migrations
351  
352 =head1 SYNOPSIS
353  
354 dbic-migration status \
355 --lib="lib" \
356 --schema_class='MyApp::Schema' \
357 --dsn='DBI:SQLite:myapp.db'
358  
359 =head1 DESCRIPTION
360  
361 This is a class which provides an interface mapping between the commandline
362 script L<dbic-migration> and the back end code that does the heavy lifting,
363 L<DBIx::Class::Migration>. This class has very little of it's own
364 functionality, since it basically acts as processing glue between that
365 commandline application and the code which does all the work.
366  
367 You should look at L<DBIx::Class::Migration> and the tutorial over at
368 L<DBIx::Class::Migration::Tutorial> to get started. This is basically
369 API level docs and a command summary which is likely to be useful as a
370 reference when you are familiar with the system.
371  
372 =head1 ATTRIBUTES
373  
374 This class defines the following attributes.
375  
376 =head2 migration
377  
378 This contains an instance of L<DBIx::Class::Migration> which is constructed
379 from various attributes described futher in these docs.
380  
381 This is basically a delegate for all the commands you perform with this
382 interface.
383  
384 =head2 includes
385  
386 Accepts ArrayRef. Not required.
387  
388 Similar to the commandline switch C<I> for the C<perl> interpreter. It adds
389 to C<@INC> for when we want to search for modules to load. This is primarily
390 useful for specifying a path to find a L</schema_class>.
391  
392 We make no attempt to check the validity of any paths specified. Buyer beware.
393  
394 =head2 schema_class
395  
396 Accepts Str. Required.
397  
398 This is the schema we use as the basic for creating, managing and running your
399 deployments. This should be the full package namespace defining your subclass
400 of L<DBIx::Class::Schema>. For example C<MyApp::Schema>.
401  
402 Uses L<MooX::Attribute::ENV> to let you populate values from %ENV. Uses key
403 DBIC_MIGRATION_SCHEMA_CLASS
404  
405 If the L</schema_class> cannot be loaded, a hard exception will be thrown.
406  
407 =head2 target_dir
408  
409 Accepts Str. Required.
410  
411 This is the directory we store our migration and fixture files. Inside this
412 directory we will create a C<fixtures> and C<migrations> sub-directory.
413  
414 Although you can specify the directory, if you leave it undefined, we will use
415 L<File::ShareDir::ProjectDistDir> to locate the C<share> directory for your
416 project and place the files there. This is the recommended approach, and is
417 considered a community practice in regards to where to store your distribution
418 non code files. Please see L<File::ShareDir::ProjectDistDir> as well as
419 L<File::ShareDir> for more information.
420  
421 Uses L<MooX::Attribute::ENV> to let you populate values from %ENV. Uses key
422 DBIC_MIGRATION_TARGET_DIR
423  
424 =head2 sandbox_dir
425  
426 Accepts Str. Optional
427  
428 Used if you wish to build the sandbox (if you are building one) in a location that
429 is not the 'target_dir'. For example you might do this to make it easier to not
430 accidentally check the sandbox into the repository or if you are reusing it across
431 several projects.
432  
433 =head2 username
434  
435 Accepts Str. Not Required
436  
437 This should be the username for the database we connect to for deploying
438 ddl, ddl changes and fixtures.
439  
440 Uses L<MooX::Attribute::ENV> to let you populate values from %ENV. Uses key
441 DBIC_MIGRATION_USERNAME
442  
443 =head2 password
444  
445 Accepts Str. Not Required
446  
447 This should be the password for the database we connect to for deploying
448 ddl, ddl changes and fixtures.
449  
450 Uses L<MooX::Attribute::ENV> to let you populate values from %ENV. Uses key
451 DBIC_MIGRATION_PASSWORD
452  
453 =head2 dsn
454  
455 Accepts Str. Not Required
456  
457 This is the DSN for the database you are going to be targeting for deploying
458 or altering ddl, and installing or generating fixtures.
459  
460 This should be a DSN suitable for L<DBIx::Class::Schema/connect>, something
461 in the form of C<DBI:SQLite:myapp-schema.db>.
462  
463 Please take care where you point this (like production :) )
464  
465 If you don't provide a value, we will automatically create a SQLite based
466 database connection with the following DSN:
467  
468 DBD:SQLite:[path to target_dir]/[db_file_name].db
469  
470 Where c<[path to target_dir]> is L</target_dir> and [db_file_name] is a converted
471 version of L</schema_class>. For example if you set L<schema_class> to:
472  
473 MyApp::Schema
474  
475 Then [db_file_name] would be C<myapp-schema>.
476  
477 Please remember that you can generate deployment files for database types
478 other than the one you've defined in L</dsn>, however since different databases
479 enforce constraints differently it would not be impossible to generate fixtures
480 that can be loaded by one database but not another. Therefore I recommend
481 always generated fixtures from a database that is consistent across enviroments.
482  
483 Uses L<MooX::Attribute::ENV> to let you populate values from %ENV. Uses key
484 DBIC_MIGRATION_DSN
485  
486 =head2 force_overwrite
487  
488 Accepts Bool. Not Required. Defaults to False.
489  
490 Used when building L</migration> to en / disable the L<DBIx::Class::DeploymentHandler>
491 option C<force_overwrite>. This is used when generating DDL and related
492 files for a given version of your L</_schema> to decide if it is ok to overwrite
493 deployment files. You might need this if you deploy a version of the database
494 during development and then need to make more changes or fixes for that version.
495  
496 =head2 to_version
497  
498 Accepts Int. Not Required.
499  
500 Used to establish a target version when running an install. You can use this
501 to force install a version of the database other then your current L</_schema>
502 version. You might want this when you need to force install a lower version as
503 part of your development process for changing the database.
504  
505 If you leave this undefined, no default value is built, however
506 L<DBIx::Class::DeploymentHandler> will assume you want whatever is the value of
507 your $schema->version.
508  
509 =head2 databases
510  
511 Accepts ArrayRef. Not Required.
512  
513 Used when building L</migration> to define the target databases we are building
514 migration files for. You can name any of the databases currently supported by
515 L<SQL::Translator>. If you leave this undefined we will derive a value based on the value
516 of L</dsn>. For example, if your L</dsn> is "DBI:SQLite:test.db", we will set
517 the value of L</databases> to C<['SQLite']>.
518  
519 =head2 sql_translator_args
520  
521 Accepts HashRef. Not Required.
522  
523 Used when building L</migration> to set the L<DBIx::Class::DeploymentHandler>
524 option C<sql_translator_args>. This can be used to specify options for
525 L<SQL::Translator>, for example:
526  
527 producer_args => { postgres_version => '9.1' }
528  
529 to define the database version for SQL producer. Defaults to setting
530 C<quote_identifiers> to a true value, which despite being documented as
531 the default, is not the case in practice.
532  
533 =head2 fixture_sets
534  
535 Accepts ArrayRef. Not Required. Defaults to ['all_tables'].
536  
537 This defines a list of fixture sets that we use for dumping or populating
538 fixtures. Defaults to the C<['all_tables']> set, which is the one set we build
539 automatically for each version of the database we prepare deployments for.
540  
541 =head2 sandbox_class
542  
543 Accepts String. Not required. Defaults to 'SqliteSandbox'
544  
545 If you don't have a database already running, we will automatically create a
546 database 'sandbox' in your L</target_dir> that is suitable for development and
547 rapid prototyping. This is intended for developers and intended to make life
548 more simple, particularly for beginners who might not have all the knowledged
549 needed to setup a database for development purposes.
550  
551 By default this sandbox is a file based L<DBD::Sqlite> database, which is an
552 easy option since changes are good this is already installed on your development
553 computer (and if not it is trivial to install).
554  
555 You can change this to either 'PostgresqlSandbox' or 'MySQLSandbox', which will
556 create a sandbox using either L<DBIx::Class::Migration::MySQLSandbox> or
557 L<DBIx::Class::Migration::PostgresqlSandbox> (which in term require the separate
558 installation of either L<Test::mysqld> or L<Test::Postgresql58>). If you are
559 using one of those open source databases in production, its probably a good
560 idea to use them in development as well, since there are enough small
561 differences between them that could make your code break if you used sqlite for
562 development and postgresql in production. However this requires a bit more
563 setup effort, so when you are starting off just sticking to the default sqlite
564 is probably the easiest thing to do.
565  
566 You should review the documenation at L<DBIx::Class::Migration::MySQLSandbox> or
567 L<DBIx::Class::Migration::PostgresqlSandbox> because those delegates also build
568 some helper scripts, intended to help you use a sandbox.
569  
570 Uses L<MooX::Attribute::ENV> to let you populate values from %ENV. Uses key
571 DBIC_MIGRATION_SANDBOX_CLASS
572  
573 If you need to create your own custom database sandboxes, please see:
574 L<DBIx::Class::Migration::Sandbox> which is the role your sandbox factory needs
575 to complete. You can signify your custom sandbox by using the full package name
576 with a '+' prepended. For example:
577  
578 sandbox_class => '+MyApp::Schema::CustomSandbox'
579  
580 You should probably look at the existing sandbox code for thoughts on what a
581 good sandbox would do.
582  
583 =head2 migration_class
584  
585 Accepts String. Not Required (Defaults: L<DBIx::Class::Migration>)
586  
587 Should point to the class that does what L<DBIx::Class::Migration> does. This
588 is exposed here for those who need to subclass L<DBIx::Class::Migration>. We
589 don't expose this attribute to the commandline, so if you are smart enough to
590 do the subclassing (and sure you need to do that), I will assume you will also
591 either subclass L<DBIx::Class::Migration:Script> or override then default
592 value using some standard technique.
593  
594 =head2 dbic_fixture_class
595  
596 Accepts: Str, Not Required.
597  
598 You can use this if you need to make a custom subclass of L<DBIx::Class::Fixtures>.
599  
600 =head2 dbic_fixtures_extra_args
601  
602 Accepts: HashRef, Not Required.
603  
604 If provided will add some additional arguments when creating an instance of
605 L</dbic_fixture_class>. You should take a look at the documentation for
606 L<DBIx::Class::Fixtures> to understand what additional arguments may be of use.
607  
608 =head2 dbi_connect_attrs
609  
610 Accepts: HashRef, Not Required.
611  
612 If you are specifying a DSN, you might need to provide some additional args
613 to L<DBI> (see L<DBIx::Class::Storage::DBI/connect_info> for more).
614  
615 =head2 dbic_connect_attrs
616  
617 Accepts: HashRef, Not Required.
618  
619 If you are specifying a DSN, you might need to provide some additional args
620 to L<DBIx::Class> (see L<DBIx::Class::Storage::DBI/connect_info> for more).
621  
622 You can also see L<DBIx::Class::Storage::DBI/DBIx::Class-specific-connection-attributes>
623 for more information on what this can do for you. Chances are good if you
624 need this you will also want to subclass L<DBIx::Class::Migration::Script> as
625 well.
626  
627 Common uses for this is to run SQL on startup and set Postgresql search paths.
628  
629 =head2 extra_schemaloader_args
630  
631 Accepts: HashRef, Not Required.
632  
633 Used to populate L<DBIx::Class::Migration/extra_schemaloader_args>
634  
635 =head2 migration_sandbox_builder_class
636  
637 Accepts: String (Classname), Not Required.
638  
639 Used to set L<DBIx::Class::Migration/db_sandbox_builder_class>. You probably
640 won't mess with this unless you are writing your own Sandbox builder class, or
641 using the alternative builder L<DBIx::Class::Migration::TempDirSandboxBuilder>
642 for creating temporary sandboxes when you want to test your migrations.
643  
644 =head1 COMMANDS
645  
646 dbic-migration -Ilib install
647  
648 Since this class uses L<MooX::Options>, it can be run directly
649 as a commandline application. The following is a list of commands we support
650 as well as the options / flags associated with each command.
651  
652 =head2 help
653  
654 See L<DBIx::Class::Migration::Script::Help::help>.
655  
656 =head2 version
657  
658 See L<DBIx::Class::Migration::Script::Help::version>.
659  
660 =head2 status
661  
662 See L<DBIx::Class::Migration::Script::Help::status>.
663  
664 =head2 prepare
665  
666 See L<DBIx::Class::Migration::Script::Help::prepare>.
667  
668 =head2 install
669  
670 See L<DBIx::Class::Migration::Script::Help::install>.
671  
672 =head2 upgrade
673  
674 See L<DBIx::Class::Migration::Script::Help::upgrade>.
675  
676 =head2 downgrade
677  
678 See L<DBIx::Class::Migration::Script::Help::downgrade>.
679  
680 =head2 dump_named_sets
681  
682 See L<DBIx::Class::Migration::Script::Help::dump_named_sets>.
683  
684 =head2 dump_all_sets
685  
686 See L<DBIx::Class::Migration::Script::Help::dump_all_sets>.
687  
688 =head2 populate
689  
690 See L<DBIx::Class::Migration::Script::Help::populate>.
691  
692 =head2 drop_tables
693  
694 See L<DBIx::Class::Migration::Script::Help::drop_tables>.
695  
696 =head2 delete_table_rows
697  
698 See L<DBIx::Class::Migration::Script::Help::delete_table_rows>.
699  
700 =head2 make_schema
701  
702 See L<DBIx::Class::Migration::Script::Help::make_schema>.
703  
704 =head2 install_if_needed
705  
706 See L<DBIx::Class::Migration::Script::Help::install_if_needed>.
707  
708 =head2 install_version_storage
709  
710 See L<DBIx::Class::Migration::Script::Help::install_version_storage>.
711  
712 =head2 diagram
713  
714 See L<DBIx::Class::Migration::Script::Help::diagram>.
715  
716 =head2 delete_named_sets
717  
718 See L<DBIx::Class::Migration::Script::Help::delete_named_sets>.
719  
720 =head2 Command Flags
721  
722 The following flags are used to modify or inform commands.
723  
724 =head3 includes
725  
726 See L<DBIx::Class::Migration::Script::Help::includes>.
727  
728 =head3 schema_class
729  
730 See L<DBIx::Class::Migration::Script::Help::schema_class>.
731  
732 =head3 target_dir
733  
734 See L<DBIx::Class::Migration::Script::Help::target_dir>.
735  
736 =head3 username
737  
738 Aliases: U
739 Value: Str
740  
741 =head3 password
742  
743 Aliases: P
744 Value: String
745  
746 =head3 dsn
747  
748 Value: String
749  
750 These three commandline flags describe how to connect to a target, physical
751 database, where we will deploy migrations and fixtures. If you don't provide
752 them, we will automatically deploy to a L<DBD::SQLite> managed database located
753 at L</target_dir>.
754  
755 dbic-migration install --username myuser --password mypass --dsn DBI:SQLite:mydb.db
756  
757 =head3 force_overwrite
758  
759 See L<DBIx::Class::Migration::Script::Help::force_overwrite>
760  
761 =head3 to_version
762  
763 See L<DBIx::Class::Migration::Script::Help::to_version>
764  
765 =head3 databases
766  
767 See L<DBIx::Class::Migration::Script::Help::databases>
768  
769 =head3 fixture_sets
770  
771 See L<DBIx::Class::Migration::Script::Help::fixture_sets>
772  
773 =head3 sandbox_class
774  
775 See L<DBIx::Class::Migration::Script::Help::sandbox_class>
776  
777 The default sqlite sandbox is documented at L<DBIx::Class::Migration::SQLiteSandbox>
778 although this single file database is pretty straightforward to use.
779  
780 If you are declaring the value in a subclass, you can use the pre-defined
781 constants to avoid typos (see L</CONSTANTS>).
782  
783 =head3 dbic_fixture_class
784  
785 See L<DBIx::Class::Migration::Script::Help::dbic_fixture_class>
786  
787 =head3 dbic_fixtures_extra_args
788  
789 See L<DBIx::Class::Migration::Script::Help::dbic_fixtures_extra_args>
790  
791 =head3 dbic_connect_attrs
792  
793 See L<DBIx::Class::Migration::Script::Help::dbic_connect_attrs>
794  
795 =head3 dbi_connect_attrs
796  
797 See L<DBIx::Class::Migration::Script::Help::dbi_connect_attrs>
798  
799 =head3 extra_schemaloader_args
800  
801 See L<DBIx::Class::Migration::Script::Help::extra_schemaloader_args>
802  
803 =head1 OPTIONAL METHODS FOR SUBCLASSES
804  
805 If you decide to make a custom subclass of L<DBIx::Class::Migration::Script>,
806 (you might do this for example to better integrate your migrations with an
807 existing framework, like L<Catalyst>) you may defined the following option
808 methods.
809  
810 =head2 default
811  
812 Returns a Hash of instantiation values.
813  
814 Merges some predefined values when instantiating. For example:
815  
816 package MusicBase::Schema::MigrationScript;
817  
818 use Moose;
819 use MusicBase::Web;
820  
821 extends 'DBIx::Class::Migration::Script';
822  
823 sub defaults {
824 schema => MusicBase::Web->model('Schema')->schema,
825 }
826  
827 __PACKAGE__->meta->make_immutable;
828 __PACKAGE__->run_if_script;
829  
830 This would create a version of your script that already includes the target
831 C<schema>. In this example we will let L<Catalyst> configuration handle which
832 database to run deployments against.
833  
834 =head1 ADDITIONAL INFORMATION FOR SUBCLASSERS
835  
836 The following methods are documented of interest to subclassers.
837  
838 =head2 run_if_script
839  
840 Class method that detects if your module is being called as a script. Place it
841 at the end of your subclass:
842  
843 __PACKAGE__->run_if_script;
844  
845 This returns true in the case you are using the class as a module, and calls
846 L</run_with_options> otherwise. Adding this lets you invoke your class module
847 as a script from the commandline (saving you the trouble of writing a thin
848 script wrapper).
849  
850 perl -Ilib lib/MyApp/Schema/MigrationScript.pm --status
851  
852 =head2 run_with_options
853  
854 Given a Hash of initial arguments, merges those with the results of values passed
855 on the commandline (via L<MooX::Options>) and run.
856  
857 =head2 run
858  
859 Actually runs commands.
860  
861 =head1 CONSTANTS
862  
863 The following constants are defined but not exported. These are used to give
864 a canonical value for the L</sandbox_class> attribute.
865  
866 =head2 SANDBOX_SQLITE
867  
868 SqliteSandbox
869  
870 =head2 SANDBOX_MYSQL
871  
872 MySQLSandbox
873  
874 =head2 SANDBOX_POSTGRESQL
875  
876 PostgresqlSandbox
877  
878 =head1 EXAMPLES
879  
880 Please see L<DBIx::Class::Migration::Tutorial> for more. Here's some basic use
881 cases.
882  
883 =head2 Prepare deployment files for a schema
884  
885 dbic-migration prepare --schema_class MyApp::Schema
886  
887 This will prepare deployment files for just SQLite
888  
889 dbic-migration prepare --database SQLite --database MySQL \
890 --schema_class MyApp::Schema
891  
892 This will prepare deployment files for both SQLite and MySQL
893  
894 =head2 Install database from deployments
895  
896 dbic-migration install --schema_class MyApp::Schema
897  
898 Creates the default sqlite database in the C<share> directory.
899  
900 dbic-migration install --schema_class MyApp::Schema --to_version 2
901  
902 Same as the previous command, but installs version 2, instead of whatever is
903 the most recent version
904  
905 dbic-migration populate --schema_class MyApp::Schema --fixture_set seed
906  
907 Populates the C<seed> fixture set to the current database (matches the database
908 version to the seed version.)
909  
910 =head1 SEE ALSO
911  
912 L<DBIx::Class::Migration>
913  
914 =head1 AUTHOR
915  
916 See L<DBIx::Class::Migration> for author information
917  
918 =head1 COPYRIGHT & LICENSE
919  
920 See L<DBIx::Class::Migration> for copyright and license information
921  
922 =cut
923  
« Catalyst/TraitFor/Model/DBIC/Schema/FromMigration.pm ↑ Summary DBIx/Class/Migration/RunScript.pm »