Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to update existing fixtures #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/DBIx/Class/Fixtures.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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} || {},
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions t/12-populate-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
2 changes: 1 addition & 1 deletion t/var/configs/use_create.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sets": [{
"class": "Artist",
"ids": ["4"]
"ids": ["3", "4"]
}]
}