1 package DBIx::Class::Fixtures;
6 use DBIx::Class::Exception;
7 use Class::Accessor::Grouped;
8 use Path::Class qw(dir file);
10 use Config::Any::JSON;
11 use Data::Dump::Streamer;
12 use Data::Visitor::Callback;
14 use File::Copy::Recursive qw/dircopy/;
15 use File::Copy qw/move/;
16 use Hash::Merge qw( merge );
18 use Class::C3::Componentised;
20 use base qw(Class::Accessor::Grouped);
22 our $namespace_counter = 0;
24 __PACKAGE__->mk_group_accessors( 'simple' => qw/config_dir _inherited_attributes debug schema_class/);
32 our $VERSION = '1.000001';
40 use DBIx::Class::Fixtures;
44 my $fixtures = DBIx::Class::Fixtures->new({ config_dir => '/home/me/app/fixture_configs' });
47 config => 'set_config.json',
48 schema => $source_dbic_schema,
49 directory => '/home/me/app/fixtures'
53 directory => '/home/me/app/fixtures',
54 ddl => '/home/me/app/sql/ddl.sql',
55 connection_details => ['dbi:mysql:dbname=app_dev', 'me', 'password'],
56 post_ddl => '/home/me/app/sql/post_ddl.sql',
61 Dump fixtures from source database to filesystem then import to another database (with same schema)
62 at any time. Use as a constant dataset for running tests against or for populating development databases
63 when impractical to use production clones. Describe fixture set using relations and conditions based
64 on your DBIx::Class schema.
66 =head1 DEFINE YOUR FIXTURE SET
68 Fixture sets are currently defined in .json files which must reside in your config_dir
69 (e.g. /home/me/app/fixture_configs/a_fixture_set.json). They describe which data to pull and dump
70 from the source database.
88 This will fetch artists with primary keys 1 and 3, the producer with primary key 5 and two of producer 5's
89 artists where 'artists' is a has_many DBIx::Class rel from Producer to Artist.
91 The top level attributes are as follows:
95 Sets must be an array of hashes, as in the example given above. Each set defines a set of objects to be
96 included in the fixtures. For details on valid set attributes see L</SET ATTRIBUTES> below.
100 Rules place general conditions on classes. For example if whenever an artist was dumped you also wanted all
101 of their cds dumped too, then you could use a rule to specify this. For example:
125 In this case all the cds of artists 1, 3 and all producer 5's artists will be dumped as well. Note that 'cds' is a
126 has_many DBIx::Class relation from Artist to CD. This is eqivalent to:
150 rules must be a hash keyed by class name.
156 To prevent repetition between configs you can include other configs. For example:
168 Includes must be an arrayref of hashrefs where the hashrefs have key 'file' which is the name of another config
169 file in the same directory. The original config is merged with its includes using Hash::Merge.
171 =head2 datetime_relative
173 Only available for MySQL and PostgreSQL at the moment, must be a value that DateTime::Format::*
174 can parse. For example:
178 class: 'RecentItems',
181 datetime_relative : "2007-10-30 00:00:00"
184 This will work when dumping from a MySQL database and will cause any datetime fields (where datatype => 'datetime'
185 in the column def of the schema class) to be dumped as a DateTime::Duration object relative to the date specified in
186 the datetime_relative value. For example if the RecentItem object had a date field set to 2007-10-25, then when the
187 fixture is imported the field will be set to 5 days in the past relative to the current time.
191 Specifies whether to automatically dump might_have relationships. Should be a hash with one attribute - fetch. Set fetch to 1 or 0.
206 Note: belongs_to rels are automatically dumped whether you like it or not, this is to avoid FKs to nowhere when importing.
207 General rules on has_many rels are not accepted at this top level, but you can turn them on for individual
208 sets - see L</SET ATTRIBUTES>.
210 =head1 SET ATTRIBUTES
214 Required attribute. Specifies the DBIx::Class object class you wish to dump.
218 Array of primary key ids to fetch, basically causing an $rs->find($_) for each. If the id is not in the source db then it
219 just won't get dumped, no warnings or death.
223 Must be either an integer or the string 'all'. Specifying an integer will effectively set the 'rows' attribute on the resultset clause,
224 specifying 'all' will cause the rows attribute to be left off and for all matching rows to be dumped. There's no randomising
225 here, it's just the first x rows.
229 A hash specifying the conditions dumped objects must match. Essentially this is a JSON representation of a DBIx::Class search clause. For example:
235 cond: { name: 'Dave' }
239 This will dump all artists whose name is 'dave'. Essentially $artist_rs->search({ name => 'Dave' })->all.
241 Sometimes in a search clause it's useful to use scalar refs to do things like:
243 $artist_rs->search({ no1_singles => \'> no1_albums' })
245 This could be specified in the cond hash like so:
251 cond: { no1_singles: '\> no1_albums' }
255 So if the value starts with a backslash the value is made a scalar ref before being passed to search.
259 An array of relationships to be used in the cond clause.
265 cond: { 'cds.position': { '>': 4 } },
270 Fetch all artists who have cds with position greater than 4.
274 Must be an array of hashes. Specifies which rels to also dump. For example:
283 cond: { position: '2' }
288 Will cause the cds of artists 1 and 3 to be dumped where the cd position is 2.
290 Valid attributes are: 'rel', 'quantity', 'cond', 'has_many', 'might_have' and 'join'. rel is the name of the DBIx::Class
291 rel to follow, the rest are the same as in the set attributes. quantity is necessary for has_many relationships,
292 but not if using for belongs_to or might_have relationships.
296 Specifies whether to fetch has_many rels for this set. Must be a hash containing keys fetch and quantity.
298 Set fetch to 1 if you want to fetch them, and quantity to either 'all' or an integer.
300 Be careful here, dumping has_many rels can lead to a lot of data being dumped.
304 As with has_many but for might_have relationships. Quantity doesn't do anything in this case.
306 This value will be inherited by all fetches in this set. This is not true for the has_many attribute.
308 =head1 RULE ATTRIBUTES
312 Same as with L</SET ATTRIBUTES>
316 Same as with L</SET ATTRIBUTES>
320 Same as with L</SET ATTRIBUTES>
324 Same as with L</SET ATTRIBUTES>
328 Same as with L</SET ATTRIBUTES>
336 =item Arguments: \%$attrs
338 =item Return Value: $fixture_object
342 Returns a new DBIx::Class::Fixture object. %attrs has only two valid keys at the
343 moment - 'debug' which determines whether to be verbose and 'config_dir' which is required and much contain a valid path to
344 the directory in which your .json configs reside.
346 my $fixtures = DBIx::Class::Fixtures->new({ config_dir => '/home/me/app/fixture_configs' });
354 unless (ref $params eq 'HASH') {
355 return DBIx::Class::Exception->throw('first arg to DBIx::Class::Fixtures->new() must be hash ref');
358 unless ($params->{config_dir}) {
359 return DBIx::Class::Exception->throw('config_dir param not specified');
362 my $config_dir = dir($params->{config_dir});
363 unless (-e $params->{config_dir}) {
364 return DBIx::Class::Exception->throw('config_dir directory doesn\'t exist');
368 config_dir => $config_dir,
369 _inherited_attributes => [qw/datetime_relative might_have rules/],
370 debug => $params->{debug}
382 =item Arguments: \%$attrs
384 =item Return Value: 1
389 config => 'set_config.json', # config file to use. must be in the config directory specified in the constructor
390 schema => $source_dbic_schema,
391 directory => '/home/me/app/fixtures' # output directory
397 all => 1, # just dump everything that's in the schema
398 schema => $source_dbic_schema,
399 directory => '/home/me/app/fixtures' # output directory
402 In this case objects will be dumped to subdirectories in the specified directory. For example:
404 /home/me/app/fixtures/artist/1.fix
405 /home/me/app/fixtures/artist/3.fix
406 /home/me/app/fixtures/producer/5.fix
408 schema and directory are required attributes. also, one of config or all must be specified.
416 unless (ref $params eq 'HASH') {
417 return DBIx::Class::Exception->throw('first arg to dump must be hash ref');
420 foreach my $param (qw/schema directory/) {
421 unless ($params->{$param}) {
422 return DBIx::Class::Exception->throw($param . ' param not specified');
426 my $schema = $params->{schema};
429 if ($params->{config}) {
431 $config_file = file($self->config_dir, $params->{config});
432 unless (-e $config_file) {
433 return DBIx::Class::Exception->throw('config does not exist at ' . $config_file);
435 $config = Config::Any::JSON->load($config_file);
438 if ($config->{includes}) {
439 $self->msg($config->{includes});
440 unless (ref $config->{includes} eq 'ARRAY') {
441 return DBIx::Class::Exception->throw('includes params of config must be an array ref of hashrefs');
443 foreach my $include_config (@{$config->{includes}}) {
444 unless ((ref $include_config eq 'HASH') && $include_config->{file}) {
445 return DBIx::Class::Exception->throw('includes params of config must be an array ref of hashrefs');
448 my $include_file = file($self->config_dir, $include_config->{file});
449 unless (-e $include_file) {
450 return DBIx::Class::Exception->throw('config does not exist at ' . $include_file);
452 my $include = Config::Any::JSON->load($include_file);
453 $self->msg($include);
454 $config = merge( $config, $include );
456 delete $config->{includes};
460 unless ($config && $config->{sets} && ref $config->{sets} eq 'ARRAY' && scalar(@{$config->{sets}})) {
461 return DBIx::Class::Exception->throw('config has no sets');
464 $config->{might_have} = { fetch => 0 } unless (exists $config->{might_have});
465 $config->{has_many} = { fetch => 0 } unless (exists $config->{has_many});
466 $config->{belongs_to} = { fetch => 1 } unless (exists $config->{belongs_to});
467 } elsif ($params->{all}) {
468 $config = { might_have => { fetch => 0 }, has_many => { fetch => 0 }, belongs_to => { fetch => 0 }, sets => [map {{ class => $_, quantity => 'all' }} $schema->sources] };
469 print Dumper($config);
471 return DBIx::Class::Exception->throw('must pass config or set all');
474 my $output_dir = dir($params->{directory});
475 unless (-e $output_dir) {
476 $output_dir->mkpath ||
477 return DBIx::Class::Exception->throw('output directory does not exist at ' . $output_dir);
480 $self->msg("generating fixtures");
481 my $tmp_output_dir = dir($output_dir, '-~dump~-' . $<);
483 if (-e $tmp_output_dir) {
484 $self->msg("- clearing existing $tmp_output_dir");
485 $tmp_output_dir->rmtree;
487 $self->msg("- creating $tmp_output_dir");
488 $tmp_output_dir->mkpath;
490 # write version file (for the potential benefit of populate)
491 my $version_file = file($tmp_output_dir, '_dumper_version');
492 write_file($version_file->stringify, $VERSION);
494 $config->{rules} ||= {};
495 my @sources = sort { $a->{class} cmp $b->{class} } @{delete $config->{sets}};
496 my %options = ( is_root => 1 );
497 foreach my $source (@sources) {
498 # apply rule to set if specified
499 my $rule = $config->{rules}->{$source->{class}};
500 $source = merge( $source, $rule ) if ($rule);
503 my $rs = $schema->resultset($source->{class});
505 if ($source->{cond} and ref $source->{cond} eq 'HASH') {
506 # if value starts with / assume it's meant to be passed as a scalar ref to dbic
507 # ideally this would substitute deeply
508 $source->{cond} = { map { $_ => ($source->{cond}->{$_} =~ s/^\\//) ? \$source->{cond}->{$_} : $source->{cond}->{$_} } keys %{$source->{cond}} };
511 $rs = $rs->search($source->{cond}, { join => $source->{join} }) if ($source->{cond});
512 $self->msg("- dumping $source->{class}");
514 my %source_options = ( set => { %{$config}, %{$source} } );
515 if ($source->{quantity}) {
516 $rs = $rs->search({}, { order_by => $source->{order_by} }) if ($source->{order_by});
517 if ($source->{quantity} eq 'all') {
518 push (@objects, $rs->all);
519 } elsif ($source->{quantity} =~ /^\d+$/) {
520 push (@objects, $rs->search({}, { rows => $source->{quantity} }));
522 DBIx::Class::Exception->throw('invalid value for quantity - ' . $source->{quantity});
525 if ($source->{ids}) {
526 my @ids = @{$source->{ids}};
527 my @id_objects = grep { $_ } map { $rs->find($_) } @ids;
528 push (@objects, @id_objects);
530 unless ($source->{quantity} || $source->{ids}) {
531 DBIx::Class::Exception->throw('must specify either quantity or ids');
535 foreach my $object (@objects) {
536 $source_options{set_dir} = $tmp_output_dir;
537 $self->dump_object($object, { %options, %source_options } );
542 foreach my $dir ($output_dir->children) {
543 next if ($dir eq $tmp_output_dir);
544 $dir->remove || $dir->rmtree;
547 $self->msg("- moving temp dir to $output_dir");
548 move($_, dir($output_dir, $_->relative($_->parent)->stringify)) for $tmp_output_dir->children;
549 if (-e $output_dir) {
550 $self->msg("- clearing tmp dir $tmp_output_dir");
551 # delete existing fixture set
552 $tmp_output_dir->remove;
561 my ($self, $object, $params, $rr_info) = @_;
562 my $set = $params->{set};
563 die 'no dir passed to dump_object' unless $params->{set_dir};
564 die 'no object passed to dump_object' unless $object;
566 my @inherited_attrs = @{$self->_inherited_attributes};
568 # write dir and gen filename
569 my $source_dir = dir($params->{set_dir}, lc($object->result_source->from));
570 mkdir($source_dir->stringify, 0777);
572 # strip dir separators from file name
573 my $file = file($source_dir, join('-', map {
574 ( my $a = $object->get_column($_) ) =~ s|[/\\]|_|g; $a;
575 } sort $object->primary_columns) . '.fix');
578 my $exists = (-e $file->stringify) ? 1 : 0;
580 $self->msg('-- dumping ' . $file->stringify, 2);
581 my %ds = $object->get_columns;
583 my $formatter= $object->result_source->schema->storage->datetime_parser;
584 # mess with dates if specified
585 if ($set->{datetime_relative}) {
586 unless ($@ || !$formatter) {
588 if ($set->{datetime_relative} eq 'today') {
589 $dt = DateTime->today;
591 $dt = $formatter->parse_datetime($set->{datetime_relative}) unless ($@);
594 while (my ($col, $value) = each %ds) {
595 my $col_info = $object->result_source->column_info($col);
598 && $col_info->{_inflate_info}
599 && uc($col_info->{data_type}) eq 'DATETIME';
601 $ds{$col} = $object->get_inflated_column($col)->subtract_datetime($dt);
604 warn "datetime_relative not supported for this db driver at the moment";
608 # do the actual dumping
609 my $serialized = Dump(\%ds)->Out();
610 write_file($file->stringify, $serialized);
611 my $mode = 0777; chmod $mode, $file->stringify;
614 # don't bother looking at rels unless we are actually planning to dump at least one type
615 return unless ($set->{might_have}->{fetch} || $set->{belongs_to}->{fetch} || $set->{has_many}->{fetch} || $set->{fetch});
617 # dump rels of object
618 my $s = $object->result_source;
620 foreach my $name (sort $s->relationships) {
621 my $info = $s->relationship_info($name);
622 my $r_source = $s->related_source($name);
623 # if belongs_to or might_have with might_have param set or has_many with has_many param set then
624 if (($info->{attrs}{accessor} eq 'single' && (!$info->{attrs}{join_type} || ($set->{might_have} && $set->{might_have}->{fetch}))) || $info->{attrs}{accessor} eq 'filter' || ($info->{attrs}{accessor} eq 'multi' && ($set->{has_many} && $set->{has_many}->{fetch}))) {
625 my $related_rs = $object->related_resultset($name);
626 my $rule = $set->{rules}->{$related_rs->result_source->source_name};
627 # these parts of the rule only apply to has_many rels
628 if ($rule && $info->{attrs}{accessor} eq 'multi') {
629 $related_rs = $related_rs->search($rule->{cond}, { join => $rule->{join} }) if ($rule->{cond});
630 $related_rs = $related_rs->search({}, { rows => $rule->{quantity} }) if ($rule->{quantity} && $rule->{quantity} ne 'all');
631 $related_rs = $related_rs->search({}, { order_by => $rule->{order_by} }) if ($rule->{order_by});
633 if ($set->{has_many}->{quantity} && $set->{has_many}->{quantity} =~ /^\d+$/) {
634 $related_rs = $related_rs->search({}, { rows => $set->{has_many}->{quantity} });
636 my %c_params = %{$params};
638 my %mock_set = map { $_ => $set->{$_} } grep { $set->{$_} } @inherited_attrs;
639 $c_params{set} = \%mock_set;
640 # use Data::Dumper; print ' -- ' . Dumper($c_params{set}, $rule->{fetch}) if ($rule && $rule->{fetch});
641 $c_params{set} = merge( $c_params{set}, $rule) if ($rule && $rule->{fetch});
642 # use Data::Dumper; print ' -- ' . Dumper(\%c_params) if ($rule && $rule->{fetch});
643 $self->dump_object($_, \%c_params) foreach $related_rs->all;
648 return unless $set && $set->{fetch};
649 foreach my $fetch (@{$set->{fetch}}) {
651 $fetch->{$_} = $set->{$_} foreach grep { !$fetch->{$_} && $set->{$_} } @inherited_attrs;
652 my $related_rs = $object->related_resultset($fetch->{rel});
653 my $rule = $set->{rules}->{$related_rs->result_source->source_name};
655 my $info = $object->result_source->relationship_info($fetch->{rel});
656 if ($info->{attrs}{accessor} eq 'multi') {
657 $fetch = merge( $fetch, $rule );
658 } elsif ($rule->{fetch}) {
659 $fetch = merge( $fetch, { fetch => $rule->{fetch} } );
662 die "relationship " . $fetch->{rel} . " does not exist for " . $s->source_name unless ($related_rs);
663 if ($fetch->{cond} and ref $fetch->{cond} eq 'HASH') {
664 # if value starts with / assume it's meant to be passed as a scalar ref to dbic
665 # ideally this would substitute deeply
666 $fetch->{cond} = { map { $_ => ($fetch->{cond}->{$_} =~ s/^\\//) ? \$fetch->{cond}->{$_} : $fetch->{cond}->{$_} } keys %{$fetch->{cond}} };
668 $related_rs = $related_rs->search($fetch->{cond}, { join => $fetch->{join} }) if ($fetch->{cond});
669 $related_rs = $related_rs->search({}, { rows => $fetch->{quantity} }) if ($fetch->{quantity} && $fetch->{quantity} ne 'all');
670 $related_rs = $related_rs->search({}, { order_by => $fetch->{order_by} }) if ($fetch->{order_by});
671 $self->dump_object($_, { %{$params}, set => $fetch }) foreach $related_rs->all;
675 sub _generate_schema {
677 my $params = shift || {};
679 $self->msg("\ncreating schema");
680 # die 'must pass version param to generate_schema_from_ddl' unless $params->{version};
682 my $schema_class = $self->schema_class || "DBIx::Class::Fixtures::Schema";
683 eval "require $schema_class";
687 my $connection_details = $params->{connection_details};
688 $namespace_counter++;
689 my $namespace = "DBIx::Class::Fixtures::GeneratedSchema_" . $namespace_counter;
690 Class::C3::Componentised->inject_base( $namespace => $schema_class );
691 $pre_schema = $namespace->connect(@{$connection_details});
692 unless( $pre_schema ) {
693 return DBIx::Class::Exception->throw('connection details not valid');
695 my @tables = map { $pre_schema->source($_)->from } $pre_schema->sources;
696 $self->msg("Tables to drop: [". join(', ', sort @tables) . "]");
697 my $dbh = $pre_schema->storage->dbh;
700 $self->msg("- clearing DB of existing tables");
701 eval { $dbh->do('SET foreign_key_checks=0') };
702 foreach my $table (@tables) {
703 eval { $dbh->do('drop table ' . $table . ($params->{cascade} ? ' cascade' : '') ) };
706 # import new ddl file to db
707 my $ddl_file = $params->{ddl};
708 $self->msg("- deploying schema using $ddl_file");
709 my $data = _read_sql($ddl_file);
711 eval { $dbh->do($_) or warn "SQL was:\n $_"};
712 if ($@) { die "SQL was:\n $_\n$@"; }
714 $self->msg("- finished importing DDL into DB");
716 # load schema object from our new DB
717 $namespace_counter++;
718 my $namespace2 = "DBIx::Class::Fixtures::GeneratedSchema_" . $namespace_counter;
719 Class::C3::Componentised->inject_base( $namespace2 => $schema_class );
720 my $schema = $namespace2->connect(@{$connection_details});
725 my $ddl_file = shift;
727 open $fh, "<$ddl_file" or die ("Can't open DDL file, $ddl_file ($!)");
728 my @data = split(/\n/, join('', <$fh>));
729 @data = grep(!/^--/, @data);
730 @data = split(/;/, join('', @data));
732 @data = grep { $_ && $_ !~ /^-- / } @data;
740 =item Arguments: \%$attrs
742 =item Return Value: 1
746 $fixtures->populate({
747 directory => '/home/me/app/fixtures', # directory to look for fixtures in, as specified to dump
748 ddl => '/home/me/app/sql/ddl.sql', # DDL to deploy
749 connection_details => ['dbi:mysql:dbname=app_dev', 'me', 'password'], # database to clear, deploy and then populate
750 post_ddl => '/home/me/app/sql/post_ddl.sql', # DDL to deploy after populating records, ie. FK constraints
751 cascade => 1, # use CASCADE option when dropping tables
754 In this case the database app_dev will be cleared of all tables, then the specified DDL deployed to it,
755 then finally all fixtures found in /home/me/app/fixtures will be added to it. populate will generate
756 its own DBIx::Class schema from the DDL rather than being passed one to use. This is better as
757 custom insert methods are avoided which can to get in the way. In some cases you might not
758 have a DDL, and so this method will eventually allow a $schema object to be passed instead.
760 If needed, you can specify a post_ddl attribute which is a DDL to be applied after all the fixtures
761 have been added to the database. A good use of this option would be to add foreign key constraints
762 since databases like Postgresql cannot disable foreign key checks.
764 If your tables have foreign key constraints you may want to use the cascade attribute which will
765 make the drop table functionality cascade, ie 'DROP TABLE $table CASCADE'.
767 directory, dll and connection_details are all required attributes.
774 unless (ref $params eq 'HASH') {
775 return DBIx::Class::Exception->throw('first arg to populate must be hash ref');
778 foreach my $param (qw/directory/) {
779 unless ($params->{$param}) {
780 return DBIx::Class::Exception->throw($param . ' param not specified');
783 my $fixture_dir = dir(delete $params->{directory});
784 unless (-e $fixture_dir) {
785 return DBIx::Class::Exception->throw('fixture directory does not exist at ' . $fixture_dir);
790 if ($params->{ddl} && $params->{connection_details}) {
791 $ddl_file = file(delete $params->{ddl});
792 unless (-e $ddl_file) {
793 return DBIx::Class::Exception->throw('DDL does not exist at ' . $ddl_file);
795 unless (ref $params->{connection_details} eq 'ARRAY') {
796 return DBIx::Class::Exception->throw('connection details must be an arrayref');
798 } elsif ($params->{schema}) {
799 return DBIx::Class::Exception->throw('passing a schema is not supported at the moment');
801 return DBIx::Class::Exception->throw('you must set the ddl and connection_details params');
804 my $schema = $self->_generate_schema({ ddl => $ddl_file, connection_details => delete $params->{connection_details}, %{$params} });
805 $self->msg("\nimporting fixtures");
806 my $tmp_fixture_dir = dir($fixture_dir, "-~populate~-" . $<);
808 my $version_file = file($fixture_dir, '_dumper_version');
809 unless (-e $version_file) {
810 # return DBIx::Class::Exception->throw('no version file found');
813 if (-e $tmp_fixture_dir) {
814 $self->msg("- deleting existing temp directory $tmp_fixture_dir");
815 $tmp_fixture_dir->rmtree;
817 $self->msg("- creating temp dir");
818 dircopy(dir($fixture_dir, $schema->source($_)->from), dir($tmp_fixture_dir, $schema->source($_)->from)) for grep { -e dir($fixture_dir, $schema->source($_)->from) } $schema->sources;
820 eval { $schema->storage->dbh->do('SET foreign_key_checks=0') };
823 my $formatter= $schema->storage->datetime_parser;
824 unless ($@ || !$formatter) {
826 if ($params->{datetime_relative_to}) {
827 $callbacks{'DateTime::Duration'} = sub {
828 $params->{datetime_relative_to}->clone->add_duration($_);
831 $callbacks{'DateTime::Duration'} = sub {
832 $formatter->format_datetime(DateTime->today->add_duration($_))
835 $callbacks{object} ||= "visit_ref";
836 $fixup_visitor = new Data::Visitor::Callback(%callbacks);
838 foreach my $source (sort $schema->sources) {
839 $self->msg("- adding " . $source);
840 my $rs = $schema->resultset($source);
841 my $source_dir = dir($tmp_fixture_dir, lc($rs->result_source->from));
842 next unless (-e $source_dir);
844 while (my $file = $source_dir->next) {
845 next unless ($file =~ /\.fix$/);
846 next if $file->is_dir;
847 my $contents = $file->slurp;
850 $HASH1 = $fixup_visitor->visit($HASH1) if $fixup_visitor;
853 $rs->populate(\@rows);
856 if ($params->{post_ddl}) {
857 my $data = _read_sql($params->{post_ddl});
859 eval { $schema->storage->dbh->do($_) or warn "SQL was:\n $_"};
860 if ($@) { die "SQL was:\n $_\n$@"; }
862 $self->msg("- finished importing post-populate DDL into DB");
865 $self->msg("- fixtures imported");
866 $self->msg("- cleaning up");
867 $tmp_fixture_dir->rmtree;
868 eval { $schema->storage->dbh->do('SET foreign_key_checks=1') };
875 my $subject = shift || return;
876 my $level = shift || 1;
877 return unless $self->debug >= $level;
879 print Dumper($subject);
881 print $subject . "\n";
887 Luke Saunders <luke@shadowcatsystems.co.uk>
889 Initial development sponsored by and (c) Takkle, Inc. 2007
893 Ash Berlin <ash@shadowcatsystems.co.uk>
894 Matt S. Trout <mst@shadowcatsystems.co.uk>
895 Drew Taylor <taylor.andrew.j@gmail.com>
899 This library is free software under the same license as perl itself