File: ChangeHandler.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (548 lines) | stat: -rw-r--r-- 15,909 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/usr/bin/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 ChangeHandler;
use Quoter;
use DSNParser;
use Sandbox;
use PerconaTest;

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');

throws_ok(
   sub { new ChangeHandler() },
   qr/I need a Quoter/,
   'Needs a Quoter',
);

my @rows;
my @dbhs;
my $q  = new Quoter();
my $ch = new ChangeHandler(
   Quoter    => $q,
   right_db  => 'test',  # dst
   right_tbl => 'foo',
   left_db   => 'test',  # src
   left_tbl  => 'test1',
   actions   => [ sub { push @rows, $_[0]; push @dbhs, $_[1]; } ],
   replace   => 0,
   queue     => 0,
);

$ch->change('INSERT', { a => 1, b => 2 }, [qw(a)] );

is_deeply(\@rows,
   ["INSERT INTO `test`.`foo`(`a`, `b`) VALUES ('1', '2')",],
   'First row',
);

$ch->change(undef, { a => 1, b => 2 }, [qw(a)] );

is_deeply(
   \@rows,
   ["INSERT INTO `test`.`foo`(`a`, `b`) VALUES ('1', '2')",],
   'Skips undef action'
);


is_deeply(\@rows,
   ["INSERT INTO `test`.`foo`(`a`, `b`) VALUES ('1', '2')",],
   'First row',
);

$ch->{queue} = 1;

$ch->change('DELETE', { a => 1, b => 2 }, [qw(a)] );

is_deeply(\@rows,
   ["INSERT INTO `test`.`foo`(`a`, `b`) VALUES ('1', '2')",],
   'Second row not there yet',
);

$ch->process_rows(1);

is_deeply(\@rows,
   [
   "INSERT INTO `test`.`foo`(`a`, `b`) VALUES ('1', '2')",
   "DELETE FROM `test`.`foo` WHERE `a`='1' LIMIT 1",
   ],
   'Second row there',
);
$ch->{queue} = 2;

$ch->change('UPDATE', { a => 1, b => 2 }, [qw(a)] );
$ch->process_rows(1);

is_deeply(\@rows,
   [
   "INSERT INTO `test`.`foo`(`a`, `b`) VALUES ('1', '2')",
   "DELETE FROM `test`.`foo` WHERE `a`='1' LIMIT 1",
   ],
   'Third row not there',
);

$ch->process_rows();

is_deeply(\@rows,
   [
   "INSERT INTO `test`.`foo`(`a`, `b`) VALUES ('1', '2')",
   "DELETE FROM `test`.`foo` WHERE `a`='1' LIMIT 1",
   "UPDATE `test`.`foo` SET `b`='2' WHERE `a`='1' LIMIT 1",
   ],
   'All rows',
);

is_deeply(
   { $ch->get_changes() },
   { REPLACE => 0, DELETE => 1, INSERT => 1, UPDATE => 1 },
   'Changes were recorded',
);


# #############################################################################
# Test that the optional dbh is passed through to our actions.
# #############################################################################
@rows = ();
@dbhs = ();
$ch->{queue} = 0;
# 42 is a placeholder for the dbh arg.
$ch->change('INSERT', { a => 1, b => 2 }, [qw(a)], 42);

is_deeply(
   \@dbhs,
   [42],
   'dbh passed through change()'
);

$ch->{queue} = 1;

@rows = ();
@dbhs = ();
$ch->change('INSERT', { a => 1, b => 2 }, [qw(a)], 42);

is_deeply(
   \@dbhs,
   [],
   'No dbh yet'
);

$ch->process_rows(1);

is_deeply(
   \@dbhs,
   [42],
   'dbh passed through process_rows()'
);


# #############################################################################
# Test switching direction (swap src/dst).
# #############################################################################
$ch = new ChangeHandler(
   Quoter    => $q,
   left_db   => 'test',
   left_tbl  => 'left_foo',
   right_db  => 'test',
   right_tbl => 'right_foo',
   actions   => [ sub { push @rows, $_[0]; push @dbhs, $_[1]; } ],
   replace   => 0,
   queue     => 0,
);

@rows = ();
@dbhs = ();

# Default is left=source.
$ch->set_src('right');
is(
   $ch->src,
   '`test`.`right_foo`',
   'Changed src',
);
is(
   $ch->dst,
   '`test`.`left_foo`',
   'Changed dst'
);

$ch->change('INSERT', { a => 1, b => 2 }, [qw(a)] );

is_deeply(
   \@rows,
   ["INSERT INTO `test`.`left_foo`(`a`, `b`) VALUES ('1', '2')",],
   'INSERT new dst',
);

