~percona-toolkit-dev/percona-toolkit/pt-agent

« back to all changes in this revision

Viewing changes to t/lib/CopyRowsInsertSelect.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 MAATKIT_WORKING_COPY environment variable is not set.  See http://code.google.com/p/maatkit/wiki/Testing"
 
5
      unless $ENV{MAATKIT_WORKING_COPY} && -d $ENV{MAATKIT_WORKING_COPY};
 
6
   unshift @INC, "$ENV{MAATKIT_WORKING_COPY}/common";
 
7
};
 
8
 
 
9
use strict;
 
10
use warnings FATAL => 'all';
 
11
use English qw(-no_match_vars);
 
12
use Test::More;
 
13
 
 
14
use DSNParser;
 
15
use Sandbox;
 
16
use MaatkitTest;
 
17
use Progress;
 
18
use Transformers;
 
19
use Retry;
 
20
use CopyRowsInsertSelect;
 
21
 
 
22
Transformers->import(qw(secs_to_time));
 
23
 
 
24
use Data::Dumper;
 
25
$Data::Dumper::Indent    = 1;
 
26
$Data::Dumper::Sortkeys  = 1;
 
27
$Data::Dumper::Quotekeys = 0;
 
28
 
 
29
my $dp  = new DSNParser(opts=>$dsn_opts);
 
30
my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
 
31
my $dbh = $sb->get_dbh_for('master');
 
32
 
 
33
if ( !$dbh ) {
 
34
   plan skip_all => 'Cannot connect to MySQL';
 
35
  
 
36
}
 
37
elsif ( !@{$dbh->selectcol_arrayref('SHOW DATABASES LIKE "sakila"')} ) {
 
38
   plan skip_all => "Sandbox master does not have the sakila database";
 
39
}
 
40
else {
 
41
   plan tests => 8;
 
42
}
 
43
 
 
44
my $rr     = new Retry();
 
45
my $osc    = new CopyRowsInsertSelect(Retry => $rr);
 
46
my $msg    = sub { print "$_[0]\n"; };
 
47
my $output = "";
 
48
 
 
49
$sb->load_file("master", "common/t/samples/osc/tbl001.sql");
 
50
$dbh->do("USE osc");
 
51
 
 
52
$osc->copy(
 
53
   dbh        => $dbh,
 
54
   from_table => 'osc.t',
 
55
   to_table   => 'osc.__new_t',
 
56
   columns    => [qw(id c)],
 
57
   chunks     => ['1=1'],
 
58
   msg        => $msg,
 
59
);
 
60
 
 
61
my $rows = $dbh->selectall_arrayref("select id, c from __new_t order by id");
 
62
is_deeply(
 
63
   $rows,
 
64
   [ [1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e'], ],
 
65
   "One chunk copy"
 
66
) or print Dumper($rows);
 
67
 
 
68
 
 
69
$dbh->do("truncate table osc.__new_t");
 
70
$output = output( sub {
 
71
   $osc->copy(
 
72
      dbh          => $dbh,
 
73
      from_table   => 'osc.t',
 
74
      to_table     => 'osc.__new_t',
 
75
      columns      => [qw(id c)],
 
76
      chunks       => ['id < 4', 'id >= 4 AND id < 6'],
 
77
      msg          => $msg,
 
78
      print        => 1,
 
79
      engine_flags => 'LOCK IN SHARE MODE',
 
80
   );
 
81
});
 
82
 
 
83
ok(
 
84
   no_diff(
 
85
      $output,
 
86
      "common/t/samples/osc/copyins001.txt",
 
87
      cmd_output => 1,
 
88
   ),
 
89
   "Prints 2 SQL statments for the 2 chunks"
 
90
);
 
91
 
 
92
$rows = $dbh->selectall_arrayref("select id, c from __new_t order by id");
 
93
is_deeply(
 
94
   $rows,
 
95
   [],
 
96
   "Doesn't exec those statements if print is true"
 
97
);
 
98
 
 
99
$dbh->do('create table osc.city like sakila.city');
 
100
$dbh->do('alter table osc.city engine=myisam');
 
101
my $chunks = [
 
102
   "`city_id` <  '71'",
 
103
   "`city_id` >= '71'  AND `city_id` < '141'",
 
104
   "`city_id` >= '141' AND `city_id` < '211'",
 
105
   "`city_id` >= '211' AND `city_id` < '281'",
 
106
   "`city_id` >= '281' AND `city_id` < '351'",
 
107
   "`city_id` >= '351' AND `city_id` < '421'",
 
108
   "`city_id` >= '421' AND `city_id` < '491'",
 
109
   "`city_id` >= '491' AND `city_id` < '561'",
 
110
   "`city_id` >= '561'",
 
111
];
 
112
my $pr = new Progress(
 
113
   jobsize => scalar @$chunks,
 
114
   spec    => [qw(percentage 10)],
 
115
   name    => "Copy rows"
 
116
);
 
117
 
 
118
$output = output(
 
119
   sub { $osc->copy(
 
120
      dbh        => $dbh,
 
121
      from_table => 'sakila.city',
 
122
      to_table   => 'osc.city',
 
123
      columns    => [qw(city_id city country_id last_update)],
 
124
      chunks     => $chunks,
 
125
      msg        => $msg,
 
126
      Progress   => $pr,
 
127
   ); },
 
128
   stderr => 1,
 
129
);
 
130
$rows = $dbh->selectall_arrayref("select count(city_id) from osc.city");
 
131
is_deeply(
 
132
   $rows,
 
133
   [[600]],
 
134
   "Copied all 600 sakila.city rows"
 
135
) or print Dumper($rows);
 
136
 
 
137
like(
 
138
   $output,
 
139
   qr/Copy rows:\s+100% 00:00 remain/,
 
140
   "Reports copy progress if Progress obj given"
 
141
);
 
142
 
 
143
 
 
144
my $sleep_cnt = 0;
 
145
 
 
146
$dbh->do("truncate table osc.__new_t");
 
147
output( sub {
 
148
   $osc->copy(
 
149
      dbh        => $dbh,
 
150
      from_table => 'osc.t',
 
151
      to_table   => 'osc.__new_t',
 
152
      columns    => [qw(id c)],
 
153
      chunks     => ['id < 4', 'id >= 4 AND id < 6'],
 
154
      msg        => $msg,
 
155
      sleep      => sub { $sleep_cnt++; },
 
156
   );
 
157
});
 
158
is(
 
159
   $sleep_cnt,
 
160
   1,
 
161
   "Calls sleep callback after each chunk (except last chunk)"
 
162
);
 
163
 
 
164
eval {
 
165
   $output = output(sub { $osc->cleanup(); } );
 
166
};
 
167
ok(
 
168
   !$EVAL_ERROR && !$output,
 
169
   "cleanup() works but doesn't do anything"
 
170
);
 
171
 
 
172
# #############################################################################
 
173
# Done.
 
174
# #############################################################################
 
175
$output = '';
 
176
{
 
177
   local *STDERR;
 
178
   open STDERR, '>', \$output;
 
179
   $osc->_d('Complete test coverage');
 
180
}
 
181
like(
 
182
   $output,
 
183
   qr/Complete test coverage/,
 
184
   '_d() works'
 
185
);
 
186
$sb->wipe_clean($dbh);
 
187
exit;