~percona-toolkit-dev/percona-toolkit/fix-change-master-bug-932614

« back to all changes in this revision

Viewing changes to t/lib/SchemaIterator.t

  • Committer: Daniel Nichter
  • Date: 2011-06-24 17:22:06 UTC
  • Revision ID: daniel@percona.com-20110624172206-c7q4s4ad6r260zz6
Add lib/, t/lib/, and sandbox/.  All modules are updated and passing on MySQL 5.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/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 tests => 29;
 
13
 
 
14
use SchemaIterator;
 
15
use FileIterator;
 
16
use Quoter;
 
17
use DSNParser;
 
18
use Sandbox;
 
19
use OptionParser;
 
20
use MySQLDump;
 
21
use TableParser;
 
22
use MaatkitTest;
 
23
 
 
24
use constant MKDEBUG => $ENV{MKDEBUG} || 0;
 
25
 
 
26
use Data::Dumper;
 
27
$Data::Dumper::Indent    = 1;
 
28
$Data::Dumper::Sortkeys  = 1;
 
29
$Data::Dumper::Quotekeys = 0;
 
30
 
 
31
my $q   = new Quoter();
 
32
my $dp  = new DSNParser(opts=>$dsn_opts);
 
33
my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
 
34
my $dbh = $sb->get_dbh_for('master');
 
35
 
 
36
my ($du, $tp);
 
37
my $fi = new FileIterator();
 
38
my $o  = new OptionParser(description => 'SchemaIterator');
 
39
$o->get_specs("$trunk/bin/pt-table-checksum");
 
40
 
 
41
my $in  = "$trunk/t/lib/samples/mysqldump-no-data/";
 
42
my $out = "t/lib/samples/SchemaIterator/";
 
43
 
 
44
sub test_so {
 
45
   my ( %args ) = @_;
 
46
   my @required_args = qw(test_name result);
 
47
   foreach my $arg ( @required_args ) {
 
48
      die "I need a $arg argument" unless defined $args{$arg};
 
49
   }
 
50
 
 
51
   @ARGV = $args{filters} ? @{$args{filters}} : ();
 
52
   $o->get_opts();
 
53
 
 
54
   my $si;
 
55
   if ( $args{files} ) {
 
56
      my $file_itr = $fi->get_file_itr(@{$args{files}});
 
57
      $si = new SchemaIterator(
 
58
         file_itr     => $file_itr,
 
59
         keep_ddl     => defined $args{keep_ddl} ? $args{keep_ddl} : 1,
 
60
         OptionParser => $o,
 
61
         Quoter       => $q,
 
62
         TableParser  => $tp,
 
63
      );
 
64
   }
 
65
   else {
 
66
      $si = new SchemaIterator(
 
67
         dbh          => $dbh,
 
68
         keep_ddl     => defined $args{keep_ddl} ? $args{keep_ddl} : 1,
 
69
         OptionParser => $o,
 
70
         Quoter       => $q,
 
71
         MySQLDump    => $du,
 
72
         TableParser  => $tp,
 
73
      );
 
74
   }
 
75
 
 
76
   # For result files, each db.tbl is printed on its own line
 
77
   # so diff works nicely.
 
78
   my $result_file = -f "$trunk/$args{result}";
 
79
 
 
80
   my $res = "";
 
81
   my @objs;
 
82
   while ( my $obj = $si->next_schema_object() ) {
 
83
      if ( $args{return_objs} ) {
 
84
         push @objs, $obj;
 
85
      }
 
86
      else {
 
87
         if ( $result_file || $args{ddl} ) {
 
88
            $res .= "$obj->{db}.$obj->{tbl}\n";
 
89
            $res .= "$obj->{ddl}\n\n" if $args{ddl} || $du;
 
90
         }
 
91
         else {
 
92
            $res .= "$obj->{db}.$obj->{tbl} ";
 
93
         }
 
94
      }
 
95
   }
 
96
 
 
97
   return \@objs if $args{return_objs};
 
98
 
 
99
   if ( $result_file ) {
 
100
      ok(
 
101
         no_diff(
 
102
            $res,
 
103
            $args{result},
 
104
            cmd_output    => 1,
 
105
            update_sample => $args{update_sample},
 
106
         ),
 
107
         $args{test_name},
 
108
      );
 
109
   }
 
110
   elsif ( $args{unlike} ) {
 
111
      unlike(
 
112
         $res,
 
113
         $args{unlike},
 
114
         $args{test_name},
 
115
      );
 
116
   }
 
117
   else {
 
118
      is(
 
119
         $res,
 
120
         $args{result},
 
121
         $args{test_name},
 
122
      );
 
123
   }
 
124
 
 
125
   return;
 
126
}
 
