-#!perl
+#!/usr/bin/env perl
+# -*-mode:cperl; indent-tabs-mode: nil-*-
## Test swap functionality
use vars qw/ $dbhX $dbhA $dbhB $dbhC $dbhD $res $command $t %pkey $SQL %sth %sql/;
END {
- $bct->stop_bucardo($dbhX);
- $dbhX->disconnect();
- $dbhA->disconnect();
- $dbhB->disconnect();
+ $bct->stop_bucardo($dbhX);
+ $dbhX->disconnect();
+ $dbhA->disconnect();
+ $dbhB->disconnect();
}
## Get A and B emptied out, and repopulated with sample data
$res = $bct->ctl($command);
like ($res, qr/Creating herd: therd.*New tables added: \d/s, $t);
if ($res =~ /New tables added: (\d+)/ and $1 < 1) {
- BAIL_OUT 'Tables were not added to herd?!';
+ BAIL_OUT 'Tables were not added to herd?!';
}
## Add all sequences, and add them to the newly created herd
## Get the statement handles ready for each table type
for my $table (sort keys %tabletype) {
- $pkey{$table} = $table =~ /test5/ ? q{"id space"} : 'id';
-
- ## INSERT
- for my $x (1..8) {
- $SQL = $table =~ /0/
- ? "INSERT INTO $table($pkey{$table}) VALUES (?)"
- : "INSERT INTO $table($pkey{$table},data1,inty) VALUES (?,'foo',$x)";
- $sth{insert}{$x}{$table}{A} = $dbhA->prepare($SQL);
- $sth{insert}{$x}{$table}{B} = $dbhB->prepare($SQL);
- if ('BYTEA' eq $tabletype{$table}) {
- $sth{insert}{$x}{$table}{A}->bind_param(1, undef, {pg_type => PG_BYTEA});
- $sth{insert}{$x}{$table}{B}->bind_param(1, undef, {pg_type => PG_BYTEA});
- }
- }
-
- ## SELECT
- $sql{select}{$table} = "SELECT inty FROM $table ORDER BY $pkey{$table}";
- $table =~ /0/ and $sql{select}{$table} =~ s/inty/$pkey{$table}/;
-
- ## DELETE
- $SQL = "DELETE FROM $table";
- $sth{deleteall}{$table}{A} = $dbhA->prepare($SQL);
+ $pkey{$table} = $table =~ /test5/ ? q{"id space"} : 'id';
+
+ ## INSERT
+ for my $x (1..8) {
+ $SQL = $table =~ /0/
+ ? "INSERT INTO $table($pkey{$table}) VALUES (?)"
+ : "INSERT INTO $table($pkey{$table},data1,inty) VALUES (?,'foo',$x)";
+ $sth{insert}{$x}{$table}{A} = $dbhA->prepare($SQL);
+ $sth{insert}{$x}{$table}{B} = $dbhB->prepare($SQL);
+ if ('BYTEA' eq $tabletype{$table}) {
+ $sth{insert}{$x}{$table}{A}->bind_param(1, undef, {pg_type => PG_BYTEA});
+ $sth{insert}{$x}{$table}{B}->bind_param(1, undef, {pg_type => PG_BYTEA});
+ }
+ }
+
+ ## SELECT
+ $sql{select}{$table} = "SELECT inty FROM $table ORDER BY $pkey{$table}";
+ $table =~ /0/ and $sql{select}{$table} =~ s/inty/$pkey{$table}/;
+
+ ## DELETE
+ $SQL = "DELETE FROM $table";
+ $sth{deleteall}{$table}{A} = $dbhA->prepare($SQL);
}
## Add one row per table type to A
for my $table (keys %tabletype) {
- my $type = $tabletype{$table};
- my $val1 = $val{$type}{1};
- $sth{insert}{1}{$table}{A}->execute($val1);
+ my $type = $tabletype{$table};
+ my $val1 = $val{$type}{1};
+ $sth{insert}{1}{$table}{A}->execute($val1);
}
## Before the commit on A, B should be empty
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $t = qq{B has not received rows for table $table before A commits};
- $res = [];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ my $type = $tabletype{$table};
+ $t = qq{B has not received rows for table $table before A commits};
+ $res = [];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## Commit, then wait for the automatic sync to finish up
## Check the second database for the new rows
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $t = qq{Row with pkey of type $type gets copied to B};
+ my $type = $tabletype{$table};
+ $t = qq{Row with pkey of type $type gets copied to B};
- $res = [[1]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[1]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## The droptest table should be populated for A, but not for B
for my $table (sort keys %tabletype) {
- $t = qq{Triggers and rules fired on A};
- $SQL = qq{SELECT type FROM droptest WHERE name = '$table' ORDER BY 1};
+ $t = qq{Triggers and rules fired on A};
+ $SQL = qq{SELECT type FROM droptest WHERE name = '$table' ORDER BY 1};
- $res = [['rule'],['trigger']];
- bc_deeply($res, $dbhA, $SQL, $t);
+ $res = [['rule'],['trigger']];
+ bc_deeply($res, $dbhA, $SQL, $t);
- $t = qq{Triggers and rules did not fire on B};
- $res = [];
- bc_deeply($res, $dbhB, $SQL, $t);
+ $t = qq{Triggers and rules did not fire on B};
+ $res = [];
+ bc_deeply($res, $dbhB, $SQL, $t);
}
## Turn off the automatic syncing
## Delete rows from A
for my $table (keys %tabletype) {
- $sth{deleteall}{$table}{A}->execute();
+ $sth{deleteall}{$table}{A}->execute();
}
$dbhA->commit();
## B should still have the rows
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $t = qq{Row with pkey of type $type is not deleted from B before kick};
+ my $type = $tabletype{$table};
+ $t = qq{Row with pkey of type $type is not deleted from B before kick};
- $res = [[1]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[1]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## Kick the sync and wait for it to finish
## Rows should be gone from B now
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $t = qq{Row with pkey of type $type is deleted from B};
+ my $type = $tabletype{$table};
+ $t = qq{Row with pkey of type $type is deleted from B};
- $res = [];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## Now add two rows at once
for my $table (keys %tabletype) {
- my $type = $tabletype{$table};
- my $val2 = $val{$type}{2};
- my $val3 = $val{$type}{3};
- $sth{insert}{2}{$table}{A}->execute($val2);
- $sth{insert}{3}{$table}{A}->execute($val3);
+ my $type = $tabletype{$table};
+ my $val2 = $val{$type}{2};
+ my $val3 = $val{$type}{3};
+ $sth{insert}{2}{$table}{A}->execute($val2);
+ $sth{insert}{3}{$table}{A}->execute($val3);
}
$dbhA->commit();
## B should have the two new rows
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $t = qq{Two rows with pkey of type $type are copied to B};
+ my $type = $tabletype{$table};
+ $t = qq{Two rows with pkey of type $type are copied to B};
- $res = [[2],[3]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[2],[3]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## Test out an update
for my $table (keys %tabletype) {
- my $type = $tabletype{$table};
- $SQL = "UPDATE $table SET inty=inty+10";
- $dbhA->do($SQL);
+ my $type = $tabletype{$table};
+ $SQL = "UPDATE $table SET inty=inty+10";
+ $dbhA->do($SQL);
}
$dbhA->commit();
$bct->ctl('kick sync swaptest 0');
## B should have the updated rows
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $t = qq{Updates of two rows with pkey of type $type are copied to B};
+ my $type = $tabletype{$table};
+ $t = qq{Updates of two rows with pkey of type $type are copied to B};
- $res = [[12],[13]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[12],[13]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## Test insert, update, and delete all at once, across multiple transactions
for my $table (keys %tabletype) {
- my $type = $tabletype{$table};
- $SQL = "UPDATE $table SET inty=inty-3";
- $dbhA->do($SQL);
- $dbhA->commit();
-
- my $val4 = $val{$type}{4};
- $sth{insert}{4}{$table}{A}->execute($val4);
- $dbhA->commit();
-
- $SQL = "DELETE FROM $table WHERE inty = 10";
- $dbhA->do($SQL);
- $dbhA->commit();
+ my $type = $tabletype{$table};
+ $SQL = "UPDATE $table SET inty=inty-3";
+ $dbhA->do($SQL);
+ $dbhA->commit();
+
+ my $val4 = $val{$type}{4};
+ $sth{insert}{4}{$table}{A}->execute($val4);
+ $dbhA->commit();
+
+ $SQL = "DELETE FROM $table WHERE inty = 10";
+ $dbhA->do($SQL);
+ $dbhA->commit();
}
$bct->ctl('kick sync swaptest 0');
## B should have the updated rows
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $t = qq{Updates of two rows with pkey of type $type are copied to B};
+ my $type = $tabletype{$table};
+ $t = qq{Updates of two rows with pkey of type $type are copied to B};
- $res = [[9],[4]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[9],[4]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $dbhA->do("COPY $table($pkey{$table},inty,data1) FROM STDIN");
- my $val5 = $val{$type}{5};
- $val5 =~ s/\0//;
- $dbhA->pg_putcopydata("$val5\t5\tfive");
- $dbhA->pg_putcopyend();
- $dbhA->commit();
+ my $type = $tabletype{$table};
+ $dbhA->do("COPY $table($pkey{$table},inty,data1) FROM STDIN");
+ my $val5 = $val{$type}{5};
+ $val5 =~ s/\0//;
+ $dbhA->pg_putcopydata("$val5\t5\tfive");
+ $dbhA->pg_putcopyend();
+ $dbhA->commit();
}
$bct->ctl('kick sync swaptest 0');
## B should have the new rows
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $t = qq{COPY to A with pkey type $type makes it way to B};
+ my $type = $tabletype{$table};
+ $t = qq{COPY to A with pkey type $type makes it way to B};
- $res = [[9],[4],[5]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[9],[4],[5]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## Same row inserted on both sides
$bct->ctl(q{message "Begin insert to swap"});
for my $table (sort keys %tabletype) {
- $dbhA->do("TRUNCATE TABLE $table");
- $dbhB->do("TRUNCATE TABLE $table");
+ $dbhA->do("TRUNCATE TABLE $table");
+ $dbhB->do("TRUNCATE TABLE $table");
- my $type = $tabletype{$table};
- my $val6 = $val{$type}{6};
- $sth{insert}{6}{$table}{A}->execute($val6);
- $sth{insert}{6}{$table}{B}->execute($val6);
+ my $type = $tabletype{$table};
+ my $val6 = $val{$type}{6};
+ $sth{insert}{6}{$table}{A}->execute($val6);
+ $sth{insert}{6}{$table}{B}->execute($val6);
}
$dbhA->commit();
$dbhB->commit();
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
- $t = qq{Insert on both sides does not choke the swap sync};
+ my $type = $tabletype{$table};
+ $t = qq{Insert on both sides does not choke the swap sync};
- $res = [[6]];
- bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[6]];
+ bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
for my $table (sort keys %tabletype) {
- $SQL = "UPDATE $table SET inty = 77";
- $dbhA->do($SQL);
- $SQL = "UPDATE $table SET inty = 88";
- $dbhB->do($SQL);
+ $SQL = "UPDATE $table SET inty = 77";
+ $dbhA->do($SQL);
+ $SQL = "UPDATE $table SET inty = 88";
+ $dbhB->do($SQL);
}
$dbhA->commit();
$dbhB->commit();
for my $table (sort keys %tabletype) {
- $SQL = "SELECT inty FROM $table";
- $t = qq{Updates on both sides of swap sync work};
+ $SQL = "SELECT inty FROM $table";
+ $t = qq{Updates on both sides of swap sync work};
- $res = [[77]];
- bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
- $res = [[88]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[77]];
+ bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
+ $res = [[88]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
$bct->ctl('kick sync swaptest 0');
for my $table (sort keys %tabletype) {
- $SQL = "SELECT inty FROM $table";
- $t = qq{Expected values are there after swap sync where source wins};
+ $SQL = "SELECT inty FROM $table";
+ $t = qq{Expected values are there after swap sync where source wins};
- $res = [[77]];
- bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
- $res = [[77]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[77]];
+ bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
+ $res = [[77]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## Delete from B, will also get deleted on A
for my $table (sort keys %tabletype) {
- $SQL = "DELETE FROM $table";
- $dbhB->do($SQL);
+ $SQL = "DELETE FROM $table";
+ $dbhB->do($SQL);
}
$dbhB->commit();
for my $table (sort keys %tabletype) {
- $SQL = "SELECT inty FROM $table";
- $t = qq{Expected values are there post-delete after swap sync where source wins};
+ $SQL = "SELECT inty FROM $table";
+ $t = qq{Expected values are there post-delete after swap sync where source wins};
- $res = [];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
- $res = [];
- bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
+ $res = [];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [];
+ bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
}
## Insert same rows to A and B, change B, extra rows to B
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
-
- ## A only: 5
- my $val5 = $val{$type}{5};
- $sth{insert}{5}{$table}{A}->execute($val5);
- $SQL = "UPDATE $table SET inty = 55";
- $dbhA->do($SQL);
-
- ## B only: 6
- my $val6 = $val{$type}{6};
- $sth{insert}{6}{$table}{B}->execute($val6);
- $SQL = "UPDATE $table SET inty = 66";
- $dbhB->do($SQL);
-
- ## A and B: 7
- my $val7 = $val{$type}{7};
- $sth{insert}{7}{$table}{A}->execute($val7);
- $sth{insert}{7}{$table}{B}->execute($val7);
-
- ## Changes to B should get clobbered
- $SQL = "UPDATE $table SET inty = 77 WHERE inty <> 55";
- $dbhA->do($SQL);
- $SQL = "UPDATE $table SET inty = 777 WHERE inty <> 66";
- $dbhB->do($SQL);
+ my $type = $tabletype{$table};
+
+ ## A only: 5
+ my $val5 = $val{$type}{5};
+ $sth{insert}{5}{$table}{A}->execute($val5);
+ $SQL = "UPDATE $table SET inty = 55";
+ $dbhA->do($SQL);
+
+ ## B only: 6
+ my $val6 = $val{$type}{6};
+ $sth{insert}{6}{$table}{B}->execute($val6);
+ $SQL = "UPDATE $table SET inty = 66";
+ $dbhB->do($SQL);
+
+ ## A and B: 7
+ my $val7 = $val{$type}{7};
+ $sth{insert}{7}{$table}{A}->execute($val7);
+ $sth{insert}{7}{$table}{B}->execute($val7);
+
+ ## Changes to B should get clobbered
+ $SQL = "UPDATE $table SET inty = 77 WHERE inty <> 55";
+ $dbhA->do($SQL);
+ $SQL = "UPDATE $table SET inty = 777 WHERE inty <> 66";
+ $dbhB->do($SQL);
}
$dbhA->commit();
$dbhB->commit();
for my $table (sort keys %tabletype) {
- $SQL = "SELECT inty FROM $table ORDER BY 1";
- $t = qq{Expected values are there post-delete after swap sync where source wins};
+ $SQL = "SELECT inty FROM $table ORDER BY 1";
+ $t = qq{Expected values are there post-delete after swap sync where source wins};
- $res = [[55],[66],[77]];
- bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
- $res = [[55],[66],[77]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[55],[66],[77]];
+ bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
+ $res = [[55],[66],[77]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## Make B the "master"
## Delete all rows, some A, and some B, make sure both sides end up empty
for my $table (sort keys %tabletype) {
- $SQL = "DELETE FROM $table WHERE inty IN (55,66)";
- $dbhA->do($SQL);
- $SQL = "DELETE FROM $table WHERE inty IN (77)";
- $dbhB->do($SQL);
+ $SQL = "DELETE FROM $table WHERE inty IN (55,66)";
+ $dbhA->do($SQL);
+ $SQL = "DELETE FROM $table WHERE inty IN (77)";
+ $dbhB->do($SQL);
}
$dbhA->commit();
$dbhB->commit();
for my $table (sort keys %tabletype) {
- $SQL = "SELECT inty FROM $table ORDER BY 1";
- $t = qq{Deletes on both sides are carried over to the other};
+ $SQL = "SELECT inty FROM $table ORDER BY 1";
+ $t = qq{Deletes on both sides are carried over to the other};
- $res = [];
- bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [];
+ bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
## Same scenario as above, but this time B should "win"
for my $table (sort keys %tabletype) {
- my $type = $tabletype{$table};
-
- ## A only: 5
- my $val5 = $val{$type}{5};
- $sth{insert}{5}{$table}{A}->execute($val5);
- $SQL = "UPDATE $table SET inty = 55";
- $dbhA->do($SQL);
-
- ## B only: 6
- my $val6 = $val{$type}{6};
- $sth{insert}{6}{$table}{B}->execute($val6);
- $SQL = "UPDATE $table SET inty = 66";
- $dbhB->do($SQL);
-
- ## A and B: 7
- my $val7 = $val{$type}{7};
- $sth{insert}{7}{$table}{A}->execute($val7);
- $sth{insert}{7}{$table}{B}->execute($val7);
-
- ## Changes to A should get clobbered
- $SQL = "UPDATE $table SET inty = 77 WHERE inty <> 55";
- $dbhA->do($SQL);
- $SQL = "UPDATE $table SET inty = 777 WHERE inty <> 66";
- $dbhB->do($SQL);
+ my $type = $tabletype{$table};
+
+ ## A only: 5
+ my $val5 = $val{$type}{5};
+ $sth{insert}{5}{$table}{A}->execute($val5);
+ $SQL = "UPDATE $table SET inty = 55";
+ $dbhA->do($SQL);
+
+ ## B only: 6
+ my $val6 = $val{$type}{6};
+ $sth{insert}{6}{$table}{B}->execute($val6);
+ $SQL = "UPDATE $table SET inty = 66";
+ $dbhB->do($SQL);
+
+ ## A and B: 7
+ my $val7 = $val{$type}{7};
+ $sth{insert}{7}{$table}{A}->execute($val7);
+ $sth{insert}{7}{$table}{B}->execute($val7);
+
+ ## Changes to A should get clobbered
+ $SQL = "UPDATE $table SET inty = 77 WHERE inty <> 55";
+ $dbhA->do($SQL);
+ $SQL = "UPDATE $table SET inty = 777 WHERE inty <> 66";
+ $dbhB->do($SQL);
}
$dbhA->commit();
$dbhB->commit();
for my $table (sort keys %tabletype) {
- $SQL = "SELECT inty FROM $table ORDER BY 1";
- $t = qq{Expected values are there post-delete after swap sync where target wins};
+ $SQL = "SELECT inty FROM $table ORDER BY 1";
+ $t = qq{Expected values are there post-delete after swap sync where target wins};
- $res = [[55],[66],[777]];
- bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
- $res = [[55],[66],[777]];
- bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
+ $res = [[55],[66],[777]];
+ bc_deeply($res, $dbhA, $sql{select}{$table}, $t);
+ $res = [[55],[66],[777]];
+ bc_deeply($res, $dbhB, $sql{select}{$table}, $t);
}
exit;