File: SchemaIterator.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 (627 lines) | stat: -rw-r--r-- 19,290 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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#!/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 tests => 42;

use SchemaIterator;
use FileIterator;
use Quoter;
use DSNParser;
use Sandbox;
use OptionParser;
use TableParser;
use PerconaTest;

use constant PTDEBUG => $ENV{PTDEBUG} || 0;

use Data::Dumper;
$Data::Dumper::Indent    = 1;
$Data::Dumper::Sortkeys  = 1;
$Data::Dumper::Quotekeys = 0;

my $q   = new Quoter();
my $dp  = new DSNParser(opts=>$dsn_opts);
my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master');

my $tp = new TableParser(Quoter => $q);
my $fi = new FileIterator();
my $o  = new OptionParser(description => 'SchemaIterator');
$o->get_specs("$trunk/bin/pt-table-checksum");

my $in  = "$trunk/t/lib/samples/mysqldump-no-data/";
my $out = "t/lib/samples/SchemaIterator/";

sub test_so {
   my ( %args ) = @_;
   my @required_args = qw(test_name result);
   foreach my $arg ( @required_args ) {
      die "I need a $arg argument" unless defined $args{$arg};
   }

   @ARGV = $args{filters} ? @{$args{filters}} : ();
   $o->get_opts();

   my $si;
   if ( $args{files} ) {
      my $file_itr = $fi->get_file_itr(@{$args{files}});
      $si = new SchemaIterator(
         file_itr     => $file_itr,
         resume       => $args{resume},
         OptionParser => $o,
         Quoter       => $q,
         TableParser  => $tp,
      );
   }
   else {
      $si = new SchemaIterator(
         dbh          => $dbh,
         resume       => $args{resume},
         OptionParser => $o,
         Quoter       => $q,
         TableParser  => $tp,
      );
   }

   # For result files, each db.tbl is printed on its own line
   # so diff works nicely.
   my $result_file = -f "$trunk/$args{result}";

   my $res = "";
   my @objs;
   eval {
      while ( my $obj = $si->next() ) {
         if ( $args{return_objs} ) {
            push @objs, $obj;
         }
         else {
            if ( $result_file || $args{ddl} ) {
               $res .= "$obj->{db}.$obj->{tbl}\n";
               $res .= "$obj->{ddl}\n\n" if $args{ddl} || $tp;
            }
            else {
               $res .= "$obj->{db}.$obj->{tbl} ";
            }
         }
      }
   };
   
   return \@objs if $args{return_objs};

   if ( $result_file ) {
      my $transform = sub { print sort_query_output(slurp_file(shift)) };
      if ($sandbox_version gt '5.7') {
         $transform = sub { print sort_query_output(fix_auto_increment(slurp_file(shift))) };
      }
      ok(
         no_diff(
            $res,
            $args{result},
            cmd_output => 1,
            transform_result => $transform,
            transform_sample => $transform,
            update_sample => 1,
         ),
         $args{test_name},
      );
   }
   elsif ( $args{like} ) {
      like(
         $res,
         $args{like},
         $args{test_name},
      );
   }
   elsif ( $args{unlike} ) {
      unlike(
         $res,
         $args{unlike},
         $args{test_name},
      );
   }
   elsif ( $args{lives_ok} ) {
      is($EVAL_ERROR, '', $args{test_name});
   }
   else {
      is(
         $res,
         $args{result},
         $args{test_name},
      );
   }

   return;
}

