1 package DBIx::Class::Fixtures;
6 use DBIx::Class 0.08100;
7 use DBIx::Class::Exception;
8 use Class::Accessor::Grouped;
10 use Data::Dump::Streamer;
11 use Data::Visitor::Callback;
12 use Hash::Merge qw( merge );
14 use Class::C3::Componentised;
17 use File::Temp qw/tempdir/;
19 use base qw(Class::Accessor::Grouped);
21 our $namespace_counter = 0;
23 __PACKAGE__->mk_group_accessors( 'simple' => qw/config_dir
24 _inherited_attributes debug schema_class dumped_objects config_attrs/);
26 our $VERSION = '1.001036';
28 $VERSION = eval $VERSION;
32 DBIx::Class::Fixtures - Dump data and repopulate a database using rules
36 use DBIx::Class::Fixtures;
40 my $fixtures = DBIx::Class::Fixtures->new({
41 config_dir => '/home/me/app/fixture_configs'
45 config => 'set_config.json',
46 schema => $source_dbic_schema,
47 directory => '/home/me/app/fixtures'
51 directory => '/home/me/app/fixtures',
52 ddl => '/home/me/app/sql/ddl.sql',
53 connection_details => ['dbi:mysql:dbname=app_dev', 'me', 'password'],
54 post_ddl => '/home/me/app/sql/post_ddl.sql',
59 Dump fixtures from source database to filesystem then import to another
60 database (with same schema) at any time. Use as a constant dataset for running
61 tests against or for populating development databases when impractical to use
62 production clones. Describe fixture set using relations and conditions based on
63 your DBIx::Class schema.
65 =head1 DEFINE YOUR FIXTURE SET
67 Fixture sets are currently defined in .json files which must reside in your
68 config_dir (e.g. /home/me/app/fixture_configs/a_fixture_set.json). They
69 describe which data to pull and dump from the source database.
92 This will fetch artists with primary keys 1 and 3, the producer with primary
93 key 5 and two of producer 5's artists where 'artists' is a has_many DBIx::Class
94 rel from Producer to Artist.
96 The top level attributes are as follows:
100 Sets must be an array of hashes, as in the example given above. Each set
101 defines a set of objects to be included in the fixtures. For details on valid
102 set attributes see L</SET ATTRIBUTES> below.
106 Rules place general conditions on classes. For example if whenever an artist
107 was dumped you also wanted all of their cds dumped too, then you could use a
108 rule to specify this. For example:
137 In this case all the cds of artists 1, 3 and all producer 5's artists will be
138 dumped as well. Note that 'cds' is a has_many DBIx::Class relation from Artist
139 to CD. This is eqivalent to:
166 rules must be a hash keyed by class name.
172 To prevent repetition between configs you can include other configs. For
181 { "file": "base.json" }
185 Includes must be an arrayref of hashrefs where the hashrefs have key 'file'
186 which is the name of another config file in the same directory. The original
187 config is merged with its includes using L<Hash::Merge>.
189 =head2 datetime_relative
191 Only available for MySQL and PostgreSQL at the moment, must be a value that
192 DateTime::Format::* can parse. For example:
196 "class": "RecentItems",
199 "datetime_relative": "2007-10-30 00:00:00"
202 This will work when dumping from a MySQL database and will cause any datetime
203 fields (where datatype => 'datetime' in the column def of the schema class) to
204 be dumped as a DateTime::Duration object relative to the date specified in the
205 datetime_relative value. For example if the RecentItem object had a date field
206 set to 2007-10-25, then when the fixture is imported the field will be set to 5
207 days in the past relative to the current time.
211 Specifies whether to automatically dump might_have relationships. Should be a
212 hash with one attribute - fetch. Set fetch to 1 or 0.
215 "might_have": { "fetch": 1 },
228 Note: belongs_to rels are automatically dumped whether you like it or not, this
229 is to avoid FKs to nowhere when importing. General rules on has_many rels are
230 not accepted at this top level, but you can turn them on for individual sets -
231 see L</SET ATTRIBUTES>.
233 =head1 SET ATTRIBUTES
237 Required attribute. Specifies the DBIx::Class object class you wish to dump.
241 Array of primary key ids to fetch, basically causing an $rs->find($_) for each.
242 If the id is not in the source db then it just won't get dumped, no warnings or
247 Must be either an integer or the string 'all'. Specifying an integer will
248 effectively set the 'rows' attribute on the resultset clause, specifying 'all'
249 will cause the rows attribute to be left off and for all matching rows to be
250 dumped. There's no randomising here, it's just the first x rows.
254 A hash specifying the conditions dumped objects must match. Essentially this is
255 a JSON representation of a DBIx::Class search clause. For example:
261 "cond": { "name": "Dave" }
265 This will dump all artists whose name is 'dave'. Essentially
266 $artist_rs->search({ name => 'Dave' })->all.
268 Sometimes in a search clause it's useful to use scalar refs to do things like:
270 $artist_rs->search({ no1_singles => \'> no1_albums' })
272 This could be specified in the cond hash like so:
278 "cond": { "no1_singles": "\> no1_albums" }
282 So if the value starts with a backslash the value is made a scalar ref before
283 being passed to search.
287 An array of relationships to be used in the cond clause.
293 "cond": { "cds.position": { ">": 4 } },
298 Fetch all artists who have cds with position greater than 4.
302 Must be an array of hashes. Specifies which rels to also dump. For example:
311 "cond": { "position": "2" }
316 Will cause the cds of artists 1 and 3 to be dumped where the cd position is 2.
318 Valid attributes are: 'rel', 'quantity', 'cond', 'has_many', 'might_have' and
319 'join'. rel is the name of the DBIx::Class rel to follow, the rest are the same
320 as in the set attributes. quantity is necessary for has_many relationships, but
321 not if using for belongs_to or might_have relationships.
325 Specifies whether to fetch has_many rels for this set. Must be a hash
326 containing keys fetch and quantity.
328 Set fetch to 1 if you want to fetch them, and quantity to either 'all' or an
331 Be careful here, dumping has_many rels can lead to a lot of data being dumped.
335 As with has_many but for might_have relationships. Quantity doesn't do anything
338 This value will be inherited by all fetches in this set. This is not true for
339 the has_many attribute.
343 In some cases your database information might be keys to values in some sort of
344 external storage. The classic example is you are using L<DBIx::Class::InflateColumn::FS>
345 to store blob information on the filesystem. In this case you may wish the ability
346 to backup your external storage in the same way your database data. The L</external>
347 attribute lets you specify a handler for this type of issue. For example:
356 "args": {"path":"__ATTR(photo_dir)__"}
362 This would use L<DBIx::Class::Fixtures::External::File> to read from a directory
363 where the path to a file is specified by the C<file> field of the C<Photo> source.
364 We use the uninflated value of the field so you need to completely handle backup
365 and restore. For the common case we provide L<DBIx::Class::Fixtures::External::File>
366 and you can create your own custom handlers by placing a '+' in the namespace:
368 "class": "+MyApp::Schema::SomeExternalStorage",
370 Although if possible I'd love to get patches to add some of the other common
371 types (I imagine storage in MogileFS, Redis, etc or even Amazon might be popular.)
373 See L<DBIx::Class::Fixtures::External::File> for the external handler interface.
375 =head1 RULE ATTRIBUTES
379 Same as with L</SET ATTRIBUTES>
383 Same as with L</SET ATTRIBUTES>
387 Same as with L</SET ATTRIBUTES>
391 Same as with L</SET ATTRIBUTES>
395 Same as with L</SET ATTRIBUTES>
397 =head1 RULE SUBSTITUTIONS
399 You can provide the following substitution patterns for your rule values. An
400 example of this might be:
405 "quantity": "__ENV(NUMBER_PHOTOS_DUMPED)__",
411 Provide a value from %ENV
415 Provide a value from L</config_attrs>
419 Create the path to a file from a list
423 Create the path to a directory from a list
431 =item Arguments: \%$attrs
433 =item Return Value: $fixture_object
437 Returns a new DBIx::Class::Fixture object. %attrs can have the following
444 required. must contain a valid path to the directory in which your .json
449 determines whether to be verbose
451 =item ignore_sql_errors:
453 ignore errors on import of DDL etc
457 A hash of information you can use to do replacements inside your configuration
458 sets. For example, if your set looks like:
466 "quantity": "__ATTR(quantity)__",
471 my $fixtures = DBIx::Class::Fixtures->new( {
472 config_dir => '/home/me/app/fixture_configs'
478 You may wish to do this if you want to let whoever runs the dumps have a bit
483 my $fixtures = DBIx::Class::Fixtures->new( {
484 config_dir => '/home/me/app/fixture_configs'
493 unless (ref $params eq 'HASH') {
494 return DBIx::Class::Exception->throw('first arg to DBIx::Class::Fixtures->new() must be hash ref');
497 unless ($params->{config_dir}) {
498 return DBIx::Class::Exception->throw('config_dir param not specified');
501 my $config_dir = io->dir($params->{config_dir});
502 unless (-e $params->{config_dir}) {
503 return DBIx::Class::Exception->throw('config_dir directory doesn\'t exist');
507 config_dir => $config_dir,
508 _inherited_attributes => [qw/datetime_relative might_have rules belongs_to/],
509 debug => $params->{debug} || 0,
510 ignore_sql_errors => $params->{ignore_sql_errors},
511 dumped_objects => {},
512 use_create => $params->{use_create} || 0,
513 use_find_or_create => $params->{use_find_or_create} || 0,
514 config_attrs => $params->{config_attrs} || {},
522 =head2 available_config_sets
524 Returns a list of all the config sets found in the L</config_dir>. These will
525 be a list of the json based files containing dump rules.
530 sub available_config_sets {
531 @config_sets = scalar(@config_sets) ? @config_sets : map {
534 -f "$_" && $_=~/json$/;
535 } shift->config_dir->all;
542 =item Arguments: \%$attrs
544 =item Return Value: 1
549 config => 'set_config.json', # config file to use. must be in the config
550 # directory specified in the constructor
551 schema => $source_dbic_schema,
552 directory => '/home/me/app/fixtures' # output directory
558 all => 1, # just dump everything that's in the schema
559 schema => $source_dbic_schema,
560 directory => '/home/me/app/fixtures' # output directory
563 In this case objects will be dumped to subdirectories in the specified
564 directory. For example:
566 /home/me/app/fixtures/artist/1.fix
567 /home/me/app/fixtures/artist/3.fix
568 /home/me/app/fixtures/producer/5.fix
570 schema and directory are required attributes. also, one of config or all must
573 Lastly, the C<config> parameter can be a Perl HashRef instead of a file name.
574 If this form is used your HashRef should conform to the structure rules defined
575 for the JSON representations.
583 unless (ref $params eq 'HASH') {
584 return DBIx::Class::Exception->throw('first arg to dump must be hash ref');
587 foreach my $param (qw/schema directory/) {
588 unless ($params->{$param}) {
589 return DBIx::Class::Exception->throw($param . ' param not specified');
593 if($params->{excludes} && !$params->{all}) {
594 return DBIx::Class::Exception->throw("'excludes' param only works when using the 'all' param");
597 my $schema = $params->{schema};
599 if ($params->{config}) {
600 $config = ref $params->{config} eq 'HASH' ?
604 my $config_file = io->catfile($self->config_dir, $params->{config});
605 $self->load_config_file("$config_file");
607 } elsif ($params->{all}) {
608 my %excludes = map {$_=>1} @{$params->{excludes}||[]};
610 might_have => { fetch => 0 },
611 has_many => { fetch => 0 },
612 belongs_to => { fetch => 0 },
615 { class => $_, quantity => 'all' };
621 DBIx::Class::Exception->throw('must pass config or set all');
624 my $output_dir = io->dir($params->{directory});
625 unless (-e "$output_dir") {
626 $output_dir->mkpath ||
627 DBIx::Class::Exception->throw("output directory does not exist at $output_dir");
630 $self->msg("generating fixtures");
631 my $tmp_output_dir = io->dir(tempdir);
633 if (-e "$tmp_output_dir") {
634 $self->msg("- clearing existing $tmp_output_dir");
635 $tmp_output_dir->rmtree;
637 $self->msg("- creating $tmp_output_dir");
638 $tmp_output_dir->mkpath;
640 # write version file (for the potential benefit of populate)
641 $tmp_output_dir->file('_dumper_version')->print($VERSION);
643 # write our current config set
644 $tmp_output_dir->file('_config_set')->print( Dumper $config );
646 $config->{rules} ||= {};
647 my @sources = sort { $a->{class} cmp $b->{class} } @{delete $config->{sets}};
649 while ( my ($k,$v) = each %{ $config->{rules} } ) {
650 if ( my $source = eval { $schema->source($k) } ) {
651 $config->{rules}{$source->source_name} = $v;
655 foreach my $source (@sources) {
656 # apply rule to set if specified
657 my $rule = $config->{rules}->{$source->{class}};
658 $source = merge( $source, $rule ) if ($rule);
661 my $rs = $schema->resultset($source->{class});
663 if ($source->{cond} and ref $source->{cond} eq 'HASH') {
664 # if value starts with \ assume it's meant to be passed as a scalar ref
665 # to dbic. ideally this would substitute deeply
668 $_ => ($source->{cond}->{$_} =~ s/^\\//) ? \$source->{cond}->{$_}
669 : $source->{cond}->{$_}
670 } keys %{$source->{cond}}
674 $rs = $rs->search($source->{cond}, { join => $source->{join} })
677 $self->msg("- dumping $source->{class}");
679 my %source_options = ( set => { %{$config}, %{$source} } );
680 if ($source->{quantity}) {
681 $rs = $rs->search({}, { order_by => $source->{order_by} })
682 if $source->{order_by};
684 if ($source->{quantity} =~ /^\d+$/) {
685 $rs = $rs->search({}, { rows => $source->{quantity} });
686 } elsif ($source->{quantity} ne 'all') {
687 DBIx::Class::Exception->throw("invalid value for quantity - $source->{quantity}");
690 elsif ($source->{ids} && @{$source->{ids}}) {
691 my @ids = @{$source->{ids}};
692 my (@pks) = $rs->result_source->primary_columns;
693 die "Can't dump multiple col-pks using 'id' option" if @pks > 1;
694 $rs = $rs->search_rs( { $pks[0] => { -in => \@ids } } );
697 DBIx::Class::Exception->throw('must specify either quantity or ids');
700 $source_options{set_dir} = $tmp_output_dir;
701 $self->dump_rs($rs, \%source_options );
704 # clear existing output dir
705 foreach my $child ($output_dir->all) {
706 if ($child->is_dir) {
707 next if ("$child" eq "$tmp_output_dir");
708 if (grep { $_ =~ /\.fix/ } $child->all) {
711 } elsif ($child =~ /_dumper_version$/) {
716 $self->msg("- moving temp dir to $output_dir");
717 $tmp_output_dir->copy("$output_dir");
719 if (-e "$output_dir") {
720 $self->msg("- clearing tmp dir $tmp_output_dir");
721 # delete existing fixture set
722 $tmp_output_dir->rmtree;
730 sub load_config_file {
731 my ($self, $config_file) = @_;
732 DBIx::Class::Exception->throw("config does not exist at $config_file")
733 unless -e "$config_file";
735 my $config = Config::Any::JSON->load($config_file);
738 if (my $incs = $config->{includes}) {
740 DBIx::Class::Exception->throw(
741 'includes params of config must be an array ref of hashrefs'
742 ) unless ref $incs eq 'ARRAY';
744 foreach my $include_config (@$incs) {
745 DBIx::Class::Exception->throw(
746 'includes params of config must be an array ref of hashrefs'
747 ) unless (ref $include_config eq 'HASH') && $include_config->{file};
749 my $include_file = $self->config_dir->file($include_config->{file});
751 DBIx::Class::Exception->throw("config does not exist at $include_file")
752 unless -e "$include_file";
754 my $include = Config::Any::JSON->load($include_file);
755 $self->msg($include);
756 $config = merge( $config, $include );
758 delete $config->{includes};
762 return DBIx::Class::Exception->throw('config has no sets')
763 unless $config && $config->{sets} &&
764 ref $config->{sets} eq 'ARRAY' && scalar @{$config->{sets}};
766 $config->{might_have} = { fetch => 0 } unless exists $config->{might_have};
767 $config->{has_many} = { fetch => 0 } unless exists $config->{has_many};
768 $config->{belongs_to} = { fetch => 1 } unless exists $config->{belongs_to};
774 my ($self, $rs, $params) = @_;
776 while (my $row = $rs->next) {
777 $self->dump_object($row, $params);
782 my ($self, $object, $params) = @_;
783 my $set = $params->{set};
785 my $v = Data::Visitor::Callback->new(
787 my ($visitor, $data) = @_;
790 my ( $self, $v ) = @_;
791 if (! defined($ENV{$v})) {
799 if(my $attr = $self->config_attrs->{$v}) {
806 my ($self, @args) = @_;
807 "".io->catfile(@args);
810 my ($self, @args) = @_;
811 "".io->catdir(@args);
815 my $subsre = join( '|', keys %$subs );
816 $_ =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $self, $2 ? split( /,/, $2 ) : () ) }eg;
824 die 'no dir passed to dump_object' unless $params->{set_dir};
825 die 'no object passed to dump_object' unless $object;
827 my @inherited_attrs = @{$self->_inherited_attributes};
830 $object->get_column($_)
831 } $object->primary_columns;
833 my $key = join("\0", @pk_vals);
835 my $src = $object->result_source;
836 my $exists = $self->dumped_objects->{$src->name}{$key}++;
839 # write dir and gen filename
840 my $source_dir = io->catdir($params->{set_dir}, $self->_name_for_source($src));
841 $source_dir->mkpath(0, 0777);
843 # Convert characters not allowed on windows
844 my $file = io->catfile("$source_dir",
845 join('-', map { s|[/\\:\*\|\?"<>]|_|g; $_; } @pk_vals) . '.fix'
850 $self->msg('-- dumping ' . "$file", 2);
852 # get_columns will return virtual columns; we just want stored columns.
853 # columns_info keys seems to be the actual storage column names, so we'll
855 my $col_info = $src->columns_info;
856 my @column_names = keys %$col_info;
857 my %columns = $object->get_columns;
858 my %ds; @ds{@column_names} = @columns{@column_names};
860 if($set->{external}) {
861 foreach my $field (keys %{$set->{external}}) {
862 my $key = $ds{$field};
863 my ($plus, $class) = ( $set->{external}->{$field}->{class}=~/^(\+)*(.+)$/);
864 my $args = $set->{external}->{$field}->{args};
866 $class = "DBIx::Class::Fixtures::External::$class" unless $plus;
869 $ds{external}->{$field} =
870 encode_base64( $class
871 ->backup($key => $args),'');
875 # mess with dates if specified
876 if ($set->{datetime_relative}) {
877 my $formatter= $object->result_source->schema->storage->datetime_parser;
878 unless ($@ || !$formatter) {
880 if ($set->{datetime_relative} eq 'today') {
881 $dt = DateTime->today;
883 $dt = $formatter->parse_datetime($set->{datetime_relative}) unless ($@);
886 while (my ($col, $value) = each %ds) {
887 my $col_info = $object->result_source->column_info($col);
890 && $col_info->{_inflate_info}
892 (uc($col_info->{data_type}) eq 'DATETIME')
893 or (uc($col_info->{data_type}) eq 'DATE')
894 or (uc($col_info->{data_type}) eq 'TIME')
895 or (uc($col_info->{data_type}) eq 'TIMESTAMP')
896 or (uc($col_info->{data_type}) eq 'INTERVAL')
899 $ds{$col} = $object->get_inflated_column($col)->subtract_datetime($dt);
902 warn "datetime_relative not supported for this db driver at the moment";
906 # do the actual dumping
907 my $serialized = Dump(\%ds)->Out();
909 $file->print($serialized);
912 # don't bother looking at rels unless we are actually planning to dump at least one type
913 my ($might_have, $belongs_to, $has_many) = map {
914 $set->{$_}{fetch} || $set->{rules}{$src->source_name}{$_}{fetch}
915 } qw/might_have belongs_to has_many/;
917 return unless $might_have
922 # dump rels of object
924 foreach my $name (sort $src->relationships) {
925 my $info = $src->relationship_info($name);
926 my $r_source = $src->related_source($name);
927 # if belongs_to or might_have with might_have param set or has_many with
928 # has_many param set then
930 ( $info->{attrs}{accessor} eq 'single' &&
931 (!$info->{attrs}{join_type} || $might_have)
933 || $info->{attrs}{accessor} eq 'filter'
935 ($info->{attrs}{accessor} eq 'multi' && $has_many)
937 my $related_rs = $object->related_resultset($name);
938 my $rule = $set->{rules}->{$related_rs->result_source->source_name};
939 # these parts of the rule only apply to has_many rels
940 if ($rule && $info->{attrs}{accessor} eq 'multi') {
941 $related_rs = $related_rs->search(
943 { join => $rule->{join} }
944 ) if ($rule->{cond});
946 $related_rs = $related_rs->search(
948 { rows => $rule->{quantity} }
949 ) if ($rule->{quantity} && $rule->{quantity} ne 'all');
951 $related_rs = $related_rs->search(
953 { order_by => $rule->{order_by} }
954 ) if ($rule->{order_by});
957 if ($set->{has_many}{quantity} &&
958 $set->{has_many}{quantity} =~ /^\d+$/) {
959 $related_rs = $related_rs->search(
961 { rows => $set->{has_many}->{quantity} }
965 my %c_params = %{$params};
969 } grep { $set->{$_} } @inherited_attrs;
971 $c_params{set} = \%mock_set;
972 $c_params{set} = merge( $c_params{set}, $rule)
973 if $rule && $rule->{fetch};
975 $self->dump_rs($related_rs, \%c_params);
980 return unless $set && $set->{fetch};
981 foreach my $fetch (@{$set->{fetch}}) {
983 $fetch->{$_} = $set->{$_} foreach
984 grep { !$fetch->{$_} && $set->{$_} } @inherited_attrs;
985 my $related_rs = $object->related_resultset($fetch->{rel});
986 my $rule = $set->{rules}->{$related_rs->result_source->source_name};
989 my $info = $object->result_source->relationship_info($fetch->{rel});
990 if ($info->{attrs}{accessor} eq 'multi') {
991 $fetch = merge( $fetch, $rule );
992 } elsif ($rule->{fetch}) {
993 $fetch = merge( $fetch, { fetch => $rule->{fetch} } );
997 die "relationship $fetch->{rel} does not exist for " . $src->source_name
998 unless ($related_rs);
1000 if ($fetch->{cond} and ref $fetch->{cond} eq 'HASH') {
1001 # if value starts with \ assume it's meant to be passed as a scalar ref
1002 # to dbic. ideally this would substitute deeply
1003 $fetch->{cond} = { map {
1004 $_ => ($fetch->{cond}->{$_} =~ s/^\\//) ? \$fetch->{cond}->{$_}
1005 : $fetch->{cond}->{$_}
1006 } keys %{$fetch->{cond}} };
1009 $related_rs = $related_rs->search(
1011 { join => $fetch->{join} }
1012 ) if $fetch->{cond};
1014 $related_rs = $related_rs->search(
1016 { rows => $fetch->{quantity} }
1017 ) if $fetch->{quantity} && $fetch->{quantity} ne 'all';
1018 $related_rs = $related_rs->search(
1020 { order_by => $fetch->{order_by} }
1021 ) if $fetch->{order_by};
1023 $self->dump_rs($related_rs, { %{$params}, set => $fetch });
1027 sub _generate_schema {
1029 my $params = shift || {};
1031 $self->msg("\ncreating schema");
1033 my $schema_class = $self->schema_class || "DBIx::Class::Fixtures::Schema";
1034 eval "require $schema_class";
1038 my $connection_details = $params->{connection_details};
1040 $namespace_counter++;
1042 my $namespace = "DBIx::Class::Fixtures::GeneratedSchema_$namespace_counter";
1043 Class::C3::Componentised->inject_base( $namespace => $schema_class );
1045 $pre_schema = $namespace->connect(@{$connection_details});
1046 unless( $pre_schema ) {
1047 return DBIx::Class::Exception->throw('connection details not valid');
1049 my @tables = map { $self->_name_for_source($pre_schema->source($_)) } $pre_schema->sources;
1050 $self->msg("Tables to drop: [". join(', ', sort @tables) . "]");
1051 my $dbh = $pre_schema->storage->dbh;
1054 $self->msg("- clearing DB of existing tables");
1055 $pre_schema->storage->txn_do(sub {
1056 $pre_schema->storage->with_deferred_fk_checks(sub {
1057 foreach my $table (@tables) {
1059 $dbh->do("drop table $table" . ($params->{cascade} ? ' cascade' : '') )
1065 # import new ddl file to db
1066 my $ddl_file = $params->{ddl};
1067 $self->msg("- deploying schema using $ddl_file");
1068 my $data = _read_sql($ddl_file);
1070 eval { $dbh->do($_) or warn "SQL was:\n $_"};
1071 if ($@ && !$self->{ignore_sql_errors}) { die "SQL was:\n $_\n$@"; }
1073 $self->msg("- finished importing DDL into DB");
1075 # load schema object from our new DB
1076 $namespace_counter++;
1077 my $namespace2 = "DBIx::Class::Fixtures::GeneratedSchema_$namespace_counter";
1078 Class::C3::Componentised->inject_base( $namespace2 => $schema_class );
1079 my $schema = $namespace2->connect(@{$connection_details});
1084 my $ddl_file = shift;
1086 open $fh, "<$ddl_file" or die ("Can't open DDL file, $ddl_file ($!)");
1087 my @data = split(/\n/, join('', <$fh>));
1088 @data = grep(!/^--/, @data);
1089 @data = split(/;/, join('', @data));
1091 @data = grep { $_ && $_ !~ /^-- / } @data;
1095 =head2 dump_config_sets
1097 Works just like L</dump> but instead of specifying a single json config set
1098 located in L</config_dir> we dump each set named in the C<configs> parameter.
1100 The parameters are the same as for L</dump> except instead of a C<directory>
1101 parameter we have a C<directory_template> which is a coderef expected to return
1102 a scalar that is a root directory where we will do the actual dumping. This
1103 coderef get three arguments: C<$self>, C<$params> and C<$set_name>. For
1106 $fixture->dump_all_config_sets({
1108 configs => [qw/one.json other.json/],
1109 directory_template => sub {
1110 my ($fixture, $params, $set) = @_;
1111 return io->catdir('var', 'fixtures', $params->{schema}->version, $set);
1117 sub dump_config_sets {
1118 my ($self, $params) = @_;
1119 my $available_config_sets = delete $params->{configs};
1120 my $directory_template = delete $params->{directory_template} ||
1121 DBIx::Class::Exception->throw("'directory_template is required parameter");
1123 for my $set (@$available_config_sets) {
1124 my $localparams = $params;
1125 $localparams->{directory} = $directory_template->($self, $localparams, $set);
1126 $localparams->{config} = $set;
1127 $self->dump($localparams);
1128 $self->dumped_objects({}); ## Clear dumped for next go, if there is one!
1132 =head2 dump_all_config_sets
1134 my %local_params = %$params;
1135 my $local_self = bless { %$self }, ref($self);
1136 $local_params{directory} = $directory_template->($self, \%local_params, $set);
1137 $local_params{config} = $set;
1138 $self->dump(\%local_params);
1141 Works just like L</dump> but instead of specifying a single json config set
1142 located in L</config_dir> we dump each set in turn to the specified directory.
1144 The parameters are the same as for L</dump> except instead of a C<directory>
1145 parameter we have a C<directory_template> which is a coderef expected to return
1146 a scalar that is a root directory where we will do the actual dumping. This
1147 coderef get three arguments: C<$self>, C<$params> and C<$set_name>. For
1150 $fixture->dump_all_config_sets({
1152 directory_template => sub {
1153 my ($fixture, $params, $set) = @_;
1154 return io->catdir('var', 'fixtures', $params->{schema}->version, $set);
1160 sub dump_all_config_sets {
1161 my ($self, $params) = @_;
1162 $self->dump_config_sets({
1164 configs=>[$self->available_config_sets],
1172 =item Arguments: \%$attrs
1174 =item Return Value: 1
1178 $fixtures->populate( {
1179 # directory to look for fixtures in, as specified to dump
1180 directory => '/home/me/app/fixtures',
1183 ddl => '/home/me/app/sql/ddl.sql',
1185 # database to clear, deploy and then populate
1186 connection_details => ['dbi:mysql:dbname=app_dev', 'me', 'password'],
1188 # DDL to deploy after populating records, ie. FK constraints
1189 post_ddl => '/home/me/app/sql/post_ddl.sql',
1191 # use CASCADE option when dropping tables
1194 # optional, set to 1 to run ddl but not populate
1197 # optional, set to 1 to run each fixture through ->create rather than have
1198 # each $rs populated using $rs->populate. Useful if you have overridden new() logic
1199 # that effects the value of column(s).
1202 # optional, same as use_create except with find_or_create.
1203 # Useful if you are populating a persistent data store.
1204 use_find_or_create => 0,
1206 # Dont try to clean the database, just populate over whats there. Requires
1207 # schema option. Use this if you want to handle removing old data yourself
1212 In this case the database app_dev will be cleared of all tables, then the
1213 specified DDL deployed to it, then finally all fixtures found in
1214 /home/me/app/fixtures will be added to it. populate will generate its own
1215 DBIx::Class schema from the DDL rather than being passed one to use. This is
1216 better as custom insert methods are avoided which can to get in the way. In
1217 some cases you might not have a DDL, and so this method will eventually allow a
1218 $schema object to be passed instead.
1220 If needed, you can specify a post_ddl attribute which is a DDL to be applied
1221 after all the fixtures have been added to the database. A good use of this
1222 option would be to add foreign key constraints since databases like Postgresql
1223 cannot disable foreign key checks.
1225 If your tables have foreign key constraints you may want to use the cascade
1226 attribute which will make the drop table functionality cascade, ie 'DROP TABLE
1229 C<directory> is a required attribute.
1231 If you wish for DBIx::Class::Fixtures to clear the database for you pass in
1232 C<dll> (path to a DDL sql file) and C<connection_details> (array ref of DSN,
1235 If you wish to deal with cleaning the schema yourself, then pass in a C<schema>
1236 attribute containing the connected schema you wish to operate on and set the
1237 C<no_deploy> attribute.
1244 DBIx::Class::Exception->throw('first arg to populate must be hash ref')
1245 unless ref $params eq 'HASH';
1247 DBIx::Class::Exception->throw('directory param not specified')
1248 unless $params->{directory};
1250 my $fixture_dir = io->dir(delete $params->{directory});
1251 DBIx::Class::Exception->throw("fixture directory '$fixture_dir' does not exist")
1252 unless -d "$fixture_dir";
1257 if ($params->{ddl} && $params->{connection_details}) {
1258 $ddl_file = io->file(delete $params->{ddl});
1259 unless (-e "$ddl_file") {
1260 return DBIx::Class::Exception->throw('DDL does not exist at ' . $ddl_file);
1262 unless (ref $params->{connection_details} eq 'ARRAY') {
1263 return DBIx::Class::Exception->throw('connection details must be an arrayref');
1265 $schema = $self->_generate_schema({
1267 connection_details => delete $params->{connection_details},
1270 } elsif ($params->{schema} && $params->{no_deploy}) {
1271 $schema = $params->{schema};
1273 DBIx::Class::Exception->throw('you must set the ddl and connection_details params');
1277 return 1 if $params->{no_populate};
1279 $self->msg("\nimporting fixtures");
1280 my $tmp_fixture_dir = io->dir(tempdir());
1281 my $config_set_path = io->file($fixture_dir, '_config_set');
1282 my $config_set = -e "$config_set_path" ? do { my $VAR1; eval($config_set_path->slurp); $VAR1 } : '';
1284 my $v = Data::Visitor::Callback->new(
1285 plain_value => sub {
1286 my ($visitor, $data) = @_;
1289 my ( $self, $v ) = @_;
1290 if (! defined($ENV{$v})) {
1297 my ($self, $v) = @_;
1298 if(my $attr = $self->config_attrs->{$v}) {
1305 my ($self, @args) = @_;
1309 my ($self, @args) = @_;
1314 my $subsre = join( '|', keys %$subs );
1315 $_ =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $self, $2 ? split( /,/, $2 ) : () ) }eg;
1321 $v->visit( $config_set );
1326 %sets_by_src = map { delete($_->{class}) => $_ }
1327 @{$config_set->{sets}}
1330 if (-e "$tmp_fixture_dir") {
1331 $self->msg("- deleting existing temp directory $tmp_fixture_dir");
1332 $tmp_fixture_dir->rmtree;
1334 $self->msg("- creating temp dir");
1335 $tmp_fixture_dir->mkpath();
1336 for ( map { $self->_name_for_source($schema->source($_)) } $schema->sources) {
1337 my $from_dir = io->catdir($fixture_dir, $_);
1338 next unless -e "$from_dir";
1339 $from_dir->copy( io->catdir($tmp_fixture_dir, $_)."" );
1342 unless (-d "$tmp_fixture_dir") {
1343 DBIx::Class::Exception->throw("Unable to create temporary fixtures dir: $tmp_fixture_dir: $!");
1347 my $formatter = $schema->storage->datetime_parser;
1348 unless ($@ || !$formatter) {
1350 if ($params->{datetime_relative_to}) {
1351 $callbacks{'DateTime::Duration'} = sub {
1352 $params->{datetime_relative_to}->clone->add_duration($_);
1355 $callbacks{'DateTime::Duration'} = sub {
1356 $formatter->format_datetime(DateTime->today->add_duration($_))
1359 $callbacks{object} ||= "visit_ref";
1360 $fixup_visitor = new Data::Visitor::Callback(%callbacks);
1363 my @sorted_source_names = $self->_get_sorted_sources( $schema );
1364 $schema->storage->txn_do(sub {
1365 $schema->storage->with_deferred_fk_checks(sub {
1366 foreach my $source (@sorted_source_names) {
1367 $self->msg("- adding " . $source);
1368 my $rs = $schema->resultset($source);
1369 my $source_dir = io->catdir($tmp_fixture_dir, $self->_name_for_source($rs->result_source));
1370 next unless (-e "$source_dir");
1372 while (my $file = $source_dir->next) {
1373 next unless ($file =~ /\.fix$/);
1374 next if $file->is_dir;
1375 my $contents = $file->slurp;
1378 $HASH1 = $fixup_visitor->visit($HASH1) if $fixup_visitor;
1379 if(my $external = delete $HASH1->{external}) {
1380 my @fields = keys %{$sets_by_src{$source}->{external}};
1381 foreach my $field(@fields) {
1382 my $key = $HASH1->{$field};
1383 my $content = decode_base64 ($external->{$field});
1384 my $args = $sets_by_src{$source}->{external}->{$field}->{args};
1385 my ($plus, $class) = ( $sets_by_src{$source}->{external}->{$field}->{class}=~/^(\+)*(.+)$/);
1386 $class = "DBIx::Class::Fixtures::External::$class" unless $plus;
1388 $class->restore($key, $content, $args);
1391 if ( $params->{use_create} ) {
1392 $rs->create( $HASH1 );
1393 } elsif( $params->{use_find_or_create} ) {
1394 $rs->find_or_create( $HASH1 );
1396 push(@rows, $HASH1);
1399 $rs->populate(\@rows) if scalar(@rows);
1401 ## Now we need to do some db specific cleanup
1402 ## this probably belongs in a more isolated space. Right now this is
1403 ## to just handle postgresql SERIAL types that use Sequences
1404 ## Will completely ignore sequences in Oracle due to having to drop
1405 ## and recreate them
1407 my $table = $rs->result_source->name;
1408 for my $column(my @columns = $rs->result_source->columns) {
1409 my $info = $rs->result_source->column_info($column);
1410 if(my $sequence = $info->{sequence}) {
1411 $self->msg("- updating sequence $sequence");
1412 $rs->result_source->storage->dbh_do(sub {
1413 my ($storage, $dbh, @cols) = @_;
1414 if ( $dbh->{Driver}->{Name} eq "Oracle" ) {
1415 $self->msg("- Cannot change sequence values in Oracle");
1418 my $sql = sprintf("SELECT setval(?, (SELECT max(%s) FROM %s));",$dbh->quote_identifier($column),$dbh->quote_identifier($table))
1420 my $sth = $dbh->prepare($sql);
1421 $sth->bind_param(1,$sequence);
1423 my $rv = $sth->execute or die $sth->errstr;
1424 $self->msg("- $sql");
1433 $self->do_post_ddl( {
1435 post_ddl=>$params->{post_ddl}
1436 } ) if $params->{post_ddl};
1438 $self->msg("- fixtures imported");
1439 $self->msg("- cleaning up");
1440 $tmp_fixture_dir->rmtree;
1444 # the overall logic is modified from SQL::Translator::Parser::DBIx::Class->parse
1445 sub _get_sorted_sources {
1446 my ( $self, $dbicschema ) = @_;
1449 my %table_monikers = map { $_ => 1 } $dbicschema->sources;
1452 foreach my $moniker (sort keys %table_monikers) {
1453 my $source = $dbicschema->source($moniker);
1455 my $table_name = $source->name;
1456 my @primary = $source->primary_columns;
1457 my @rels = $source->relationships();
1459 my %created_FK_rels;
1460 foreach my $rel (sort @rels) {
1461 my $rel_info = $source->relationship_info($rel);
1463 # Ignore any rel cond that isn't a straight hash
1464 next unless ref $rel_info->{cond} eq 'HASH';
1466 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} keys(%{$rel_info->{cond}});
1468 # determine if this relationship is a self.fk => foreign.pk (i.e. belongs_to)
1470 if ( exists $rel_info->{attrs}{is_foreign_key_constraint} ) {
1471 $fk_constraint = $rel_info->{attrs}{is_foreign_key_constraint};
1472 } elsif ( $rel_info->{attrs}{accessor}
1473 && $rel_info->{attrs}{accessor} eq 'multi' ) {
1476 $fk_constraint = not $source->_compare_relationship_keys(\@keys, \@primary);
1479 # Dont add a relation if its not constraining
1480 next unless $fk_constraint;
1482 my $rel_table = $source->related_source($rel)->source_name;
1483 # Make sure we don't create the same relation twice
1484 my $key_test = join("\x00", sort @keys);
1485 next if $created_FK_rels{$rel_table}->{$key_test};
1487 if (scalar(@keys)) {
1488 $created_FK_rels{$rel_table}->{$key_test} = 1;
1490 # calculate dependencies: do not consider deferrable constraints and
1491 # self-references for dependency calculations
1492 if (! $rel_info->{attrs}{is_deferrable} and $rel_table ne $table_name) {
1493 $tables{$moniker}{$rel_table}++;
1497 $tables{$moniker} = {} unless exists $tables{$moniker};
1500 # resolve entire dep tree
1501 my $dependencies = {
1502 map { $_ => _resolve_deps ($_, \%tables) } (keys %tables)
1505 # return the sorted result
1507 keys %{$dependencies->{$a} || {} } <=> keys %{ $dependencies->{$b} || {} }
1514 my ( $question, $answers, $seen ) = @_;
1518 my %seen = map { $_ => $seen->{$_} + 1 } ( keys %$seen );
1519 $seen{$question} = 1;
1521 for my $dep (keys %{ $answers->{$question} }) {
1522 return {} if $seen->{$dep};
1523 my $subdeps = _resolve_deps( $dep, $answers, \%seen );
1524 $ret->{$_} += $subdeps->{$_} for ( keys %$subdeps );
1531 my ($self, $params) = @_;
1533 my $schema = $params->{schema};
1534 my $data = _read_sql($params->{post_ddl});
1536 eval { $schema->storage->dbh->do($_) or warn "SQL was:\n $_"};
1537 if ($@ && !$self->{ignore_sql_errors}) { die "SQL was:\n $_\n$@"; }
1539 $self->msg("- finished importing post-populate DDL into DB");
1544 my $subject = shift || return;
1545 my $level = shift || 1;
1546 return unless $self->debug >= $level;
1548 print Dumper($subject);
1550 print $subject . "\n";
1554 # Helper method for ensuring that the name used for a given source
1555 # is always the same (This is used to name the fixture directories
1558 sub _name_for_source {
1559 my ($self, $source) = @_;
1561 return ref $source->name ? $source->source_name : $source->name;
1566 Luke Saunders <luke@shadowcatsystems.co.uk>
1568 Initial development sponsored by and (c) Takkle, Inc. 2007
1572 Ash Berlin <ash@shadowcatsystems.co.uk>
1574 Matt S. Trout <mst@shadowcatsystems.co.uk>
1576 John Napiorkowski <jjnapiork@cpan.org>
1578 Drew Taylor <taylor.andrew.j@gmail.com>
1580 Frank Switalski <fswitalski@gmail.com>
1582 Chris Akins <chris.hexx@gmail.com>
1584 Tom Bloor <t.bloor@shadowcat.co.uk>
1586 Samuel Kaufman <skaufman@cpan.org>
1590 This library is free software under the same license as perl itself