Skip to content

Deferred fk checks #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: maint/0.0828xx
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
abraxxa: Alexander Hartmaier <abraxxa@cpan.org>
acca: Alexander Kuznetsov <acca@cpan.org>
acme: Leon Brocard <acme@astray.com>
agregory: Andrew Gregory <andrew.gregory.8@gmail.com>
aherzog: Adam Herzog <adam@herzogdesigns.com>
Alexander Keusch <cpan@keusch.at>
alexrj: Alessandro Ranellucci <aar@cpan.org>
Expand Down
28 changes: 28 additions & 0 deletions lib/DBIx/Class/Storage/DBI/MSSQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use base qw/
/;
use mro 'c3';

use Context::Preserve 'preserve_context';
use Scope::Guard ();
use Try::Tiny;
use namespace::clean;

Expand Down Expand Up @@ -185,6 +187,32 @@ sub _ping {
};
}

sub with_deferred_fk_checks {
my ( $self, $sub ) = @_;

my $txn_scope_guard = $self->txn_scope_guard;

$self->_do_query(
'EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"');

my $sg = Scope::Guard->new(
sub {
$self->_do_query(
'EXEC sp_msforeachtable "ALTER TABLE ? CHECK CONSTRAINT ALL"'
);
} );

return preserve_context { $sub->() } after => sub {
# explicitly check constraints because MSSQL does not check when
# re-enabling them
$self->_do_query(
'EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL"'
);
$txn_scope_guard->commit;
$sg->dismiss;
};
}

package # hide from PAUSE
DBIx::Class::Storage::DBI::MSSQL::DateTime::Format;

Expand Down
11 changes: 11 additions & 0 deletions lib/DBIx/Class/Storage/DBI/SQLite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use warnings;
use base qw/DBIx::Class::Storage::DBI/;
use mro 'c3';

use Context::Preserve 'preserve_context';
use SQL::Abstract::Util 'is_plain_value';
use DBIx::Class::_Util qw(modver_gt_or_eq sigwarn_silencer);
use DBIx::Class::Carp;
Expand Down Expand Up @@ -360,6 +361,16 @@ sub _dbi_attrs_for_bind {
return $bindattrs;
}

sub with_deferred_fk_checks {
my ($self, $sub) = @_;

my $txn_scope_guard = $self->txn_scope_guard;

$self->_do_query('PRAGMA defer_foreign_keys = ON');

return preserve_context { $sub->() } after => sub { $txn_scope_guard->commit };
}

=head2 connect_call_use_foreign_keys

Used as:
Expand Down