PT-1256 pt-table-sync does not use the character set for the table it is synchronizing

This commit is contained in:
Carlos Salguero
2018-02-09 16:46:16 -03:00
parent 19936df442
commit 75ab0ba653
6 changed files with 145 additions and 3 deletions

View File

@@ -1,5 +1,9 @@
Changelog for Percona Toolkit
v3.0.7
* Fixed bug PT-1256: pt-table-sync does not use the character set for the table it is synchronizing
v3.0.6 released 2017-12-20
* Fixed bug PT-234: Genaral log parser cannot handle timestamps with tz

View File

@@ -55,7 +55,7 @@ BEGIN {
{
package Percona::Toolkit;
our $VERSION = '3.0.6';
our $VERSION = '3.0.7';
use strict;
use warnings FATAL => 'all';
@@ -2137,6 +2137,7 @@ sub parse {
}
}
$self->{dsn_props} = \%final_props;
return \%final_props;
}
@@ -2319,6 +2320,20 @@ sub get_dbh {
}
}
if ($self->{dsn_props}->{D} && $self->{dsn_props}->{t}) {
PTDEBUG && _d("DSN has a schema and table: $self->{dsn_props}->{D}.$self->{dsn_props}->{t}");
PTDEBUG && _d("Trying to set the default charset for the connection");
my (undef, $create_table) = eval { $dbh->selectrow_array("SHOW CREATE TABLE $self->{dsn_props}->{D}.$self->{dsn_props}->{t}") };
if ($create_table && $create_table =~ m/DEFAULT CHARSET=(\S+)\s*/) {
PTDEBUG && _d("Detected table's character set: $1");
PTDEBUG && _d("Executing: SET NAMES '$1'");
$dbh->do("SET NAMES '$1'");
} else {
PTDEBUG && _d("Cannot get the default character set for the table");
}
}
PTDEBUG && _d('DBH info: ',
$dbh,
Dumper($dbh->selectrow_hashref(
@@ -10644,6 +10659,16 @@ sub sync_a_table {
: $tbl_struct->{engine} eq 'InnoDB' ? 1
: 0;
if ($tbl_struct->{charset}) {
PTDEBUG && _d("Detected table's character set: $tbl_struct->{charset}");
PTDEBUG && _d("Executing: SET NAMES '$tbl_struct->{charset}'");
$src->{dbh}->do("SET NAMES '$tbl_struct->{charset}'");
$src->{misc_dbh}->do("SET NAMES '$tbl_struct->{charset}'");
$dst->{dbh}->do("SET NAMES '$tbl_struct->{charset}'");
$dst->{misc_dbh}->do("SET NAMES '$tbl_struct->{charset}'");
} else {
PTDEBUG && _d("Cannot get the default character set for the table");
}
# Turn off AutoCommit if we're using transactions.
$src->{dbh}->{AutoCommit} = !$use_txn;
$src->{misc_dbh}->{AutoCommit} = !$use_txn;
@@ -12977,6 +13002,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA.
=head1 VERSION
pt-table-sync 3.0.6
pt-table-sync 3.0.7
=cut

View File

@@ -27,6 +27,7 @@ innodb_lock_wait_timeout = 3
general_log
general_log_file = genlog
lower_case_table_names = 0
#character-set-server = utf8
# fkc test
binlog_format = STATEMENT

15
t/lib/samples/charset.sql Normal file
View File

@@ -0,0 +1,15 @@
DROP DATABASE IF EXISTS `test`;
CREATE DATABASE `test`;
CREATE TABLE `test`.`t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`f2` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) COMMENT "test1" ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `test`.`t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`f2` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

97
t/pt-table-sync/pt-1256.t Normal file
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 utf8;
use Encode qw(decode encode);
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;
use PerconaTest;
use Sandbox;
require "$trunk/bin/pt-table-sync";
binmode(STDIN, ':utf8') or die "Can't binmode(STDIN, ':utf8'): $OS_ERROR";
binmode(STDOUT, ':utf8') or die "Can't binmode(STDOUT, ':utf8'): $OS_ERROR";
my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $master_dbh = $sb->get_dbh_for('master');
my $slave1_dbh = $sb->get_dbh_for('slave1');
my $slave2_dbh = $sb->get_dbh_for('slave2');
if ( !$master_dbh ) {
plan skip_all => 'Cannot connect to sandbox master';
}
elsif ( !$slave1_dbh ) {
plan skip_all => 'Cannot connect to sandbox slave1';
}
elsif ( !$slave1_dbh ) {
plan skip_all => 'Cannot connect to sandbox slave2';
}
else {
plan tests => 5;
}
my ($output, $status);
my @args = ('--sync-to-master', 'h=127.1,P=12346,u=msandbox,p=msandbox',
qw(-t test.t1 --print --execute));
# use lib/samples dir since the main change is in DSNParser
$sb->load_file('master', "t/lib/samples/charset.sql");
my $want = encode('UTF-8','абвгд');
$master_dbh->do("SET NAMES 'utf8'");
$slave1_dbh->do("SET NAMES 'utf8'");
$slave1_dbh->do("SET NAMES 'utf8'");
$master_dbh->do("INSERT INTO test.t1 VALUES (NULL, '$want')");
$sb->wait_for_slaves();
$slave1_dbh->do("DELETE FROM test.t1 WHERE id=1 LIMIT 1");
$slave1_dbh->do("FLUSH TABLES");
# 1
($output, $status) = full_output(
sub { pt_table_sync::main(@args) },
);
like(
$output,
qr/REPLACE INTO `test`.`t1`/,
"PT-1256 Set the correct charset"
);
# 2
my $row = $slave1_dbh->selectrow_hashref("SELECT f2 FROM test.t1 WHERE id = 1");
is(
$row->{f2},
$want,
"Character set is correct",
);
# 3
$output = `$trunk/bin/pt-table-sync --execute --lock-and-rename h=127.1,P=12345,u=msandbox,p=msandbox,D=test,t=t1 t=t2 2>&1`;
$output = `/tmp/12345/use -e 'show create table test.t2'`;
like($output, qr/COMMENT='test1'/, '--lock-and-rename worked');
$row = $slave1_dbh->selectrow_hashref("SELECT f2 FROM test.t2 WHERE id = 1");
is(
$row->{f2},
$want,
"Character set is correct",
);
# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($master_dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;

View File

@@ -44,7 +44,7 @@ $dbh->do("CREATE TABLE IF NOT EXISTS percona_test.load_data (i int)");
`echo 1 > /tmp/load_data_test.$$`;
eval {
$dbh->do("LOAD DATA LOCAL INFILE '/tmp/load_data_test.$$' INTO TABLE percona_test.load_data");
$dbh->do("LOAD DATA INFILE '/tmp/load_data_test.$$' INTO TABLE percona_test.load_data");
};
if ( $EVAL_ERROR ) {