diff --git a/bin/pt-online-schema-change b/bin/pt-online-schema-change index 1db26704..d2a0b598 100755 --- a/bin/pt-online-schema-change +++ b/bin/pt-online-schema-change @@ -9275,13 +9275,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}); } { @@ -9298,7 +9297,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. diff --git a/t/pt-online-schema-change/pt-1574.t b/t/pt-online-schema-change/pt-1574.t new file mode 100644 index 00000000..702f6c50 --- /dev/null +++ b/t/pt-online-schema-change/pt-1574.t @@ -0,0 +1,73 @@ +#!/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 => 3; +} + +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, +); + +isnt( + $exit_status, + 0, + "PT-1544 There is no unique index exit status", +); + +like( + $output, + qr/at least one UNIQUE and NOT NULLABLE index/s, + "PT-1544 Message you need an unique index.", +); + +# ############################################################################# +# Done. +# ############################################################################# +$sb->wipe_clean($dbh); +ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox"); +done_testing; diff --git a/t/pt-online-schema-change/samples/pt-1574.sql b/t/pt-online-schema-change/samples/pt-1574.sql new file mode 100644 index 00000000..9b0f3929 --- /dev/null +++ b/t/pt-online-schema-change/samples/pt-1574.sql @@ -0,0 +1,22 @@ +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 `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);