From: Tom Hukins Date: Wed, 22 Aug 2007 10:28:58 +0000 (+0000) Subject: Fix the behaviour of code refs within array refs for on_(dis)?connect_do X-Git-Tag: v0.08010~75 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=commitdiff_plain;h=1dafdb2a28c700eabf975685d823036b1b9d69f4 Fix the behaviour of code refs within array refs for on_(dis)?connect_do and enhance tests to spot the previous mistake. --- diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 3338b93..c0b17ad 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -366,7 +366,7 @@ array reference, its return value is ignored. =item on_disconnect_do -Takes arguments in the same for as L and executes them +Takes arguments in the same form as L and executes them immediately before disconnecting from the database. Note, this only runs if you explicitly call L on the @@ -792,7 +792,8 @@ sub _do_query { my ($self, $action) = @_; if (ref $action eq 'CODE') { - $action->($self); + $action = $action->($self); + $self->_do_query($_) foreach @$action; } else { my @to_run = (ref $action eq 'ARRAY') ? (@$action) : ($action); diff --git a/t/92storage_on_connect_do.t b/t/92storage_on_connect_do.t index 0c35923..62712af 100644 --- a/t/92storage_on_connect_do.t +++ b/t/92storage_on_connect_do.t @@ -17,6 +17,7 @@ ok $schema->connection( on_connect_do => [ 'CREATE TABLE TEST_empty (id INTEGER)', [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ], + \&insert_from_subref, ], on_disconnect_do => [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped], @@ -25,7 +26,7 @@ ok $schema->connection( is_deeply $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'), - [ [ 2 ] ], + [ [ 2 ], [ 3 ], [ 7 ] ], 'on_connect_do() worked'; eval { $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent'); }; ok $@, 'Searching for nonexistent table dies'; @@ -50,10 +51,20 @@ ok $disconnected, 'on_disconnect_do() called after disconnect()'; sub check_exists { my $storage = shift; ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists'; + return; } sub check_dropped { my $storage = shift; eval { $storage->dbh->do('SELECT 1 FROM TEST_empty'); }; ok $@, 'Reading from dropped table fails'; + return; +} + +sub insert_from_subref { + my $storage = shift; + return [ + [ 'INSERT INTO TEST_empty VALUES (?)', {}, 3 ], + 'INSERT INTO TEST_empty VALUES (7)', + ]; }