use base qw/DBIx::Class::Storage::DBI/;
use mro 'c3';
-use Scope::Guard ();
use Context::Preserve 'preserve_context';
use namespace::clean;
$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 { $sub->() } after => sub {
+ $txn_scope_guard->commit;
+ $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE')
+ if $self->transaction_depth;
+ };
}
=head2 connect_call_datetime_setup
use base qw/DBIx::Class::Storage::DBI/;
use mro 'c3';
use DBIx::Class::Carp;
-use Scope::Guard ();
use Context::Preserve 'preserve_context';
use Try::Tiny;
use List::Util 'first';
$self->_do_query('alter session set constraints = deferred');
- my $sg = Scope::Guard->new(sub {
- $self->_do_query('alter session set constraints = immediate');
- });
-
return
- preserve_context { $sub->() } after => sub { $txn_scope_guard->commit };
+ preserve_context { $sub->() } after => sub {
+ $txn_scope_guard->commit;
+ $self->_do_query('alter session set constraints = immediate')
+ if $self->transaction_depth;
+ };
}
=head1 ATTRIBUTES
use base qw/DBIx::Class::Storage::DBI/;
-use Scope::Guard ();
use Context::Preserve 'preserve_context';
use DBIx::Class::Carp;
use Try::Tiny;
$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 { $sub->() } after => sub {
+ $txn_scope_guard->commit;
+ $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE')
+ if $self->transaction_depth;
+ };
}
# only used when INSERT ... RETURNING is disabled
trackid => 1, cd => 9999, position => 1, title => 'Track1'
});
} qr/constraint/i, 'with_deferred_fk_checks is off';
+
+ throws_ok {
+ $schema->storage->with_deferred_fk_checks(sub {
+ $schema->resultset('Track')->create({
+ trackid => 9999, cd => 9999, position => 1, title => 'orphaned deferred FK track',
+ });
+ });
+ } qr/constraint/i, 'unsatisfied deferred FK throws';
+ ok !$schema->resultset('Track')->find(9999), 'orphaned deferred FK track not inserted';
+
+ throws_ok {
+ $schema->storage->with_deferred_fk_checks(sub {
+ $schema->resultset('CD')->create({
+ artist => 1, cdid => 9999, year => '2003', title => 'dupe PK cd'
+ }) foreach 0..1;
+ });
+ } qr/unique/i, 'unique constraint violation inside deferred block propagated';
+ ok !$schema->resultset('CD')->find(9999), 'duplicate PK track not inserted';
}
done_testing;
});
} qr/constraint/i, 'with_deferred_fk_checks is off';
+ throws_ok {
+ $schema->storage->with_deferred_fk_checks(sub {
+ $schema->resultset('Track')->create({
+ trackid => 9999, cd => 9999, position => 1, title => 'orphaned deferred FK track',
+ });
+ });
+ } qr/constraint/i, 'unsatisfied deferred FK throws';
+ ok !$schema->resultset('Track')->find(9999), 'orphaned deferred FK track not inserted';
+
+ throws_ok {
+ $schema->storage->with_deferred_fk_checks(sub {
+ $schema->resultset('CD')->create({
+ artist => 1, cdid => 9999, year => '2003', title => 'dupe PK cd'
+ }) foreach 0..1;
+ });
+ } qr/unique/i, 'unique constraint violation inside deferred block propagated';
+ ok !$schema->resultset('CD')->find(9999), 'duplicate PK track not inserted';
# test auto increment using sequences WITHOUT triggers
for (1..5) {
});
} qr/constraint/i, 'with_deferred_fk_checks is off';
+throws_ok {
+ $schema->storage->with_deferred_fk_checks(sub {
+ $schema->resultset('Track')->create({
+ trackid => 9999, cd => 9999, position => 1, title => 'orphaned deferred FK track',
+ });
+ });
+} qr/constraint/i, 'unsatisfied deferred FK throws';
+ok !$schema->resultset('Track')->find(9999), 'orphaned deferred FK track not inserted';
+
+throws_ok {
+ $schema->storage->with_deferred_fk_checks(sub {
+ $schema->resultset('CD')->create({
+ artist => 1, cdid => 9999, year => '2003', title => 'dupe PK cd'
+ }) foreach 0..1;
+ });
+} qr/unique/i, 'unique constraint violation inside deferred block propagated';
+ok !$schema->resultset('CD')->find(9999), 'duplicate PK track not inserted';
+
done_testing;
# clean up our mess