]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/commitdiff
fixed updating of nullable has_many rels (RT#65561)
authorAlexander Hartmaier <abraxxa@cpan.org>
Wed, 9 Feb 2011 16:34:08 +0000 (17:34 +0100)
committerAlexander Hartmaier <abraxxa@cpan.org>
Wed, 9 Feb 2011 16:34:08 +0000 (17:34 +0100)
Changes
lib/DBIx/Class/ResultSet/RecursiveUpdate.pm
t/lib/DBSchema/Result/Dvd.pm
t/lib/RunTests.pm

diff --git a/Changes b/Changes
index a543d471f6e2eb838c3fbbd73a9a9f21a9541d2b..656f3cbbffc4e05e93543d431e531b18a571ef8d 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 Revision history for DBIx::Class::ResultSet::RecursiveUpdate
 
 {{ $NEXT }}
 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
 
 0.21      2010-10-28 16:56:18 Europe/Vienna
     - Warn instead of throwing an exception if a key is neither
index 1d6168132f00ea8cc4c3d3604640121a97ef32f7..c0db8b415ebac682e0a1bad8fe19b18722fb0604 100644 (file)
@@ -326,7 +326,7 @@ sub _update_relation {
                 ->{is_nullable};
     }
 
                 ->{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";
         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] } );
             }
                 $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' ) {
 
         #my $rel_delist_cnt = $rs_rel_delist->count;
         if ( $if_not_submitted eq 'delete' ) {
 
index e9d6627773c4e9a8ae7641d1c492dca394ca5cf4..aedd4354af028c112d81492a15ae728ccd7a22ac 100644 (file)
@@ -46,7 +46,7 @@ __PACKAGE__->add_columns(
 );
 __PACKAGE__->set_primary_key('dvd_id');
 __PACKAGE__->belongs_to('owner', 'DBSchema::Result::User', 'owner');
 );
 __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');
 __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');
index 4f9c614a9787c69ce614139f4994a9b751d9b3fd..204b93ce81140d8d6d594fdab1207051c03e77c8 100644 (file)
@@ -11,7 +11,7 @@ use DBIx::Class::ResultSet::RecursiveUpdate;
 sub run_tests {
     my $schema = shift;
 
 sub run_tests {
     my $schema = shift;
 
-    plan tests => 55;
+    plan tests => 59;
 
     my $dvd_rs  = $schema->resultset('Dvd');
     my $user_rs = $schema->resultset('User');
 
     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' );
 
     $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 = {
     $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',
         );
         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 } );
     $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',
         );
     $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 =
 
     @tags = $schema->resultset('Tag')->all;
     $dvd_updated =
This page took 0.022615 seconds and 4 git commands to generate.