Fix test suite to work again with DBICTEST_SQLITE_USE_FILE
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest.pm
index aa20b0c..fa92a95 100644 (file)
@@ -7,6 +7,7 @@ use warnings;
 use DBICTest::Util 'local_umask';
 use DBICTest::Schema;
 use DBICTest::Util::LeakTracer qw/populate_weakregistry assert_empty_weakregistry/;
+use DBIx::Class::_Util 'detected_reinvoked_destructor';
 use Carp;
 use Path::Class::File ();
 use File::Spec;
@@ -142,6 +143,7 @@ END {
 
 $SIG{INT} = sub { _cleanup_dbfile(); exit 1 };
 
+my $need_global_cleanup;
 sub _cleanup_dbfile {
     # cleanup if this is us
     if (
@@ -151,6 +153,10 @@ sub _cleanup_dbfile {
         or
       $ENV{DBICTEST_LOCK_HOLDER} == $$
     ) {
+        if ($need_global_cleanup and my $dbh = DBICTest->schema->storage->_dbh) {
+          $dbh->disconnect;
+        }
+
         my $db_file = _sqlite_dbfilename();
         unlink $_ for ($db_file, "${db_file}-journal");
     }
@@ -217,7 +223,7 @@ sub _database {
           $dbh->{Callbacks} = {
             connect => sub { $guard_cb->('connect') },
             disconnect => sub { $guard_cb->('disconnect') },
-            DESTROY => sub { $guard_cb->('DESTROY') },
+            DESTROY => sub { &detected_reinvoked_destructor; $guard_cb->('DESTROY') },
           };
         }
       },
@@ -315,6 +321,7 @@ sub init_schema {
     my $schema;
 
     if ($args{compose_connection}) {
+      $need_global_cleanup = 1;
       $schema = DBICTest::Schema->compose_connection(
                   'DBICTest', $self->_database(%args)
                 );
@@ -343,7 +350,10 @@ sub init_schema {
 }
 
 END {
+  # Make sure we run after any cleanup in other END blocks
+  push @{ B::end_av()->object_2svref }, sub {
     assert_empty_weakregistry($weak_registry, 'quiet');
+  };
 }
 
 =head2 deploy_schema