DBIx/Class/Migration.pm

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