Skip to content

Commit

Permalink
Ensure ::Schema::Versioned connects only once by reusing the main con…
Browse files Browse the repository at this point in the history
…nection
  • Loading branch information
ribasushi committed Sep 10, 2014
1 parent bb96193 commit 81023d8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/DBIx/Class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
10 changes: 6 additions & 4 deletions lib/DBIx/Class/Schema/Versioned.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');

Expand All @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions t/94versioning.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 81023d8

Please sign in to comment.