From c0ea3179c3ed23b6aad357e4cf24e73fbdd5494c Mon Sep 17 00:00:00 2001 From: frank-cizmich Date: Wed, 23 Sep 2015 19:21:42 -0300 Subject: [PATCH] pt-osc fixed fk underscore toggling lp1498128 --- bin/pt-online-schema-change | 7 +-- .../rename_fk_constraints.t | 46 +++++++++---------- .../samples/bug-1215587.sql | 9 +++- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/bin/pt-online-schema-change b/bin/pt-online-schema-change index ef209d9f..de40257e 100755 --- a/bin/pt-online-schema-change +++ b/bin/pt-online-schema-change @@ -9880,11 +9880,8 @@ sub create_new_table { # This is in contrast to previous behavior were we added underscores # indefinitely, sometimes exceeding the allowed name limit # https://bugs.launchpad.net/percona-toolkit/+bug/1215587 - if ( $sql =~ /CONSTRAINT `_/ ) { - $sql =~ s/^ CONSTRAINT `_/ CONSTRAINT `/gm; - } else { - $sql =~ s/^ CONSTRAINT `/ CONSTRAINT `_/gm; - } + $sql =~ s/^ CONSTRAINT `(_?)/' CONSTRAINT `'.($1 eq '' ? '_' : '')/gme; + if ( $o->get('default-engine') ) { $sql =~ s/\s+ENGINE=\S+//; } diff --git a/t/pt-online-schema-change/rename_fk_constraints.t b/t/pt-online-schema-change/rename_fk_constraints.t index cb1aabfe..1c8b13f5 100644 --- a/t/pt-online-schema-change/rename_fk_constraints.t +++ b/t/pt-online-schema-change/rename_fk_constraints.t @@ -42,8 +42,7 @@ my $sample = "t/pt-online-schema-change/samples/"; $sb->load_file('master', "$sample/bug-1215587.sql"); # run once: we expect constraint names to be prefixed with one underscore -# note: We're running just a neutral no-op alter. We are only interested in constraint name -# changes. +# if they havre't one, and to remove one if they already do. ($output, $exit_status) = full_output( sub { pt_online_schema_change::main(@args, "$master_dsn,D=bug1215587,t=Table1", @@ -52,24 +51,24 @@ $sb->load_file('master', "$sample/bug-1215587.sql"); ); -my $constraints = $master_dbh->selectall_hashref("SELECT CONSTRAINT_NAME, TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE table_schema='bug1215587' and (TABLE_NAME='Table1' OR TABLE_NAME='Table2') and CONSTRAINT_NAME LIKE '%fkey%'", 'table_name'); +my $constraints = $master_dbh->selectall_arrayref("SELECT TABLE_NAME, CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE table_schema='bug1215587' and (TABLE_NAME='Table1' OR TABLE_NAME='Table2') and CONSTRAINT_NAME LIKE '%fkey%' ORDER BY TABLE_NAME, CONSTRAINT_NAME"); -is( - $constraints->{Table1}->{constraint_name}, - '_fkey1', - "Altered table: constraint name prefixed one underscore after 1st run" +is_deeply( + $constraints, + [ + ['Table1', 'fkey1a'], + ['Table1', '_fkey1b'], + ['Table2', 'fkey2b'], + ['Table2', '_fkey2a'], + ], + "First run adds or removes underscore from constraint names, accordingly" ); -is( - $constraints->{Table2}->{constraint_name}, - '_fkey2', - "Child table : constraint name prefixed one underscore after 1st run" -); # run second time -# we expect underscores to be removed +# we expect constraints to be the same as we started (toggled back) ($output, $exit_status) = full_output( sub { pt_online_schema_change::main(@args, "$master_dsn,D=bug1215587,t=Table1", @@ -77,19 +76,18 @@ is( qw(--execute)) }, ); -$constraints = $master_dbh->selectall_hashref("SELECT CONSTRAINT_NAME, TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE table_schema='bug1215587' and (TABLE_NAME='Table1' OR TABLE_NAME='Table2') and CONSTRAINT_NAME LIKE '%fkey%'", 'table_name'); +$constraints = $master_dbh->selectall_arrayref("SELECT TABLE_NAME, CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE table_schema='bug1215587' and (TABLE_NAME='Table1' OR TABLE_NAME='Table2') and CONSTRAINT_NAME LIKE '%fkey%' ORDER BY TABLE_NAME, CONSTRAINT_NAME"); -is( - $constraints->{'Table1'}->{constraint_name}, - 'fkey1', - "Altered table: constraint name removed underscore after 2nd run" -); - -is( - $constraints->{'Table2'}->{constraint_name}, - 'fkey2', - "Child table : constraint name removed underscore after 2nd run" +is_deeply( + $constraints, + [ + ['Table1', 'fkey1b'], + ['Table1', '_fkey1a'], + ['Table2', 'fkey2a'], + ['Table2', '_fkey2b'], + ], + "Second run toggles constraint names back to how they were" ); diff --git a/t/pt-online-schema-change/samples/bug-1215587.sql b/t/pt-online-schema-change/samples/bug-1215587.sql index 1e6a5152..3d4b067f 100644 --- a/t/pt-online-schema-change/samples/bug-1215587.sql +++ b/t/pt-online-schema-change/samples/bug-1215587.sql @@ -16,8 +16,13 @@ CREATE TABLE `Table2` ( ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `Table1` - ADD CONSTRAINT `fkey1` FOREIGN KEY (`T2ID`) REFERENCES `Table2` (`ID`) ON DELETE NO ACTION; + ADD CONSTRAINT `_fkey1a` FOREIGN KEY (`T2ID`) REFERENCES `Table2` (`ID`) ON DELETE NO ACTION; + +ALTER TABLE `Table1` + ADD CONSTRAINT `fkey1b` FOREIGN KEY (`T2ID`) REFERENCES `Table2` (`ID`) ON DELETE NO ACTION; ALTER TABLE `Table2` - ADD CONSTRAINT `fkey2` FOREIGN KEY (`ID`) REFERENCES `Table1` (`T2ID`) ON DELETE NO ACTION; + ADD CONSTRAINT `fkey2a` FOREIGN KEY (`ID`) REFERENCES `Table1` (`T2ID`) ON DELETE NO ACTION; +ALTER TABLE `Table2` + ADD CONSTRAINT `_fkey2b` FOREIGN KEY (`ID`) REFERENCES `Table1` (`T2ID`) ON DELETE NO ACTION;