DBIx/Class/Migration/Script.pm

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