diff --git a/lib/DBIx/Class/Fixtures.pm b/lib/DBIx/Class/Fixtures.pm index 53c4e26..1aace91 100644 --- a/lib/DBIx/Class/Fixtures.pm +++ b/lib/DBIx/Class/Fixtures.pm @@ -511,6 +511,7 @@ sub new { dumped_objects => {}, use_create => $params->{use_create} || 0, use_find_or_create => $params->{use_find_or_create} || 0, + update_existing => $params->{update_existing} || 0, config_attrs => $params->{config_attrs} || {}, }; @@ -1209,6 +1210,11 @@ sub dump_all_config_sets { # Useful if you are populating a persistent data store. use_find_or_create => 0, + # optional, use in conjunction with use_find_or_create to update existing + # records with the updated fixture where they can already be found with + # find_or_create + update_existing => 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 # no_deploy => 1 @@ -1397,7 +1403,8 @@ sub populate { if ( $params->{use_create} ) { $rs->create( $HASH1 ); } elsif( $params->{use_find_or_create} ) { - $rs->find_or_create( $HASH1 ); + my $row = $rs->find_or_create( $HASH1 ); + $row->update($HASH1) if $params->{update_existing}; } else { push(@rows, $HASH1); } diff --git a/t/12-populate-basic.t b/t/12-populate-basic.t index b8cb40b..9195e69 100644 --- a/t/12-populate-basic.t +++ b/t/12-populate-basic.t @@ -103,3 +103,15 @@ $fixtures->populate({ use_find_or_create => 1 }); is( $schema->resultset( "Artist" )->find({ artistid => 4 })->name, "Test Name", "idempotent use_find_or_create => 1 ok" ); +# Update the existing record to something else, and then check that it gets +# updated to the original fixture value with update_existing +$schema->resultset( "Artist" )->find({ artistid => 3 })->update({ name => "We Are Goth2" }); +$fixtures->populate({ + directory => $tempdir, + connection_details => ['dbi:SQLite:'.io->catfile($tempdir, qw[ DBIxClass.db ])->name, '', ''], + schema => $schema, + no_deploy => 1, + use_find_or_create => 1, + update_existing => 1 +}); +is( $schema->resultset( "Artist" )->find({ artistid => 3 })->name, "We Are Goth", "update_existing => 1 ok" ); diff --git a/t/var/configs/use_create.json b/t/var/configs/use_create.json index a545630..5a40f6b 100644 --- a/t/var/configs/use_create.json +++ b/t/var/configs/use_create.json @@ -1,6 +1,6 @@ { "sets": [{ "class": "Artist", - "ids": ["4"] + "ids": ["3", "4"] }] }