From: Peter Rabbitson Date: Sat, 28 Apr 2012 02:01:33 +0000 (+0200) Subject: Stop the DBI parser from disconnecting externally supplied DBI handles X-Git-Tag: v0.11011~17 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c3919fdd12bd325e67f705e82aaf355940bc861b;p=dbsrgits%2FSQL-Translator.git Stop the DBI parser from disconnecting externally supplied DBI handles --- diff --git a/Changes b/Changes index 3c49720..9edefbd 100644 --- a/Changes +++ b/Changes @@ -22,6 +22,7 @@ * Correct Data Type in SQLT::Parser::DBI::PostgreSQL (patch from Andrew Pam) * Fix index issue in SQLT::Parser::DBI::PostgreSQL * Add column and table comments in SQLT::Parser::DBI::PostgreSQL(patch from Andrew Pam) +* Stop the DBI parser from disconnecting externally supplied DBI handles (RT#35868) * Fixed alter_drop_constraint for foreign keys and applying multiple changes via alter_field to a column in Postgres Producer * Added a working mechanism for naming foreign keys in the PostgreSQL producer diff --git a/lib/SQL/Translator/Parser/DBI.pm b/lib/SQL/Translator/Parser/DBI.pm index a653657..86236ea 100644 --- a/lib/SQL/Translator/Parser/DBI.pm +++ b/lib/SQL/Translator/Parser/DBI.pm @@ -132,6 +132,7 @@ sub parse { my $db_user = $args->{'db_user'}; my $db_password = $args->{'db_password'}; + my $dbh_is_local; unless ( $dbh ) { die 'No DSN' unless $dsn; $dbh = DBI->connect( $dsn, $db_user, $db_password, @@ -142,6 +143,7 @@ sub parse { RaiseError => 1, } ); + $dbh_is_local = 1; } die 'No database handle' unless defined $dbh; @@ -153,16 +155,17 @@ sub parse { SQL::Translator::load( $pkg ); - eval { + my $s = eval { no strict 'refs'; &{ $sub }( $tr, $dbh ) or die "No result from $pkg"; }; + my $err = $@; - $dbh->disconnect if defined $dbh; + eval { $dbh->disconnect } if (defined $dbh and $dbh_is_local); - die $@ if $@; + die $err if $err; - return 1; + return $s; } 1; diff --git a/t/66-postgres-dbi-parser.t b/t/66-postgres-dbi-parser.t index c7c64c9..86a287d 100644 --- a/t/66-postgres-dbi-parser.t +++ b/t/66-postgres-dbi-parser.t @@ -8,7 +8,7 @@ use SQL::Translator::Schema::Constants; use Test::SQL::Translator qw(maybe_plan table_ok); BEGIN { - maybe_plan(60, 'SQL::Translator::Parser::DBI::PostgreSQL'); + maybe_plan(61, 'SQL::Translator::Parser::DBI::PostgreSQL'); SQL::Translator::Parser::DBI::PostgreSQL->import('parse'); } @@ -32,7 +32,6 @@ SKIP: { ok($dbh, "dbh setup correctly"); $dbh->do('SET client_min_messages=WARNING'); -my $t = SQL::Translator->new( trace => 0 ); my $sql = q[ drop table if exists sqlt_test2; drop table if exists sqlt_test1; @@ -74,10 +73,18 @@ $| = 1; $dbh->do($sql); -my $data = SQL::Translator::Parser::DBI::PostgreSQL::parse( $t, $dbh ); +my $t = SQL::Translator->new( + trace => 0, + parser => 'DBI', + parser_args => { dbh => $dbh }, +); +$t->translate; my $schema = $t->schema; isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' ); + +ok ($dbh->ping, 'External handle still connected'); + my @tables = $schema->get_tables; my $t1 = $schema->get_table("sqlt_test1");