1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#!/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 --set-vars innodb_lock_wait_timeout-3 else the
# tool will die.
my $master_dsn = 'h=127.1,P=12345,u=msandbox,p=msandbox';
my @args = (qw(--set-vars innodb_lock_wait_timeout=3));
my $output;
my $exit_status;
my $sample = "t/pt-online-schema-change/samples/";
# ############################################################################
# https://bugs.launchpad.net/percona-toolkit/+bug/1068562
# pt-online-schema-change loses data when renaming columns
# ############################################################################
$sb->load_file('master', "$sample/data-loss-bug-1068562.sql");
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args,
"$master_dsn,D=bug1068562,t=simon",
"--alter", "change old_column_name new_column_name varchar(255) NULL",
qw(--execute)) },
);
ok(
$exit_status,
"Die if --execute without --no-check-alter"
) or diag($output);
like(
$output,
qr/Specify --no-check-alter to disable this check/,
"--check-alter error message"
);
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args,
"$master_dsn,D=bug1068562,t=simon",
"--alter", "change old_column_name new_column_name varchar(255) NULL",
qw(--execute --no-check-alter)) },
);
my $rows = $master_dbh->selectall_arrayref("SELECT * FROM bug1068562.simon ORDER BY id");
is_deeply(
$rows,
[ [qw(1 a)], [qw(2 b)], [qw(3 c)] ],
"bug1068562.simon: No data lost"
) or diag(Dumper($rows));
# #############################################################################
# Now try with sakila.city.
# #############################################################################
my $orig = $master_dbh->selectall_arrayref(q{SELECT city FROM sakila.city});
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args,
"$master_dsn,D=sakila,t=city",
"--alter", "change column `city` `some_cities` varchar(50) NOT NULL",
qw(--execute --alter-foreign-keys-method auto --no-check-alter)) },
);
is(
$exit_status,
0,
"sakila.city: Exit status 0",
) or diag($output);
my $mod = $master_dbh->selectall_arrayref(q{SELECT some_cities FROM sakila.city});
is_deeply(
$orig,
$mod,
"sakila.city: No data missing after first rename"
);
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args,
"$master_dsn,D=sakila,t=city",
"--alter", "change column `some_cities` city varchar(50) NOT NULL",
qw(--execute --alter-foreign-keys-method auto --no-check-alter)) },
);
my $mod2 = $master_dbh->selectall_arrayref(q{SELECT city FROM sakila.city});
is_deeply(
$orig,
$mod2,
"sakila.city: No data missing after second rename"
);
# #############################################################################
# Try with sakila.staff
# #############################################################################
diag("Reloading sakila");
my $master_port = $sb->port_for('master');
system "$trunk/sandbox/load-sakila-db $master_port";
$sb->wait_for_slaves();
$orig = $master_dbh->selectall_arrayref(q{SELECT first_name, last_name FROM sakila.staff});
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args,
"$master_dsn,D=sakila,t=staff",
"--alter", "change column first_name first_name_mod varchar(45) NOT NULL, change column last_name last_name_mod varchar(45) NOT NULL",
qw(--execute --alter-foreign-keys-method rebuild_constraints --no-check-alter)) },
);
$mod = $master_dbh->selectall_arrayref(q{SELECT first_name_mod, last_name_mod FROM sakila.staff});
is_deeply(
$orig,
$mod,
"sakila.staff: No columns went missing with a double rename"
);
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args,
"$master_dsn,D=sakila,t=staff",
"--alter", "change column first_name_mod first_name varchar(45) NOT NULL, change column last_name_mod last_name varchar(45) NOT NULL",
qw(--execute --alter-foreign-keys-method auto --no-check-alter)) },
);
$mod2 = $master_dbh->selectall_arrayref(q{SELECT first_name, last_name FROM sakila.staff});
is_deeply(
$orig,
$mod2,
"sakila.staff: No columns went missing when renaming the columns back"
);
# #############################################################################
# --dry-run and other stuff
# #############################################################################
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args,
"$master_dsn,D=sakila,t=staff",
"--alter", "change column first_name first_name_mod varchar(45) NOT NULL, change column last_name last_name_mod varchar(45) NOT NULL",
qw(--dry-run --alter-foreign-keys-method auto)) },
);
is(
$exit_status,
0,
"No error with --dry-run"
);
like(
$output,
qr/first_name to first_name_mod.+?last_name to last_name_mod/ms,
"--dry-run warns about renaming columns"
);
# CHANGE COLUMN same_name same_name
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args,
"$master_dsn,D=sakila,t=staff",
"--alter", "change column first_name first_name varchar(45) NOT NULL",
qw(--execute --alter-foreign-keys-method auto)) },
);
unlike(
$output,
qr/fist_name to fist_name/,
"No warning if CHANGE col col"
);
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($master_dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;
|