sub sort_query_output {
   my $queries = shift;
   my @queries = split /\n\n/, $queries;
   
   my $sorted;
   for my $query (@queries) {
      $sorted .= join "\n", sort map { my $c = $_; $c =~ s/,$//; $c } split /\n/, $query;
   }
   return $sorted;
}

# Starting with MySQL 8, AUTO_INCREMENT value is maintained between sessions.
# Since we cannot predict the value of the test environment for each table and since its value
# is not important for these tests, we just replace it by 0
sub fix_auto_increment {
   my $in = shift;
   $in =~ s/AUTO_INCREMENT=\d+/AUTO_INCREMENT=0/g;
   return $in;
}

SKIP: {
   skip "Cannot connect to sandbox master", 22 unless $dbh;
   $sb->wipe_clean($dbh);

   # ########################################################################
   # Test simple, unfiltered get_db_itr().
   # ########################################################################
   test_so(
      result    => "$out/all-dbs-tbls-$sandbox_version.txt",
      test_name => "Iterate all schema objects with dbh",
   );

   # ########################################################################
   # Test filters.
   # ########################################################################
   $sb->load_file('master', "t/lib/samples/SchemaIterator.sql");
   
   test_so(
      filters   => [qw(-d this_db_does_not_exist)],
      result    => "",
      test_name => "No databases match",
   );
   
   test_so(
      filters   => [qw(-t this_table_does_not_exist)],
      result    => "",
      test_name => "No tables match",
   );
   
   # Filter by --databases (-d).
   test_so(
      filters   => [qw(--databases d1)],
      result    => "d1.t1 d1.t2 d1.t3 ",
      test_name => '--databases',
   ); 
   
   # Filter by --databases (-d) and --tables (-t).
   test_so(
      filters   => [qw(-d d1 -t t2)],
      result    => "d1.t2 ",
      test_name => '--databases and --tables',
   );
   
   # Ignore some dbs and tbls.
   test_so(
      filters   => ['--ignore-databases', 'mysql,sakila,d1,d3,percona_test,sys'],
      result    => "d2.t1 ",
      test_name => '--ignore-databases',
   );
   
   test_so(
      filters   => ['--ignore-databases', 'mysql,sakila,d2,d3,percona_test,sys',
                    '--ignore-tables', 't1,t2'],
      result    => "d1.t3 ",
      test_name => '--ignore-databases and --ignore-tables',
   );
   
   # Select some dbs but ignore some tables.
   test_so(
      filters   => ['-d', 'd1', '--ignore-tables', 't1,t3'],
      result    => "d1.t2 ",
      test_name => '--databases and --ignore-tables',
   );
   
   # Filter by engines.  This also tests that --engines is case-insensitive
   test_so(
      filters   => ['-d', 'd1,d2,d3', '--engines', 'INNODB'],
      result    => ($sandbox_version ge '5.5' ? 'd1.t2 d2.t1 ' : "d1.t2 "),
      test_name => '--engines',
   );
   
   test_so(
      filters   => ['-d', 'd1,d2,d3', '--ignore-engines', 'innodb,myisam'],
      result    => "d1.t3 ",
      test_name => '--ignore-engines',
   );
   
   # Filter by regex.
   test_so(
      filters   => ['--databases-regex', 'd[13]', '--tables-regex', 't[^3]'],
      result    => "d1.t1 d1.t2 ",
      test_name => '--databases-regex and --tables-regex',
   );
   
   test_so(
      filters   => ['--ignore-databases-regex', '(?:^d[23]|mysql|info|sakila|percona_test|sys)',
                    '--ignore-tables-regex', 't[^23]'],
      result    => "d1.t2 d1.t3 ",
      test_name => '--ignore-databases-regex',
   );
   
   # ########################################################################
   # Filter views.
   # ########################################################################
   SKIP: {
      skip 'Sandbox master does not have the sakila database', 1
         unless @{$dbh->selectcol_arrayref("SHOW DATABASES LIKE 'sakila'")};
   
      test_so(
         filters   => [qw(-d sakila)],
         result    => "",  # hack; uses unlike instead
         unlike    => qr/
             actor_info
            |customer_list
            |film_list
            |nicer_but_slower_film_list
            |sales_by_film_category
            |sales_by_store
            |staff_list/x,
         test_name => "Iterator does not return views",
      );
   };
   
   # ########################################################################
   # Issue 806: mk-table-sync --tables does not honor schema qualier
   # ########################################################################
   # Filter by db-qualified table.  There is t1 in both d1 and d2.
   # We want only d1.t1.
   
   test_so(
      filters   => [qw(-t d1.t1)],
      result    => "d1.t1 ",
      test_name => '-t d1.t1 (issue 806)',
   );
   
   test_so(
      filters   => [qw(-d d1 -t d1.t1)],
      result    => "d1.t1 ",
      test_name => '-d d1 -t d1.t1 (issue 806)',
   );
   
   test_so(
      filters   => [qw(-d d2 -t d1.t1)],
      result    => "",
      test_name => '-d d2 -t d1.t1 (issue 806)',
   );
   
   test_so(
      filters   => ['-t','d1.t1,d1.t3'],
      result    => "d1.t1 d1.t3 ",
      test_name => '-t d1.t1,d1.t3 (issue 806)',
   );
   
   my $want = $sandbox_version le '5.6' ? "d1.t2 d1.t3 d2.t1 " : 'd1.t2 d1.t3 d2.t1 sys.sys_config ';
   test_so(
      filters   => ['--ignore-databases', 'mysql,sakila,percona_test',
                    '--ignore-tables', 'd1.t1'],
      result    => $want,
      test_name => '--ignore-databases and --ignore-tables d1.t1 (issue 806)',
   );
   
   test_so(
      filters   => ['-t','d1.t3,d2.t1'],
      result    => "d1.t3 d2.t1 ",
      test_name => '-t d1.t3,d2.t1 (issue 806)',
   );
   
   # ########################################################################
   # Issue 1161: make_filter() with only --tables db.foo filter does not work
   # ########################################################################
   # mk-index-usage does not have any of the schema filters with default
   # values like --engines so when we do --tables that will be the only
   # filter.
   $o = new OptionParser(description => 'SchemaIterator');
   $o->get_specs("$trunk/bin/pt-index-usage");
   
   test_so(
      filters   => [qw(-t d1.t1)],
      result    => "d1.t1 ",
      test_name => '-t d1.t1 (issue 1161)',
   );
   
   # ########################################################################
   # Issue 1193: Make SchemaIterator skip PERFORMANCE_SCHEMA
   # ########################################################################
   SKIP: {
      skip "Test for MySQL v5.5", 1 unless $sandbox_version ge '5.5';
   
      test_so(
         result    => "", # hack, uses unlike instead
         unlike    => qr/^performance_schema/,
         test_name => "performance_schema automatically ignored",
      );
   }
   
   # ########################################################################
   # Getting CREATE TALBE (ddl).
   # ########################################################################
   test_so(
      filters   => [qw(-t mysql.user)],
      result    => "$out/mysql-user-ddl-$sandbox_version.txt",
      test_name => "Get CREATE TABLE with dbh",
   );
   
   $sb->wipe_clean($dbh);
};

# ############################################################################
# Test getting schema from mysqldump files.
# ############################################################################

test_so(
   files     => ["$in/dump001.txt"],
   result    => "test.a test.b test2.a ",
   test_name => "Iterate schema in dump001.txt",
);

test_so(
   files     => ["$in/all-dbs.txt"],
   result    => "$out/all-dbs.txt",
   ddl       => 1,
   test_name => "Iterate schema in all-dbs.txt",
);

test_so(
   files     => ["$in/dump001.txt", "$in/dump001.txt"],
   result    => "$out/multiple-files.txt",
   ddl       => 1,
   test_name => "Iterate schema in multiple files",
);

test_so(
   files     => ["$in/dump001.txt"],
   filters   => [qw(--databases TEST2)],
   result    => "$out/all-dbs-dump001.txt",
   test_name => "Filter dump file by --databases",
);

# ############################################################################
# Getting tbl_struct.
# ############################################################################
my $objs = test_so(
   files     => ["$in/dump001.txt"],
   result      => "",  # hack to let return_objs work
   test_name   => "",  # hack to let return_objs work
   return_objs => 1,
);

my $n_tbl_structs = grep { exists $_->{tbl_struct} } @$objs;

is(
   $n_tbl_structs,
   scalar @$objs,
   'Got tbl_struct for each schema object'
);

# ############################################################################
# Resume
# ############################################################################
test_so(
   filters   => [qw(-d sakila)],
   result    => $sandbox_version ge '5.1'
                ? "$out/resume-from-sakila-payment.txt"
                : "$out/resume-from-sakila-payment-5.0.txt",
   resume    => 'sakila.payment',
   test_name => "Resume"
);

# Ignore the table being resumed from; resume from next table.
test_so(
   filters   => [qw(-d sakila --ignore-tables sakila.payment)],
   result    => $sandbox_version ge '5.1'
                ? "$out/resume-from-ignored-sakila-payment.txt"
                : "$out/resume-from-ignored-sakila-payment-5.0.txt",
   resume    => 'sakila.payment',
   test_name => "Resume from ignored table"
);

# ############################################################################
# pt-table-checksum v2 fails when --resume + --ignore-database is used
# https://bugs.launchpad.net/percona-toolkit/+bug/911385
# ############################################################################

test_so(
   filters   => ['--ignore-databases', 'sakila,mysql,sys'],
   result    => "",
   lives_ok  => 1,
   resume    => 'sakila.payment',
   test_name => "Bug 911385: ptc works with --resume + --ignore-database"
);

$dbh->do("CREATE DATABASE zakila");
$dbh->do("CREATE TABLE zakila.bug_911385 (i int)");
test_so(
   filters   => ['--ignore-databases', 'sakila,mysql,sys'],
   result    => "zakila.bug_911385 ",
   resume    => 'sakila.payment',
   test_name => "Bug 911385: ...and continues to the next db"
);
$dbh->do("DROP DATABASE zakila");

test_so(
   filters   => ['--ignore-tables-regex', 'payment', '--ignore-databases', 'mysql,sys'],
   result    => "",
   lives_ok  => 1,
   resume    => 'sakila.payment',
   test_name => "Bug 911385: ptc works with --resume + --ignore-tables-regex"
);

test_so(
   filters   => ['--ignore-tables-regex', 'payment', '--ignore-databases', 'mysql,sys'],
   result    => "sakila.rental sakila.staff sakila.store ",
   resume    => 'sakila.payment',
   test_name => "Bug 911385: ...and continues to the next table"
);

# #############################################################################
# Bug 1047335: pt-duplicate-key-checker fails when it encounters a crashed table
# https://bugs.launchpad.net/percona-toolkit/+bug/1047335
# #############################################################################


SKIP: {
   skip "No /dev/urandom, can't corrupt the database", 2
      unless -e q{/dev/urandom};

   skip "Cannot test on MySQL > 5.7 since there are no .frm files", 2 if $sandbox_version gt '5.7';

   my $master3_port   = 2900;
   my $master_basedir = "/tmp/$master3_port";
   diag(`$trunk/sandbox/stop-sandbox $master3_port >/dev/null`);
   diag(`$trunk/sandbox/start-sandbox master $master3_port >/dev/null`);
   my $dbh3 = $sb->get_dbh_for("master3");

   $sb->load_file('master3', "t/lib/samples/bug_1047335_crashed_table.sql");

   # Create the SI object before crashing the table
   my $tmp_si = new SchemaIterator(
            dbh          => $dbh3,
            OptionParser => $o,
            Quoter       => $q,
            TableParser  => $tp,
            # This is needed because the way we corrupt tables
            # accidentally removes the database from SHOW DATABASES
            db           => 'bug_1047335',
         );

   my $db_dir = "$master_basedir/data/bug_1047335";
   my $myi    = glob("$db_dir/crashed_table.[Mm][Yy][Iy]");
   my $frm    = glob("$db_dir/crashed_table.[Ff][Rr][Mm]");

   die "Cannot find .myi file for crashed_table" unless $myi && -f $myi;


   # Truncate the .myi file to corrupt it
   truncate($myi, 4096);

   use File::Slurp qw( write_file );

   # Corrupt the .frm file
   open my $urand_fh, q{<}, "/dev/urandom"
      or die "Cannot open /dev/urandom";
   write_file($frm, scalar(<$urand_fh>), slurp_file($frm), scalar(<$urand_fh>));
   close $urand_fh;

   $dbh3->do("FLUSH TABLES");
   eval { $dbh3->do("SELECT etc FROM bug_1047335.crashed_table WHERE etc LIKE '10001' ORDER BY id ASC LIMIT 1") };

   my $w = '';
   {
      local $SIG{__WARN__} = sub { $w .= shift };
      1 while $tmp_si->next();
   }

   like(
      $w,
      qr/bug_1047335.crashed_table because SHOW CREATE TABLE failed:/,
      "->next() gives a warning if ->get_create_table dies from a strange error",
   );


   $dbh3->do(q{DROP DATABASE IF EXISTS bug_1047335_2});
   $dbh3->do(q{CREATE DATABASE bug_1047335_2});
   
   my $broken_frm = "$trunk/t/lib/samples/broken_tbl.frm";
   my $db_dir_2   = "$master_basedir/data/bug_1047335_2";
   
   diag(`cp $broken_frm $db_dir_2 2>&1`);
   
   $dbh3->do("FLUSH TABLES");
   
   my $tmp_si2 = new SchemaIterator(
            dbh          => $dbh3,
            OptionParser => $o,
            Quoter       => $q,
            TableParser  => $tp,
            # This is needed because the way we corrupt tables
            # accidentally removes the database from SHOW DATABASES
            db           => 'bug_1047335_2',
         );
   
   $w = '';
   {
      local $SIG{__WARN__} = sub { $w .= shift };
      1 while $tmp_si2->next();
   }
   
   like(
      $w,
      qr/\QSkipping bug_1047335_2.broken_tbl because SHOW CREATE TABLE failed:/,
      "...same as above, but using t/lib/samples/broken_tbl.frm",
   );
   
   # This might fail. Doesn't matter -- stop_sandbox will just rm -rf the folder
   eval {
      $dbh3->do("DROP DATABASE IF EXISTS bug_1047335");
      $dbh3->do("DROP DATABASE IF EXISTS bug_1047335_2");
   };
   
   diag(`$trunk/sandbox/stop-sandbox $master3_port >/dev/null`);
}

# #############################################################################
# Bug 1136559: Deep recursion on subroutine "SchemaIterator::_iterate_dbh"
# #############################################################################

$sb->wipe_clean($dbh);
diag(`/tmp/12345/use < $trunk/t/lib/samples/100-dbs.sql`);

test_so(
   filters   => [],
   result    => "foo001.bar001 ",
   lives_ok  => 1,
   test_name => "Bug 1136559: Deep recursion on subroutine SchemaIterator::_iterate_dbh",
);

diag(`/tmp/12345/use < $trunk/t/lib/samples/100-dbs-drop.sql`);


# #############################################################################
# https://bugs.launchpad.net/percona-toolkit/+bug/1304062 
# #############################################################################

$sb->load_file('master', "t/lib/samples/SchemaIterator.sql");
$dbh->do("CREATE TABLE d3.t1 (id int auto_increment primary key, c  char(8))");

test_so(
   filters   => ['--ignore-tables', 'd1.t1,d2.t1'],
   like    => qr/^d1.t2 d1.t3 d3.t1 mysql/,
   result => "",
   test_name => '--ignore-tables (bug 1304062)',
);

my $si = new SchemaIterator(
   dbh          => $dbh,
   OptionParser => $o,
   Quoter       => $q,
   TableParser  => $tp,
);
for my $db (qw( information_schema performance_schema lost+found percona_schema )) {
   is(
      $si->database_is_allowed($db),
      0,
      "database is allowed: $db",
   );
}

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