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";
10
use warnings FATAL => 'all';
11
use English qw(-no_match_vars);
20
use CopyRowsInsertSelect;
22
Transformers->import(qw(secs_to_time));
25
$Data::Dumper::Indent = 1;
26
$Data::Dumper::Sortkeys = 1;
27
$Data::Dumper::Quotekeys = 0;
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');
34
plan skip_all => 'Cannot connect to MySQL';
37
elsif ( !@{$dbh->selectcol_arrayref('SHOW DATABASES LIKE "sakila"')} ) {
38
plan skip_all => "Sandbox master does not have the sakila database";
45
my $osc = new CopyRowsInsertSelect(Retry => $rr);
46
my $msg = sub { print "$_[0]\n"; };
49
$sb->load_file("master", "common/t/samples/osc/tbl001.sql");
54
from_table => 'osc.t',
55
to_table => 'osc.__new_t',
56
columns => [qw(id c)],
61
my $rows = $dbh->selectall_arrayref("select id, c from __new_t order by id");
64
[ [1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e'], ],
66
) or print Dumper($rows);
69
$dbh->do("truncate table osc.__new_t");
70
$output = output( sub {
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'],
79
engine_flags => 'LOCK IN SHARE MODE',
86
"common/t/samples/osc/copyins001.txt",
89
"Prints 2 SQL statments for the 2 chunks"
92
$rows = $dbh->selectall_arrayref("select id, c from __new_t order by id");
96
"Doesn't exec those statements if print is true"
99
$dbh->do('create table osc.city like sakila.city');
100
$dbh->do('alter table osc.city engine=myisam');
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'",
112
my $pr = new Progress(
113
jobsize => scalar @$chunks,
114
spec => [qw(percentage 10)],
121
from_table => 'sakila.city',
122
to_table => 'osc.city',
123
columns => [qw(city_id city country_id last_update)],
130
$rows = $dbh->selectall_arrayref("select count(city_id) from osc.city");
134
"Copied all 600 sakila.city rows"
135
) or print Dumper($rows);
139
qr/Copy rows:\s+100% 00:00 remain/,
140
"Reports copy progress if Progress obj given"
146
$dbh->do("truncate table osc.__new_t");
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'],
155
sleep => sub { $sleep_cnt++; },
161
"Calls sleep callback after each chunk (except last chunk)"
165
$output = output(sub { $osc->cleanup(); } );
168
!$EVAL_ERROR && !$output,
169
"cleanup() works but doesn't do anything"
172
# #############################################################################
174
# #############################################################################
178
open STDERR, '>', \$output;
179
$osc->_d('Complete test coverage');
183
qr/Complete test coverage/,
186
$sb->wipe_clean($dbh);