1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
#!/usr/bin/env perl
# -*-mode:cperl; indent-tabs-mode: nil-*-
## Test bucardo_delta and bucardo_track table tasks
use 5.008003;
use strict;
use warnings;
use Data::Dumper;
use lib 't','.';
use DBD::Pg;
use Test::More;
use MIME::Base64;
use vars qw/ $bct $dbhX $dbhA $dbhB $dbhC $res $command $t $SQL %pkey %sth %sql $sth $count/;
use BucardoTesting;
$bct = BucardoTesting->new() or BAIL_OUT "Creation of BucardoTesting object failed\n";
$location = '';
my $numtabletypes = keys %tabletype;
my $numsequences = keys %sequences;
plan tests => 164;
pass("*** Beginning delta tests");
END {
$bct and $bct->stop_bucardo($dbhX);
$dbhX and $dbhX->disconnect();
$dbhA and $dbhA->disconnect();
$dbhB and $dbhB->disconnect();
$dbhC and $dbhC->disconnect();
}
## Get Postgres databases A, B, and C created
$dbhA = $bct->repopulate_cluster('A');
$dbhB = $bct->repopulate_cluster('B');
$dbhC = $bct->repopulate_cluster('C');
## Create a bucardo database, and install Bucardo into it
$dbhX = $bct->setup_bucardo('A');
## Tell Bucardo about these databases (one source and two targets)
for my $name (qw/ A B C /) {
$t = "Adding database from cluster $name works";
my ($dbuser,$dbport,$dbhost) = $bct->add_db_args($name);
$command = "bucardo add db $name dbname=bucardo_test user=$dbuser port=$dbport host=$dbhost";
$res = $bct->ctl($command);
like ($res, qr/Added database "$name"/, $t);
}
## Put all pk tables into a relgroup
$t = q{Adding all PK tables on the master works};
$res = $bct->ctl(q{bucardo add tables '*bucardo*test*' '*Bucardo*test*' db=A relgroup=trelgroup pkonly});
like ($res, qr/Created the relgroup named "trelgroup".*are now part of/s, $t);
## Add all sequences, and add them to the newly created relgroup
$t = q{Adding all sequences on the master works};
$res = $bct->ctl("bucardo add all sequences relgroup=trelgroup");
like ($res, qr/New sequences added: \d/, $t);
## Create a new dbgroup going from A to B and C
$t = q{Created a new dbgroup};
$res = $bct->ctl(q{ bucardo add dbgroup pg A:source B:target C:target });
like ($res, qr/Created dbgroup "pg"/, $t);
## Create a new sync
$t = q{Created a new sync};
$res = $bct->ctl(q{ bucardo add sync dtest relgroup=trelgroup dbs=pg autokick=false });
like ($res, qr/Added sync "dtest"/, $t);
## Make sure the bucardo_delta and bucardo_track tables are empty
for my $table (sort keys %tabletype) {
my $tracktable = "track_public_$table";
my $deltatable = "delta_public_$table";
$t = "The track table $tracktable is empty";
$SQL = qq{SELECT 1 FROM bucardo."$tracktable"};
$count = $dbhA->do($SQL);
is ($count, '0E0', $t);
$t = "The delta table $deltatable is empty";
$SQL = qq{SELECT 1 FROM bucardo."$deltatable"};
$count = $dbhA->do($SQL);
is ($count, '0E0', $t);
}
## Start up Bucardo with this new sync
$bct->restart_bucardo($dbhX);
## Add a row to A
$bct->add_row_to_database('A', 1);
## Make sure that bucardo_track is empty and bucardo_delta has the expected value
for my $table (sort keys %tabletype) {
my $tracktable = "track_public_$table";
my $deltatable = "delta_public_$table";
$t = "The track table $tracktable is empty";
$SQL = qq{SELECT 1 FROM bucardo."$tracktable"};
$count = $dbhA->do($SQL);
is ($count, '0E0', $t);
my $pkeyname = $table =~ /test5/ ? q{"id space"} : 'id';
$t = "The delta table $deltatable contains the correct id";
$SQL = qq{SELECT $pkeyname FROM bucardo."$deltatable"};
$dbhA->do(q{SET TIME ZONE 'UTC'});
$res = $dbhA->selectall_arrayref($SQL);
my $type = $tabletype{$table};
my $val1 = $val{$type}{1};
is_deeply ($res, [[$val1]], $t) or die;
}
## Kick off the sync
$bct->ctl('bucardo kick dtest 0');
## All rows should be on A, B, and C
my $expected = [[1]];
$bct->check_for_row($expected, [qw/A B C/]);
## Make sure that bucardo_track now has a row
for my $table (sort keys %tabletype) {
my $tracktable = "track_public_$table";
$t = "The track table $tracktable contains the proper entry";
$SQL = qq{SELECT target FROM bucardo."$tracktable"};
$res = $dbhA->selectall_arrayref($SQL);
is_deeply ($res, [['dbgroup pg']], $t);
}
## Run the purge program
$bct->ctl('bucardo purge');
for my $table (sort keys %tabletype) {
my $tracktable = "track_public_$table";
my $deltatable = "delta_public_$table";
$t = "The track table $tracktable contains no entries post purge";
$SQL = qq{SELECT 1 FROM bucardo."$tracktable"};
$count = $dbhA->do($SQL);
is ($count, '0E0', $t);
$t = "The delta table $deltatable contains no entries post purge";
$SQL = qq{SELECT 1 FROM bucardo."$deltatable"};
$count = $dbhA->do($SQL);
is ($count, '0E0', $t);
}
## Create a doubled up entry in the delta table (two with same timestamp and pk)
$bct->add_row_to_database('A', 22, 0);
$bct->add_row_to_database('A', 28, 0);
$dbhA->commit();
## Check for two entries per table
for my $table (sort keys %tabletype) {
my $tracktable = "track_public_$table";
my $deltatable = "delta_public_$table";
$t = "The track table $tracktable is empty";
$SQL = qq{SELECT 1 FROM bucardo."$tracktable"};
$count = $dbhA->do($SQL);
is ($count, '0E0', $t);
$t = "The delta table $deltatable contains two entries";
$SQL = qq{SELECT 1 FROM bucardo."$deltatable"};
$count = $dbhA->do($SQL);
is ($count, 2, $t);
}
## Kick it off
$bct->ctl('bucardo kick dtest 0');
## Run the purge program
$bct->ctl('bucardo purge');
for my $table (sort keys %tabletype) {
my $tracktable = "track_public_$table";
my $deltatable = "delta_public_$table";
$t = "The track table $tracktable contains no entries post purge";
$SQL = qq{SELECT 1 FROM bucardo."$tracktable"};
$count = $dbhA->do($SQL);
is ($count, '0E0', $t);
$t = "The delta table $deltatable contains no entries post purge";
$SQL = qq{SELECT 1 FROM bucardo."$deltatable"};
$count = $dbhA->do($SQL);
is ($count, '0E0', $t);
}
exit;
|