From 8626c07a99f4867e72ff2fe5f7d9cc5694e13e72 Mon Sep 17 00:00:00 2001 From: Brian Fraser Date: Thu, 30 Aug 2012 18:39:53 -0300 Subject: [PATCH] Fix for 1041372: pt-osc and long table names --- bin/pt-online-schema-change | 28 +++++++++++++++++++++++--- t/pt-online-schema-change/bugs.t | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/bin/pt-online-schema-change b/bin/pt-online-schema-change index ad4cb524..555e416f 100755 --- a/bin/pt-online-schema-change +++ b/bin/pt-online-schema-change @@ -7380,7 +7380,6 @@ sub main { eval { $new_tbl = create_new_table( orig_tbl => $orig_tbl, - suffix => '_new', Cxn => $cxn, Quoter => $q, OptionParser => $o, @@ -8097,7 +8096,7 @@ sub create_new_table{ my $tries = $args{tries} || 10; # don't try forever my $prefix = $args{prefix} || '_'; - my $suffix = $args{suffix} || '_new'; + my $suffix = '_new'; my $table_name = $orig_tbl->{tbl} . $suffix; print "Creating new table...\n"; @@ -8105,6 +8104,14 @@ sub create_new_table{ my @old_tables; while ( $tryno++ < $tries ) { $table_name = $prefix . $table_name; + + if ( length($table_name) > 64 ) { + my $truncated_table_name = substr($table_name, 0, 60) . $suffix; + PTDEBUG && _d($table_name, 'is over 64 characters long, truncating to', + $truncated_table_name); + $table_name = $truncated_table_name; + } + my $quoted = $q->quote($orig_tbl->{db}, $table_name); # Generate SQL to create the new table. We do not use CREATE TABLE LIKE @@ -8183,9 +8190,17 @@ sub swap_tables { } elsif ( $o->get('execute') ) { print "Swapping tables...\n"; - + while ( $tries-- ) { $table_name = $prefix . $table_name; + + if ( length($table_name) > 64 ) { + my $truncated_table_name = substr($table_name, 0, 64); + PTDEBUG && _d($table_name, 'is over 64 characters long, truncating to', + $truncated_table_name); + $table_name = $truncated_table_name; + } + my $sql = "RENAME TABLE $orig_tbl->{name} " . "TO " . $q->quote($orig_tbl->{db}, $table_name) . ", $new_tbl->{name} TO $orig_tbl->{name}"; @@ -8524,6 +8539,13 @@ sub create_triggers { my $prefix = 'pt_osc_' . $orig_tbl->{db} . '_' . $orig_tbl->{tbl}; $prefix =~ s/\W/_/g; + if ( length($prefix) > 60 ) { + my $truncated_prefix = substr($prefix, 0, 60); + PTDEBUG && _d('Trigger prefix', $prefix, 'is over 60 characters long,', + 'truncating to', $truncated_prefix); + $prefix = $truncated_prefix; + } + # To be safe, the delete trigger must specify all the columns of the # primary key/unique index. We use null-safe equals, because unique # unique indexes can be nullable. diff --git a/t/pt-online-schema-change/bugs.t b/t/pt-online-schema-change/bugs.t index 11128b8d..296c0844 100644 --- a/t/pt-online-schema-change/bugs.t +++ b/t/pt-online-schema-change/bugs.t @@ -159,6 +159,40 @@ like $output, ); } +# ############################################################################ +# Bug 1041372: ptc-osc and long table names +# https://bugs.launchpad.net/percona-toolkit/+bug/1041372 +# ############################################################################ +my $orig_tbl = 'very_very_very_very_very_very_very_very_very_long_table_name'; + +$master_dbh->do(q{DROP DATABASE IF EXISTS `bug_1041372`}); +$master_dbh->do(q{CREATE DATABASE `bug_1041372`}); + +for my $i ( 0..4 ) { + my $tbl = $orig_tbl . ("a" x $i); + $master_dbh->do(qq{create table `bug_1041372`.$tbl (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY )}); + $master_dbh->do(qq{insert into `bug_1041372`.$tbl values (1), (2), (3), (4), (5)}); + + ($output) = full_output(sub { pt_online_schema_change::main(@args, + '--alter', "ADD COLUMN ptosc INT", + '--execute', "$master_dsn,D=bug_1041372,t=$tbl")}); + + like( + $output, + qr/\QSuccessfully altered `bug_1041372`.`$tbl`/, + "pt-osc works on long table names (length " . length($tbl) . ")" + ); +} + +my $triggers = $master_dbh->selectall_arrayref(qq{SHOW TRIGGERS FROM `bug_1041372`}); +is_deeply( + $triggers, + [], + "No triggers left for long table names" +) or diag(Dumper($triggers)); + +$master_dbh->do(q{DROP DATABASE IF EXISTS `bug_1041372`}); + # ############################################################################# # Done. # #############################################################################