$ch->change('DELETE', { a => 1, b => 2 }, [qw(a)] );
$ch->process_rows(1);
is_deeply(\@rows,
   [
   "INSERT INTO `test`.`left_foo`(`a`, `b`) VALUES ('1', '2')",
   "DELETE FROM `test`.`left_foo` WHERE `a`='1' LIMIT 1",
   ],
   'DELETE new dst',
);


# #############################################################################
# Test fetch_back().
# #############################################################################
SKIP: {
   skip 'Cannot connect to sandbox master', 1 unless $master_dbh;

   $master_dbh->do('DROP DATABASE IF EXISTS test');
   $master_dbh->do('CREATE DATABASE IF NOT EXISTS test');

   $ch = new ChangeHandler(
      Quoter    => $q,
      right_db  => 'test',  # dst
      right_tbl => 'foo',
      left_db   => 'test',  # src
      left_tbl  => 'test1',
      actions   => [ sub { push @rows, $_[0]; push @dbhs, $_[1]; } ],
      replace   => 0,
      queue     => 0,
   );

   @rows = ();
   $ch->{queue} = 0;
   $ch->fetch_back($master_dbh);
   `/tmp/12345/use < $trunk/t/lib/samples/before-TableSyncChunk.sql`;
   # This should cause it to fetch the row from test.test1 where a=1
   $ch->change('UPDATE', { a => 1, __foo => 'bar' }, [qw(a)] );
   $ch->change('DELETE', { a => 1, __foo => 'bar' }, [qw(a)] );
   # TODO: Sometimes this is retrieved as (b, a) instead of (a, b)
   #$ch->change('INSERT', { a => 1, __foo => 'bar' }, [qw(a)] );
   is_deeply(
      \@rows,
      [
         "UPDATE `test`.`foo` SET `b`='en' WHERE `a`='1' LIMIT 1",
         "DELETE FROM `test`.`foo` WHERE `a`='1' LIMIT 1",
  #      "INSERT INTO `test`.`foo`(`a`, `b`) VALUES ('1', 'en')",
      ],
      'Fetch-back',
   );
}

# #############################################################################
# Issue 371: Make mk-table-sync preserve column order in SQL
# #############################################################################
my $row = {
   id  => 1,
   foo => 'foo',
   bar => 'bar',
};
my $tbl_struct = {
   col_posn => { id=>0, foo=>1, bar=>2 },
};
$ch = new ChangeHandler(
   Quoter     => $q,
   right_db   => 'test',       # dst
   right_tbl  => 'issue_371',
   left_db    => 'test',       # src
   left_tbl   => 'issue_371',
   actions    => [ sub { push @rows, @_ } ],
   replace    => 0,
   queue      => 0,
   tbl_struct => $tbl_struct,
);

@rows = ();
@dbhs = ();

is(
   $ch->make_INSERT($row, [qw(id foo bar)]),
   "INSERT INTO `test`.`issue_371`(`id`, `foo`, `bar`) VALUES ('1', 'foo', 'bar')",
   'make_INSERT() preserves column order'
);

is(
   $ch->make_REPLACE($row, [qw(id foo bar)]),
   "REPLACE INTO `test`.`issue_371`(`id`, `foo`, `bar`) VALUES ('1', 'foo', 'bar')",
   'make_REPLACE() preserves column order'
);

is(
   $ch->make_UPDATE($row, [qw(id foo)]),
   "UPDATE `test`.`issue_371` SET `bar`='bar' WHERE `id`='1' AND `foo`='foo' LIMIT 1",
   'make_UPDATE() preserves column order'
);

is(
   $ch->make_DELETE($row, [qw(id foo bar)]),
   "DELETE FROM `test`.`issue_371` WHERE `id`='1' AND `foo`='foo' AND `bar`='bar' LIMIT 1",
   'make_DELETE() preserves column order'
);

# Test what happens if the row has a column that not in the tbl struct.
$row->{other_col} = 'zzz';

is(
   $ch->make_INSERT($row, [qw(id foo bar)]),
   "INSERT INTO `test`.`issue_371`(`id`, `foo`, `bar`, `other_col`) VALUES ('1', 'foo', 'bar', 'zzz')",
   'make_INSERT() preserves column order, with col not in tbl'
);

is(
   $ch->make_REPLACE($row, [qw(id foo bar)]),
   "REPLACE INTO `test`.`issue_371`(`id`, `foo`, `bar`, `other_col`) VALUES ('1', 'foo', 'bar', 'zzz')",
   'make_REPLACE() preserves column order, with col not in tbl'
);

is(
   $ch->make_UPDATE($row, [qw(id foo)]),
   "UPDATE `test`.`issue_371` SET `bar`='bar', `other_col`='zzz' WHERE `id`='1' AND `foo`='foo' LIMIT 1",
   'make_UPDATE() preserves column order, with col not in tbl'
);

