Merge lp:~percona-toolkit-dev/percona-toolkit/pt-table-sync-del-rows-bug-1223458 into lp:~percona-toolkit-dev/percona-toolkit/release-2.2.6

Proposed by Daniel Nichter
Status: Merged
Approved by: Daniel Nichter
Approved revision: 604
Merged at revision: 603
Proposed branch: lp:~percona-toolkit-dev/percona-toolkit/pt-table-sync-del-rows-bug-1223458
Merge into: lp:~percona-toolkit-dev/percona-toolkit/release-2.2.6
Diff against target: 249 lines (+195/-2)
4 files modified
bin/pt-table-sync (+87/-1)
t/pt-table-sync/basics.t (+1/-1)
t/pt-table-sync/safety_checks.t (+88/-0)
t/pt-table-sync/samples/on_del_cas.sql (+19/-0)
To merge this branch: bzr merge lp:~percona-toolkit-dev/percona-toolkit/pt-table-sync-del-rows-bug-1223458
Reviewer Review Type Date Requested Status
Daniel Nichter Approve
Review via email: mp+199017@code.launchpad.net
To post a comment you must log in.
604. By Daniel Nichter

Note that --check-child-tables only prints first affect child table in error message.

Revision history for this message
Daniel Nichter (daniel-nichter) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/pt-table-sync'
2--- bin/pt-table-sync 2013-11-08 01:47:46 +0000
3+++ bin/pt-table-sync 2013-12-14 03:36:20 +0000
4@@ -10943,6 +10943,27 @@
5 }
6 }
7
8+ my $replace = $o->get('replace')
9+ || $o->get('replicate')
10+ || $o->get('sync-to-master');
11+ if ( $replace && $o->get('execute') && $o->get('check-child-tables') ) {
12+ my $child_tables = find_child_tables(
13+ tbl => $src,
14+ dbh => $src->{dbh},
15+ Quoter => $q,
16+ );
17+ if ( $child_tables ) {
18+ foreach my $tbl ( @$child_tables ) {
19+ my $ddl = $tp->get_create_table(
20+ $src->{dbh}, $tbl->{db}, $tbl->{tbl});
21+ if ( $ddl && $ddl =~ m/(ON (?:DELETE|UPDATE) (?:SET|CASCADE))/ ) {
22+ my $fk = $1;
23+ die "REPLACE statements on $src->{db}.$src->{tbl} can adversely affect child table $tbl->{name} because it has an $fk foreign key constraint. See --[no]check-child-tables in the documentation for more information. --check-child-tables error\n"
24+ }
25+ }
26+ }
27+ }
28+
29 return;
30 }
31
32@@ -11363,6 +11384,46 @@
33 }
34 }
35
36+sub find_child_tables {
37+ my ( %args ) = @_;
38+ my @required_args = qw(tbl dbh Quoter);
39+ foreach my $arg ( @required_args ) {
40+ die "I need a $arg argument" unless $args{$arg};
41+ }
42+ my ($tbl, $dbh, $q) = @args{@required_args};
43+
44+ if ( lc($tbl->{tbl_struct}->{engine} || '') eq 'myisam' ) {
45+ PTDEBUG && _d(q{MyISAM table, not looking for child tables});
46+ return;
47+ }
48+
49+ PTDEBUG && _d('Finding child tables');
50+
51+ my $sql = "SELECT table_schema, table_name "
52+ . "FROM information_schema.key_column_usage "
53+ . "WHERE constraint_schema='$tbl->{db}' "
54+ . "AND referenced_table_name='$tbl->{tbl}'";
55+ PTDEBUG && _d($sql);
56+ my $rows = $dbh->selectall_arrayref($sql);
57+ if ( !$rows || !@$rows ) {
58+ PTDEBUG && _d('No child tables found');
59+ return;
60+ }
61+
62+ my @child_tables;
63+ foreach my $row ( @$rows ) {
64+ my $tbl = {
65+ db => $row->[0],
66+ tbl => $row->[1],
67+ name => $q->quote(@$row),
68+ };
69+ push @child_tables, $tbl;
70+ }
71+
72+ PTDEBUG && _d('Child tables:', Dumper(\@child_tables));
73+ return \@child_tables;
74+}
75+
76 sub _d {
77 my ($package, undef, $line) = caller 0;
78 @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
79@@ -11589,7 +11650,7 @@
80
81 Also be careful with tables that have foreign key constraints with C<ON DELETE>
82 or C<ON UPDATE> definitions because these might cause unintended changes on the
83-child tables.
84+child tables. See L<"--[no]check-child-tables">.
85
86 In general, this tool is best suited when your tables have a primary key or
87 unique index. Although it can synchronize data in tables lacking a primary key
88@@ -11875,6 +11936,31 @@
89 binmode on STDOUT without the utf8 layer, and runs SET NAMES after
90 connecting to MySQL.
91
92+=item --[no]check-child-tables
93+
94+default: yes
95+
96+Check if L<"--execute"> will adversely affect child tables. When
97+L<"--replace">, L<"--replicate">, or L<"--sync-to-master"> is specified,
98+the tool may sync tables using C<REPLACE> statements. If a table being
99+synced has child tables with C<ON DELETE CASCADE>, C<ON UPDATE CASCADE>,
100+or C<ON UPDATE SET NULL>, the tool prints an error and skips the table because
101+C<REPLACE> becomes C<DELETE> then C<INSERT>, so the C<DELETE> will cascade
102+to the child table and delete its rows. In the worst case, this can delete
103+all rows in child tables!
104+
105+Specify C<--no-check-child-tables> to disable this check. To completely
106+avoid affecting child tables, also specify C<--no-foreign-key-checks>
107+so MySQL will not cascade any operations from the parent to child tables.
108+
109+This check is only preformed if L<"--execute"> and one of L<"--replace">,
110+L<"--replicate">, or L<"--sync-to-master"> is specified. L<"--print">
111+does not check child tables.
112+
113+The error message only prints the first child table found with an
114+C<ON DELETE CASCADE>, C<ON UPDATE CASCADE>, or C<ON UPDATE SET NULL>
115+foreign key constraint. There could be other affected child tables.
116+
117 =item --[no]check-master
118
119 default: yes
120
121=== modified file 't/pt-table-sync/basics.t'
122--- t/pt-table-sync/basics.t 2013-03-02 17:17:23 +0000
123+++ t/pt-table-sync/basics.t 2013-12-14 03:36:20 +0000
124@@ -182,7 +182,7 @@
125 sub {
126 pt_table_sync::main('h=127.1,P=12345,u=msandbox,p=msandbox',
127 qw(--print --execute --replicate percona.checksums),
128- qw(--no-foreign-key-checks))
129+ qw(--no-foreign-key-checks --no-check-child-tables))
130 }
131 );
132
133
134=== added file 't/pt-table-sync/safety_checks.t'
135--- t/pt-table-sync/safety_checks.t 1970-01-01 00:00:00 +0000
136+++ t/pt-table-sync/safety_checks.t 2013-12-14 03:36:20 +0000
137@@ -0,0 +1,88 @@
138+#!/usr/bin/env perl
139+
140+BEGIN {
141+ die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
142+ unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
143+ unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
144+};
145+
146+use strict;
147+use warnings FATAL => 'all';
148+use English qw(-no_match_vars);
149+use Test::More;
150+use Data::Dumper;
151+
152+use PerconaTest;
153+use Sandbox;
154+require "$trunk/bin/pt-table-sync";
155+
156+my $output;
157+my $dp = new DSNParser(opts=>$dsn_opts);
158+my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
159+my $master_dbh = $sb->get_dbh_for('master');
160+my $slave_dbh = $sb->get_dbh_for('slave1');
161+
162+if ( !$master_dbh ) {
163+ plan skip_all => 'Cannot connect to sandbox master';
164+}
165+elsif ( !$slave_dbh ) {
166+ plan skip_all => 'Cannot connect to sandbox slave';
167+}
168+
169+my $master_dsn = $sb->dsn_for('master');
170+my $slave1_dsn = $sb->dsn_for('slave1');
171+
172+# #############################################################################
173+# --[no]check-child-tables
174+# pt-table-sync deletes child table rows Edit
175+# https://bugs.launchpad.net/percona-toolkit/+bug/1223458
176+# #############################################################################
177+
178+$sb->load_file('master', 't/pt-table-sync/samples/on_del_cas.sql');
179+
180+$master_dbh->do("INSERT INTO on_del_cas.parent VALUES (1), (2)");
181+$master_dbh->do("INSERT INTO on_del_cas.child1 VALUES (null, 1)");
182+$master_dbh->do("INSERT INTO on_del_cas.child2 VALUES (null, 1)");
183+$sb->wait_for_slaves();
184+
185+$output = output(
186+ sub {
187+ pt_table_sync::main($slave1_dsn, qw(--sync-to-master),
188+ qw(--execute -d on_del_cas))
189+ },
190+ stderr => 1,
191+);
192+
193+like(
194+ $output,
195+ qr/on on_del_cas.parent can adversely affect child table `on_del_cas`.`child2` because it has an ON DELETE CASCADE/,
196+ "check-child-tables: error message"
197+);
198+
199+my $rows = $slave_dbh->selectall_arrayref("select * from on_del_cas.child2");
200+is_deeply(
201+ $rows,
202+ [ [1,1] ],
203+ "check-child-tables: child2 row not deleted"
204+) or diag(Dumper($rows));
205+
206+$output = output(
207+ sub {
208+ pt_table_sync::main($slave1_dsn, qw(--sync-to-master),
209+ qw(--print -d on_del_cas))
210+ },
211+ stderr => 1,
212+);
213+
214+unlike(
215+ $output,
216+ qr/on on_del_cas.parent can adversely affect child table `on_del_cas`.`child2` because it has an ON DELETE CASCADE/,
217+ "check-child-tables: no error message with --print"
218+);
219+
220+# #############################################################################
221+# Done.
222+# #############################################################################
223+$sb->wipe_clean($master_dbh);
224+ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
225+done_testing;
226
227=== added file 't/pt-table-sync/samples/on_del_cas.sql'
228--- t/pt-table-sync/samples/on_del_cas.sql 1970-01-01 00:00:00 +0000
229+++ t/pt-table-sync/samples/on_del_cas.sql 2013-12-14 03:36:20 +0000
230@@ -0,0 +1,19 @@
231+DROP DATABASE IF EXISTS on_del_cas;
232+CREATE DATABASE on_del_cas;
233+USE on_del_cas;
234+
235+CREATE TABLE parent (
236+ id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
237+) ENGINE=InnoDB;
238+
239+CREATE TABLE child1 (
240+ id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
241+ parent_id INT NOT NULL,
242+ FOREIGN KEY fk1 (parent_id) REFERENCES parent (id) ON DELETE RESTRICT
243+) ENGINE=InnoDB;
244+
245+CREATE TABLE child2 (
246+ id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
247+ parent_id INT NOT NULL,
248+ FOREIGN KEY fk1 (parent_id) REFERENCES parent (id) ON DELETE CASCADE
249+) ENGINE=InnoDB;

Subscribers

People subscribed via source and target branches

to all changes: