[Bast-commits] r8921 - in DBIx-Class/0.08/trunk: . lib/DBIx/Class/Relationship t

ribasushi at dev.catalyst.perl.org ribasushi at dev.catalyst.perl.org
Sat Mar 6 23:52:50 GMT 2010


Author: ribasushi
Date: 2010-03-06 23:52:50 +0000 (Sat, 06 Mar 2010)
New Revision: 8921

Modified:
   DBIx-Class/0.08/trunk/Changes
   DBIx-Class/0.08/trunk/lib/DBIx/Class/Relationship/CascadeActions.pm
   DBIx-Class/0.08/trunk/t/81transactions.t
Log:
Cascading delete needs a guard to remain atomic

Modified: DBIx-Class/0.08/trunk/Changes
===================================================================
--- DBIx-Class/0.08/trunk/Changes	2010-03-06 23:15:13 UTC (rev 8920)
+++ DBIx-Class/0.08/trunk/Changes	2010-03-06 23:52:50 UTC (rev 8921)
@@ -1,6 +1,8 @@
 Revision history for DBIx::Class
 
         - DBIx::Class::InflateColumn::File entered deprecated state
+        - Cascading delete/update are now wrapped in a transaction
+          for atomicity
         - Fix regression where SQL files with comments were not
           handled properly by ::Schema::Versioned.
         - Fix regression on not properly throwing when $obj->relationship

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/Relationship/CascadeActions.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/Relationship/CascadeActions.pm	2010-03-06 23:15:13 UTC (rev 8920)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/Relationship/CascadeActions.pm	2010-03-06 23:52:50 UTC (rev 8921)
@@ -16,15 +16,24 @@
     # be handling this anyway. Assuming we have joins we probably actually
     # *could* do them, but I'd rather not.
 
-  my $ret = $self->next::method(@rest);
-
   my $source = $self->result_source;
   my %rels = map { $_ => $source->relationship_info($_) } $source->relationships;
   my @cascade = grep { $rels{$_}{attrs}{cascade_delete} } keys %rels;
-  foreach my $rel (@cascade) {
-    $self->search_related($rel)->delete_all;
+
+  if (@cascade) {
+    my $guard = $source->schema->txn_scope_guard;
+
+    my $ret = $self->next::method(@rest);
+
+    foreach my $rel (@cascade) {
+      $self->search_related($rel)->delete_all;
+    }
+
+    $guard->commit;
+    return $ret;
   }
-  return $ret;
+
+  $self->next::method(@rest);
 }
 
 sub update {
@@ -32,22 +41,31 @@
   return $self->next::method(@rest) unless ref $self;
     # Because update cascades on a class *really* don't make sense!
 
-  my $ret = $self->next::method(@rest);
-
   my $source = $self->result_source;
   my %rels = map { $_ => $source->relationship_info($_) } $source->relationships;
   my @cascade = grep { $rels{$_}{attrs}{cascade_update} } keys %rels;
-  foreach my $rel (@cascade) {
-    next if (
-      $rels{$rel}{attrs}{accessor}
-        &&
-      $rels{$rel}{attrs}{accessor} eq 'single'
-        &&
-      !exists($self->{_relationship_data}{$rel})
-    );
-    $_->update for grep defined, $self->$rel;
+
+  if (@cascade) {
+    my $guard = $source->schema->txn_scope_guard;
+
+    my $ret = $self->next::method(@rest);
+
+    foreach my $rel (@cascade) {
+      next if (
+        $rels{$rel}{attrs}{accessor}
+          &&
+        $rels{$rel}{attrs}{accessor} eq 'single'
+          &&
+        !exists($self->{_relationship_data}{$rel})
+      );
+      $_->update for grep defined, $self->$rel;
+    }
+
+    $guard->commit;
+    return $ret;
   }
-  return $ret;
+
+  $self->next::method(@rest);
 }
 
 1;

Modified: DBIx-Class/0.08/trunk/t/81transactions.t
===================================================================
--- DBIx-Class/0.08/trunk/t/81transactions.t	2010-03-06 23:15:13 UTC (rev 8920)
+++ DBIx-Class/0.08/trunk/t/81transactions.t	2010-03-06 23:52:50 UTC (rev 8921)
@@ -150,10 +150,9 @@
   no warnings 'redefine';
   no strict 'refs';
 
-  # die in rollback, but maintain sanity for further tests ...
+  # die in rollback
   local *{"DBIx::Class::Storage::DBI::SQLite::txn_rollback"} = sub{
     my $storage = shift;
-    $storage->{transaction_depth}--;
     die 'FAILED';
   };
 
@@ -180,6 +179,9 @@
   $schema->storage->_dbh->rollback;
 }
 
+# reset schema object (the txn_rollback meddling screws it up)
+$schema = DBICTest->init_schema();
+
 # Test nested failed txn_do()
 {
   is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');




More information about the Bast-commits mailing list