Skip to content

Commit 2dbdb86

Browse files
Merge pull request #1006 from percona/PT-1151_LP_1225577_pt-online-schema-change_can_silently_drop_rows
PT-1151 - LP #1225577: pt-online-schema-change can silently drop rows
2 parents cd6183d + 20b3196 commit 2dbdb86

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

bin/pt-online-schema-change

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11211,7 +11211,7 @@ sub get_unique_index_fields {
1121111211
$clean .= $suffix;
1121211212

1121311213
my $fields = [];
11214-
my $fields_re = qr/\s(?:PRIMARY|UNIQUE)\s+(?:INDEX|KEY|)\s*(?:.*?)\s*\((.*?)\)/i;
11214+
my $fields_re = qr/\s(?:(?:(?:PRIMARY|UNIQUE)\s+(?:INDEX|KEY|))|UNIQUE)\s*(?:.*?)\s*\((.*?)\)/i;
1121511215

1121611216
while($clean =~ /$fields_re/g) {
1121711217
push @$fields, [ split /\s*,\s*/, $1 ];

t/pt-online-schema-change/pt-153.t

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use Sandbox;
1919
use SqlModes;
2020
use File::Temp qw/ tempdir /;
2121

22-
plan tests => 6;
22+
plan tests => 10;
2323

2424
require "$trunk/bin/pt-online-schema-change";
2525

@@ -44,7 +44,7 @@ $sb->load_file('source', "$sample/pt-153.sql");
4444

4545
($output, $exit_status) = full_output(
4646
sub { pt_online_schema_change::main(@args, "$source_dsn,D=test,t=t1",
47-
'--execute',
47+
'--execute',
4848
'--alter', "ADD UNIQUE INDEX c1 (f2, f3)",
4949
),
5050
},
@@ -64,7 +64,7 @@ like(
6464

6565
($output, $exit_status) = full_output(
6666
sub { pt_online_schema_change::main(@args, "$source_dsn,D=test,t=t1",
67-
'--execute',
67+
'--execute',
6868
'--alter', "ADD UNIQUE INDEX c1 (f2, f3), PRIMARY KEY (f3), UNIQUE KEY k2 (f3)",
6969
),
7070
},
@@ -88,6 +88,47 @@ like(
8888
"PT-153 Adding multiple unique indexes -> multime example queries.",
8989
);
9090

91+
# UNIQUE is possible without INDEX or KEY, we need to check this as well.
92+
($output, $exit_status) = full_output(
93+
sub { pt_online_schema_change::main(@args, "$source_dsn,D=test,t=t1",
94+
'--execute',
95+
'--alter', "ADD UNIQUE c1 (f2, f3)",
96+
),
97+
},
98+
);
99+
100+
isnt(
101+
$exit_status,
102+
0,
103+
"PT-153 Adding unique index without index/key keyword exit status != 0.",
104+
);
105+
106+
like(
107+
$output,
108+
qr/You are trying to add an unique key. This can result in data loss if the data is not unique/s,
109+
"PT-153 Adding unique index without index/key keyword warning message.",
110+
);
111+
112+
($output, $exit_status) = full_output(
113+
sub { pt_online_schema_change::main(@args, "$source_dsn,D=test,t=t1",
114+
'--execute',
115+
'--alter', "ADD UNIQUE(f2, f3)",
116+
),
117+
},
118+
);
119+
120+
isnt(
121+
$exit_status,
122+
0,
123+
"PT-153 Adding unique index without index/key keyword and index name exit status != 0.",
124+
);
125+
126+
like(
127+
$output,
128+
qr/You are trying to add an unique key. This can result in data loss if the data is not unique/s,
129+
"PT-153 Adding unique index without index/key keyword and index name warning message.",
130+
);
131+
91132
$source_dbh->do("DROP DATABASE IF EXISTS test");
92133

93134
# #############################################################################

0 commit comments

Comments
 (0)