delete $row->{other_col};

SKIP: {
   skip 'Cannot connect to sandbox master', 3 unless $master_dbh;

   $master_dbh->do('DROP TABLE IF EXISTS test.issue_371');
   $master_dbh->do('CREATE TABLE test.issue_371 (id INT, foo varchar(16), bar char)');
   $master_dbh->do("INSERT INTO test.issue_371 VALUES (1,'foo','a'),(2,'bar','b')");

   $ch->fetch_back($master_dbh);

   is(
      $ch->make_INSERT($row, [qw(id foo)]),
      "INSERT INTO `test`.`issue_371`(`id`, `foo`, `bar`) VALUES ('1', 'foo', 'a')",
      'make_INSERT() preserves column order, with fetch-back'
   );

   is(
      $ch->make_REPLACE($row, [qw(id foo)]),
      "REPLACE INTO `test`.`issue_371`(`id`, `foo`, `bar`) VALUES ('1', 'foo', 'a')",
      'make_REPLACE() preserves column order, with fetch-back'
   );

   is(
      $ch->make_UPDATE($row, [qw(id foo)]),
      "UPDATE `test`.`issue_371` SET `bar`='a' WHERE `id`='1' AND `foo`='foo' LIMIT 1",
      'make_UPDATE() preserves column order, with fetch-back'
   );
};

# #############################################################################
# Issue 641: Make mk-table-sync use hex for binary/blob data
# #############################################################################
$tbl_struct = {
   cols     => [qw(a x b)],
   type_for => {a=>'int', x=>'blob', b=>'varchar'},
};
$ch = new ChangeHandler(
   Quoter     => $q,
   left_db    => 'test',
   left_tbl   => 'lt',
   right_db   => 'test',
   right_tbl  => 'rt',
   actions    => [ sub {} ],
   replace    => 0,
   queue      => 0,
   tbl_struct => $tbl_struct,
);

is(
   $ch->make_fetch_back_query('1=1'),
   "SELECT `a`, IF(BINARY(`x`)='', '', CONCAT('0x', HEX(`x`))) AS `x`, `b` FROM `test`.`lt` WHERE 1=1 LIMIT 1",
   "Wraps BLOB column in CONCAT('0x', HEX(col)) AS col"
);

$ch = new ChangeHandler(
   Quoter     => $q,
   left_db    => 'test',
   left_tbl   => 'lt',
   right_db   => 'test',
   right_tbl  => 'rt',
   actions    => [ sub {} ],
   replace    => 0,
   queue      => 0,
   hex_blob   => 0,
   tbl_struct => $tbl_struct,
);

is(
   $ch->make_fetch_back_query('1=1'),
   "SELECT `a`, `x`, `b` FROM `test`.`lt` WHERE 1=1 LIMIT 1",
   "Disable blob hexing"
);

# #############################################################################
# Issue 1052: mk-table-sync inserts "0x" instead of "" for empty blob and text
# column values
# #############################################################################
$tbl_struct = {
   cols     => [qw(t)],
   type_for => {t=>'blob'},
};
$ch = new ChangeHandler(
   Quoter     => $q,
   left_db    => 'test',
   left_tbl   => 't',
   right_db   => 'test',
   right_tbl  => 't',
   actions    => [ sub {} ],
   replace    => 0,
   queue      => 0,
   tbl_struct => $tbl_struct,
);

is(
   $ch->make_fetch_back_query('1=1'),
   "SELECT IF(BINARY(`t`)='', '', CONCAT('0x', HEX(`t`))) AS `t` FROM `test`.`t` WHERE 1=1 LIMIT 1",
   "Don't prepend 0x to blank blob/text column value (issue 1052)"
);

# #############################################################################
# An update to the above bug; It should only hexify for blob and binary, not
# for text columns; The latter not only breaks for UTF-8 data, but also
# breaks now that hex-looking columns aren't automatically left unquoted.
# #############################################################################
$tbl_struct = {
   cols     => [qw(t)],
   type_for => {t=>'text'},
};
$ch = new ChangeHandler(
   Quoter     => $q,
   left_db    => 'test',
   left_tbl   => 't',
   right_db   => 'test',
   right_tbl  => 't',
   actions    => [ sub {} ],
   replace    => 0,
   queue      => 0,
   tbl_struct => $tbl_struct,
);

is(
   $ch->make_fetch_back_query('1=1'),
   "SELECT `t` FROM `test`.`t` WHERE 1=1 LIMIT 1",
   "Don't prepend 0x to blank blob/text column value (issue 1052)"
);

# #############################################################################