127
 
 
128
SKIP: {
 
129
   skip "Cannot connect to sandbox master", 22 unless $dbh;
 
130
   $sb->wipe_clean($dbh);
 
131
 
 
132
   # ########################################################################
 
133
   # Test simple, unfiltered get_db_itr().
 
134
   # ########################################################################
 
135
   test_so(
 
136
      result    => "$out/all-dbs-tbls.txt",
 
137
      test_name => "Iterate all schema objects with dbh",
 
138
   );
 
139
 
 
140
   # ########################################################################
 
141
   # Test filters.
 
142
   # ########################################################################
 
143
   $sb->load_file('master', "t/lib/samples/SchemaIterator.sql");
 
144
 
 
145
   test_so(
 
146
      filters   => [qw(-d this_db_does_not_exist)],
 
147
      result    => "",
 
148
      test_name => "No databases match",
 
149
   );
 
150
 
 
151
   test_so(
 
152
      filters   => [qw(-t this_table_does_not_exist)],
 
153
      result    => "",
 
154
      test_name => "No tables match",
 
155
   );
 
156
 
 
157
   # Filter by --databases (-d).
 
158
   test_so(
 
159
      filters   => [qw(--databases d1)],
 
160
      result    => "d1.t1 d1.t2 d1.t3 ",
 
161
      test_name => '--databases',
 
162
   ); 
 
163
 
 
164
   # Filter by --databases (-d) and --tables (-t).
 
165
   test_so(
 
166
      filters   => [qw(-d d1 -t t2)],
 
167
      result    => "d1.t2 ",
 
168
      test_name => '--databases and --tables',
 
169
   );
 
170
 
 
171
   # Ignore some dbs and tbls.
 
172
   test_so(
 
173
      filters   => ['--ignore-databases', 'mysql,sakila,d1,d3'],
 
174
      result    => "d2.t1 ",
 
175
      test_name => '--ignore-databases',
 
176
   );
 
177
 
 
178
   test_so(
 
179
      filters   => ['--ignore-databases', 'mysql,sakila,d2,d3',
 
180
                    '--ignore-tables', 't1,t2'],
 
181
      result    => "d1.t3 ",
 
182
      test_name => '--ignore-databases and --ignore-tables',
 
183
   );
 
184
 
 
185
   # Select some dbs but ignore some tables.
 
186
   test_so(
 
187
      filters   => ['-d', 'd1', '--ignore-tables', 't1,t3'],
 
188
      result    => "d1.t2 ",
 
189
      test_name => '--databases and --ignore-tables',
 
190
   );
 
191
 
 
192
   # Filter by engines.  This also tests that --engines is case-insensitive
 
193
   test_so(
 
194
      filters   => ['-d', 'd1,d2,d3', '--engines', 'INNODB'],
 
195
      result    => "d1.t2 ",
 
196
      test_name => '--engines',
 
197
   );
 
198
 
 
199
   test_so(
 
200
      filters   => ['-d', 'd1,d2,d3', '--ignore-engines', 'innodb,myisam'],
 
201
      result    => "d1.t3 ",
 
202
      test_name => '--ignore-engines',
 
203
   );
 
204
   
 
205
   # Filter by regex.
 
206
   test_so(
 
207
      filters   => ['--databases-regex', 'd[13]', '--tables-regex', 't[^3]'],
 
208
      result    => "d1.t1 d1.t2 ",
 
209
      test_name => '--databases-regex and --tables-regex',
 
210
   );
 
211
 
 
212
   test_so(
 
213
      filters   => ['--ignore-databases-regex', '(?:^d[23]|mysql|info|sakila)',
 
214
                    '--ignore-tables-regex', 't[^23]'],
 
215
      result    => "d1.t2 d1.t3 ",
 
216
      test_name => '--ignore-databases-regex',
 
217
   );
 
218
 
 
219
   # ########################################################################
 
220
   # Filter views.
 
221
   # ########################################################################
 
222
   SKIP: {
 
223
      skip 'Sandbox master does not have the sakila database', 1
 
224
         unless @{$dbh->selectcol_arrayref('SHOW DATABASES LIKE "sakila"')};
 
225
 
 
226
      test_so(
 
227
         filteres  => [qw(-d sakila)],
 
228
         result    => "",  # hack; uses unlike instead
 
229
         unlike    => qr/
 
230
             actor_info
 
231
            |customer_list
 
232
            |film_list
 
233
            |nicer_but_slower_film_list
 
234
            |sales_by_film_category
 
235
            |sales_by_store
 
236
            |staff_list/x,
 
237
         test_name => "Iterator does not return views",
 
238
      );
 
239
   };
 
240
 
 
241
   # ########################################################################
 
242
   # Issue 806: mk-table-sync --tables does not honor schema qualier
 
243
   # ########################################################################
 
244
   # Filter by db-qualified table.  There is t1 in both d1 and d2.
 
245
   # We want only d1.t1.
 
246
   test_so(
 
247
      filters   => [qw(-t d1.t1)],
 
248
      result    => "d1.t1 ",
 
249
      test_name => '-t d1.t1 (issue 806)',
 
250
   );
 
251
 
 
252
   test_so(
 
253
      filters   => [qw(-d d1 -t d1.t1)],
 
254
      result    => "d1.t1 ",
 
255
      test_name => '-d d1 -t d1.t1 (issue 806)',
 
256
   );
 
257
 
 
258
   test_so(
 
259
      filters   => [qw(-d d2 -t d1.t1)],
 
260
      result    => "",
 
261
      test_name => '-d d2 -t d1.t1 (issue 806)',
 
262
   );
 
263
 
 
264
   test_so(
 
265
      filters   => ['-t','d1.t1,d1.t3'],
 
266
      result    => "d1.t1 d1.t3 ",
 
267
      test_name => '-t d1.t1,d1.t3 (issue 806)',
 
268
   );
 
269
 
 
270
   test_so(
 
271
      filters   => ['--ignore-databases', 'mysql,sakila',
 
272
                    '--ignore-tables', 'd1.t1'],
 
273
      result    => "d1.t2 d1.t3 d2.t1 ",
 
274
      test_name => '--ignore-databases and --ignore-tables d1.t1 (issue 806)',
 
275
   );
 
276
 
 
277
   test_so(
 
278
      filters   => ['-t','d1.t3,d2.t1'],
 
279
      result    => "d1.t3 d2.t1 ",
 
280
      test_name => '-t d1.t3,d2.t1 (issue 806)',
 
281
   );
 
282
 
 
283
   # ########################################################################
 
284
   # Issue 1161: make_filter() with only --tables db.foo filter does not work
 
285
   # ########################################################################
 
286
   # mk-index-usage does not have any of the schema filters with default
 
287
   # values like --engines so when we do --tables that will be the only
 
288
   # filter.
 
289
   $o = new OptionParser(description => 'SchemaIterator');
 
290
   $o->get_specs("$trunk/bin/pt-index-usage");
 
291
 
 
292
   test_so(
 
293
      filters   => [qw(-t d1.t1)],
 
294
      result    => "d1.t1 ",
 
295
      test_name => '-t d1.t1 (issue 1161)',
 
296
   );
 
297
 
 
298
   # ########################################################################
 
299
   # Issue 1193: Make SchemaIterator skip PERFORMANCE_SCHEMA
 
300
   # ########################################################################
 
301
   SKIP: {
 
302
      skip "Test for MySQL v5.5", 1 unless $sandbox_version ge '5.5';
 
303
 
 
304
      test_so(
 
305
         result    => "", # hack, uses unlike instead
 
306
         unlike    => qr/^performance_schema/,
 
307
         test_name => "performance_schema automatically ignored",
 
308
      );
 
309
   }
 
310
 
 
311
   # ########################################################################
 
312
   # Getting CREATE TALBE (ddl).
 
313
   # ########################################################################
 
314
   $du = new MySQLDump();
 
315
   test_so(
 
316
      filters   => [qw(-t mysql.user)],
 
317
      result    => "$out/mysql-user-ddl.txt",
 
318
      test_name => "Get CREATE TABLE with dbh",
 
319
   );
 
320
 
 
321
   # Kill the MySQLDump obj in case the next tests don't want to use it.
 
322
   $du = undef;
 
323
 
 
324
   $sb->wipe_clean($dbh);
 
325
};
 
