From 986ee14c997fe16c849e26580c772db0f407875d Mon Sep 17 00:00:00 2001 From: Alexander Hartmaier Date: Wed, 9 Feb 2011 17:34:08 +0100 Subject: [PATCH] fixed updating of nullable has_many rels (RT#65561) --- Changes | 1 + lib/DBIx/Class/ResultSet/RecursiveUpdate.pm | 4 ++-- t/lib/DBSchema/Result/Dvd.pm | 2 +- t/lib/RunTests.pm | 24 ++++++++++++++------- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Changes b/Changes index a543d47..656f3cb 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for DBIx::Class::ResultSet::RecursiveUpdate {{ $NEXT }} + - Fixed updating of nullable has_many rels (RT#65561) 0.21 2010-10-28 16:56:18 Europe/Vienna - Warn instead of throwing an exception if a key is neither diff --git a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm index 1d61681..c0db8b4 100644 --- a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm +++ b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm @@ -326,7 +326,7 @@ sub _update_relation { ->{is_nullable}; } - $if_not_submitted = $all_fks_nullable ? 'nullify' : 'delete' + $if_not_submitted = $all_fks_nullable ? 'set_to_null' : 'delete' unless defined $if_not_submitted; #warn "\tNULLABLE: $all_fks_nullable ACTION: $if_not_submitted\n"; @@ -387,9 +387,9 @@ sub _update_relation { $rs_rel_delist = $rs_rel_delist->search_rs( { -not => [@cond] } ); } + #warn "\tCOND: " . Dumper(\@cond); } - #warn "\tCOND: " . Dumper(\%cond); #my $rel_delist_cnt = $rs_rel_delist->count; if ( $if_not_submitted eq 'delete' ) { diff --git a/t/lib/DBSchema/Result/Dvd.pm b/t/lib/DBSchema/Result/Dvd.pm index e9d6627..aedd435 100644 --- a/t/lib/DBSchema/Result/Dvd.pm +++ b/t/lib/DBSchema/Result/Dvd.pm @@ -46,7 +46,7 @@ __PACKAGE__->add_columns( ); __PACKAGE__->set_primary_key('dvd_id'); __PACKAGE__->belongs_to('owner', 'DBSchema::Result::User', 'owner'); -__PACKAGE__->belongs_to('current_borrower', 'DBSchema::Result::User', 'current_borrower'); +__PACKAGE__->belongs_to('current_borrower', 'DBSchema::Result::User', 'current_borrower', { join_type => "LEFT" }); __PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.dvd_id' }); __PACKAGE__->has_many('viewings', 'DBSchema::Result::Viewing', { 'foreign.dvd_id' => 'self.dvd_id' }); __PACKAGE__->many_to_many('tags', 'dvdtags' => 'tag'); diff --git a/t/lib/RunTests.pm b/t/lib/RunTests.pm index 4f9c614..204b93c 100644 --- a/t/lib/RunTests.pm +++ b/t/lib/RunTests.pm @@ -11,7 +11,7 @@ use DBIx::Class::ResultSet::RecursiveUpdate; sub run_tests { my $schema = shift; - plan tests => 55; + plan tests => 59; my $dvd_rs = $schema->resultset('Dvd'); my $user_rs = $schema->resultset('User'); @@ -282,6 +282,8 @@ TODO: { $dvd = $dvd_rs->find(1); is( $dvd->get_column('owner'), $user->id, 'foreign key set' ); + # has_many where foreign cols are nullable + my $available_dvd_rs = $dvd_rs->search({ current_borrower => undef }); $dvd_rs->update( { current_borrower => $user->id } ); ok( $user->borrowed_dvds->count > 1, 'Precond' ); $updates = { @@ -294,21 +296,27 @@ TODO: { updates => $updates, if_not_submitted => 'set_to_null', ); - is( $user->borrowed_dvds->count, 1, 'set_to_null' ); + is( $user->borrowed_dvds->count, 1, 'borrowed_dvds update with if_not_submitted => set_to_null ok' ); + is( $available_dvd_rs->count, 5, "previously borrowed dvds weren't deleted"); + + $dvd_rs->update( { current_borrower => $user->id } ); + $user = + DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update( + resultset => $user_rs, + updates => $updates, + ); + is( $user->borrowed_dvds->count, 1, 'borrowed_dvds update without if_not_submitted ok' ); + is( $available_dvd_rs->count, 5, "previously borrowed dvds weren't deleted"); - # has_many where foreign cols are nullable $dvd_rs->update( { current_borrower => $user->id } ); - $updates = { - id => $user->id, - borrowed_dvds => [ { id => $dvd->id }, ] - }; $user = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update( resultset => $user_rs, updates => $updates, if_not_submitted => 'delete', ); - is( $user->borrowed_dvds->count, 1, 'if_not_submitted delete' ); + is( $user->borrowed_dvds->count, 1, 'borrowed_dvds update with if_not_submitted => delete ok' ); + is( $dvd_rs->count, 1, 'all dvds except the one borrowed by the user were deleted'); @tags = $schema->resultset('Tag')->all; $dvd_updated = -- 2.43.0