#!/bin/bash

set -xe

# Configuration variables
SOURCE_DB="DB"
SOURCE_HOST="HOST"
SOURCE_PORT="5432"
SOURCE_PASSWORD="PASSWORD"
SOURCE_USER="USER"

TARGET_DB="DB"
TARGET_HOST="HOST"
TARGET_PORT="5432"
TARGET_USER="USER"
TARGET_PASSWORD="PASSWORD"

TARGET_APP_USER="suno"
TARGET_APP_USER_PASSWORD="suno-passwd"

SCHEMA_DUMP_FILE="schema_dump.sql"

## Clean up previous attempts. This is at the top as dump schema will include it in the schema dump file
echo "Cleanup previous publication with same name..."
PGPASSWORD=$SOURCE_PASSWORD psql -h $SOURCE_HOST -p $SOURCE_PORT -U $SOURCE_USER -d $SOURCE_DB -c \
"DROP PUBLICATION rds_db_publication;" || echo "ok, no publication existed"
echo "Publication dropped."



# Create an app user on the target db who will own the tables
#
# 1. Drop the user if it already exists
# 2 Create new user with permissions on the target dbs
PGPASSWORD=$TARGET_PASSWORD psql -h $TARGET_HOST -p $TARGET_PORT -U $TARGET_USER -d $TARGET_DB -c \
"
    REASSIGN OWNED BY $TARGET_APP_USER TO $TARGET_USER;
    DROP OWNED BY $TARGET_APP_USER;
    DROP USER IF EXISTS $TARGET_APP_USER;
" || echo "ok, no user existed"

PGPASSWORD=$TARGET_PASSWORD psql -h $TARGET_HOST -p $TARGET_PORT -U $TARGET_USER -d $TARGET_DB -c \
"
    CREATE USER $TARGET_APP_USER WITH ENCRYPTED PASSWORD '$TARGET_APP_USER_PASSWORD';
    ALTER DATABASE $TARGET_DB OWNER TO $TARGET_APP_USER;
    GRANT USAGE, CREATE ON SCHEMA public TO $TARGET_APP_USER;
"



# Step 1: Dump the Schema from the Source Database
echo "Dumping the schema from the source database..."
PGPASSWORD=$SOURCE_PASSWORD pg_dump -h $SOURCE_HOST -p $SOURCE_PORT -U $SOURCE_USER -d $SOURCE_DB --schema-only --no-owner --no-acl -f $SCHEMA_DUMP_FILE

echo "Schema dump complete. Do you want to import the schema into the target database? (yes/no)"
read user_input

if [[ $user_input == "yes" ]]; then
    # Step 2: Import the Schema into the Target Database using the app user
    echo "Importing the schema into the target database using the app user..."
    PGPASSWORD=$TARGET_APP_USER_PASSWORD psql -h $TARGET_HOST -p $TARGET_PORT -U $TARGET_APP_USER -d $TARGET_DB -f $SCHEMA_DUMP_FILE

    # Step 4: Setup Logical Replication
    # Configure the source database
    echo "Setting up logical replication..."
    PGPASSWORD=$SOURCE_PASSWORD psql -h $SOURCE_HOST -p $SOURCE_PORT -U $SOURCE_USER -d $SOURCE_DB -c \
    "CREATE PUBLICATION rds_db_publication FOR ALL TABLES;"
    echo "Publication created."

    # Configure the target database, starting replication
    PGPASSWORD=$TARGET_PASSWORD psql -h $TARGET_HOST -p $TARGET_PORT -U $TARGET_USER -d $TARGET_DB -c \
     "CREATE SUBSCRIPTION render_db_subscription CONNECTION 'host=$SOURCE_HOST port=$SOURCE_PORT dbname=$SOURCE_DB
     user=$SOURCE_USER password=$SOURCE_PASSWORD' PUBLICATION rds_db_publication WITH (copy_data = true, create_slot = true, disable_on_error = true);"

    echo "Subscription created"

    echo "Logical replication setup is complete."
else
    echo "User did not type yes, stopping at dump"
fi
