Fix exception on complex update/delete under a replicated setup
[dbsrgits/DBIx-Class-Historic.git] / t / lib / DBICTest / BaseSchema.pm
index 5fb9022..12cb5b8 100644 (file)
@@ -7,42 +7,37 @@ use base qw(DBICTest::Base DBIx::Class::Schema);
 
 use Fcntl qw(:DEFAULT :seek :flock);
 use Time::HiRes 'sleep';
+use Scope::Guard ();
 use DBICTest::Util::LeakTracer qw(populate_weakregistry assert_empty_weakregistry);
 use DBICTest::Util 'local_umask';
 use namespace::clean;
 
-{
-  package # moar hide
-    DBICTest::SQLTracerObj;
-  use base 'DBIx::Class::Storage::Statistics';
-
-  sub query_start { push @{$_[0]{sqlbinds}}, [ ($_[1] =~ /^\s*(\S+)/)[0], [ $_[1], @{ $_[2]||[] } ] ] }
-
-  # who the hell came up with this API >:(
-  for my $txn (qw(begin rollback commit)) {
-    no strict 'refs';
-    *{"txn_$txn"} = sub { push @{$_[0]{sqlbinds}}, [ uc $txn => [ uc $txn ] ] };
-  }
-
-  sub svp_begin { push @{$_[0]{sqlbinds}}, [ SAVEPOINT => [ "SAVEPOINT $_[1]" ] ] }
-  sub svp_release { push @{$_[0]{sqlbinds}}, [ RELEASE_SAVEPOINT => [ "RELEASE $_[1]" ] ] }
-  sub svp_rollback { push @{$_[0]{sqlbinds}}, [ ROLLBACK_TO_SAVEPOINT => [ "ROLLBACK TO $_[1]" ] ] }
-
-}
-
 sub capture_executed_sql_bind {
   my ($self, $cref) = @_;
 
   $self->throw_exception("Expecting a coderef to run") unless ref $cref eq 'CODE';
 
+  require DBICTest::SQLTracerObj;
+
   # hack around stupid, stupid API
   no warnings 'redefine';
   local *DBIx::Class::Storage::DBI::_format_for_trace = sub { $_[1] };
   Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
 
-  local $self->storage->{debugcb};
-  local $self->storage->{debugobj} = my $tracer_obj = DBICTest::SQLTracerObj->new;
-  local $self->storage->{debug} = 1;
+  # can not use local() due to an unknown number of storages
+  # (think replicated)
+  my $orig_states = { map
+    { $_ => $self->storage->$_ }
+    qw(debugcb debugobj debug)
+  };
+
+  my $sg = Scope::Guard->new(sub {
+    $self->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states;
+  });
+
+  $self->storage->debugcb(undef);
+  $self->storage->debugobj( my $tracer_obj = DBICTest::SQLTracerObj->new );
+  $self->storage->debug(1);
 
   local $Test::Builder::Level = $Test::Builder::Level + 2;
   $cref->();
@@ -273,7 +268,10 @@ sub clone {
 }
 
 END {
-  assert_empty_weakregistry($weak_registry, 'quiet');
+  # Make sure we run after any cleanup in other END blocks
+  push @{ B::end_av()->object_2svref }, sub {
+    assert_empty_weakregistry($weak_registry, 'quiet');
+  };
 }
 
 1;