From: Peter Rabbitson <ribasushi@cpan.org>
Date: Tue, 9 Sep 2014 23:48:35 +0000 (+0200)
Subject: Ensure ::Schema::Versioned connects only once by reusing the main connection
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=81023d83ad94dc8e6601d7c8aad598673f90ad18;p=dbsrgits%2FDBIx-Class-Historic.git

Ensure ::Schema::Versioned connects only once by reusing the main connection
---

diff --git a/Changes b/Changes
index eace9ac..4a830a2 100644
--- a/Changes
+++ b/Changes
@@ -51,6 +51,8 @@ Revision history for DBIx::Class
           to SQL::Translator (partially RT#87731)
         - Fix SQLT constraint naming when DBIC table names are fully qualified
           (PR#48)
+        - Ensure ::Schema::Versioned connects only once by reusing the main
+          connection (GH#57)
         - Fix inability to handle multiple consecutive transactions with
           savepoints on DBD::SQLite < 1.39
         - Fix CDBICompat to match Class::DBI behavior handling non-result
diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm
index f2229f0..88b6bf4 100644
--- a/lib/DBIx/Class.pm
+++ b/lib/DBIx/Class.pm
@@ -416,6 +416,8 @@ jasonmay: Jason May <jason.a.may@gmail.com>
 
 jegade: Jens Gassmann <jens.gassmann@atomix.de>
 
+jeneric: Eric A. Miller <emiller@cpan.org>
+
 jesper: Jesper Krogh
 
 jgoulah: John Goulah <jgoulah@cpan.org>
diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm
index 95adc66..e92f356 100644
--- a/lib/DBIx/Class/Schema/Versioned.pm
+++ b/lib/DBIx/Class/Schema/Versioned.pm
@@ -204,6 +204,7 @@ use base 'DBIx::Class::Schema';
 use DBIx::Class::Carp;
 use Time::HiRes qw/gettimeofday/;
 use Try::Tiny;
+use Scalar::Util 'weaken';
 use namespace::clean;
 
 __PACKAGE__->mk_classdata('_filedata');
@@ -589,9 +590,10 @@ sub _on_connect
 {
   my ($self) = @_;
 
-  my $conn_info = $self->storage->connect_info;
-  $self->{vschema} = DBIx::Class::Version->connect(@$conn_info);
-  my $conn_attrs = $self->{vschema}->storage->_dbic_connect_attributes || {};
+  weaken (my $w_self = $self );
+
+  $self->{vschema} = DBIx::Class::Version->connect(sub { $w_self->storage->dbh });
+  my $conn_attrs = $self->storage->_dbic_connect_attributes || {};
 
   my $vtable = $self->{vschema}->resultset('Table');
 
@@ -600,7 +602,7 @@ sub _on_connect
 
   # check for legacy versions table and move to new if exists
   unless ($self->_source_exists($vtable)) {
-    my $vtable_compat = DBIx::Class::VersionCompat->connect(@$conn_info)->resultset('TableCompat');
+    my $vtable_compat = DBIx::Class::VersionCompat->connect(sub { $w_self->storage->dbh })->resultset('TableCompat');
     if ($self->_source_exists($vtable_compat)) {
       $self->{vschema}->deploy;
       map { $vtable->new_result({ installed => $_->Installed, version => $_->Version })->insert } $vtable_compat->all;
diff --git a/t/94versioning.t b/t/94versioning.t
index 93fcca7..a154d8f 100644
--- a/t/94versioning.t
+++ b/t/94versioning.t
@@ -285,6 +285,17 @@ my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_versio
   ok($get_db_version_run == 0, "attributes pulled from list connect_info");
 }
 
+# at this point we have v1, v2 and v3 still connected
+# make sure they are the only connections and everything else is gone
+is
+  scalar( grep
+    { defined $_ and $_->{Active} }
+    map
+      { @{$_->{ChildHandles}} }
+      values %{ { DBI->installed_drivers } }
+  ), 3, "Expected number of connections at end of script"
+;
+
 END {
   unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
     $ddl_dir->rmtree;