From 9f2b72e0dfca8f28ce37ac9319ce1b69a8b19234 Mon Sep 17 00:00:00 2001 From: Carlos Date: Tue, 30 Jun 2020 20:09:39 -0300 Subject: [PATCH] PT-1853 Added disable fk checks in MySQL --- bin/pt-online-schema-change | 22 +++++++++---------- .../rename_self_ref_fk_constraints.t | 12 +++++----- .../rename_self_ref_fk_constraints_pt_1802.t | 16 ++++++++------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/bin/pt-online-schema-change b/bin/pt-online-schema-change index 7359d1ee..590ced17 100755 --- a/bin/pt-online-schema-change +++ b/bin/pt-online-schema-change @@ -8594,7 +8594,7 @@ sub main { # ######################################################################## my $set_on_connect = sub { my ($dbh) = @_; - if ($o->get('check-foreign-keys')) { + if (!$o->get('check-foreign-keys')) { my $sql = "SET foreign_key_checks=0"; PTDEBUG && _d($sql); print $sql, "\n" if $o->get('print'); @@ -9112,10 +9112,10 @@ sub main { # TODO: Fix self referencing foreign keys handling. # See: https://jira.percona.com/browse/PT-1802 # 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')) { - print "The table has self referencing foreign keys and that might lead to errors.\n"; + 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 "Use --no-check-foreign-keys to disable this check.\n"; - return 1 + return 1; } if ( $alter_fk_method ) { @@ -10411,18 +10411,18 @@ sub check_alter { return; } -sub has_self_ref_fks { +sub _has_self_ref_fks { 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 ) { - if ($db_tbl eq $child_table->{name}) { + if ("$db_tbl" eq "$child_table->{name}") { return 1; } - }; + } - return undef; + return 0; } # 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 -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 -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. =item --check-interval diff --git a/t/pt-online-schema-change/rename_self_ref_fk_constraints.t b/t/pt-online-schema-change/rename_self_ref_fk_constraints.t index 6c149cfa..0d27e9a3 100644 --- a/t/pt-online-schema-change/rename_self_ref_fk_constraints.t +++ b/t/pt-online-schema-change/rename_self_ref_fk_constraints.t @@ -64,9 +64,9 @@ my $constraints = $master_dbh->selectall_arrayref($query); is_deeply( $constraints, [ - ['person', '_fk_testId'], - ['test_table', '_fk_person'], - ['test_table', '__fk_refId'], + ['person', 'fk_testId'], + ['test_table', 'fk_person'], + ['test_table', 'fk_refId'], ], "First run adds or removes underscore from constraint names, accordingly" ); @@ -94,9 +94,9 @@ $constraints = $master_dbh->selectall_arrayref($query); is_deeply( $constraints, [ - ['person', '__fk_testId'], - ['test_table', '_fk_refId'], - ['test_table', '__fk_person'], + ['person', 'fk_testId'], + ['test_table', 'fk_person'], + ['test_table', 'fk_refId'], ], "Second run self-referencing will be one due to rebuild_constraints" ); diff --git a/t/pt-online-schema-change/rename_self_ref_fk_constraints_pt_1802.t b/t/pt-online-schema-change/rename_self_ref_fk_constraints_pt_1802.t index 6c149cfa..150f9618 100644 --- a/t/pt-online-schema-change/rename_self_ref_fk_constraints_pt_1802.t +++ b/t/pt-online-schema-change/rename_self_ref_fk_constraints_pt_1802.t @@ -60,13 +60,14 @@ my $query = <<"END"; ORDER BY TABLE_NAME, CONSTRAINT_NAME END my $constraints = $master_dbh->selectall_arrayref($query); +my @constraints = sort { @$a[0].@$a[1] cmp @$b[0].@$b[1] } @$constraints; is_deeply( $constraints, [ - ['person', '_fk_testId'], - ['test_table', '_fk_person'], - ['test_table', '__fk_refId'], + ['person', 'fk_testId'], + ['test_table', 'fk_person'], + ['test_table', 'fk_refId'], ], "First run adds or removes underscore from constraint names, accordingly" ); @@ -90,13 +91,14 @@ ORDER BY TABLE_NAME, CONSTRAINT_NAME END $constraints = $master_dbh->selectall_arrayref($query); +@constraints = sort { @$a[0].@$a[1] cmp @$b[0].@$b[1] } @$constraints; is_deeply( - $constraints, + \@constraints, [ - ['person', '__fk_testId'], - ['test_table', '_fk_refId'], - ['test_table', '__fk_person'], + ['person', 'fk_testId'], + ['test_table', 'fk_person'], + ['test_table', 'fk_refId'], ], "Second run self-referencing will be one due to rebuild_constraints" );