Merge pull request #356 from percona/PT-1574

PT-1574 Improved detection of nullable keys
This commit is contained in:
Carlos Salguero
2018-08-13 12:45:35 -03:00
committed by GitHub
3 changed files with 146 additions and 5 deletions

View File

@@ -5714,7 +5714,7 @@ sub next {
if ( !$self->{have_rows} ) {
$self->{nibbleno}++;
PTDEBUG && _d('Nibble:', $self->{nibble_sth}->{Statement}, 'params:',
join(', ', (@{$self->{lower} || []}, @{$self->{upper} || []})));
join(', ', (@{$self->{lower}} || [], @{$self->{upper} }||[])));
if ( my $callback = $self->{callbacks}->{exec_nibble} ) {
$self->{have_rows} = $callback->(%callback_args);
}
@@ -6113,7 +6113,7 @@ sub _next_boundaries {
PTDEBUG && _d($self->{ub_sth}->{Statement}, 'params:',
join(', ', @{$self->{lower}}), $self->{limit});
join(', ', @{$self->{lower}} || []), $self->{limit});
$self->{ub_sth}->execute(@{$self->{lower}}, $self->{limit});
my $boundary = $self->{ub_sth}->fetchall_arrayref();
PTDEBUG && _d('Next boundary:', Dumper($boundary));
@@ -6154,6 +6154,9 @@ sub identical_boundaries {
if scalar @$b1 != scalar @$b2; # shouldn't happen
my $n_vals = scalar @$b1;
for my $i ( 0..($n_vals-1) ) {
next if (!defined($b1->[$i]) && !defined($b2->[$i]));
return 0 if (!defined($b1->[$i]) && defined($b2->[$i])); # diff
return 0 if (defined($b1->[$i]) && !defined($b2->[$i])); # diff
return 0 if $b1->[$i] ne $b2->[$i]; # diff
}
return 1;
@@ -9299,13 +9302,12 @@ sub main {
{
my $indexes = $new_tbl->{tbl_struct}->{keys}; # brevity
foreach my $index ( $tp->sort_indexes($new_tbl->{tbl_struct}) ) {
if ( $index eq 'PRIMARY' || $indexes->{$index}->{is_unique} ) {
if ( $index eq 'PRIMARY' || ($indexes->{$index}->{is_unique} && $indexes->{$index}->{is_nullable} == 0)) {
PTDEBUG && _d('Delete trigger new index:', Dumper($index));
$new_tbl->{del_index} = $index;
last;
}
}
PTDEBUG && _d('New table delete index:', $new_tbl->{del_index});
}
{
@@ -9322,7 +9324,8 @@ sub main {
if ( !$new_tbl->{del_index} ) {
die "The new table $new_tbl->{name} does not have a PRIMARY KEY "
. "or a unique index which is required for the DELETE trigger.\n";
. "or a unique index which is required for the DELETE trigger.\n"
. "Please check you have at least one UNIQUE and NOT NULLABLE index.\n";
}
# Determine whether to use the new or orig table delete index.

View File

@@ -0,0 +1,97 @@
#!/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 threads::shared;
use Thread::Semaphore;
use English qw(-no_match_vars);
use Test::More;
use Data::Dumper;
use PerconaTest;
use Sandbox;
use SqlModes;
use File::Temp qw/ tempdir /;
if ($sandbox_version lt '5.7') {
plan skip_all => 'This test needs MySQL 5.7+';
} else {
plan tests => 5;
}
require "$trunk/bin/pt-online-schema-change";
my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master');
my $dsn = $sb->dsn_for("master");
# 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;
$sb->load_file('master', "t/pt-online-schema-change/samples/pt-1574.sql");
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args, "$dsn,D=test,t=t1",
'--execute', "--chunk-index", "idx_id", "--chunk-size", "1",
"--nocheck-plan", '--alter', "engine=innodb",
),
},
stderr => 1,
);
my $sql_mode = $dbh->selectcol_arrayref('SELECT @@sql_mode');
warn Data::Dumper::Dumper($sql_mode);
isnt(
$exit_status,
0,
"PT-1574, PT-1590 There is no unique index exit status",
);
like(
$output,
qr/at least one UNIQUE and NOT NULLABLE index/s,
"PT-1574, PT-1590 Message you need an unique index.",
);
($output, $exit_status) = full_output(
sub { pt_online_schema_change::main(@args, "$dsn,D=test,t=t2",
'--execute', "--chunk-index", "idx_id", "--chunk-size", "1",
"--nocheck-plan", '--alter', "engine=innodb",
),
},
stderr => 1,
);
is(
$exit_status,
0,
"PT-1574, PT-1590 Exit status 0 with null fields",
);
like(
$output,
qr/Successfully altered `test`.`t2`/s,
"PT-1574, PT-1590 Successfully altered `test`.`t2`",
);
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;

View File

@@ -0,0 +1,41 @@
DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
USE test;
CREATE TABLE `test`.`t1` (
`id` int(11) DEFAULT NULL,
`site_name` varchar(25) DEFAULT NULL,
`last_update` datetime DEFAULT NULL,
UNIQUE KEY `idx_id` (`id`),
KEY `idx_last_update` (`last_update`),
KEY `idx_site_name` (`site_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `test`.`t1` VALUES
(1385108873,'Carolyn Ryan','2018-01-13 17:05:24'),
(2140660022,'Patricia Garza','2018-01-13 19:07:51'),
(1473481373,'Rachel George','2017-12-05 21:09:53'),
(1394124308,'Mrs. Ms. Miss Janet Dixon','2017-10-28 07:07:41'),
(1978918050,'Louis Gray Jr. Sr. I II I','2017-11-01 22:10:39'),
(1275940242,'Lois Spencer','2018-02-22 01:01:38'),
(NULL,NULL,NULL),
(NULL,NULL,NULL);
CREATE TABLE `t2` (
`id` int(11) DEFAULT NULL,
`site_name` varchar(25) NOT NULL,
`last_update` datetime DEFAULT NULL,
PRIMARY KEY (`site_name`),
UNIQUE KEY `idx_id` (`id`),
KEY `idx_last_update` (`last_update`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `test`.`t2` VALUES
(1385108873,'Carolyn Ryan','2018-01-13 17:05:24'),
(2140660022,'Patricia Garza','2018-01-13 19:07:51'),
(1473481373,'Rachel George','2017-12-05 21:09:53'),
(1394124308,'Mrs. Ms. Miss Janet Dixon','2017-10-28 07:07:41'),
(1978918050,'Louis Gray Jr. Sr. I II I','2017-11-01 22:10:39'),
(1275940242,'Lois Spencer','2018-02-22 01:01:38'),
(NULL,"aaa",NULL);