diff --git a/bin/pt-duplicate-key-checker b/bin/pt-duplicate-key-checker index 6c4e55a7..e952a8b7 100755 --- a/bin/pt-duplicate-key-checker +++ b/bin/pt-duplicate-key-checker @@ -2664,7 +2664,7 @@ sub remove_prefix_duplicates { } else { PTDEBUG && _d($right_name, 'not left-prefix of', $left_name); - next LEFT_KEY; + next RIGHT_KEY; } } # RIGHT_KEY } # LEFT_KEY diff --git a/lib/DuplicateKeyFinder.pm b/lib/DuplicateKeyFinder.pm index 3cc6b102..e43f9a43 100644 --- a/lib/DuplicateKeyFinder.pm +++ b/lib/DuplicateKeyFinder.pm @@ -359,7 +359,7 @@ sub remove_prefix_duplicates { } else { PTDEBUG && _d($right_name, 'not left-prefix of', $left_name); - next LEFT_KEY; + next RIGHT_KEY; } } # RIGHT_KEY } # LEFT_KEY diff --git a/t/lib/DuplicateKeyFinder.t b/t/lib/DuplicateKeyFinder.t index 4fee786f..a59f8f79 100644 --- a/t/lib/DuplicateKeyFinder.t +++ b/t/lib/DuplicateKeyFinder.t @@ -9,7 +9,7 @@ BEGIN { use strict; use warnings FATAL => 'all'; use English qw(-no_match_vars); -use Test::More tests => 37; +use Test::More tests => 38; use DuplicateKeyFinder; use Quoter; @@ -729,6 +729,48 @@ is_deeply( 'Issue 1192' ); + +# ############################################################################# +# https://bugs.launchpad.net/percona-toolkit/+bug/894140 +# ############################################################################# +$ddl = load_file('t/lib/samples/dupekeys/dupe-cluster-bug-894140.sql'); +$dupes = []; +($keys, $ck) = $tp->get_keys($ddl, $opt); +$dk->get_duplicate_keys( + $keys, + clustered_key => $ck, + clustered => 1, + callback => $callback, + tbl_info => { engine => 'InnoDB', ddl => $ddl }, +); + +is_deeply( + $dupes, + [ + { + cols => [ 'row_id' ], + ddl => 'UNIQUE KEY `row_id` (`row_id`),', + dupe_type => 'exact', + duplicate_of => 'PRIMARY', + duplicate_of_cols => [ 'row_id' ], + duplicate_of_ddl => 'PRIMARY KEY (`row_id`),', + key => 'row_id', + reason => 'row_id is a duplicate of PRIMARY', + }, + { + cols => [ 'player_id' ], + ddl => 'KEY `player_id_2` (`player_id`)', + dupe_type => 'exact', + duplicate_of => 'player_id', + duplicate_of_cols => [ 'player_id' ], + duplicate_of_ddl => 'UNIQUE KEY `player_id` (`player_id`),', + key => 'player_id_2', + reason => 'player_id_2 is a duplicate of player_id', + }, + ], + 'Finds duplicates OK on uppercase columns', +); + # ############################################################################# # Done. # ############################################################################# diff --git a/t/lib/samples/dupekeys/dupe-cluster-bug-894140.sql b/t/lib/samples/dupekeys/dupe-cluster-bug-894140.sql new file mode 100644 index 00000000..6150b8bd --- /dev/null +++ b/t/lib/samples/dupekeys/dupe-cluster-bug-894140.sql @@ -0,0 +1,8 @@ +CREATE TABLE `bug_894140` ( + `row_id` bigint(20) NOT NULL AUTO_INCREMENT, + `player_id` bigint(20) DEFAULT NULL, + PRIMARY KEY (`row_id`), + UNIQUE KEY `row_id` (`row_id`), + UNIQUE KEY `player_id` (`player_id`), + KEY `player_id_2` (`player_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 diff --git a/t/pt-duplicate-key-checker/basics.t b/t/pt-duplicate-key-checker/basics.t index 26f4d062..33382719 100644 --- a/t/pt-duplicate-key-checker/basics.t +++ b/t/pt-duplicate-key-checker/basics.t @@ -23,7 +23,7 @@ if ( !$dbh ) { plan skip_all => 'Cannot connect to sandbox master'; } else { - plan tests => 8; + plan tests => 9; } my $output; @@ -95,6 +95,17 @@ ok( 'No results for nonexistent db' ); +$dbh->do('create database test'); +$sb->load_file('master', 't/lib/samples/dupekeys/dupe-cluster-bug-894140.sql', 'test'); + +ok( + no_diff( + sub { pt_duplicate_key_checker::main(@args, qw(-d test)) }, + "$sample/bug-894140.txt", + ), + "Bug 894140" +); + # ############################################################################# # Done. # ############################################################################# diff --git a/t/pt-duplicate-key-checker/samples/bug-894140.txt b/t/pt-duplicate-key-checker/samples/bug-894140.txt new file mode 100644 index 00000000..6becfff4 --- /dev/null +++ b/t/pt-duplicate-key-checker/samples/bug-894140.txt @@ -0,0 +1,29 @@ +# ######################################################################## +# test.bug_894140 +# ######################################################################## + +# row_id is a duplicate of PRIMARY +# Key definitions: +# UNIQUE KEY `row_id` (`row_id`), +# PRIMARY KEY (`row_id`), +# Column types: +# `row_id` bigint(20) not null auto_increment +# To remove this duplicate index, execute: +ALTER TABLE `test`.`bug_894140` DROP INDEX `row_id`; + +# player_id_2 is a duplicate of player_id +# Key definitions: +# KEY `player_id_2` (`player_id`) +# UNIQUE KEY `player_id` (`player_id`), +# Column types: +# `player_id` bigint(20) default null +# To remove this duplicate index, execute: +ALTER TABLE `test`.`bug_894140` DROP INDEX `player_id_2`; + +# ######################################################################## +# Summary of indexes +# ######################################################################## + +# Size Duplicate Indexes 17 +# Total Duplicate Indexes 2 +# Total Indexes 4