Skip to content

Commit 5f14441

Browse files
committed
PT-2375 - fixed pt-table-sync for tables with generated columns
Previously, pt-table-sync generated DML statements that included the generated columns of a table, which is however rejected by the database and not necessary for the required sync statements.
1 parent b6dff19 commit 5f14441

File tree

5 files changed

+181
-1
lines changed

5 files changed

+181
-1
lines changed

bin/pt-table-sync

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,6 +3708,7 @@ sub sort_cols {
37083708
my @cols;
37093709
if ( $self->{tbl_struct} ) {
37103710
my $pos = $self->{tbl_struct}->{col_posn};
3711+
my $is_generated = $self->{tbl_struct}->{is_generated};
37113712
my @not_in_tbl;
37123713
@cols = sort {
37133714
$pos->{$a} <=> $pos->{$b}
@@ -3721,6 +3722,9 @@ sub sort_cols {
37213722
1;
37223723
}
37233724
}
3725+
grep {
3726+
!$is_generated->{$_}
3727+
}
37243728
sort keys %$row;
37253729
push @cols, @not_in_tbl if @not_in_tbl;
37263730
}

lib/ChangeHandler.pm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ sub get_changes {
462462

463463

464464
# Sub: sort_cols
465-
# Sort a row's columns based on their real order in the table.
465+
# Sort a row's columns based on their real order in the table, and remove
466+
# generated columns.
466467
# This requires that the optional tbl_struct arg was passed to <new()>.
467468
# If not, the rows are sorted alphabetically.
468469
#
@@ -476,6 +477,7 @@ sub sort_cols {
476477
my @cols;
477478
if ( $self->{tbl_struct} ) {
478479
my $pos = $self->{tbl_struct}->{col_posn};
480+
my $is_generated = $self->{tbl_struct}->{is_generated};
479481
my @not_in_tbl;
480482
@cols = sort {
481483
$pos->{$a} <=> $pos->{$b}
@@ -489,6 +491,9 @@ sub sort_cols {
489491
1;
490492
}
491493
}
494+
grep {
495+
!$is_generated->{$_}
496+
}
492497
sort keys %$row;
493498
push @cols, @not_in_tbl if @not_in_tbl;
494499
}

t/lib/ChangeHandler.t

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,85 @@ SKIP: {
339339
);
340340
};
341341

342+
# #############################################################################
343+
# PT-2375: pt-table-sync must handle generated columns correctly
344+
# #############################################################################
345+
$row = {
346+
id => 1,
347+
foo => 'foo',
348+
bar => 'bar',
349+
};
350+
$tbl_struct = {
351+
col_posn => { id=>0, foo=>1, bar=>2 },
352+
is_generated => {foo=>1}
353+
};
354+
$ch = new ChangeHandler(
355+
Quoter => $q,
356+
right_db => 'test', # dst
357+
right_tbl => 'pt-2375',
358+
left_db => 'test', # src
359+
left_tbl => 'pt-2375',
360+
actions => [ sub { push @rows, @_ } ],
361+
replace => 0,
362+
queue => 0,
363+
tbl_struct => $tbl_struct,
364+
);
365+
366+
@rows = ();
367+
@dbhs = ();
368+
369+
is(
370+
$ch->make_INSERT($row, [qw(id foo bar)]),
371+
"INSERT INTO `test`.`pt-2375`(`id`, `bar`) VALUES ('1', 'bar')",
372+
'make_INSERT() omits generated columns'
373+
);
374+
375+
is(
376+
$ch->make_REPLACE($row, [qw(id foo bar)]),
377+
"REPLACE INTO `test`.`pt-2375`(`id`, `bar`) VALUES ('1', 'bar')",
378+
'make_REPLACE() omits generated columns'
379+
);
380+
381+
is(
382+
$ch->make_UPDATE($row, [qw(id foo)]),
383+
"UPDATE `test`.`pt-2375` SET `bar`='bar' WHERE `id`='1' AND `foo`='foo' LIMIT 1",
384+
'make_UPDATE() omits generated columns from SET phrase but includes in WHERE phrase'
385+
);
386+
387+
is(
388+
$ch->make_DELETE($row, [qw(id foo bar)]),
389+
"DELETE FROM `test`.`pt-2375` WHERE `id`='1' AND `foo`='foo' AND `bar`='bar' LIMIT 1",
390+
'make_DELETE() includes generated columns in WHERE phrase'
391+
);
392+
393+
SKIP: {
394+
skip 'Cannot connect to sandbox master', 3 unless $master_dbh;
395+
396+
$master_dbh->do('DROP TABLE IF EXISTS test.`pt-2375`');
397+
$master_dbh->do('CREATE TABLE test.`pt-2375` (id INT, foo varchar(16) as ("foo"), bar char)');
398+
$master_dbh->do("INSERT INTO test.`pt-2375` (`id`, `bar`) VALUES (1,'a'),(2,'b')");
399+
400+
$ch->fetch_back($master_dbh);
401+
402+
is(
403+
$ch->make_INSERT($row, [qw(id foo)]),
404+
"INSERT INTO `test`.`pt-2375`(`id`, `bar`) VALUES ('1', 'a')",
405+
'make_INSERT() omits generated columns, with fetch-back'
406+
);
407+
408+
is(
409+
$ch->make_REPLACE($row, [qw(id foo)]),
410+
"REPLACE INTO `test`.`pt-2375`(`id`, `bar`) VALUES ('1', 'a')",
411+
'make_REPLACE() omits generated columns, with fetch-back'
412+
);
413+
414+
is(
415+
$ch->make_UPDATE($row, [qw(id foo)]),
416+
"UPDATE `test`.`pt-2375` SET `bar`='a' WHERE `id`='1' AND `foo`='foo' LIMIT 1",
417+
'make_UPDATE() omits generated columns from SET phrase, with fetch-back'
418+
);
419+
};
420+
342421
# #############################################################################
343422
# Issue 641: Make mk-table-sync use hex for binary/blob data
344423
# #############################################################################

t/pt-table-sync/pt-2375.t

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env perl
2+
3+
BEGIN {
4+
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
5+
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
6+
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
7+
};
8+
9+
use strict;
10+
use warnings FATAL => 'all';
11+
use English qw(-no_match_vars);
12+
use Test::More;
13+
14+
use PerconaTest;
15+
use Sandbox;
16+
require "$trunk/bin/pt-table-sync";
17+
18+
my $dp = new DSNParser(opts=>$dsn_opts);
19+
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
20+
my $source_dbh = $sb->get_dbh_for('source');
21+
my $replica1_dbh = $sb->get_dbh_for('replica1');
22+
23+
if ( !$source_dbh ) {
24+
plan skip_all => 'Cannot connect to sandbox source';
25+
}
26+
elsif ( !$replica1_dbh ) {
27+
plan skip_all => 'Cannot connect to sandbox replica1';
28+
}
29+
else {
30+
plan tests => 3;
31+
}
32+
33+
my $output;
34+
35+
# #############################################################################
36+
# Test generated REPLACE statements.
37+
# #############################################################################
38+
$sb->load_file('source', "t/pt-table-sync/samples/pt-2375.sql");
39+
$sb->wait_for_replicas();
40+
$replica1_dbh->do("delete from `test`.`test_table` where `id`=1");
41+
42+
$output = remove_traces(output(
43+
sub { pt_table_sync::main('--sync-to-source',
44+
'h=127.0.0.1,P=12346,u=msandbox,p=msandbox',
45+
qw(-t test.test_table --print --execute))
46+
},
47+
));
48+
chomp($output);
49+
is(
50+
$output,
51+
"REPLACE INTO `test`.`test_table`(`id`, `value`) VALUES ('1', '24');",
52+
"Generated columns are not used in REPLACE statements"
53+
);
54+
55+
# #############################################################################
56+
# Test generated UPDATE statements.
57+
# #############################################################################
58+
$sb->load_file('source', "t/pt-table-sync/samples/pt-2375.sql");
59+
$sb->wait_for_replicas();
60+
$replica1_dbh->do("update `test`.`test_table` set `value`=55 where `id`=2");
61+
62+
$output = remove_traces(output(
63+
sub { pt_table_sync::main(qw(--print --execute),
64+
"h=127.0.0.1,P=12346,u=msandbox,p=msandbox,D=test,t=test_table",
65+
"h=127.0.0.1,P=12345,u=msandbox,p=msandbox,D=test,t=test_table");
66+
}
67+
));
68+
chomp($output);
69+
is(
70+
$output,
71+
"UPDATE `test`.`test_table` SET `value`='55' WHERE `id`='2' LIMIT 1;",
72+
"Generated columns are not used in UPDATE statements"
73+
);
74+
75+
# #############################################################################
76+
# Done.
77+
# #############################################################################
78+
$sb->wipe_clean($source_dbh);
79+
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
80+
exit;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
DROP DATABASE IF EXISTS test;
2+
CREATE DATABASE test;
3+
USE test;
4+
5+
CREATE TABLE `test_table` (
6+
`id` INT AUTO_INCREMENT PRIMARY KEY,
7+
`value` INT NOT NULL,
8+
`derived_value` INT AS (2*`value`)
9+
) ENGINE=InnoDB;
10+
11+
INSERT INTO `test_table` (`value`) VALUES (24);
12+
INSERT INTO `test_table` (`value`) VALUES (42);

0 commit comments

Comments
 (0)