#!/bin/sh

initdb -D pub
echo "wal_level = logical" >> pub/postgresql.conf
echo "log_line_prefix = '%t pub [%p] '" >> pub/postgresql.conf
pg_ctl -D pub -w start

initdb -D sub
echo "port = 5433" >> sub/postgresql.conf
echo "log_line_prefix = '%t sub [%p] '" >> sub/postgresql.conf
pg_ctl -D sub -w start

cat <<EOF | psql
CREATE TABLE hoge (i int primary key, j int);
ALTER TABLE hoge REPLICA IDENTIFY DEFAULT;
CREATE PUBLICATION mypub FOR TABLE hoge;
INSERT INTO hoge VALUES (generate_series(1,10), 99);
EOF

cat <<EOF | psql -p 5433
CREATE TABLE hoge (i int primary key, j int);
CREATE SUBSCRIPTION mysub CONNECTION '' PUBLICATION mypub;
EOF

sleep 3

cat <<EOF | psql -p 5433
DROP SUBSCRIPTION mysub;
CREATE SUBSCRIPTION mysub CONNECTION '' PUBLICATION mypub;
EOF
