DBIx/Class/Migration.pm

filter: Statementstmt 87.3% 241 / 276 Branchbran 51.3% 39 / 76 Conditioncond 57.1% 8 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 89.0% 65 / 73 Podpod 61.5% 16 / 26 totaltotal 78.2% 371 / 474 CC 49 SCAR 11.1
CC
49
Cov
79%
install_if_needed
20.0
delete_named_sets
6.0
_delete_path
6.0
CRAP 3.0
« DBIx/Class/Migration/RunScript/Trait/Populate.pm ↑ Summary DBIx/Class/Migration/ShareDirBuilder.pm »
1 package DBIx::Class::Migration;
2  
3 9 our $VERSION = "0.075";
4 9 $VERSION = eval $VERSION;
5  
6 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use JSON::MaybeXS qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use File::Copy 'cp';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::Spec::Functions 'catdir', 'catfile', 'updir';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use File::Path 'mkpath', 'remove_tree';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use DBIx::Class::Migration::Types -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Devel::PartialDump;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use SQL::Translator;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Log::Any '$log', default_adapter => 'Stderr';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶sub _log_die {
Subroutine _log_die: called CC=1 SCAR=0.0
18 1 my ($msg) = @_;
19 1 $log->error($msg);
20 1 croak $msg;
21 }
22  
23 9 has db_sandbox_class => (
24 is => 'ro',
25 default => 'DBIx::Class::Migration::SqliteSandbox',
26 isa => LoadableClass,
27 );
28  
29 9 has db_sandbox => (is=>'lazy');
30  
31 9 has db_sandbox_dir => (is=>'ro', predicate=>'has_db_sandbox_dir', isa=>Str);
32  
33 9 has db_sandbox_builder_class => (
34 is => 'lazy',
35 isa => LoadableClass,
36 );
37  
38 12 ▶ sub _build_db_sandbox_builder_class {
Subroutine _build_db_sandbox_builder_class: called CC=2 SCAR=7.2
39 12 my $self = shift;
40 12 ▶ return $self->has_db_sandbox_dir ?
Branch: 1 branch
BranchTrueFalseTotal
$self->has_db_sandbox_dir ? : 0 12 12
41 'DBIx::Class::Migration::SandboxDirSandboxBuilder' :
42 'DBIx::Class::Migration::TargetDirSandboxBuilder';
43 }
44  
45 9 has db_sandbox_builder => (is=>'lazy');
46  
47 12 ▶ sub _build_db_sandbox_builder {
Subroutine _build_db_sandbox_builder: called CC=1 SCAR=0.0
48 12 my $self = shift;
49 12 $self->db_sandbox_builder_class
50 ->new(migration=>$self);
51 }
52  
53 12 ▶ sub _build_db_sandbox {
Subroutine _build_db_sandbox: called CC=1 SCAR=0.0
54 12 shift->db_sandbox_builder->build;
55 }
56  
57 9 has schema_class => (
58 is => 'ro',
59 predicate=>'has_schema_class',
60 required=>0,
61 isa => LoadableDBICSchemaClass,
62 coerce=>1);
63  
64 9 has schema_args => (is=>'lazy', isa=>ArrayRef);
65  
66 13 ▶ sub _build_schema_args {
Subroutine _build_schema_args: called CC=1 SCAR=0.0
67 13 +[ shift->db_sandbox->make_sandbox ];
68 }
69  
70 9 has schema => (is=>'lazy', predicate=>'has_schema');
71  
72 13 ▶ sub _build_schema {
Subroutine _build_schema: called CC=1 SCAR=0.0
73 13 my ($self) = @_;
74 13 $self->schema_class->connect(@{$self->schema_args});
75 }
76  
77 9 has target_dir_builder_class => (
78 is => 'ro',
79 default => 'DBIx::Class::Migration::ShareDirBuilder',
80 isa => LoadableClass,
81 );
82  
83 9 has target_dir_builder => (is => 'lazy');
84  
85 23 ▶ sub _infer_schema_class {
Subroutine _infer_schema_class: called CC=3 SCAR=14.2
86 23 my $self = shift;
87 23 ▶ return $self->has_schema_class ?
Branch: 2 branches
BranchTrueFalseTotal
$self->has_schema_class ? : 23 0 23
$self->has_schema ? : 0 0 0
88 $self->schema_class : $self->has_schema ?
89 ref($self->schema) :
90 _log_die "Can't infer schema class without a --schema or --schema_class";
91 }
92  
93 1 ▶ sub _build_target_dir_builder {
Subroutine _build_target_dir_builder: called CC=1 SCAR=0.0
94 1 my $inferred_schema_class = (my $self = shift)
95 ->_infer_schema_class;
96 1 $self->target_dir_builder_class
97 ->new(schema_class=>$inferred_schema_class);
98 }
99  
100 9 has target_dir => (is => 'lazy', isa=> AbsolutePath, coerce => 1);
101  
102 1 ▶ sub _build_target_dir {
Subroutine _build_target_dir: called CC=1 SCAR=0.0
103 1 shift->target_dir_builder->build;
104 }
105  
106 9 has dbic_dh_args => (
107 is=>'rw', isa=>HashRef,
108 lazy => 1, builder => '_build_dbic_dh_args',
109 );
110  
111 8 ▶ sub _build_dbic_dh_args {
Subroutine _build_dbic_dh_args: called CC=1 SCAR=0.0
112 8 +{ sql_translator_args => { quote_identifiers => 1 } }
113 }
114  
115 9 has schema_loader_class => (
116 is => 'ro',
117 default => 'DBIx::Class::Migration::SchemaLoader',
118 isa => LoadableClass,
119 );
120  
121 9 has schema_loader => (is=>'lazy');
122  
123 4 ▶ sub _build_schema_loader {
Subroutine _build_schema_loader: called CC=1 SCAR=0.0
124 4 my $self = shift;
125 4 $self->schema_loader_class->new(
126 schema=>$self->schema);
127 }
128  
129 9 has dbic_fixture_class => (
130 is => 'ro',
131 default => 'DBIx::Class::Fixtures',
132 isa => LoadableClass,
133 );
134  
135 9 has dbic_fixtures_extra_args => (is=>'lazy', isa=>HashRef);
136  
137 7 ▶ sub _build_dbic_fixtures_extra_args {
Subroutine _build_dbic_fixtures_extra_args: called CC=1 SCAR=0.0
138 7 return +{};
139 }
140  
141 9 has deployment_handler_class => (
142 is => 'ro',
143 default => 'DBIx::Class::DeploymentHandler',
144 isa => LoadableClass,
145 );
146  
147 9 has extra_schemaloader_args => (is=>'lazy', isa=>HashRef);
148  
149 4 ▶ sub _build_extra_schemaloader_args {
Subroutine _build_extra_schemaloader_args: called CC=1 SCAR=0.0
150 4 return +{};
151 }
152  
153 15 ▶ sub _infer_database_from_storage {
Subroutine _infer_database_from_storage: called CC=1 SCAR=0.0
154 15 return (ref(shift) =~m/DBI::(.+)$/)[0];
155 }
156  
157 15 ▶ sub _normalize_inferred_storage {
Subroutine _normalize_inferred_storage: called CC=3 SCAR=11.4
158 15 my $inferred_storage = shift;
159 15 ▶ return 'MySQL' if $inferred_storage eq 'mysql';
Branch: 1 branch
BranchTrueFalseTotal
if $inferred_storage eq "mysql" 0 15 15
160 15 ▶ return 'PostgreSQL' if $inferred_storage eq 'Pg';
Branch: 1 branch
BranchTrueFalseTotal
if $inferred_storage eq "Pg" 0 15 15
161 15 return $inferred_storage;
162 }
163  
164 15 ▶ sub _infer_database_from_schema {
Subroutine _infer_database_from_schema: called CC=2 SCAR=8.4
165 15 (my $storage = shift->storage)
166 ->ensure_connected;
167 15 ▶ if(my $inferred_storage = _infer_database_from_storage($storage)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $inferred_storage = _infer_database_from_storage($storage)) { } 15 0 15
168 15 return _normalize_inferred_storage($inferred_storage);
169 } else {
170 0 print "Cannot infer target database, defaulting to SQLite\n";
171 0 return 'SQLite';
172 }
173 }
174  
175 111 ▶sub normalized_dbic_dh_args {
Subroutine normalized_dbic_dh_args: called CC=2 SCAR=6.9
Pod undocumented
176 111 my $self = shift;
177 111 my %args = %{$self->dbic_dh_args};
178 111 ▶ unless($args{databases}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"databases"}) 14 97 111
179 14 $args{databases} = [_infer_database_from_schema($self->schema)];
180 14 $self->dbic_dh_args(\%args); ## Cache the normalization for next run
181 }
182  
183 111 return %{$self->dbic_dh_args};
184 }
185  
186 111 ▶sub dbic_dh {
Subroutine dbic_dh: called CC=2 SCAR=6.9
Pod documented
187 111 my ($self, @args) = @_;
188 111 my %dbic_dh_args = $self->normalized_dbic_dh_args;
189  
190 111 ▶ _log_die "A \$VERSION needs to be specified in your schema class ${\$self->_infer_schema_class}"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->schema->schema_version 1 110 111
191 unless $self->schema->schema_version;
192  
193 110 my $dh = $self->deployment_handler_class->new({
194 schema => $self->schema,
195 %dbic_dh_args, @args,
196 script_directory => catdir($self->target_dir, 'migrations'),
197 });
198  
199 110 return $dh;
200 }
201  
202 8 ▶sub prepare_install { shift->dbic_dh->prepare_install }
Subroutine prepare_install: called CC=1 SCAR=0.0
Pod undocumented
203 1 ▶sub prepare_upgrade { shift->dbic_dh->prepare_upgrade }
Subroutine prepare_upgrade: called CC=1 SCAR=0.0
Pod undocumented
204 1 ▶sub prepare_downgrade { shift->dbic_dh->prepare_downgrade }
Subroutine prepare_downgrade: called CC=1 SCAR=0.0
Pod undocumented
205 7 ▶sub install { shift->dbic_dh->install }
Subroutine install: called CC=1 SCAR=0.0
Pod documented
206 1 ▶sub upgrade { shift->dbic_dh->upgrade }
Subroutine upgrade: called CC=1 SCAR=0.0
Pod documented
207  
208 sub downgrade {
209 1 my ($self, %args) = @_;
210 1 ▶ unless($self->dbic_dh_args->{to_version}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dbic_dh_args->{"to_version"}) 1 0 1
211 1 my $to_version = $self->dbic_dh->schema_version;
212 1 ▶ $to_version = $to_version->numify if ref $to_version; # version object
Branch: 1 branch
BranchTrueFalseTotal
if ref $to_version 1 0 1
213 1 $to_version -= 1;
214 1 warn "No to_version is specified, downgrading to version $to_version";
215 1 $args{to_version} = $to_version;
216 }
217 return $self->dbic_dh(%args)->downgrade;
218 }
219  
220  
221 1 ▶before 'downgrade', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
222 1 my ($self, $args) = @_;
223 9 };
224  
225  
226 0 ▶sub dump { Devel::PartialDump->new->dump(shift) }
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
227  
228 0 ▶sub version { print "Application version is $VERSION\n" }
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
229  
230 3 ▶sub status {
Subroutine status: called CC=2 SCAR=7.6
Pod documented
231 3 my $dbic_dh = shift->dbic_dh;
232 2 print "Schema is ${\$dbic_dh->schema_version}\n";
233 2 ▶ if($dbic_dh->version_storage_is_installed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbic_dh->version_storage_is_installed) { } 0 2 2
234 0 print "Deployed database is ${\$dbic_dh->database_version}\n";
235 } else {
236 2 print "Database is not currently installed\n";
237 }
238 }
239  
240 8 ▶sub _create_file_at_path {
Subroutine _create_file_at_path: called CC=2 SCAR=7.0
241 8 my ($path, $data) = @_;
242 8 ▶ open(my $fh, '>', $path)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or _log_die("Can't create ${path}: $!") 8 0 8
243 || _log_die "Can't create $path: $!";
244 8 print $fh $data;
245 8 close $fh;
246 }
247  
248 8 ▶sub _sets_data_from_sources {
Subroutine _sets_data_from_sources: called CC=1 SCAR=0.0
249 8 map { +{
250 40 class=> $_,
251 quantity => "all",
252 } } @_;
253 }
254  
255 8 ▶sub _create_all_fixture_config_from_sources {
Subroutine _create_all_fixture_config_from_sources: called CC=1 SCAR=0.0
256 8 JSON->new->pretty(1)->encode({
257 "belongs_to" => { "fetch" => 0 },
258 "has_many" => { "fetch" => 0 },
259 "might_have" => { "fetch" => 0 },
260 "sets" => [ _sets_data_from_sources(@_) ],
261 });
262 }
263  
264 8 ▶sub _filter_private_sources { grep {$_!~/^__/} @_ }
Subroutine _filter_private_sources: called CC=1 SCAR=0.0
265  
266 8 ▶sub _filter_views {
Subroutine _filter_views: called CC=1 SCAR=0.0
267 8 my ($self, @sources) = @_;
268 8 grep { ref($self->schema->source($_)) !~ m/View$/ } @sources;
269 }
270  
271 24 ▶sub _prepare_fixture_conf_dir {
Subroutine _prepare_fixture_conf_dir: called CC=2 SCAR=6.9
272 24 my ($dir, $version) = @_;
273 24 my $fixture_conf_dir = catdir($dir,
274 'fixtures', $version, 'conf');
275  
276 24 ▶ mkpath($fixture_conf_dir)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $fixture_conf_dir 8 16 24
277 unless -d $fixture_conf_dir;
278  
279 24 return $fixture_conf_dir;
280 }
281  
282 8 ▶sub _create_all_fixture_set {
Subroutine _create_all_fixture_set: called CC=1 SCAR=0.0
283 8 my $path = shift;
284 8 my $conf = _create_all_fixture_config_from_sources(@_);
285 8 _create_file_at_path($path, $conf);
286 }
287  
288 8 ▶sub _has_previous_version {
Subroutine _has_previous_version: called CC=3 SCAR=11.1
289 8 ▶ return 0 if !$_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 8 8
290 8 ▶ return $_[0]->numify - 1 if ref $_[0]; # version object
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 1 7 8
291 7 $_[0]-1
292 }
293  
294 1 ▶sub _only_from_when_not_to {
Subroutine _only_from_when_not_to: called CC=1 SCAR=0.0
295 1 my ($from_dir, $to_dir) = @_;
296 grep {
297 1 not -e catfile($to_dir, ($_ =~ /\/([^\/]+)$/))
298 9 ▶ } <$from_dir/*>;
Subroutine BEGIN: called CC=1 SCAR=0.0
299 }
300  
301 1 ▶sub _copy_from_to {
Subroutine _copy_from_to: called CC=3 SCAR=12.7
302 1 my ($from_dir, $to_dir) = @_;
303 1 print "Copying Fixture Confs from $from_dir to $to_dir\n";
304 (cp($_, $to_dir)
305 || _log_die "Could not copy $_: $!")
306 1 ▶ for _only_from_when_not_to($from_dir, $to_dir);
Branch: 1 branch
BranchTrueFalseTotal
cp($_, $to_dir) or _log_die("Could not copy ${_}: $!") 0 0 0
307 }
308  
309 1 ▶sub prepare_up_down_grades {
Subroutine prepare_up_down_grades: called CC=3 SCAR=12.3
Pod undocumented
310 1 my ($self, $previous, $schema_version) = @_;
311 1 ▶ $self->dbic_dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
$self->dbic_dh->version_storage_is_installed or _log_die("No Database to create up or downgrades from!") 1 0 1
312 || _log_die "No Database to create up or downgrades from!";
313  
314 1 ▶ if($self->dbic_dh->database_version < $schema_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dbic_dh->database_version < $schema_version) { } 1 0 1
315 1 $self->prepare_upgrade;
316 1 $self->prepare_downgrade;
317 } else {
318 0 print "Your Database version must be lower than than your schema version\n";
319 0 print "in order to prepare upgrades / downgrades\n";
320 }
321 }
322  
323 8 ▶sub prepare {
Subroutine prepare: called CC=3 SCAR=11.0
Pod documented
324 8 my $self = shift;
325 8 ▶ my $schema_version = $self->dbic_dh->schema_version
Condition33%1/3
$self->dbic_dh->schema_version || _log_die('Your Schema has no version!')
l!l&&r!l&&!r
800
MC/DC0%0/2
$self->dbic_dh->schema_version || _log_die('Your Schema has no version!')
$self->dbic_dh->schema_version _log_die('Your Schema has no version!')
Truth table33%1/3
A || B
A= $self->dbic_dh->schema_versionB= _log_die('Your Schema has no version!')
ABresult
1X1
011
000
326 || _log_die "Your Schema has no version!";
327  
328 8 $self->prepare_install;
329 8 my $fixture_conf_dir = _prepare_fixture_conf_dir(
330 $self->target_dir, $schema_version);
331  
332 8 my @sources = _filter_private_sources($self->schema->sources);
333 8 my @real_tables = $self->_filter_views(@sources);
334 8 my $all_tables_path = catfile($fixture_conf_dir,'all_tables.json');
335 8 _create_all_fixture_set($all_tables_path, @real_tables);
336  
337 8 ▶ if(my $previous = _has_previous_version($schema_version)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $previous = _has_previous_version($schema_version)) { } 1 7 8
338 1 $self->prepare_up_down_grades($previous, $schema_version);
339 1 my $previous_fixtures_conf = _prepare_fixture_conf_dir(
340 $self->target_dir, $previous);
341 1 _copy_from_to($previous_fixtures_conf, $fixture_conf_dir);
342 } else {
343 7 print "There is no current database deployed, so I can't prepare upgrades\n";
344 7 print "or downgrades\n";
345 }
346 }
347  
348 1 ▶sub drop_tables {
Subroutine drop_tables: called CC=1 SCAR=0.0
Pod documented
349 1 my $self = shift;
350 1 my $schema = $self->_schema_from_database;
351  
352 1 ▶ $schema->storage->with_deferred_fk_checks(sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
353 1 foreach my $source ($schema->sources) {
354 6 my $table = $schema->source($source)->name;
355 6 print "Dropping table $table\n";
356 6 my $tableq = $schema->storage->dbh->quote_identifier($table);
357 6 ▶ if(ref($schema->storage) =~m/Pg$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($schema->storage) =~ /Pg$/) { } 0 6 6
358 0 $schema->storage->dbh->do("drop table $tableq CASCADE");
359 } else {
360 6 $schema->storage->dbh->do("drop table $tableq");
361 }
362 }
363 1 });
364 }
365  
366 2 ▶sub delete_table_rows {
Subroutine delete_table_rows: called CC=1 SCAR=0.0
Pod documented
367 2 my $self = shift;
368 2 my $schema = $self->_schema_from_database;
369  
370 2 ▶ $schema->storage->with_deferred_fk_checks(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
371 2 my $txn = $schema->txn_scope_guard;
372 2 foreach my $source ($schema->sources) {
373 12 ▶ next if ($source eq 'DbixClassDeploymenthandlerVersion' ||
Branch: 1 branch
BranchTrueFalseTotal
if $source eq "DbixClassDeploymenthandlerVersion" or $source =~ /^__/ 2 10 12
Condition66%2/3
$source eq "DbixClassDeploymenthandlerVersion" or $source =~ /^__/
l!l&&r!l&&!r
2010
MC/DC50%1/2
$source eq "DbixClassDeploymenthandlerVersion" or $source =~ /^__/
$source eq "DbixClassDeploymenthandlerVersion" $source =~ /^__/
Truth table66%2/3
A or B
A= $source eq "DbixClassDeploymenthandlerVersion"B= $source =~ /^__/
ABresult
1X1
011
000
374 $source =~ m/^__/);
375 10 $schema->resultset($source)->delete;
376 }
377 2 $txn->commit;
378 2 });
379 }
380  
381 2 ▶sub _prepare_fixture_data_dir {
Subroutine _prepare_fixture_data_dir: called CC=2 SCAR=7.0
382 2 my ($dir, $version, $set) = @_;
383 2 my $fixture_conf_dir = catdir($dir, 'fixtures', $version, $set);
384 2 ▶ mkpath($fixture_conf_dir)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $fixture_conf_dir 0 2 2
385 unless -d $fixture_conf_dir;
386 2 return $fixture_conf_dir;
387 }
388  
389 15 ▶sub build_dbic_fixtures_init_args {
Subroutine build_dbic_fixtures_init_args: called CC=4 SCAR=13.9
Pod undocumented
390 15 my $self = shift;
391 15 my $version = $self->dbic_dh_args->{to_version};
392 $version ||= $self->dbic_dh->version_storage_is_installed ?
393 15 ▶ $self->dbic_dh->database_version : do {
Branch: 1 branch
BranchTrueFalseTotal
$self->dbic_dh->version_storage_is_installed ? : 8 5 13
Condition66%2/3
$version ||= $self->dbic_dh->version_storage_is_installed ? $self->dbic_dh->database_version : do { print "Since this database is not versioned, we will assume version "; print "${\$self->dbic_dh->schema_version;}\n"; $self->dbic_dh->schema_version }
l!l&&r!l&&!r
2130
MC/DC0%0/2
$version ||= $self->dbic_dh->version_storage_is_installed ? $self->dbic_dh->database_version : do { print "Since this database is not versioned, we will assume version "; print "${\$self->dbic_dh->schema_version;}\n"; $self->dbic_dh->schema_version }
$version $self->dbic_dh->version_storage_is_installed ? $self->dbic_dh->database_version : do { print "Since this database is not versioned, we will assume version "; print "${\$self->dbic_dh->schema_version;}\n"; $self->dbic_dh->schema_version }
Truth table66%2/3
A ||= B
A= $versionB= $self->dbic_dh->version_storage_is_installed ? $self->dbic_dh->database_version : do { print "Since this database is not versioned, we will assume version "; print "${\$self->dbic_dh->schema_version;}\n"; $self->dbic_dh->schema_version }
ABresult
1X1
011
000
394 5 print "Since this database is not versioned, we will assume version ";
395 5 print "${\$self->dbic_dh->schema_version}\n";
396 5 $self->dbic_dh->schema_version;
397 };
398  
399 15 my $conf_dir = _prepare_fixture_conf_dir($self->target_dir, $version);
400  
401 15 print "Reading configurations from $conf_dir\n";
402  
403 return {
404 config_dir => $conf_dir,
405 debug => ($ENV{DBIC_MIGRATION_DEBUG}||0),
406 15 ▶ %{$self->dbic_fixtures_extra_args}};
Condition50%1/2
$ENV{'DBIC_MIGRATION_DEBUG'} || 0
l!l
015
MC/DC0%0/1
$ENV{'DBIC_MIGRATION_DEBUG'} || 0
$ENV{'DBIC_MIGRATION_DEBUG'}
Truth table50%1/2
A
A= $ENV{'DBIC_MIGRATION_DEBUG'}
Aresult
11
00
407 }
408  
409 6 ▶sub build_dbic_fixtures {
Subroutine build_dbic_fixtures: called CC=1 SCAR=0.0
Pod undocumented
410 6 my $dbic_fixtures = (my $self = shift)->dbic_fixture_class;
411 6 $dbic_fixtures->new($self->build_dbic_fixtures_init_args);
412 }
413  
414 9 ▶sub _schema_from_database {
Subroutine _schema_from_database: called CC=1 SCAR=0.0
415 9 my $self = shift;
416 my $schema = $self->schema_loader
417 ->schema_from_database(
418 $self->_infer_schema_class,
419 9 %{$self->extra_schemaloader_args});
420 # SQL_IDENTIFIER_QUOTE_CHAR
421 9 $schema->storage->sql_maker->quote_char($schema->storage->dbh->get_info(29));
422 9 $schema;
423 }
424  
425 1 ▶sub dump_named_sets {
Subroutine dump_named_sets: called CC=2 SCAR=7.0
Pod documented
426 1 ▶ (my $self = shift)->dbic_dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
(my $self = shift())->dbic_dh->version_storage_is_installed or print "Target DB is not versioned. Dump may not be reliable.\n" 1 0 1
427 || print "Target DB is not versioned. Dump may not be reliable.\n";
428  
429 1 my $schema = $self->_schema_from_database;
430  
431 $self->build_dbic_fixtures->dump_config_sets({
432 schema => $schema,
433 1 configs => [map { "$_.json" } @_],
434 1 ▶ directory_template => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
435 1 my ($fixture, $params, $set) = @_;
436 1 $set =~s/\.json//;
437 1 my $fixture_conf_dir = catfile($fixture->config_dir,updir,$set);
438 1 ▶ mkpath($fixture_conf_dir)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $fixture_conf_dir 1 0 1
439 unless -d $fixture_conf_dir;
440 1 return $fixture_conf_dir;
441 },
442 1 });
443 }
444  
445 3 ▶sub dump_all_sets {
Subroutine dump_all_sets: called CC=2 SCAR=7.0
Pod documented
446 3 ▶ (my $self = shift)->dbic_dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
(my $self = shift())->dbic_dh->version_storage_is_installed or print "Target DB is not versioned. Dump may not be reliable.\n" 3 0 3
447 || print "Target DB is not versioned. Dump may not be reliable.\n";
448  
449 3 my $schema = $self->_schema_from_database;
450  
451 $self->build_dbic_fixtures->dump_all_config_sets({
452 schema => $schema,
453 3 ▶ directory_template => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
454 3 my ($fixture, $params, $set) = @_;
455 3 $set =~s/\.json//;
456 3 my $fixture_conf_dir = catfile($fixture->config_dir,updir,$set);
457 3 ▶ mkpath($fixture_conf_dir)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $fixture_conf_dir 3 0 3
458 unless -d $fixture_conf_dir;
459 3 return $fixture_conf_dir;
460 },
461 3 });
462 }
463  
464 0 ▶sub delete_named_sets {
Subroutine delete_named_sets: not called CC=2 SCAR=17.9
Pod documented
465 0 my ($self, @sets) = @_;
466 0 my $fixtures = $self->build_dbic_fixtures;
467 0 my @paths = map { catfile($fixtures->config_dir,updir,$_) } @sets;
468 0 foreach my $path (@paths) {
469 0 $self->_delete_path($path);
470 }
471 }
472  
473 0 ▶ sub _delete_path {
Subroutine _delete_path: not called CC=2 SCAR=17.9
474 0 my ($self, $path) = @_;
475 0 ▶ return unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 0 0
476 0 print "Deleting $path \n";
477 0 remove_tree($path);
478 }
479  
480  
481 2 ▶sub populate_set_to_schema {
Subroutine populate_set_to_schema: called CC=1 SCAR=0.0
Pod undocumented
482 2 my ($self, $target_set, $schema) = @_;
483 2 $self->build_dbic_fixtures->populate({
484 no_deploy => 1,
485 schema => $schema,
486 directory => $target_set });
487  
488 2 print "Restored set $target_set to database\n";
489 }
490  
491 2 ▶sub populate {
Subroutine populate: called CC=3 SCAR=11.0
Pod documented
492 2 ▶ (my $self = shift)->dbic_dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
(my $self = shift())->dbic_dh->version_storage_is_installed or _log_die("No Database to populate!") 2 0 2
493 || _log_die "No Database to populate!";
494  
495 2 my $version = $self->dbic_dh->database_version;
496 2 my $schema = $self->_schema_from_database;
497  
498 2 foreach my $set(@_) {
499 2 my $target_set = _prepare_fixture_data_dir(
500 $self->target_dir, $version, $set);
501 2 $self->populate_set_to_schema($target_set, $schema);
502 }
503 }
504  
505 0 ▶sub make_schema {
Subroutine make_schema: not called CC=2 SCAR=17.9
Pod documented
506 0 ▶ (my $self = shift)->dbic_dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
(my $self = shift())->dbic_dh->version_storage_is_installed or _log_die("No Database to make Schema from!") 0 0 0
507 || _log_die "No Database to make Schema from!";
508 0 my $schema = $self->schema_loader
509 ->generate_dump(
510 $self->_infer_schema_class,
511 catdir($self->target_dir, 'dumped_db'));
512 }
513  
514 0 ▶sub diagram {
Subroutine diagram: not called CC=2 SCAR=17.9
Pod documented
515 0 my $self = shift;
516 0 my $number_tables = scalar $self->schema->sources;
517 0 my $dimension = int sqrt($number_tables * 13);
518 0 my $trans = SQL::Translator->new(
519 parser => 'SQL::Translator::Parser::DBIx::Class',
520 parser_args => { dbic_schema => $self->schema },
521 producer => 'GraphViz',
522 producer_args => {
523 skip_tables => 'dbix_class_deploymenthandler_versions',
524 width => $dimension,
525 height => $dimension,
526 show_constraints => 1,
527 show_datatypes => 1,
528 show_sizes => 1,
529 out_file => $self->_diagram_default_outfile });
530  
531 0 ▶ $trans->translate
Branch: 1 branch
BranchTrueFalseTotal
$trans->translate or _log_die($trans->error) 0 0 0
532 or _log_die $trans->error;
533 }
534  
535 0 ▶ sub _diagram_default_outfile {
Subroutine _diagram_default_outfile: not called CC=1 SCAR=6.9
536 0 my $self = shift;
537 0 catfile $self->target_dir, 'db-diagram-v' . $self->dbic_dh->schema_version . '.png';
538 }
539  
540 0 ▶sub install_if_needed {
Subroutine install_if_needed: not called CC=4 SCAR=30.0
Pod documented
541 0 my ($self, %callbacks) = @_;
542 0 ▶ if(!$self->dbic_dh->version_storage_is_installed) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->dbic_dh->version_storage_is_installed) 0 0 0
543 0 $self->install;
544 0 ▶ if(my $on_install = delete($callbacks{on_install})) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $on_install = delete $callbacks{'on_install'}) { } 0 0 0
elsif (my $default_fixture_sets = delete $callbacks{'default_fixture_sets'}) { } 0 0 0
545 0 $on_install->($self->schema, $self);
546 } elsif( my $default_fixture_sets = delete($callbacks{default_fixture_sets})) {
547 0 $self->populate(@$default_fixture_sets);
548 }
549 }
550 }
551  
552 1 ▶sub install_version_storage {
Subroutine install_version_storage: called CC=2 SCAR=7.2
Pod documented
553 1 my $self = shift;
554 1 ▶ if(!$self->dbic_dh->version_storage_is_installed) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->dbic_dh->version_storage_is_installed) { } 1 0 1
555 1 $self->dbic_dh->install_version_storage;
556 1 $self->dbic_dh->add_database_version({ version => $self->dbic_dh->schema_version });
557 1 print "Version storage has been installed in the target database\n";
558 } else {
559 0 print "Version storage is already installed in the target database!\n";
560 }
561 }
562  
563 8 ▶before [qw/install upgrade downgrade/], sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
564 8 my ($self, @args) = @_;
565 8 ▶ %ENV = (
Branch: 1 branch
BranchTrueFalseTotal
$self->dbic_dh->version_storage_is_installed ? : 2 6 8
566 %ENV,
567 DBIC_MIGRATION_FIXTURES_CLASS => $self->dbic_fixture_class,
568 DBIC_MIGRATION_FIXTURES_INIT_ARGS => JSON::MaybeXS->new->encode($self->build_dbic_fixtures_init_args),
569 DBIC_MIGRATION_SCHEMA_CLASS => $self->schema_class,
570 DBIC_MIGRATION_TARGET_DIR => $self->target_dir,
571 DBIC_MIGRATION_FIXTURE_DIR => catdir($self->target_dir, 'fixtures', $self->dbic_dh->schema_version),
572 DBIC_MIGRATION_SCHEMA_VERSION => $self->dbic_dh->schema_version,
573 DBIC_MIGRATION_TO_VERSION => $self->dbic_dh->to_version,
574 DBIC_MIGRATION_DATABASE_VERSION => (
575 $self->dbic_dh->version_storage_is_installed ? $self->dbic_dh->database_version : 0),
576 );
577 9 };
578  
579 # We need to explicitly disconnect so that we can properly
580 # shutdown some databases (like Postgresql) without generating
581 # errors in cleanup. Basically if we don't disconnect we often
582 # end up with blocking commands running on the server at the time
583 # we are trying to shut it down.
584  
585 16 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=4 SCAR=14.1
Pod undocumented
586 16 my $self = shift;
587 16 ▶ return unless $self->has_schema && $self->schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_schema and $self->schema 1 15 16
Condition66%2/3
$self->has_schema and $self->schema
!ll&&!rl&&r
1015
MC/DC50%1/2
$self->has_schema and $self->schema
$self->has_schema $self->schema
Truth table66%2/3
A and B
A= $self->has_schemaB= $self->schema
ABresult
0X0
100
111
588 15 ▶ if(my $storage = $self->schema->storage) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $storage = $self->schema->storage) 15 0 15
589 15 $storage->disconnect;
590 }
591 }
592  
593 9 __PACKAGE__->meta->make_immutable;
594  
595 =head1 NAME
596  
597 DBIx::Class::Migration - Use the best tools together for sane database migrations
598  
599 =head1 SYNOPSIS
600  
601 use DBIx::Class::Migration;
602  
603 my $migration = DBIx::Class::Migration->new(
604 schema_class => 'MyApp::Schema',
605 schema_args => \@connect_opts);
606  
607 Alternatively:
608  
609 use DBIx::Class::Migration;
610 use MyApp::Schema;
611  
612 my $migration = DBIx::Class::Migration->new(
613 schema => MyApp::Schema->connect(@connect_opts));
614  
615 Informational Commands:
616  
617 $migration->status;
618  
619 Preparing and using Migrations:
620  
621 $migration->prepare;
622 $migration->install;
623 $migration->upgrade;
624 $migration->downgrade;
625  
626 Commands for working with Fixtures:
627  
628 $migration->dump_named_sets;
629 $migration->dump_all_sets;
630 $migration->populate;
631  
632 Utility Commands:
633  
634 $migration->drop_tables;
635 $migration->delete_table_rows;
636 $migration->make_schema;
637 $migration->install_if_needed;
638 $migration->install_version_storage;
639 $migration->diagram;
640  
641 =head1 DESCRIPTION
642  
643 L<DBIx::Class::DeploymentHandler> is a state of the art solution to the problem
644 of creating sane workflows for versioning L<DBIx::Class> managed database
645 projects. However, since it is more of a toolkit for building custom versioning
646 and migration workflows than an expression of a particular migration practice,
647 it might not always be the most approachable tool. If you are starting a new
648 L<DBIx::Class> project and you don't have a particular custom workflow need,
649 you might prefer to simply be given a reasonable clear and standard practice,
650 rather than a toolkit with a set of example scripts.
651  
652 L<DBIx::Class::Migration> defines some logic which combines both
653 L<DBIx::Class::DeploymentHandler> and L<DBIx::Class::Fixtures>, along with
654 a standard tutorial, to give you a simple and straightforward approach to
655 solving the problem of how to best create database versions, migrations and
656 testing data. Additionally it builds on tools like L<Test::mysqld> and
657 L<Test::Postgresql58> along with L<DBD::Sqlite> in order to assist you in quickly
658 creating a local development database sandbox. It offers some integration
659 points to testing your database, via tools like L<Test::DBIx::Class> in order to
660 make testing your database driven logic less painful. Lastly, we offer some
661 thoughts on good development patterns in using databases with application
662 frameworks like L<Catalyst>.
663  
664 L<DBIx::Class::Migration> offers code and advice based on my experience of using
665 L<DBIx::Class> for several years, which hopefully can help you bootstrap a new
666 project. The solutions given should work for you if you want to use L<DBIx::Class>
667 and have database migrations, but don't really know what to do next. These
668 solutions should scale upward from a small project to a medium project involving
669 many developers and more than one target environment (DEV -> QA -> Production.)
670 If you have very complex database versioning requirements, huge teams and
671 difficult architectual issues, you might be better off building something on
672 top of L<DBIx::Class::DeploymentHandler> directly.
673  
674 L<DBIx::Class::Migration> is a base class upon which interfaces like
675 L<DBIx::Class::Migration::Script> are built.
676  
677 Please see L<DBIx::Class::Migration::Tutorial> for more approachable
678 documentation. If you want to read a high level feature overview, see
679 L<DBIx::Class::Migration::Features>. The remainder of this POD is API level
680 documentation on the various internals.
681  
682 =head1 ATTRIBUTES
683  
684 This class defines the following attributes.
685  
686 =head2 db_sandbox_builder_class
687  
688 Accept Str. Defaults to 'DBIx::Class::Migration::TargetDirSandboxBuilder'
689  
690 The name of the helper class which builds the class that builds database
691 sandboxs. By default we build database sandboxes in the L</target_dir>, which
692 is what L<DBIx::Class::Migration::TargetDirSandboxBuilder> does. We can also
693 build database sandboxes in a temporary directory using
694 L<DBIx::Class::Migration::TempDirSandboxBuilder>. You might prefer that for
695 running tests, for example.
696  
697 =head2 db_sandbox_class
698  
699 Accepts Str. Not Required (defaults to 'DBIx::Class::Migration::SqliteSandbox').
700  
701 Unless you already have a database setup and running (as you probably do in
702 production) we need to auto create a database 'sandbox' that is isolated to
703 your development local. This class is a delegate that performs this job if you
704 don't want to go to the trouble of installing and setting up a local database
705 yourself.
706  
707 This must point to a class that expects C<target_dir> and C<schema_class> for
708 initialization arguments and must define a method C<make_sandbox> that returns
709 an array which can be sent to L<DBIx::Class::Schema/connect>.
710  
711 This defaults to L<DBIx::Class::Migration::SqliteSandbox>. Currently we have
712 support for MySQL and Postgresql via L<DBIx::Class::Migration::MySQLSandbox>
713 and L<DBIx::Class::Migration::PgSandbox>, but you will need to side install
714 L<Test::mysqld> and L<Test::Postgresql58> (In other words you'd need to add
715 these C<Test::*> namespace modules to your C<Makefile.PL> or C<dist.ini>).
716  
717 =head2 db_sandbox
718  
719 Accepts: Object. Not required.
720  
721 This is an instantiated object as defined by L</db_sandbox_class>. It is a
722 delegate for the work of automatically creating a local database sandbox that
723 is useful for developers and for quickly bootstrapping a project.
724  
725 =head2 schema_class
726  
727 Accepts Str. Not Required (but if missing, you need to populate L</schema>).
728  
729 This is the schema we use as the basis for creating, managing and running your
730 deployments. This should be the full package namespace defining your subclass
731 of L<DBIx::Class::Schema>. For example C<MyApp::Schema>.
732  
733 If the L</schema_class> cannot be loaded, a hard exception will be thrown.
734  
735 =head2 schema_args
736  
737 Accepts ArrayRef. Required but lazily builds from defaults
738  
739 Provides arguments passed to C<connect> on your L</schema_class>. Should
740 connect to a database.
741  
742 This is an arrayref that would work the same as L<DBIx::Class::Schema/connect>.
743 If you choose to create an instance of L<DBIx::Class::Migration> by providing a
744 L<schema_class>, you can use this to customize how we connect to a database.
745  
746 If you don't provide a value, we will automatically create a SQLite based
747 database connection with the following DSN:
748  
749 DBD:SQLite:[path to target_dir]/[db_file_name].db
750  
751 Where C<[path to target_dir]> is L</target_dir> and [db_file_name] is a converted
752 version of L</schema_class>. For example if you set L<schema_class> to:
753  
754 MyApp::Schema
755  
756 Then [db_file_name] would be C<myapp-schema>.
757  
758 Basically, this means you can start testing your database designs right off
759 without a lot of effort, just point at a L<schema_class> and get deploying!
760  
761 =head2 schema
762  
763 Accepts: Object of L<DBIx::Class::Schema>. Not required.
764  
765 If you already have a connected schema (subclass of L<DBIx::Class::Schema>)
766 you can simple point to it, skipping L<schema_class> and L<schema_args>. You
767 might for example be using L<Catalyst> and want to build deployments for a
768 database that is listed in configuration:
769  
770 use MyCatalyst::App;
771 use DBIx::Class::Migration;
772  
773 my $migration = DBIx::Class::Migration->new(
774 schema => MyCatalyst::App->model('Schema')->schema,
775 %{MyCatalyst::App->config->{extra_migration_init_args}};
776 );
777  
778 =head2 target_dir_builder_class
779  
780 Accepts: Str, Defaults to 'DBIx::Class::Migration::ShareDirBuilder'
781 This is a class that is used as a helper to build L</target_dir> should the
782 user not provide a value. Default is L<DBIx::Class::Migration::ShareDirBuilder>
783  
784 =head2 target_dir_builder
785  
786 An instance of whatever is in L</target_dir_builder_class>. Used by the lazy
787 build method of L</target_dir> to default a directory where the migrations are
788 actually placed.
789  
790 =head2 target_dir
791  
792 Accepts Str. Required (lazy builds to your distribution C</share> directory).
793  
794 This is the directory we store our migration and fixture files. Inside this
795 directory we will create a C<fixtures> and C<migrations> sub-directory.
796  
797 Although you can specify the directory, if you leave it undefined, we will use
798 L<File::ShareDir::ProjectDistDir> to locate the C</share> directory for your
799 project and place the files there. This is the recommended approach, and is
800 considered a community practice in regards to where to store your distribution
801 non code files. Please see L<File::ShareDir::ProjectDistDir> as well as
802 L<File::ShareDir> for more information.
803  
804 This uses whatever is in L</schema_class> to determine your project (and look
805 for a C<share> directory, which you'll need to create in your project root).
806 If you don't have a L</schema_class> defined, you must have a L</schema>,
807 and we'll infer the class via C<< ref($self->schema) >>.
808  
809 B<NOTE:> You'll need to make the C</share> directory if you are going to use
810 the default option. We don't automatically create it for you.
811  
812 =head2 schema_loader_class
813  
814 Accepts Str. Required
815  
816 Because your application subclass of L<DBIx::Class::Schema> is going to
817 change a lot, sometimes we need to generate our own schema and get one that is
818 in a known, good state. Mostly this is used by the commands to drop tables
819 and clear tables.
820  
821 Defaults to L<DBIx::Class::Migration::SchemaLoader>. You'll probably only
822 need to change this if your database is crazy and you need to massage the
823 init arguments to L<DBIx::Class::Schema::Loader>.
824  
825 =head2 schema_loader
826  
827 Accepts Object. Required but lazy builds.
828  
829 This is a factory that provider autoloaded schema based on the current schema's
830 database. It is automatically created and you are unlikely to need to set this
831 manually.
832  
833 =head2 dbic_fixture_class
834  
835 Accepts Str. Required
836  
837 This is the class we use when creating instances of L<DBIx::Class::Fixtures>.
838 You'll probably need to review the docs for that and understand how configuration
839 rules work in order to best take advantage of the system.
840  
841 Defaults to L<DBIx::Class::Fixtures>. You'll probably not need to change this
842 unless you have some unusual needs regarding fixtures.
843  
844 =head2 dbic_fixtures_extra_args
845  
846 Accepts HashRef. Required, but Defaults to Empty Hashref
847  
848 Allows you to pass some additional arguments when creating instances of
849 L</dbic_fixture_class>. These arguments can be used to override the default
850 initial arguments.
851  
852 =head2 deployment_handler_class
853  
854 Accepts Str. Required
855  
856 This is the class we use when creating instances of L<DBIx::Class::DeploymentHandler>.
857 It would be ideal that you review those docs in order to better understand the
858 overall architecture of the system.
859  
860 Defaults to L<DBIx::Class::DeploymentHandler>. You'll probably not need to
861 change this unless you need a custom deployment handler, and if you do, I
862 can't be sure this framework will work correctly, particularly if you are not
863 using monotonic versioning.
864  
865 =head2 dbic_dh_args
866  
867 Accepts HashRef. Required and defaults to a hashref setting
868 C<sql_translator_args>'s C<quote_identifiers> to a true value, which
869 despite being documented as the default, is not the case in practice.
870  
871 Used to pass custom args when building a L<DBIx::Class::DeploymentHandler>.
872 Please see the docs for that class for more. Useful args might be C<databases>,
873 C<to_version> and C<force_overwrite>.
874  
875 =head2 dbic_dh
876  
877 Accepts Instance of L<DBIx::Class::DeploymentHandler>. Required but lazily
878 built from default data and L<dbic_dh_args>.
879  
880 You probably won't need to build your own deployment handler and pass it in
881 (unlike L<schema>, where it might actually be useful). Be careful it you do
882 since this framework makes some assumptions about your deployment handler (for
883 example we assume you are using the monotonic versioning).
884  
885 When this attribute is lazily built, we merge L</dbic_dh_args> with the
886 following defaults:
887  
888 schema => Points to $self->schema
889 script_directory => Points to catdir($self->target_dir, 'migrations')
890 databases => Inferred from your connected schema, defaults to SQLite
891  
892 L</dbic_dh_args> will overwrite the defaults, if you pass them.
893  
894 =head2 extra_schemaloader_args
895  
896 Optional. Accepts a HashRef of arguments you can use to influence how
897 L<DBIx::Class::Schema::Loader> works. This HashRef would get passed as
898 C<loader_options> (see L<DBIx::Class::Schema::Loader/make_schema_at>.
899  
900 Meaningful options are described at L<DBIx::Class::Schema::Loader::Base>.
901  
902 =head1 METHODS
903  
904 This class defines the following methods for public use
905  
906 =head2 new
907  
908 Used to create an new instance of L<DBIx::Class::Migration>. There's a couple
909 of paths to creating this instance.
910  
911 =head3 Specify a schema_class and optionally schema_args
912  
913 use DBIx::Class::Migration;
914 my $migration = DBIx::Class::Migration->new(
915 schema_class => 'MyApp::Schema',
916 schema_args => [@connect_info],
917 );
918  
919 This is probably the most general approach, and is recommended unless you
920 already have a connected instance of your L<DBIx::Class::Schema> subclass.
921  
922 L</schema_args> would be anything you'd pass to L<DBIx::Class::Schema/connect>.
923 see L</schema_args> for how we construct default connect information if you
924 choose to leave this undefined.
925  
926 =head3 Specify a schema
927  
928 There may be some cases when you already have a schema object constructed and
929 would prefer to just use that. For example, you may be using L<Catalyst> and
930 wish to build custom scripts using the built-in dependency and service lookup:
931  
932 use MyCatalyst::App;
933 use DBIx::Class::Migration;
934  
935 my $migration = DBIx::Class::Migration->new(
936 schema => MyCatalyst::App->model('Schema')->schema,
937 %{MyCatalyst::App->config->{extra_migration_init_args}};
938 );
939  
940 Be careful of potential locking issues when using some databases like SQLite.
941  
942 =head3 OPTIONAL: Specify a target_dir
943  
944 Optionally, you can specify your migrations target directory (where your
945 migrations get created), in your init arguments. This option can be combined
946 with either approach listed above.
947  
948 use DBIx::Class::Migration;
949 my $migration = DBIx::Class::Migration->new(
950 schema_class => 'MyApp::Schema',
951 schema_args => [@connect_info],
952 target_dir => '/opt/database-migrations',
953 );
954  
955 If you leave this undefined we default to using the C<share> directory in your
956 distribution root. This is generally the community supported place for non
957 code data, but if you have huge fixture sets you might wish to place them in
958 an alternative location.
959  
960 =head3 OPTIONAL: Specify a db_sandbox_dir
961  
962 Be default if you allow for a local database sandbox (as you might during early
963 development and you don't want to work to make a database) that sandbox gets
964 built in the 'target_dir'. Since other bits in the target_dir are probably
965 going to be in your project repository and the sandbox generally isnt, you
966 might wish to build the sandbox in an alternative location. This setting
967 allows that:
968  
969 use DBIx::Class::Migration;
970 my $migration = DBIx::Class::Migration->new(
971 schema_class => 'MyApp::Schema',
972 schema_args => [@connect_info],
973 db_sandbox_dir => '~/opt/database-sandbox',
974 );
975  
976 This then gives you a nice totally standalone database sandbox which you can
977 reuse for other projects, etc.
978  
979 =head3 OPTIONAL: Specify dbic_dh_args
980  
981 Optionally, you can specify additional arguments to the constructor for the
982 L</dbic_dh> attribute. Useful arguments might include additional C<databases>
983 we should build fixtures for, C<to_version> and C<force_overwrite>.
984  
985 See L<DBIx::Class::DeploymentHandler/> for more information on supported init
986 arguments. See L</dbic_dh> for how we merge default arguments with your custom
987 arguments.
988  
989 =head3 Other Initial Arguments
990  
991 For normal usage the remaining init args are probably not particularly useful
992 and reflect a desire for long term code flexibility and clean design.
993  
994 =head2 version
995  
996 Prints to STDOUT a message regarding the version of L<DBIC:Migration> that you
997 are currently running.
998  
999 =head2 status
1000  
1001 Returns the state of the deployed database (if it is deployed) and the state
1002 of the current C<schema> version. Sends this as a string to STDOUT
1003  
1004 =head2 prepare
1005  
1006 Creates a C<fixtures> and C<migrations> directory under L</target_dir> (if they
1007 don't already exist) and makes deployment files for the current schema. If
1008 deployment files exist, will fail unless you L</overwrite_migrations>.
1009  
1010 The C<migrations> directory reflects a directory structure as documented in
1011 L<DBIx::Class::DeploymentHandler>.
1012  
1013 If this is the first version, we create directories and initial DLL, etc. For
1014 versions greater than 1, we will also generate diffs and copy any fixture
1015 configs etc (as well as generating a fresh 'all_table.json' fixture config). For
1016 safety reasons, we never overwrite any fixture configs.
1017  
1018 =head2 install
1019  
1020 Installs either the current schema version (if already prepared) or the target
1021 version specified via any C<to_version> flags sent as an L<dbic_dh_args> to the
1022 database which is connected via L</schema>.
1023  
1024 If you try to install to a database that has already been installed, you'll get
1025 an error. See L</drop_tables>.
1026  
1027 =head2 upgrade
1028  
1029 Run upgrade files to bring the database into sync with the current schema
1030 version.
1031  
1032 =head2 downgrade
1033  
1034 Run down files to bring the database down to the previous version from what is
1035 installed to the database
1036  
1037 =head2 drop_tables
1038  
1039 Drops all the tables in the connected database with no backup or recovery. For
1040 real! (Make sure you are not connected to Prod, for example).
1041  
1042 =head2 delete_table_rows
1043  
1044 Does a C<delete> on each table in the database, which clears out all your data
1045 but preserves tables. For Real! You might want this if you need to load
1046 and unload fixture sets during testing, or perhaps to get rid of data that
1047 accumulated in the database while running an app in development, before dumping
1048 fixtures.
1049  
1050 =head2 dump_named_sets
1051  
1052 Given an array of fixture set names, dump them for the current database version.
1053  
1054 =head2 dump_all_sets
1055  
1056 Takes no arguments just dumps all the sets we can find for the current database
1057 version.
1058  
1059 =head2 make_schema
1060  
1061 Given an existing database, reverse engineer a L<DBIx::Class> Schema in the
1062 L</target_dir> (under C<dumped_db>). You can use this if you need to bootstrap
1063 your DBIC files.
1064  
1065 =head2 populate
1066  
1067 Given an array of fixture set names, populate the current database version with
1068 the matching sets for that version.
1069  
1070 Skips the table C<dbix_class_deploymenthandler_versions>, so you don't lose
1071 deployment info (this is different from L</drop_tables> which does delete it.)
1072  
1073 =head2 diagram
1074  
1075 Experimental feature. Although not specifically a migration task, I find it
1076 useful to output visuals of my databases. This command will place a file in
1077 your L</target_dir> called C<db-diagram-vXXX.png> where C<XXX> is he current
1078 C<schema> version.
1079  
1080 This is using the Graphviz producer (L<SQL::Translator::Producer::GraphViz>)
1081 which in turn requires L<Graphviz>. Since this is not always trivial to
1082 install, I do not require it. You will need to add it manually to your
1083 C<Makefile.PL> or C<dist.ini> and manage it yourself.
1084  
1085 This feature is experimental and currently does not offer any options, as I
1086 am still determining the best way to meet the need without exceeding the
1087 scope of L<DBIx::Class::Migration>. Consider this command a 'freebee' and
1088 please don't depend on it in your custom code.
1089  
1090 =head2 install_if_needed
1091  
1092 If the database is not installed, do so. Accepts a hash of callbacks or
1093 instructions to perform should installation be needed/
1094  
1095 $migration->install_if_needed(
1096 on_install => sub {
1097 my ($schema, $local_migration) = @_;
1098 DBIx::Class::Migration::Population->new(
1099 schema=>shift)->populate('all_tables');
1100 });
1101  
1102 The following callbacks / instructions are permitted
1103  
1104 =over 4
1105  
1106 =item on_install
1107  
1108 Accepts: Coderef
1109  
1110 Given a coderef, execute it after the database is installed. The coderef
1111 gets passed two arguments: C<$schema> and C<$self> (the current migration
1112 object).
1113  
1114 =item default_fixture_sets
1115  
1116 Accepts: Arrayref of fixture sets
1117  
1118 $migration->install_if_needed(
1119 default_fixture_sets => ['all_tables']);
1120  
1121 After database installation, populate the fixtures in order.
1122  
1123 =back
1124  
1125 =head2 install_version_storage
1126  
1127 If the targeted (connected) database does not have the versioning tables
1128 installed, this will install them. The version is set to whatever your
1129 C<schema> version currently is.
1130  
1131 You will only need to use this command in the case where you have an existing
1132 database that you are reverse engineering and you need to setup versioning
1133 storage since you can't rebuild the database from scratch (such as if you have
1134 a huge production database that you now want to start versioning).
1135  
1136 =head2 delete_named_sets
1137  
1138 Given a (or a list) of fixture sets, delete them if the exist in the current
1139 schema version.
1140  
1141 Yes, this really deletes, you've been warned (check in your code to a source
1142 control repository).
1143  
1144 =head1 ENVIRONMENT
1145  
1146 When running L<DBIx::Class::Migration> we set some C<%ENV> variables during
1147 installation, up / downgrading, so that your Perl run scripts (see
1148 L<DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator\'PERL SCRIPTS'>)
1149 can receive some useful information. The Following C<%ENV> variables are set:
1150  
1151 DBIC_MIGRATION_SCHEMA_CLASS => $self->schema_class
1152 DBIC_MIGRATION_TARGET_DIR => $self->target_dir
1153 DBIC_MIGRATION_FIXTURE_DIR => catdir($self->target_dir, 'fixtures', $self->dbic_dh->schema_version),
1154 DBIC_MIGRATION_SCHEMA_VERSION => $self->dbic_dh->schema_version
1155 DBIC_MIGRATION_TO_VERSION => $self->dbic_dh->to_version
1156 DBIC_MIGRATION_DATABASE_VERSION => $self->dbic_dh->schema_version || 0
1157  
1158 You might find having these available in your migration scripts useful for
1159 doing things like 'populate a database from a fixture set, if it exists, but
1160 if not run a bunch of inserts.
1161  
1162 =head1 THANKS
1163  
1164 Because of the awesomeness of CPAN and the work of many others, all this
1165 functionality is provided with a few hundred lines of code. In fact, I spent
1166 a lot more time writing docs and tests than anything else. Here are some
1167 particular projects / people I'd like to thank:
1168  
1169 First, thanks to C<mst> for providing me a big chunk of code that served to
1170 kickstart my work, and served as an valuable prototype.
1171  
1172 Thanks to C<frew> for the awesome L<DBIx::Class::DeploymentHandler> which gives
1173 us such a powerful base for organizing database versions. Thanks to all the
1174 authors of L<DBIx::Class::Fixtures> for giving me a foundation for managing
1175 sets of data. Lastly, thanks to the L<DBIx::Class> cabal for all the work done
1176 in making the L<DBIx::Class> ORM so amazingly powerful.
1177  
1178 Additionally thanks to the creators / maintainers for L<Test::mysqld> and
1179 L<Test::Postgresql58>, which made it easy to create developer level sandboxes for
1180 these popular open source databases.
1181  
1182 As usual, thanks to the L<Moose> cabal for making Perl programming fun and
1183 beautiful. Lastly, a shout-out to the L<Dist::Zilla> cabal for making it so I
1184 don't need to write my own build and deployment tools.
1185  
1186 =head1 AUTHOR
1187  
1188 John Napiorkowski L<email:jjnapiork@cpan.org>
1189  
1190 =head1 CONTRIBUTORS
1191  
1192 The following is a list of identified contributors. Please let me know if I
1193 missed you.
1194  
1195 https://github.com/pjcj
1196 https://github.com/chromatic
1197 https://github.com/bentglasstube
1198 https://github.com/logie17
1199 https://github.com/RsrchBoy
1200 https://github.com/vkroll
1201 https://github.com/felliott
1202 https://github.com/mkrull
1203 https://github.com/moltar
1204 https://github.com/andyjones
1205 https://github.com/pnu
1206 https://github.com/n7st
1207 https://github.com/willsheppard
1208 https://github.com/mulletboy2
1209 https://github.com/mohawk2
1210 https://github.com/manwar
1211 https://github.com/upasana-me
1212 https://github.com/rabbiveesh
1213  
1214 =head1 SEE ALSO
1215  
1216 L<DBIx::Class::DeploymentHandler>, L<DBIx::Class::Fixtures>, L<DBIx::Class>,
1217 L<DBIx::Class::Schema::Loader>, L<Moo>, L<DBIx::Class::Migration::Script>,
1218 L<DBIx::Class::Migration::Population>, L<dbic-migration>, L<SQL::Translator>,
1219 L<Test::mysqld>, L<Test::Postgresql58>.
1220  
1221 =head1 COPYRIGHT & LICENSE
1222  
1223 Copyright 2013-2015, John Napiorkowski L<email:jjnapiork@cpan.org>
1224  
1225 This library is free software; you can redistribute it and/or modify it under
1226 the same terms as Perl itself.
1227  
1228 =cut
1229  
« DBIx/Class/Migration/RunScript/Trait/Populate.pm ↑ Summary DBIx/Class/Migration/ShareDirBuilder.pm »