326
 
 
327
# ############################################################################
 
328
# Test getting schema from mysqldump files.
 
329
# ############################################################################
 
330
 
 
331
test_so(
 
332
   files     => ["$in/dump001.txt"],
 
333
   result    => "test.a test.b test2.a ",
 
334
   test_name => "Iterate schema in dump001.txt",
 
335
);
 
336
 
 
337
test_so(
 
338
   files     => ["$in/all-dbs.txt"],
 
339
   result    => "$out/all-dbs.txt",
 
340
   ddl       => 1,
 
341
   test_name => "Iterate schema in all-dbs.txt",
 
342
);
 
343
 
 
344
test_so(
 
345
   files     => ["$in/dump001.txt", "$in/dump001.txt"],
 
346
   result    => "$out/dump001-twice.txt",
 
347
   ddl       => 1,
 
348
   test_name => "Iterate schema in multiple files",
 
349
);
 
350
 
 
351
test_so(
 
352
   files     => ["$in/dump001.txt"],
 
353
   filters   => [qw(--databases TEST2)],
 
354
   result    => "test2.a ",
 
355
   test_name => "Filter dump file by --databases",
 
356
);
 
357
 
 
358
# ############################################################################
 
359
# Getting tbl_struct.
 
360
# ############################################################################
 
