From: Peter Rabbitson Date: Thu, 30 Aug 2012 17:06:13 +0000 (+0200) Subject: Replace inadequate $dbh->ping SQLite implementation (RT#78420) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8d1fb3e2db62af871fa19e9d632c317021a356a5;p=dbsrgits%2FDBIx-Class-Historic.git Replace inadequate $dbh->ping SQLite implementation (RT#78420) When SQLite attempts to connect to a file that is not a database, it nevertheless maintains a true $dbh->{Active} and $dbh->ping. Replace with a schema listing SELECT, and fix test erroneously assuming it can portably do chmod 000 --- diff --git a/Changes b/Changes index 2fef676..1ffaaf4 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for DBIx::Class + * Fixes + - Replace inadequate $dbh->ping SQLite implementation with our own, + fixes RT#78420 + 0.08200 2012-08-24 (UTC) * Fixes - Change one of the new tests for the previous release to not require diff --git a/lib/DBIx/Class/Storage/DBI/SQLite.pm b/lib/DBIx/Class/Storage/DBI/SQLite.pm index 6943c77..309767f 100644 --- a/lib/DBIx/Class/Storage/DBI/SQLite.pm +++ b/lib/DBIx/Class/Storage/DBI/SQLite.pm @@ -8,6 +8,7 @@ use mro 'c3'; use DBIx::Class::Carp; use Scalar::Util 'looks_like_number'; +use Try::Tiny; use namespace::clean; __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::SQLite'); @@ -91,6 +92,11 @@ sub _exec_svp_rollback { $self->_dbh->do("ROLLBACK TRANSACTION TO SAVEPOINT $name"); } +sub _ping { + my $self = shift; + try { $self->_dbh->do('SELECT * FROM sqlite_master LIMIT 1'); 1 }; +} + sub deployment_statements { my $self = shift; my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_; diff --git a/t/storage/reconnect.t b/t/storage/reconnect.t index b28734b..784a245 100644 --- a/t/storage/reconnect.t +++ b/t/storage/reconnect.t @@ -36,26 +36,24 @@ 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. +# create a new one full of garbage, prevent SQLite from connecting. $schema->storage->_dbh->disconnect; move( $db_orig, $db_tmp ) or die "failed to move $db_orig to $db_tmp: $!"; -open DBFILE, '>', $db_orig; -print DBFILE 'THIS IS NOT A REAL DATABASE'; -close DBFILE; -chmod 0000, $db_orig; +open my $db_file, '>', $db_orig; +print $db_file 'THIS IS NOT A REAL DATABASE'; +close $db_file; -### Try the operation again... it should fail, since there's no db +### Try the operation again... it should fail, since there's no valid db { - # Catch the DBI connection error - local $SIG{__WARN__} = sub {}; - dies_ok { - my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } ); - } 'The operation failed'; + # Catch the DBI connection error + local $SIG{__WARN__} = sub {}; + throws_ok { + my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } ); + } qr/not a database/, 'The operation failed'; } -# otherwise can't unlink the fake db file -$schema->storage->_dbh->disconnect if $^O eq 'MSWin32'; +ok (! $schema->storage->connected, 'We are not connected' ); ### Now, move the db file back to the correct name unlink($db_orig) or die "could not delete $db_orig: $!";