-- UDR TEST
~/tmp/bdr/reinit-udr.sh
export PGPORT=5440 PGHOST=/tmp
export PATH=/home/andres/build/postgres/9.4-assert/install/bin/:$PATH
~/tmp/bdr/start-udr-5440

\c postgres
CREATE DATABASE udr_primary;
CREATE DATABASE udr_secondary;
\c udr_primary
CREATE EXTENSION btree_gist;
CREATE EXTENSION bdr;
CREATE TABLE initial_data(id serial primary key, data text);
INSERT INTO initial_data(data) VALUES('my name is andres');
\c udr_secondary
CREATE EXTENSION btree_gist;
CREATE EXTENSION bdr;

SELECT bdr.bdr_subscribe(
	local_node_name := 'demo-secondary',
	subscribe_to_dsn := 'dbname=udr_primary',
        node_local_dsn := 'dbname=udr_secondary'
);

SELECT bdr.bdr_node_join_wait_for_ready();
SELECT * FROM initial_data;
\c udr_primary
UPDATE initial_data SET data = 'My name is Andres Freund' WHERE id = 1;
\c udr_secondary
SELECT * FROM initial_data;
\c udr_primary
CREATE TABLE anothertable(key text primary key, data text);
SELECT bdr.bdr_replicate_ddl_command($$CREATE TABLE anothertable(key text primary key, data text);$$);
SELECT bdr.bdr_replicate_ddl_command($$CREATE TABLE public.anothertable(key text primary key, data text);$$);
INSERT INTO anothertable(key, data) VALUES ('k1', 'data');
\c udr_secondary
SELECT * FROM anothertable;


-- BDR TEST
~/tmp/bdr/reinit.sh
export PGPORT=5440 PGHOST=/tmp
export PATH=/home/andres/build/postgres/9.4-assert/install/bin/:$PATH
~/tmp/bdr/start-5440

\c postgres
CREATE DATABASE bdr_demo_1;
CREATE DATABASE bdr_demo_2;
\c bdr_demo_1
CREATE EXTENSION btree_gist;
CREATE EXTENSION bdr;
CREATE TABLE initial_data(id serial primary key, data text);
INSERT INTO initial_data(data) VALUES('my name is andres');
\c bdr_demo_2
CREATE EXTENSION btree_gist;
CREATE EXTENSION bdr;

\c bdr_demo_1

SELECT bdr.bdr_group_create(
	local_node_name := 'bdr-demo-1',
	node_external_dsn := 'dbname=bdr_demo_1'
	);

\c bdr_demo_2

SELECT bdr.bdr_group_join(
	local_node_name := 'bdr-demo-2',
	node_external_dsn := 'dbname=bdr_demo_2',
	join_using_dsn := 'dbname=bdr_demo_1'
);

SELECT bdr.bdr_node_join_wait_for_ready();

SELECT * FROM initial_data;

CREATE TABLE session(id bigint primary key, username text not null, valid bool not null);
CREATE SEQUENCE session_id_seq USING bdr;
ALTER TABLE session ALTER COLUMN id SET DEFAULT nextval('public.session_id_seq');
SELECT nextval('session_id_seq');
\c bdr_demo_1
SELECT nextval('session_id_seq');
\c bdr_demo_2
ALTER TABLE session ALTER COLUMN id SET DEFAULT nextval('public.session_id_seq');
\c bdr_demo_1
\d session
INSERT INTO session(username, valid) VALUES ('andres', true);
\c bdr_demo_2
UPDATE session SET valid = false WHERE username = 'andres';

ALTER SYSTEM SET bdr.default_apply_delay = '30s';
SELECT pg_reload_conf();

INSERT INTO session(username, valid) VALUES ('someone', true);
\c bdr_demo_1
SELECT * FROM session;
\watch

\c bdr_demo_1
UPDATE session SET valid = true WHERE username = 'andres';
\c bdr_demo_2
UPDATE session SET valid = false WHERE username = 'andres';
\c bdr_demo_2
SELECT conflict_resolution, local_tuple, remote_tuple FROM bdr.bdr_conflict_history WHERE object_name = 'session';
