Subject: | Patch to fix use of undefined value as hash in cleanup |
Hi,
I see the following warning at a few places:
(in cleanup) Can't use an undefined value as a HASH reference at
/usr/local/libdata/perl5/site_perl/Catalyst/Model/DBI.pm line 104 during
global destruction.
The solution is pretty simple. To show where that is happening i wrote
a test case, but it only shows a warning and doesn't fail without the fix.
At least Catalyst::Model::DBI has a fully exercising test with this
using DBD::SQLite.
Regards,
Simon
Subject: | Catalyst-Model-DBI-fix-and-test.diff |
Index: t/lib/TestApp/Model/DBI.pm
===================================================================
--- t/lib/TestApp/Model/DBI.pm (revision 0)
+++ t/lib/TestApp/Model/DBI.pm (revision 0)
@@ -0,0 +1,5 @@
+package TestApp::Model::DBI;
+use strict;
+use base 'Catalyst::Model::DBI';
+
+1;
Index: t/lib/SetupDB.pm
===================================================================
--- t/lib/SetupDB.pm (revision 0)
+++ t/lib/SetupDB.pm (revision 0)
@@ -0,0 +1,31 @@
+use DBI;
+
+my $dbfile = $ENV{'TESTAPP_DB_FILE'};
+
+if (-e $dbfile) {
+ unlink($dbfile);
+}
+
+my $dbh = DBI->connect("dbi:SQLite:$dbfile") or die($DBI::errstr);
+
+my $sql = <<'EOSQL';
+CREATE TABLE user (
+ id integer not null,
+ name text not null,
+ password text not null,
+ active integer not null,
+ CONSTRAINT pk_user PRIMARY KEY (id)
+);
+CREATE UNIQUE INDEX idx_user_name
+ ON user (name);
+
+INSERT INTO user VALUES (1, 'joe', 'x', 1);
+INSERT INTO user VALUES (2, 'bob', 'y', 1);
+INSERT INTO user VALUES (3, 'martin', 'z', 1);
+EOSQL
+
+for (split(m/;\s*/, $sql)) {
+ $dbh->do($_) or die($dbh->errstr);
+}
+$dbh->disconnect();
+
Index: t/lib/TestApp.pm
===================================================================
--- t/lib/TestApp.pm (revision 0)
+++ t/lib/TestApp.pm (revision 0)
@@ -0,0 +1,33 @@
+package TestApp;
+use strict;
+use Catalyst;
+
+TestApp->config($ENV{'TESTAPP_CONFIG'});
+TestApp->setup(@{$ENV{'TESTAPP_PLUGINS'}});
+
+sub user_id :Local
+{
+ my ($self, $c) = @_;
+ my $req = $c->request();
+ my $res = $c->response();
+ my $dbh = $c->model('DBI')->dbh();
+ my $sth;
+ my $sql;
+ my $id;
+
+ $sql = 'SELECT id FROM user WHERE name = ?';
+ $sth = $dbh->prepare($sql) or die($dbh->errstr());
+ $sth->execute($req->param('name')) or die($dbh->errstr());
+ $sth->bind_columns(\$id) or die($dbh->errstr());
+ $sth->fetch() or die($dbh->errstr());
+ $sth->finish();
+
+ if ($id) {
+ $res->body("user has id $id");
+ }
+ else {
+ $res->body("user not found");
+ }
+}
+
+1;
Index: t/04cleanup.t
===================================================================
--- t/04cleanup.t (revision 0)
+++ t/04cleanup.t (revision 0)
@@ -0,0 +1,34 @@
+use strict;
+use FindBin;
+use Test::More;
+use lib "$FindBin::Bin/lib";
+
+BEGIN {
+ eval { require DBD::SQLite }
+ or plan skip_all =>
+ "DBD::SQLite is required for this test";
+
+ plan tests => 2;
+
+ unless (exists($ENV{'TESTAPP_DB_FILE'})) {
+ $ENV{'TESTAPP_DB_FILE'} = "$FindBin::Bin/test.db";
+ }
+
+ $ENV{'TESTAPP_CONFIG'} = {
+ 'name' => 'TestApp',
+ 'Model::DBI' => {
+ 'dsn' => 'dbi:SQLite:' . $ENV{'TESTAPP_DB_FILE'},
+ },
+ };
+
+ $ENV{'TESTAPP_PLUGINS'} = [];
+}
+
+use SetupDB;
+
+use Catalyst::Test 'TestApp';
+
+{
+ ok(my $res = request('http://localhost/user_id?name=joe'), 'request ok');
+ is($res->content(), 'user has id 1', 'user found');
+}
Index: lib/Catalyst/Model/DBI.pm
===================================================================
--- lib/Catalyst/Model/DBI.pm (revision 8514)
+++ lib/Catalyst/Model/DBI.pm (working copy)
@@ -101,6 +101,7 @@ Returns true if the database handle is active and ping
sub connected {
my $self = shift;
+ return unless $self->_dbh;
return $self->_dbh->{Active} && $self->_dbh->ping;
}
Index: MANIFEST
===================================================================
--- MANIFEST (revision 8514)
+++ MANIFEST (working copy)
@@ -3,8 +3,12 @@ lib/Catalyst/Helper/Model/DBI.pm
lib/Catalyst/Model/DBI.pm
Makefile.PL
MANIFEST This list of files
+META.yml Module meta-data (added by MakeMaker)
README
t/01use.t
t/02pod.t
t/03podcoverage.t
-META.yml Module meta-data (added by MakeMaker)
+t/04cleanup.t
+t/lib/SetupDB.pm
+t/lib/TestApp.pm
+t/lib/TestApp/Model/DBI.pm