From cadf35abc496a470ff6d552570fb876a6efc9a62 Mon Sep 17 00:00:00 2001 From: Sveta Smirnova Date: Thu, 7 Dec 2023 21:51:47 +0300 Subject: [PATCH] PT-2083 - Pt-archiver --charset option is not working for MySQL8.0 - Changed logic, introduced by commit 1d1c13f - Added test case --- bin/pt-archiver | 6 +-- t/pt-archiver/pt-2083.t | 86 +++++++++++++++++++++++++++++++ t/pt-archiver/pt-2279.t | 2 +- t/pt-archiver/samples/pt-2083.sql | 18 +++++++ 4 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 t/pt-archiver/pt-2083.t create mode 100644 t/pt-archiver/samples/pt-2083.sql diff --git a/bin/pt-archiver b/bin/pt-archiver index c6992c41..3e14ba7a 100755 --- a/bin/pt-archiver +++ b/bin/pt-archiver @@ -6368,11 +6368,11 @@ sub main { } my ($dbh_version) = $dbh->selectrow_array("SELECT version()"); - #if ($dbh_version =~ m/^(\d+\.\d+)\.\d+.*/ && $1 ge '8.0' && !$o->get('charset')) { - if ($dbh_version =~ m/^(\d+\.\d+)\.\d+.*/ && $1 ge '8.0') { + if ( $dbh_version =~ m/^(\d+\.\d+)\.\d+.*/ && $1 ge '8.0' && !$got_charset ) { PTDEBUG && _d("MySQL 8.0+ detected and charset was not specified.\n Setting character_set_client = utf8mb4 and --charset=utf8"); $dbh->do('/*!40101 SET character_set_connection = utf8mb4 */;'); - $o->set('charset', 'utf8'); + } elsif ( $got_charset ) { + $dbh->do("/*!40101 SET character_set_connection = ${got_charset} */;"); } $table->{dbh} = $dbh; diff --git a/t/pt-archiver/pt-2083.t b/t/pt-archiver/pt-2083.t new file mode 100644 index 00000000..3962e1c0 --- /dev/null +++ b/t/pt-archiver/pt-2083.t @@ -0,0 +1,86 @@ +#!/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 charnames ':full'; + +use PerconaTest; +use Sandbox; +require "$trunk/bin/pt-archiver"; + +my $dp = new DSNParser(opts=>$dsn_opts); +my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp); +my $dbh = $sb->get_dbh_for('master'); + +if ( !$dbh ) { + plan skip_all => 'Cannot connect to sandbox master'; +} + +my $output; +my $cnf = "/tmp/12345/my.sandbox.cnf"; +my $cmd = "$trunk/bin/pt-archiver"; + +$sb->wipe_clean($dbh); +$sb->create_dbs($dbh, ['test']); + +# Test --bulk-insert +$sb->load_file('master', 't/pt-archiver/samples/pt-2083.sql'); + +$output = output( + sub { pt_archiver::main(qw(--commit-each --where 1=1 --statistics --charset latin1), + '--source', "L=1,D=test,t=table_1,F=$cnf", + '--dest', "t=table_1_dest") }, +); + +unlike( + $output, + qr/Character set mismatch/, + 'No character set mismatch error' +) or diag($output); + +my @copied = $dbh->selectrow_array('SELECT c1 FROM test.table_1_dest'); + +like( + $copied[0], + qr/I love MySQL!/, + 'Rows copied into the table successfully' +) or diag($copied[0]); + +# Test --file +$sb->load_file('master', 't/pt-archiver/samples/pt-2083.sql'); + +$output = output( + sub { pt_archiver::main(qw(--where 1=1 --statistics --charset latin1), + '--source', "L=1,D=test,t=table_1,F=$cnf", + '--file', '/tmp/%Y-%m-%d-%D_%H:%i:%s.%t') }, +); + +unlike( + $output, + qr/Character set mismatch/, + 'No character set mismatch error' +) or diag($output); + +like( + `cat /tmp/*.table_1`, + qr/I love MySQL!/, + 'Rows copied into the file successfully' +) or diag($output); + +# ############################################################################# +# Done. +# ############################################################################# +diag(`rm -f /tmp/*.table_1`); +$sb->wipe_clean($dbh); +ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox"); +done_testing; +exit; diff --git a/t/pt-archiver/pt-2279.t b/t/pt-archiver/pt-2279.t index 11c9620d..fc4022dc 100644 --- a/t/pt-archiver/pt-2279.t +++ b/t/pt-archiver/pt-2279.t @@ -67,7 +67,7 @@ unlike( unlike( $output, qr/Cannot open :encoding(utf8mb4)/, - 'Destination file can be read if encodin utf8mb4 is used' + 'Destination file can be read if encoding utf8mb4 is used' ) or diag($output); # ############################################################################# diff --git a/t/pt-archiver/samples/pt-2083.sql b/t/pt-archiver/samples/pt-2083.sql new file mode 100644 index 00000000..2aa39a50 --- /dev/null +++ b/t/pt-archiver/samples/pt-2083.sql @@ -0,0 +1,18 @@ +DROP TABLE IF EXISTS test.table_1; +DROP TABLE IF EXISTS test.table_1_dest; + +SET NAMES latin1; + +CREATE TABLE test.table_1 ( + id int(11) NOT NULL, + c1 varchar(20) DEFAULT NULL, + PRIMARY KEY (id) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +INSERT INTO test.table_1 VALUES(1, 'I love MySQL!'); + +CREATE TABLE test.table_1_dest ( + id int(11) NOT NULL, + c1 varchar(20) DEFAULT NULL, + PRIMARY KEY (id) +) ENGINE=InnoDB DEFAULT CHARSET=latin1;