PT-1853 Added disable fk checks in MySQL

This commit is contained in:
Carlos
2020-06-30 20:09:39 -03:00
parent 2e62d07ba0
commit 9f2b72e0df
3 changed files with 26 additions and 24 deletions

View File

@@ -8594,7 +8594,7 @@ sub main {
# ######################################################################## # ########################################################################
my $set_on_connect = sub { my $set_on_connect = sub {
my ($dbh) = @_; my ($dbh) = @_;
if ($o->get('check-foreign-keys')) { if (!$o->get('check-foreign-keys')) {
my $sql = "SET foreign_key_checks=0"; my $sql = "SET foreign_key_checks=0";
PTDEBUG && _d($sql); PTDEBUG && _d($sql);
print $sql, "\n" if $o->get('print'); print $sql, "\n" if $o->get('print');
@@ -9112,10 +9112,10 @@ sub main {
# TODO: Fix self referencing foreign keys handling. # TODO: Fix self referencing foreign keys handling.
# See: https://jira.percona.com/browse/PT-1802 # See: https://jira.percona.com/browse/PT-1802
# https://jira.percona.com/browse/PT-1853 # https://jira.percona.com/browse/PT-1853
if (has_self_ref_fks($orig_tbl->{db}, $orig_tbl->{tbl}, $child_tables) and $o->get('check-foreign-keys')) { if (_has_self_ref_fks($orig_tbl->{db}, $orig_tbl->{tbl}, $child_tables) && $o->get('check-foreign-keys')) {
print "The table has self referencing foreign keys and that might lead to errors.\n"; print "The table has self-referencing foreign keys and that might lead to errors.\n";
print "Use --no-check-foreign-keys to disable this check.\n"; print "Use --no-check-foreign-keys to disable this check.\n";
return 1 return 1;
} }
if ( $alter_fk_method ) { if ( $alter_fk_method ) {
@@ -10411,18 +10411,18 @@ sub check_alter {
return; return;
} }
sub has_self_ref_fks { sub _has_self_ref_fks {
my ($orig_db, $orig_table, $child_tables) = @_; my ($orig_db, $orig_table, $child_tables) = @_;
my $db_tbl = sprintf("`%s`.`%s`", $orig_db, $orig_table); my $db_tbl = sprintf('`%s`.`%s`', $orig_db, $orig_table);
foreach my $child_table ( @$child_tables ) { foreach my $child_table ( @$child_tables ) {
if ($db_tbl eq $child_table->{name}) { if ("$db_tbl" eq "$child_table->{name}") {
return 1; return 1;
} }
}; }
return undef; return 0;
} }
# This function tries to detect if the --alter param is adding unique indexes. # This function tries to detect if the --alter param is adding unique indexes.
@@ -12201,9 +12201,9 @@ L<"--print"> and verify that the triggers are correct.
default: yes default: yes
Check for self referencing foreing keys. Currently self referencing FKs are Check for self-referencing foreing keys. Currently self referencing FKs are
not fully supported so to prevent errors, this program wont't run if the table not fully supported so to prevent errors, this program wont't run if the table
has self referen foreign keys. Use this parameter to disable self referencing has self-referencing foreign keys. Use this parameter to disable self-referencing
FK checks. FK checks.
=item --check-interval =item --check-interval

View File

@@ -64,9 +64,9 @@ my $constraints = $master_dbh->selectall_arrayref($query);
is_deeply( is_deeply(
$constraints, $constraints,
[ [
['person', '_fk_testId'], ['person', 'fk_testId'],
['test_table', '_fk_person'], ['test_table', 'fk_person'],
['test_table', '__fk_refId'], ['test_table', 'fk_refId'],
], ],
"First run adds or removes underscore from constraint names, accordingly" "First run adds or removes underscore from constraint names, accordingly"
); );
@@ -94,9 +94,9 @@ $constraints = $master_dbh->selectall_arrayref($query);
is_deeply( is_deeply(
$constraints, $constraints,
[ [
['person', '__fk_testId'], ['person', 'fk_testId'],
['test_table', '_fk_refId'], ['test_table', 'fk_person'],
['test_table', '__fk_person'], ['test_table', 'fk_refId'],
], ],
"Second run self-referencing will be one due to rebuild_constraints" "Second run self-referencing will be one due to rebuild_constraints"
); );

View File

@@ -60,13 +60,14 @@ my $query = <<"END";
ORDER BY TABLE_NAME, CONSTRAINT_NAME ORDER BY TABLE_NAME, CONSTRAINT_NAME
END END
my $constraints = $master_dbh->selectall_arrayref($query); my $constraints = $master_dbh->selectall_arrayref($query);
my @constraints = sort { @$a[0].@$a[1] cmp @$b[0].@$b[1] } @$constraints;
is_deeply( is_deeply(
$constraints, $constraints,
[ [
['person', '_fk_testId'], ['person', 'fk_testId'],
['test_table', '_fk_person'], ['test_table', 'fk_person'],
['test_table', '__fk_refId'], ['test_table', 'fk_refId'],
], ],
"First run adds or removes underscore from constraint names, accordingly" "First run adds or removes underscore from constraint names, accordingly"
); );
@@ -90,13 +91,14 @@ ORDER BY TABLE_NAME, CONSTRAINT_NAME
END END
$constraints = $master_dbh->selectall_arrayref($query); $constraints = $master_dbh->selectall_arrayref($query);
@constraints = sort { @$a[0].@$a[1] cmp @$b[0].@$b[1] } @$constraints;
is_deeply( is_deeply(
$constraints, \@constraints,
[ [
['person', '__fk_testId'], ['person', 'fk_testId'],
['test_table', '_fk_refId'], ['test_table', 'fk_person'],
['test_table', '__fk_person'], ['test_table', 'fk_refId'],
], ],
"Second run self-referencing will be one due to rebuild_constraints" "Second run self-referencing will be one due to rebuild_constraints"
); );