From: Dagfinn Ilmari Mannsåker Date: Sat, 18 Oct 2014 17:31:43 +0000 (+0100) Subject: Silence with_deferred_fk_checks() warning on PostgreSQL 9.4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=commitdiff_plain;h=2131aa2eac548e0b10325b97c10d22a61fffd9c9;hp=1af6b9688df2a5474d08c04419518cb3cb8656d9 Silence with_deferred_fk_checks() warning on PostgreSQL 9.4 SET CONSTRAINTS can only be used in transaction blocks, and 9.4 warns about this, so make sure we issue 'SET CONSTRAINTS ALL IMMEDIATE' before committing the transaction. Also fix it for Informix, which uses the same syntax and has the same restriction. --- diff --git a/Changes b/Changes index cb3f5be..7d23dd9 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for DBIx::Class * Fixes - Fix $rs->create() with no values on Oracle + - Silence with_deferred_fk_checks() warning on PostgreSQL 9.4 * Misc - Speed up skipping CDBICompat tests when dependencies are missing diff --git a/lib/DBIx/Class/Storage/DBI/Informix.pm b/lib/DBIx/Class/Storage/DBI/Informix.pm index e8c123d..507ee51 100644 --- a/lib/DBIx/Class/Storage/DBI/Informix.pm +++ b/lib/DBIx/Class/Storage/DBI/Informix.pm @@ -66,11 +66,12 @@ sub with_deferred_fk_checks { $self->_do_query('SET CONSTRAINTS ALL DEFERRED'); - my $sg = Scope::Guard->new(sub { - $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE'); - }); - - return preserve_context { $sub->() } after => sub { $txn_scope_guard->commit }; + return preserve_context { + my $sg = Scope::Guard->new(sub { + $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE'); + }); + $sub->() + } after => sub { $txn_scope_guard->commit }; } =head2 connect_call_datetime_setup diff --git a/lib/DBIx/Class/Storage/DBI/Pg.pm b/lib/DBIx/Class/Storage/DBI/Pg.pm index 7c20330..056b9cc 100644 --- a/lib/DBIx/Class/Storage/DBI/Pg.pm +++ b/lib/DBIx/Class/Storage/DBI/Pg.pm @@ -30,11 +30,12 @@ sub with_deferred_fk_checks { $self->_do_query('SET CONSTRAINTS ALL DEFERRED'); - my $sg = Scope::Guard->new(sub { - $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE'); - }); - - return preserve_context { $sub->() } after => sub { $txn_scope_guard->commit }; + return preserve_context { + my $sg = Scope::Guard->new(sub { + $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE'); + }); + $sub->() + } after => sub { $txn_scope_guard->commit }; } # only used when INSERT ... RETURNING is disabled diff --git a/t/72pg.t b/t/72pg.t index 623a5a1..67906a5 100644 --- a/t/72pg.t +++ b/t/72pg.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use Test::Warn; use Test::Exception; use Sub::Name; use Config; @@ -437,7 +438,7 @@ lives_ok { $cds->update({ year => '2010' }) } 'Update on prefetched rs'; $schema->source('CD')->name('dbic_t_schema.cd'); $schema->source('Track')->name('dbic_t_schema.track'); - lives_ok { + lives_and { warning_is { $schema->storage->with_deferred_fk_checks(sub { $schema->resultset('Track')->create({ trackid => 999, cd => 999, position => 1, title => 'deferred FK track' @@ -446,7 +447,7 @@ lives_ok { $cds->update({ year => '2010' }) } 'Update on prefetched rs'; artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd' }); }); - } 'with_deferred_fk_checks code survived'; + } undef } 'with_deferred_fk_checks code survived'; is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track', 'code in with_deferred_fk_checks worked';