Files
percona-toolkit/t/pt-online-schema-change/pt-153.t
Sveta Smirnova 20b3196cda PT-1151 - LP #1225577: pt-online-schema-change can silently drop rows
- Found case when --check-unique-index-change does not catch error:
  UNIQUE without KEY or INDEX keyword and fixed regular expression
2025-09-08 02:26:30 +03:00

140 lines
3.8 KiB
Perl

#!/usr/bin/env perl
BEGIN {
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};
use strict;
use warnings FATAL => 'all';
use threads;
use English qw(-no_match_vars);
use Test::More;
use Data::Dumper;
use PerconaTest;
use Sandbox;
use SqlModes;
use File::Temp qw/ tempdir /;
plan tests => 10;
require "$trunk/bin/pt-online-schema-change";
my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $source_dbh = $sb->get_dbh_for('source');
my $source_dsn = 'h=127.1,P=12345,u=msandbox,p=msandbox';
if ( !$source_dbh ) {
plan skip_all => 'Cannot connect to sandbox source';
}
# The sandbox servers run with lock_wait_timeout=3 and it's not dynamic
# so we need to specify --set-vars innodb_lock_wait_timeout=3 else the
# tool will die.
my @args = (qw(--set-vars innodb_lock_wait_timeout=3));
my $output;
my $exit_status;
my $sample = "t/pt-online-schema-change/samples/";
$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',
'--alter', "ADD UNIQUE INDEX c1 (f2, f3)",
),
},
);
isnt(
$exit_status,
0,
"PT-153 Adding unique index 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 warning message.",
);
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args, "$source_dsn,D=test,t=t1",
'--execute',
'--alter', "ADD UNIQUE INDEX c1 (f2, f3), PRIMARY KEY (f3), UNIQUE KEY k2 (f3)",
),
},
);
isnt(
$exit_status,
0,
"PT-153 Adding multiple unique indexes 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 multiple unique indexes warning message.",
);
like(
$output,
qr/SELECT IF\(COUNT\(DISTINCT f2, f3\).*?SELECT IF\(COUNT\(DISTINCT f3\)/s,
"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");
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($source_dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;