X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FFixtures.pm;h=24439971d5abbcb7c307029e4d6aad61b9fc28d4;hb=dcdf675f7c1e751f53bce149fe7af49bf079771b;hp=ec48ec2630926af104cf8f7dca3be8074844967a;hpb=bb6d61a766e65cc45b17161091b610a1d589f81f;p=dbsrgits%2FDBIx-Class-Fixtures.git diff --git a/lib/DBIx/Class/Fixtures.pm b/lib/DBIx/Class/Fixtures.pm index ec48ec2..2443997 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; @@ -18,7 +18,6 @@ use Hash::Merge qw( merge ); use Data::Dumper; use Class::C3::Componentised; use MIME::Base64; -use File::Temp qw/tempdir/; use base qw(Class::Accessor::Grouped); @@ -27,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.001018'; +our $VERSION = '1.001025'; + +$VERSION = eval $VERSION; =head1 NAME @@ -512,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} || {}, }; @@ -629,7 +631,7 @@ sub dump { } $self->msg("generating fixtures"); - my $tmp_output_dir = dir(tmpdir()); + my $tmp_output_dir = tempdir(); if (-e $tmp_output_dir) { $self->msg("- clearing existing $tmp_output_dir"); @@ -719,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"); @@ -843,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 @@ -867,7 +868,7 @@ sub dump_object { $ds{external}->{$field} = encode_base64( $class - ->backup($key => $args)); + ->backup($key => $args),''); } } @@ -1044,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; @@ -1192,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 @@ -1271,7 +1276,7 @@ sub populate { return 1 if $params->{no_populate}; $self->msg("\nimporting fixtures"); - my $tmp_fixture_dir = dir(tmpdir()); + 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 } : ''; @@ -1331,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($_) ); @@ -1363,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) { @@ -1387,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); } @@ -1450,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