Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/pt-online-schema-change
Original file line number Diff line number Diff line change
Expand Up @@ -11211,7 +11211,7 @@ sub get_unique_index_fields {
$clean .= $suffix;

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

while($clean =~ /$fields_re/g) {
push @$fields, [ split /\s*,\s*/, $1 ];
Expand Down
47 changes: 44 additions & 3 deletions t/pt-online-schema-change/pt-153.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use Sandbox;
use SqlModes;
use File::Temp qw/ tempdir /;

plan tests => 6;
plan tests => 10;

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

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

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

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

# UNIQUE is possible without INDEX or KEY, we need to check this as well.
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args, "$source_dsn,D=test,t=t1",
'--execute',
'--alter', "ADD UNIQUE c1 (f2, f3)",
),
},
);

isnt(
$exit_status,
0,
"PT-153 Adding unique index without index/key keyword exit status != 0.",
);

like(
$output,
qr/You are trying to add an unique key. This can result in data loss if the data is not unique/s,
"PT-153 Adding unique index without index/key keyword warning message.",
);

($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args, "$source_dsn,D=test,t=t1",
'--execute',
'--alter', "ADD UNIQUE(f2, f3)",
),
},
);

isnt(
$exit_status,
0,
"PT-153 Adding unique index without index/key keyword and index name exit status != 0.",
);

like(
$output,
qr/You are trying to add an unique key. This can result in data loss if the data is not unique/s,
"PT-153 Adding unique index without index/key keyword and index name warning message.",
);

$source_dbh->do("DROP DATABASE IF EXISTS test");

# #############################################################################
Expand Down