X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FFixtures.pm;h=58cf9813d564534f7186f48a35c6412d47be0bff;hb=01a10324059f89f12b5060cf39253e17101f2d8d;hp=4fe0470df2a3c3d77b1bbc2bf8b3ab61027f3657;hpb=885f16cb813dbf04a09029303b7f40bf2fd53d2f;p=dbsrgits%2FDBIx-Class-Fixtures.git diff --git a/lib/DBIx/Class/Fixtures.pm b/lib/DBIx/Class/Fixtures.pm index 4fe0470..58cf981 100644 --- a/lib/DBIx/Class/Fixtures.pm +++ b/lib/DBIx/Class/Fixtures.pm @@ -6,7 +6,7 @@ use warnings; use DBIx::Class 0.08100; use DBIx::Class::Exception; use Class::Accessor::Grouped; -use Path::Class qw(dir file); +use Path::Class qw(dir file tempdir); use File::Spec::Functions 'catfile', 'catdir'; use Config::Any::JSON; use Data::Dump::Streamer; @@ -26,7 +26,9 @@ our $namespace_counter = 0; __PACKAGE__->mk_group_accessors( 'simple' => qw/config_dir _inherited_attributes debug schema_class dumped_objects config_attrs/); -our $VERSION = '1.001017'; +our $VERSION = '1.001026'; + +$VERSION = eval $VERSION; =head1 NAME @@ -419,7 +421,7 @@ Provide a value from L Create the path to a file from a list -=heade catdir +=head2 catdir Create the path to a directory from a list @@ -511,6 +513,7 @@ sub new { ignore_sql_errors => $params->{ignore_sql_errors}, dumped_objects => {}, use_create => $params->{use_create} || 0, + use_find_or_create => $params->{use_find_or_create} || 0, config_attrs => $params->{config_attrs} || {}, }; @@ -628,7 +631,7 @@ sub dump { } $self->msg("generating fixtures"); - my $tmp_output_dir = dir($output_dir, '-~dump~-' . $<); + my $tmp_output_dir = tempdir(); if (-e $tmp_output_dir) { $self->msg("- clearing existing $tmp_output_dir"); @@ -718,8 +721,7 @@ sub dump { } $self->msg("- moving temp dir to $output_dir"); - move($_, dir($output_dir, $_->relative($_->parent)->stringify)) - for $tmp_output_dir->children; + dircopy($tmp_output_dir, $output_dir); if (-e $output_dir) { $self->msg("- clearing tmp dir $tmp_output_dir"); @@ -842,7 +844,7 @@ sub dump_object { # write dir and gen filename - my $source_dir = $params->{set_dir}->subdir(lc $src->from); + my $source_dir = $params->{set_dir}->subdir($self->_name_for_source($src)); $source_dir->mkpath(0, 0777); # strip dir separators from file name @@ -866,7 +868,7 @@ sub dump_object { $ds{external}->{$field} = encode_base64( $class - ->backup($key => $args)); + ->backup($key => $args),''); } } @@ -1043,7 +1045,7 @@ sub _generate_schema { unless( $pre_schema ) { return DBIx::Class::Exception->throw('connection details not valid'); } - my @tables = map { $pre_schema->source($_)->from } $pre_schema->sources; + my @tables = map { $self->_name_for_source($pre_schema->source($_)) } $pre_schema->sources; $self->msg("Tables to drop: [". join(', ', sort @tables) . "]"); my $dbh = $pre_schema->storage->dbh; @@ -1191,10 +1193,14 @@ sub dump_all_config_sets { # optional, set to 1 to run ddl but not populate no_populate => 0, - # optional, set to 1 to run each fixture through ->create rather than have + # optional, set to 1 to run each fixture through ->create rather than have # each $rs populated using $rs->populate. Useful if you have overridden new() logic - # that effects the value of column(s). - use_create => 0, + # that effects the value of column(s). + use_create => 0, + + # optional, same as use_create except with find_or_create. + # Useful if you are populating a persistent data store. + use_find_or_create => 0, # Dont try to clean the database, just populate over whats there. Requires # schema option. Use this if you want to handle removing old data yourself @@ -1270,7 +1276,7 @@ sub populate { return 1 if $params->{no_populate}; $self->msg("\nimporting fixtures"); - my $tmp_fixture_dir = dir($fixture_dir, "-~populate~-" . $<); + my $tmp_fixture_dir = tempdir(); my $version_file = file($fixture_dir, '_dumper_version'); my $config_set_path = file($fixture_dir, '_config_set'); my $config_set = -e $config_set_path ? do { my $VAR1; eval($config_set_path->slurp); $VAR1 } : ''; @@ -1330,7 +1336,7 @@ sub populate { } $self->msg("- creating temp dir"); $tmp_fixture_dir->mkpath(); - for ( map { $schema->source($_)->from } $schema->sources) { + for ( map { $self->_name_for_source($schema->source($_)) } $schema->sources) { my $from_dir = $fixture_dir->subdir($_); next unless -e $from_dir; dircopy($from_dir, $tmp_fixture_dir->subdir($_) ); @@ -1362,7 +1368,7 @@ sub populate { foreach my $source (sort $schema->sources) { $self->msg("- adding " . $source); my $rs = $schema->resultset($source); - my $source_dir = $tmp_fixture_dir->subdir( lc $rs->result_source->from ); + my $source_dir = $tmp_fixture_dir->subdir( $self->_name_for_source($rs->result_source) ); next unless (-e $source_dir); my @rows; while (my $file = $source_dir->next) { @@ -1386,6 +1392,8 @@ sub populate { } if ( $params->{use_create} ) { $rs->create( $HASH1 ); + } elsif( $params->{use_find_or_create} ) { + $rs->find_or_create( $HASH1 ); } else { push(@rows, $HASH1); } @@ -1449,6 +1457,16 @@ sub msg { } } +# Helper method for ensuring that the name used for a given source +# is always the same (This is used to name the fixture directories +# for example) + +sub _name_for_source { + my ($self, $source) = @_; + + return ref $source->name ? $source->source_name : $source->name; +} + =head1 AUTHOR Luke Saunders @@ -1465,6 +1483,8 @@ sub msg { Frank Switalski + Chris Akins + =head1 LICENSE This library is free software under the same license as perl itself