361
my $objs = test_so(
 
362
   files     => ["$in/dump001.txt"],
 
363
   result      => "",  # hack to let return_objs work
 
364
   test_name   => "",  # hack to let return_objs work
 
365
   return_objs => 1,
 
366
);
 
367
 
 
368
my $n_tbl_structs = grep { exists $_->{tbl_struct} } @$objs;
 
369
 
 
370
is(
 
371
   $n_tbl_structs,
 
372
   0,
 
373
   'No tbl_struct without TableParser'
 
374
);
 
375
 
 
376
$tp = new TableParser(Quoter => $q);
 
377
 
 
378
$objs = test_so(
 
379
   files     => ["$in/dump001.txt"],
 
380
   result      => "",  # hack to let return_objs work
 
381
   test_name   => "",  # hack to let return_objs work
 
382
   return_objs => 1,
 
383
);
 
384
 
 
385
$n_tbl_structs = grep { exists $_->{tbl_struct} } @$objs;
 
386
 
 
387
is(
 
388
   $n_tbl_structs,
 
389
   scalar @$objs,
 
390
   'Got tbl_struct for each schema object'
 
391
);
 
392
 
 
393
# Kill the TableParser obj in case the next tests don't want to use it.
 
394
$tp = undef;
 
395
 
 
396
# ############################################################################
 
397
# keep_ddl
 
398
# ############################################################################
 
399
$objs = test_so(
 
400
   files       => ["$in/dump001.txt"],
 
401
   result      => "",  # hack to let return_objs work
 
402
   test_name   => "",  # hack to let return_objs work
 
403
   return_objs => 1,
 
404
   keep_ddl    => 0,
 
405
);
 
406
 
 
407
my $n_ddls = grep { exists $_->{ddl} } @$objs;
 
408
 
 
409
is(
 
410
   $n_ddls,
 
411
   0,
 
412
   'DDL deleted unless keep_ddl'
 
413
);
 
414
 
 
415
# #############################################################################
 
416
# Done.
 
417
# #############################################################################
 
418
exit;