mirror of
https://github.com/percona/percona-toolkit.git
synced 2026-04-17 01:01:39 +08:00
Add 'DROP PRIMARY KEY' to --check-alter.
This commit is contained in:
@@ -8986,6 +8986,28 @@ sub check_alter {
|
|||||||
|
|
||||||
my $ok = 1;
|
my $ok = 1;
|
||||||
|
|
||||||
|
# ########################################################################
|
||||||
|
# Check for DROP PRIMARY KEY.
|
||||||
|
# ########################################################################
|
||||||
|
if ( $alter =~ m/DROP\s+PRIMARY\s+KEY/i ) {
|
||||||
|
my $msg = "--alter contains 'DROP PRIMARY KEY'. Dropping and "
|
||||||
|
. "altering the primary key can be very dangerous, "
|
||||||
|
. "especially if the original table does not have other "
|
||||||
|
. "unique indexes.\n";
|
||||||
|
if ( $dry_run ) {
|
||||||
|
print $msg;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$ok = 0;
|
||||||
|
warn $msg
|
||||||
|
. "The tool should handle this correctly, but you should "
|
||||||
|
. "test it first and carefully examine the triggers which "
|
||||||
|
. "rely on the PRIMARY KEY or a unique index. Specify "
|
||||||
|
. "--no-check-alter to disable this check and perform the "
|
||||||
|
. "--alter.\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# ########################################################################
|
# ########################################################################
|
||||||
# Check for renamed columns.
|
# Check for renamed columns.
|
||||||
# https://bugs.launchpad.net/percona-toolkit/+bug/1068562
|
# https://bugs.launchpad.net/percona-toolkit/+bug/1068562
|
||||||
@@ -9032,6 +9054,7 @@ sub check_alter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( !$ok ) {
|
if ( !$ok ) {
|
||||||
|
# check_alter.t relies on this output.
|
||||||
die "--check-alter failed.\n";
|
die "--check-alter failed.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10286,6 +10309,15 @@ code that does this is not a full-blown SQL parser, so we recommend that users
|
|||||||
run the tool with L<"--dry-run"> and check if it's detecting the renames
|
run the tool with L<"--dry-run"> and check if it's detecting the renames
|
||||||
correctly.
|
correctly.
|
||||||
|
|
||||||
|
=item DROP PRIMARY KEY
|
||||||
|
|
||||||
|
If L<"--alter"> contain C<DROP PRIMARY KEY> (case- and space-insensitive),
|
||||||
|
a warning is printed and the tool exits unless L<"--dry-run"> is specified.
|
||||||
|
Altering the primary key can be very dangerous, but the tool can handle it.
|
||||||
|
The tool's triggers, particularly the DELETE trigger, are most affected by
|
||||||
|
altering the primary key because the tool prefers to use the primary key
|
||||||
|
for its triggers.
|
||||||
|
|
||||||
=back
|
=back
|
||||||
|
|
||||||
=item --[no]check-plan
|
=item --[no]check-plan
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ $sb->load_file('master', "$sample/del-trg-bug-1062324.sql");
|
|||||||
sub { pt_online_schema_change::main(@args,
|
sub { pt_online_schema_change::main(@args,
|
||||||
"$master_dsn,D=test,t=t1",
|
"$master_dsn,D=test,t=t1",
|
||||||
"--alter", "drop key 2bpk, drop key c3, drop primary key, drop c1, add primary key (c2, c3(4)), add key (c3(4))",
|
"--alter", "drop key 2bpk, drop key c3, drop primary key, drop c1, add primary key (c2, c3(4)), add key (c3(4))",
|
||||||
qw(--execute --no-drop-new-table --no-swap-tables)) },
|
qw(--no-check-alter --execute --no-drop-new-table --no-swap-tables)) },
|
||||||
);
|
);
|
||||||
|
|
||||||
# Since _t1_new no longer has the c1 column, the bug caused this
|
# Since _t1_new no longer has the c1 column, the bug caused this
|
||||||
@@ -242,7 +242,7 @@ $sb->load_file('master', "$sample/del-trg-bug-1062324.sql");
|
|||||||
sub { pt_online_schema_change::main(@args,
|
sub { pt_online_schema_change::main(@args,
|
||||||
"$master_dsn,D=test,t=t1",
|
"$master_dsn,D=test,t=t1",
|
||||||
"--alter", "drop primary key, add column _id int unsigned not null primary key auto_increment FIRST",
|
"--alter", "drop primary key, add column _id int unsigned not null primary key auto_increment FIRST",
|
||||||
qw(--execute --no-drop-new-table --no-swap-tables)) },
|
qw(--no-check-alter --execute --no-drop-new-table --no-swap-tables)) },
|
||||||
);
|
);
|
||||||
|
|
||||||
eval {
|
eval {
|
||||||
|
|||||||
65
t/pt-online-schema-change/check_alter.t
Normal file
65
t/pt-online-schema-change/check_alter.t
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#!/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 English qw(-no_match_vars);
|
||||||
|
use Test::More;
|
||||||
|
|
||||||
|
use Data::Dumper;
|
||||||
|
use PerconaTest;
|
||||||
|
use Sandbox;
|
||||||
|
|
||||||
|
require "$trunk/bin/pt-online-schema-change";
|
||||||
|
|
||||||
|
my $dp = new DSNParser(opts=>$dsn_opts);
|
||||||
|
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
|
||||||
|
my $master_dbh = $sb->get_dbh_for('master');
|
||||||
|
my $slave_dbh = $sb->get_dbh_for('slave1');
|
||||||
|
|
||||||
|
if ( !$master_dbh ) {
|
||||||
|
plan skip_all => 'Cannot connect to sandbox master';
|
||||||
|
}
|
||||||
|
elsif ( !$slave_dbh ) {
|
||||||
|
plan skip_all => 'Cannot connect to sandbox slave1';
|
||||||
|
}
|
||||||
|
|
||||||
|
# The sandbox servers run with lock_wait_timeout=3 and it's not dynamic
|
||||||
|
# so we need to specify --lock-wait-timeout=3 else the tool will die.
|
||||||
|
my $master_dsn = 'h=127.1,P=12345,u=msandbox,p=msandbox';
|
||||||
|
my @args = (qw(--lock-wait-timeout 3));
|
||||||
|
my $output;
|
||||||
|
my $exit_status;
|
||||||
|
my $sample = "t/pt-online-schema-change/samples/";
|
||||||
|
|
||||||
|
# #############################################################################
|
||||||
|
# DROP PRIMARY KEY
|
||||||
|
# #############################################################################
|
||||||
|
|
||||||
|
$sb->load_file('master', "$sample/del-trg-bug-1103672.sql");
|
||||||
|
|
||||||
|
($output, $exit_status) = full_output(
|
||||||
|
sub { pt_online_schema_change::main(@args,
|
||||||
|
"$master_dsn,D=test,t=t1",
|
||||||
|
"--alter", "drop primary key, add column _id int unsigned not null primary key auto_increment FIRST",
|
||||||
|
qw(--execute)),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
like(
|
||||||
|
$output,
|
||||||
|
qr/--check-alter failed/,
|
||||||
|
"DROP PRIMARY KEY"
|
||||||
|
);
|
||||||
|
|
||||||
|
# #############################################################################
|
||||||
|
# Done.
|
||||||
|
# #############################################################################
|
||||||
|
$sb->wipe_clean($master_dbh);
|
||||||
|
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
|
||||||
|
done_testing;
|
||||||
Reference in New Issue
Block a user