| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | use Test::More; |
|---|
| 5 | |
|---|
| 6 | BEGIN { |
|---|
| 7 | if (! $ENV{DBI_URI}) { |
|---|
| 8 | plan skip_all => 'set DBI_URI, DBI_USER, DBI_PASSWORD to run these tests'; |
|---|
| 9 | } else { |
|---|
| 10 | plan tests => 10; |
|---|
| 11 | } |
|---|
| 12 | use_ok('DBIx::Replicate', qw/dbix_replicate/); |
|---|
| 13 | }; |
|---|
| 14 | |
|---|
| 15 | my $engine = $ENV{DBI_ENGINE} || ''; |
|---|
| 16 | if ($engine eq '' && $ENV{DBI_URI} =~ /^dbi:mysql:/i) { |
|---|
| 17 | $engine = 'engine=innodb'; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | my $dbh = DBI->connect($ENV{DBI_URI}, $ENV{DBI_USER}, $ENV{DBI_PASSWORD}) |
|---|
| 21 | or die DBI->errstr; |
|---|
| 22 | |
|---|
| 23 | # prepare tables |
|---|
| 24 | $dbh->do('drop table if exists drtest_src') |
|---|
| 25 | or die $dbh->errstr; |
|---|
| 26 | $dbh->do( |
|---|
| 27 | "create table drtest_src (id int not null primary key, str varchar(63)) $engine" |
|---|
| 28 | ) or die $dbh->errstr; |
|---|
| 29 | $dbh->do('drop table if exists drtest_dest') |
|---|
| 30 | or die $dbh->errstr; |
|---|
| 31 | $dbh->do( |
|---|
| 32 | "create table drtest_dest (id int not null primary key, str varchar(63)) $engine" |
|---|
| 33 | ) or die $dbh->errstr; |
|---|
| 34 | my $st = $dbh->prepare('replace into drtest_src (id,str) values (?,?)') |
|---|
| 35 | or die $dbh->errstr; |
|---|
| 36 | |
|---|
| 37 | foreach my $copy_mode (qw/primary_key copy_by/) { |
|---|
| 38 | |
|---|
| 39 | my %args = ( |
|---|
| 40 | src_conn => $dbh, |
|---|
| 41 | src_table => 'drtest_src', |
|---|
| 42 | dest_conn => $dbh, |
|---|
| 43 | dest_table => 'drtest_dest', |
|---|
| 44 | $copy_mode => [ qw/id/ ], |
|---|
| 45 | columns => [ qw/id str/ ], |
|---|
| 46 | ($copy_mode eq 'primary_key' ? (limit => 5) : ()), |
|---|
| 47 | ); |
|---|
| 48 | # copy empty tables |
|---|
| 49 | dbix_replicate(\%args); |
|---|
| 50 | is_deeply( |
|---|
| 51 | $dbh->selectall_arrayref('select * from drtest_src order by id'), |
|---|
| 52 | $dbh->selectall_arrayref('select * from drtest_dest order by id'), |
|---|
| 53 | ); |
|---|
| 54 | |
|---|
| 55 | # fill in data and copy |
|---|
| 56 | for (my $i = 0; $i < 1000; $i++) { |
|---|
| 57 | $st->execute($i, "this is a test $i") |
|---|
| 58 | or die $dbh->errstr; |
|---|
| 59 | } |
|---|
| 60 | dbix_replicate(\%args); |
|---|
| 61 | is_deeply( |
|---|
| 62 | $dbh->selectall_arrayref('select * from drtest_src order by id'), |
|---|
| 63 | $dbh->selectall_arrayref('select * from drtest_dest order by id'), |
|---|
| 64 | ); |
|---|
| 65 | |
|---|
| 66 | # remove some of the rows |
|---|
| 67 | $dbh->do('delete from drtest_src where id%13=0') |
|---|
| 68 | or die $dbh->errstr; |
|---|
| 69 | dbix_replicate(\%args); |
|---|
| 70 | is_deeply( |
|---|
| 71 | $dbh->selectall_arrayref('select * from drtest_src order by id'), |
|---|
| 72 | $dbh->selectall_arrayref('select * from drtest_dest order by id'), |
|---|
| 73 | ); |
|---|
| 74 | |
|---|
| 75 | # insert a couple of rows |
|---|
| 76 | for (my $i = 0; $i < 2000; $i += 17) { |
|---|
| 77 | $st->execute($i, "this is a test $i") |
|---|
| 78 | or die $dbh->errstr; |
|---|
| 79 | } |
|---|
| 80 | dbix_replicate(\%args); |
|---|
| 81 | is_deeply( |
|---|
| 82 | $dbh->selectall_arrayref('select * from drtest_src order by id'), |
|---|
| 83 | $dbh->selectall_arrayref('select * from drtest_dest order by id'), |
|---|
| 84 | ); |
|---|
| 85 | |
|---|
| 86 | # limit rows to be copied by using extra_cond |
|---|
| 87 | if ($copy_mode eq 'primary_key') { |
|---|
| 88 | dbix_replicate({ |
|---|
| 89 | %args, |
|---|
| 90 | extra_cond => 'id%7=0' |
|---|
| 91 | }); |
|---|
| 92 | is_deeply( |
|---|
| 93 | [ |
|---|
| 94 | grep { |
|---|
| 95 | $_->[0] %7 == 0 |
|---|
| 96 | } @{$dbh->selectall_arrayref( |
|---|
| 97 | 'select * from drtest_src order by id' |
|---|
| 98 | )}, |
|---|
| 99 | ], |
|---|
| 100 | $dbh->selectall_arrayref('select * from drtest_dest order by id'), |
|---|
| 101 | ); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | $dbh->do('delete from drtest_src') |
|---|
| 105 | or die $dbh->errstr; |
|---|
| 106 | $dbh->do('delete from drtest_dest') |
|---|
| 107 | or die $dbh->errstr; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | $dbh->do('drop table drtest_src') |
|---|
| 111 | or die $dbh->errstr; |
|---|
| 112 | $dbh->do('drop table drtest_dest') |
|---|
| 113 | or die $dbh->errstr; |
|---|