SKIP: {
   skip 'Cannot connect to sandbox master', 1 unless $master_dbh;
   $sb->load_file('master', "t/lib/samples/issue_641.sql");

   @rows = ();
   $tbl_struct = {
      cols     => [qw(id b)],
      col_posn => {id=>0, b=>1},
      type_for => {id=>'int', b=>'blob'},
   };
   $ch = new ChangeHandler(
      Quoter     => $q,
      left_db    => 'issue_641',
      left_tbl   => 'lt',
      right_db   => 'issue_641',
      right_tbl  => 'rt',
      actions   => [ sub { push @rows, $_[0]; } ],
      replace    => 0,
      queue      => 0,
      tbl_struct => $tbl_struct,
   );
   $ch->fetch_back($master_dbh);

   $ch->change('UPDATE', {id=>1}, [qw(id)] );
   $ch->change('INSERT', {id=>1}, [qw(id)] );

   is_deeply(
      \@rows,
      [
         "UPDATE `issue_641`.`rt` SET `b`=0x089504E470D0A1A0A0000000D4948445200000079000000750802000000E55AD965000000097048597300000EC300000EC301C76FA8640000200049444154789C4CBB7794246779FFBBF78F7B7EBE466177677772CE3D9D667AA67BA62776CE39545557CE3974EE9EB049AB9556392210414258083 WHERE `id`='1' LIMIT 1",
         "INSERT INTO `issue_641`.`rt`(`id`, `b`) VALUES ('1', 0x089504E470D0A1A0A0000000D4948445200000079000000750802000000E55AD965000000097048597300000EC300000EC301C76FA8640000200049444154789C4CBB7794246779FFBBF78F7B7EBE466177677772CE3D9D667AA67BA62776CE39545557CE3974EE9EB049AB9556392210414258083)",
      ],
      "UPDATE and INSERT binary data as hex"
   );
}

# #############################################################################
# Issue 387: More useful comments in mk-table-sync statements
# #############################################################################
@rows = ();
$ch = new ChangeHandler(
   Quoter    => $q,
   right_db  => 'test',  # dst
   right_tbl => 'foo',
   left_db   => 'test',  # src
   left_tbl  => 'test1',
   actions   => [ sub { push @rows, $_[0]; push @dbhs, $_[1]; } ],
   replace   => 0,
   queue     => 1,
);

$ch->change('INSERT', { a => 1, b => 2 }, [qw(a)] );
$ch->process_rows(1, "trace");

is_deeply(
   \@rows,
   ["INSERT INTO `test`.`foo`(`a`, `b`) VALUES ('1', '2') /*percona-toolkit trace*/",],
   "process_rows() appends trace msg to SQL statements"
);

# #############################################################################
# ChangeHandler doesn't quote varchar columns with hex-looking values
# https://bugs.launchpad.net/percona-toolkit/+bug/1038276
# #############################################################################
SKIP: {
   skip 'Cannot connect to sandbox master', 1 unless $master_dbh;
   $sb->load_file('master', "t/lib/samples/bug_1038276.sql");

   @rows = ();
   $tbl_struct = {
      cols      => [qw(id b)],
      col_posn  => {id=>0, b=>1},
      type_for  => {id=>'int', b=>'varchar'},
   };
   $ch = new ChangeHandler(
      Quoter     => $q,
      left_db    => 'bug_1038276',
      left_tbl   => 'lt',
      right_db   => 'bug_1038276',
      right_tbl  => 'rt',
      actions   => [ sub { push @rows, $_[0]; } ],
      replace    => 0,
      queue      => 0,
      tbl_struct => $tbl_struct,
   );
   $ch->fetch_back($master_dbh);

   $ch->change('UPDATE', {id=>1}, [qw(id)] );
   $ch->change('INSERT', {id=>1}, [qw(id)] );

   is_deeply(
      \@rows,
      [
         "UPDATE `bug_1038276`.`rt` SET `b`='0x89504E470D0A1A0A0000000D4948445200000079000000750802000000E55AD965000000097048597300000EC300000EC301C76FA8640000200049444154789C4CBB7794246779FFBBF78F7B7EBE466177677772CE3D9D667AA67BA62776CE39545557CE3974EE9EB049AB9556392210414258083' WHERE `id`='1' LIMIT 1",
         "INSERT INTO `bug_1038276`.`rt`(`id`, `b`) VALUES ('1', '0x89504E470D0A1A0A0000000D4948445200000079000000750802000000E55AD965000000097048597300000EC300000EC301C76FA8640000200049444154789C4CBB7794246779FFBBF78F7B7EBE466177677772CE3D9D667AA67BA62776CE39545557CE3974EE9EB049AB9556392210414258083')",
      ],
      "UPDATE and INSERT quote data regardless of how it looks if tbl_struct->quote_val is true"
   );
}

# #############################################################################
# Done.
# #############################################################################
$sb->wipe_clean($master_dbh);
$sb->wipe_clean($slave1_dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");

done_testing;