#!/usr/bin/env bash

publication_db_port=15432
subscription_db_port=15433

## publication 
function create_publication() {
    initdb -D data_pub
    sed  -i "s/#port = 5432/port=${publication_db_port}/" data_pub/postgresql.conf 
    sed  -i 's/#wal_level = replica/wal_level = logical/'  data_pub/postgresql.conf 
      
    pg_ctl -D data_pub start 

    psql postgres -p ${publication_db_port} -c "alter user $USER with password 'postgres'";
    psql postgres -p ${publication_db_port} -c "create table test(id bigserial primary key, name varchar(300));";

    ## case 1:no problem , and replication is ok
    # psql postgres -p ${publication_db_port} -c "create PUBLICATION pub_test for table test;";
    # psql postgres -p ${publication_db_port} -c "select * from pg_create_logical_replication_slot('test','pgoutput');";
    # psql postgres -p ${publication_db_port} -c "insert into test select n , 'undefined' from generate_series(0,1000000) n;";

    ## case 2: no problem , but first 1000000 row do not replicate to subscriber 
    # psql postgres -p ${publication_db_port} -c "create PUBLICATION pub_test for table test;";
    # psql postgres -p ${publication_db_port} -c "insert into test select n , 'undefined' from generate_series(0,1000000) n;";
    # psql postgres -p ${publication_db_port} -c "select * from pg_create_logical_replication_slot('test','pgoutput');";

    ## case 3: no problem and replication is ok 
    # psql postgres -p ${publication_db_port} -c "select * from pg_create_logical_replication_slot('test','pgoutput');";
    # psql postgres -p ${publication_db_port} -c "create PUBLICATION pub_test for table test;";
    # psql postgres -p ${publication_db_port} -c "insert into test select n , 'undefined' from generate_series(0,1000000) n;";

    ## case 4: throw the error
    # psql postgres -p ${publication_db_port} -c "select * from pg_create_logical_replication_slot('test','pgoutput');";
    # psql postgres -p ${publication_db_port} -c "insert into test select n , 'undefined' from generate_series(0,1000000) n;";
    # psql postgres -p ${publication_db_port} -c "create PUBLICATION pub_test for table test;";

    ## case 5: no problem , but first 1000000 row do not replicate to subscriber 
    # psql postgres -p ${publication_db_port} -c "insert into test select n , 'undefined' from generate_series(0,1000000) n;";
    # psql postgres -p ${publication_db_port} -c "select * from pg_create_logical_replication_slot('test','pgoutput');";
    # psql postgres -p ${publication_db_port} -c "create PUBLICATION pub_test for table test;";

    ## case 6: no problem , but first 1000000 row do not replicate to subscriber 
    # psql postgres -p ${publication_db_port} -c "insert into test select n , 'undefined' from generate_series(0,1000000) n;";
    # psql postgres -p ${publication_db_port} -c "create PUBLICATION pub_test for table test;";
    # psql postgres -p ${publication_db_port} -c "select * from pg_create_logical_replication_slot('test','pgoutput');";

}

function create_sub(){
    initdb -D data_sub
    sed  -i "s/#port = 5432/port=${subscription_db_port}/" data_sub/postgresql.conf 

    pg_ctl -D data_sub start 

    psql postgres -p ${subscription_db_port} -c "create table test(id bigserial primary key, name varchar(300));";
    psql postgres -p ${subscription_db_port} -c "create subscription mysub1 CONNECTION 'host=127.0.0.1 port=${publication_db_port} user=$USER password=postgres dbname=postgres ' PUBLICATION pub_test with (create_slot=false, enabled=true,connect=true,copy_data=false , slot_name='test')";
}


function setup(){
    create_publication
    sleep 10;
    create_sub
}

setup

