Skip Menu |

This queue is for tickets about the Catalyst-Model-DBI CPAN distribution.

Report information
The Basics
Id: 39884
Status: resolved
Priority: 0/
Queue: Catalyst-Model-DBI

People
Owner: Nobody in particular
Requestors: janus [...] errornet.de
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.19
Fixed in: (no value)



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
Hi, Thanks for the patch, I will apply it as soon as I have a chance. I am currently busy with a few things but will try either this or next week. On Wed Oct 08 03:52:38 2008, janus wrote: Show quoted text
> Hi, > I see the following warning at a few places: > (in cleanup) Can't use an undefined value as a HASH
reference at Show quoted text
> /usr/local/libdata/perl5/site_perl/Catalyst/Model/DBI.pm line 104
during Show quoted text
> global destruction. > > The solution is pretty simple. To show where that is happening i
wrote Show quoted text
> a test case, but it only shows a warning and doesn't fail without
the fix. Show quoted text
> At least Catalyst::Model::DBI has a fully exercising test with this > using DBD::SQLite. > > Regards, > Simon
Patch applied, new version 0.20 uploaded to PAUSE/CPAN.