summaryrefslogtreecommitdiff
path: root/dev/multidb.test
blob: 5d98d0238c256fdd0e8a900771e84531af653128 (plain)
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
##!/bin/bash

## A quick self-contained test of the makedelta system
## With multiple databases!
## Not a replacement for the real tests so much as a development aid

## Uses:
## Four databases, A, B, C, and D (aka mtest1 mtest2 mtest3 mtest4)
## One table: foobar
## Two syncs: AB A <=> B and BC (B <=> C) -> D
## For safety, this runs on port 5492.
## Do not change it to 5432, as this drops the bucardo database! :)
## This must be run from the root Bucardo source code directory

export PGPORT=5492

## Ensure we use the Bucardo.pm in the current directory
export PERL5LIB=.

## Quick check that our scripts compile cleanly
perl -c bucardo || exit
perl -c Bucardo.pm || exit

## Just in case, stop any running Bucardos
./bucardo stop --quiet 2>/dev/null

## Bail if the cluster is not reachable
psql -q -c 'select 1' >/dev/null 2>/dev/null

if [[ $? -ne 0 ]];then
  echo Could not connect to Postgres on port $PGPORT
  echo Tried: psql -q -c 'select 1'
  exit
fi

## Terminate any old connections, and drop databases
echo Dropping existing test databases
psql -AX -qt -c "select pg_terminate_backend(pid) FROM pg_stat_activity where datname IN ('mtest1','mtest2','mtest3','mtest4','bucardo')" >/dev/null
psql -qc 'drop database mtest1' 2>/dev/null
psql -qc 'drop database mtest2' 2>/dev/null
psql -qc 'drop database mtest3' 2>/dev/null
psql -qc 'drop database mtest4' 2>/dev/null
psql -qc 'drop database bucardo' 2>/dev/null

echo Creating test databases
psql -qc 'create database mtest1'

## Create tables foobar and foobar2. The latter will be removed from certain dbs
psql mtest1 -qc 'create sequence mseq increment by 3 start with 1'
psql mtest1 -qc "create table foobar(id int not null default nextval('mseq'), email text)"
psql mtest1 -qc "create unique index foobar_unique on foobar(id)"
psql mtest1 -qc 'create sequence mseq2 increment by 3 start with 1'
psql mtest1 -qc "create table foobar2(id int not null default nextval('mseq2'), email text)"
psql mtest1 -qc "create unique index foobar_unique2 on foobar2(id)"

psql -qc 'create database mtest2 template mtest1'
psql mtest2 -qc 'alter sequence mseq restart with 2'

psql -qc 'create database mtest3 template mtest1'
psql mtest3 -qc 'alter sequence mseq restart with 3'

psql -qc 'create database mtest4 template mtest1'

## Remove foobar from C and D
#psql mtest3 -qc 'drop table foobar cascade'
#psql mtest4 -qc 'drop table foobar cascade'

## Now remove foobar2 from A and B
psql mtest1 -qc 'drop table foobar2 cascade'
psql mtest2 -qc 'drop table foobar2 cascade'

echo Installing Bucardo
./bucardo install --batch --quiet >/tmp/foobar
if [[ $? -ne 0 ]];then
  echo Failed to install Bucardo
  exit
fi

./bucardo set log_level=debug --quiet

./bucardo add db source_sg dbname=mtest1 --quiet
./bucardo add db target_sg dbname=mtest2 --quiet
./bucardo add table foobar relgroup=sgherd --quiet
./bucardo add dbgroup sggroup source_sg:source target_sg:target
./bucardo add sync sg_sync herd=sgherd dbs=sggroup

./bucardo add db source_lg dbname=mtest3 --quiet
./bucardo add db target_lg dbname=mtest4 --quiet
./bucardo add table foobar2 relgroup=lgherd db=source_lg 
./bucardo add dbgroup lggroup source_lg:source target_lg:target
./bucardo add sync lg_sync herd=lgherd dbs=lggroup

rm -f log.bucardo
./bucardo start --logdest=.  --no-exit-on-nosync
sleep 2

./bucardo activate sync sg_sync
./bucardo activate sync lg_sync

./bucardo stop 
sleep 2

echo Restarting
./bucardo start --logdest=.  --no-exit-on-nosync
sleep 3
./bucardo stop 
sleep 2

exit




## Make a simple sync for table foobar2 going from B to C
./bucardo add table foobar2 relgroup=mgroup2 db=B
./bucardo add sync BC relgroup=mgroup2 dbs=C:source,B:source --quiet

echo Starting Bucardo
rm -f log.bucardo
./bucardo start --logdest=. --quiet
sleep 4

./bucardo stop --quiet
sleep 2
./bucardo start --logdest=. --quiet
sleep 4

exit

echo Makedelta is off, so row \"alice\" added to A will not make it to C
psql -AX -qt mtest1 -c "insert into foobar(email) values ('alice')"
sleep 2

psql -A -t mtest1 -c "select 'A:', * from foobar"
psql -A -t mtest2 -c "select 'B:', * from foobar"
psql -A -t mtest3 -c "select 'C:', * from foobar"
psql -A -t mtest4 -c "select 'D:', * from foobar"

./bucardo update table foobar makedelta=B

./bucardo stop --quiet
sleep 2
./bucardo start --logdest=. --quiet

echo Makedelta is on for B, so row \"bob\" added to A will make it to C
psql -AX -qt mtest1 -c "insert into foobar(email) values ('bob')"
sleep 3

psql -At mtest1 -c "select 'A:', * from foobar"
psql -At mtest2 -c "select 'B:', * from foobar"
psql -At mtest3 -c "select 'C:', * from foobar"
psql -At mtest4 -c "select 'D:', * from foobar"

./bucardo update table foobar set makedelta=A
./bucardo message Changed makedelta
./bucardo reload sync AB

echo Makedelta is on for A only, so row \"bob\" deleted from A will not make it to C
psql -AX -qt mtest1 -c "delete from foobar where email = 'bob'"
sleep 3

psql -At mtest1 -c "select 'A:', * from foobar"
psql -At mtest2 -c "select 'B:', * from foobar"
psql -At mtest3 -c "select 'C:', * from foobar"
psql -At mtest4 -c "select 'D:', * from foobar"

./bucardo update table foobar set makedelta=on
./bucardo message Changed makedelta
./bucardo reload sync AB

echo Makedelta is on for everyone, so row \"mallory\" to A will not make it to B,C,D
psql -AX -qt mtest1 -c "insert into foobar(email) values ('mallory')"
sleep 3

psql -At mtest1 -c "select 'A:', * from foobar"
psql -At mtest2 -c "select 'B:', * from foobar"
psql -At mtest3 -c "select 'C:', * from foobar"
psql -At mtest4 -c "select 'D:', * from foobar"

./bucardo update table foobar set makedelta=off
./bucardo message Changed makedelta
./bucardo reload sync AB

echo Makedelta is off for everyone, so row \"mallory\" removed from A will stay on C
psql -AX -qt mtest1 -c "delete from foobar where email = 'mallory'"
sleep 3

psql -At mtest1 -c "select 'A:', * from foobar"
psql -At mtest2 -c "select 'B:', * from foobar"
psql -At mtest3 -c "select 'C:', * from foobar"
psql -At mtest4 -c "select 'D:', * from foobar"


echo Stopping Bucardo
./bucardo stop --quiet