--Apple-Mail-6--780610601
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed
On Jul 30, 2007, at 5:28 PM, Brandon Black wrote:
> On 7/30/07, Adam Herzog <adam@herzogdesigns.com> wrote:>> Okay; I think I have this fixed.>>>> I ended up having to create a new, dummy db and chmoding it in order>> to prevent SQLite from connecting. The fix ended up being fairly>> straightforward once I had a sane test. No exception was being thrown>> if the connection failed, because it was checking $dbh (which was>> always true) instead of $DBI::errstr, which had the actual failure>> message.>>>> Can somebody put their eyes on this and let me know if I've>> overlooked anything (and commit if I haven't.)>> Ugh, that's cute. Here's what the DBI docs say:>> 'If the connect fails (see below), it returns "undef" and sets both> $DBI::err and $DBI::errstr. (It does not explicitly set $!.) You> should generally test the return status of "connect" and "print> $DBI::errstr" if it has failed.'>> How about we check all three, as in: "if !$dbh || $@ || $DBI::errstr"> on the appropriate line?I saw that in the docs, but I guess I was too focused on my own use
case, where $dbh was becoming a hashref (with RaiseError, PrintError,
et al), and thus evaluating as true. I didn't think that checking
$dbh would buy us anything.
But, I guess in a situation where an empty $dbh is coming from a
coderef (and not setting $@ or $DBI::errstr) AND 'unsafe' is set (so
it isn't getting turned into a hashref), we'll need to check.
Updated patch is attached.
-A
--Apple-Mail-6--780610601
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream; x-unix-mode=0644; name=reconnect_2.diff
Content-Disposition: attachment;
filename=reconnect_2.diff
Index: t/33storage_reconnect.t
===================================================================
--- t/33storage_reconnect.t (revision 3637)
+++ t/33storage_reconnect.t (working copy)
@@ -1,12 +1,17 @@
use strict;
use warnings;
+use FindBin;
+use File::Copy;
use Test::More;
use lib qw(t/lib);
use DBICTest;
-plan tests => 2;
+plan tests => 5;
+my $db_orig = "$FindBin::Bin/var/DBIxClass.db";
+my $db_tmp = "$db_orig.tmp";
+
# Set up the "usual" sqlite for DBICTest
my $schema = DBICTest->init_schema;
@@ -24,3 +29,31 @@
# 4. Success!
my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
cmp_ok(@art_two, '==', 3, "Three artists returned");
+
+### Now, disconnect the dbh, and move the db file;
+# create a new one and chmod 000 to prevent SQLite from connecting.
+$schema->storage->_dbh->disconnect;
+move( $db_orig, $db_tmp );
+open DBFILE, '>', $db_orig;
+print DBFILE 'THIS IS NOT A REAL DATABASE';
+close DBFILE;
+chmod 0000, $db_orig;
+
+### Try the operation again... it should fail, since there's no db
+eval {
+ my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
+};
+ok( $@, 'The operation failed' );
+
+### Now, move the db file back to the correct name
+unlink($db_orig);
+move( $db_tmp, $db_orig );
+
+### Try the operation again... this time, it should succeed
+my @art_four;
+eval {
+ @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
+};
+ok( !$@, 'The operation succedded' );
+cmp_ok( @art_four, '==', 3, "Three artists returned" );
+
Index: lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- lib/DBIx/Class/Storage/DBI.pm (revision 3637)
+++ lib/DBIx/Class/Storage/DBI.pm (working copy)
@@ -785,7 +785,7 @@
$DBI::connect_via = $old_connect_via if $old_connect_via;
$self->throw_exception("DBI Connection failed: " . ($@||$DBI::errstr))
- if !$dbh || $@;
+ if !$dbh || $@ || $DBI::errstr;
$self->_dbh_autocommit($dbh->{AutoCommit});
--Apple-Mail-6--780610601
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed
--Apple-Mail-6--780610601
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline