|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# ======================================== CONFIG ================================================= |
| 4 | + |
| 5 | +# Define source and target schemas and chain ID to delete data for |
| 6 | +read -p "Enter source schema number: " SOURCE_SCHEMA_NUM |
| 7 | +read -p "Enter target schema number: " TARGET_SCHEMA_NUM |
| 8 | +read -p "Enter chain ID to delete data for: " CHAIN_ID |
| 9 | + |
| 10 | +SOURCE_SCHEMA="chain_data_${SOURCE_SCHEMA_NUM}" |
| 11 | +TARGET_SCHEMA="chain_data_${TARGET_SCHEMA_NUM}" |
| 12 | + |
| 13 | +# Read the DATABASE_URL from the .env |
| 14 | +# Example: DATABASE_URL=postgres://user:password@localhost:5432/grants_stack_indexer?sslmode=no-verify |
| 15 | +source .env |
| 16 | +DATABASE_URL=${DATABASE_URL:-} |
| 17 | + |
| 18 | +if [ -z "$DATABASE_URL" ]; then |
| 19 | + echo "Error: DATABASE_URL is not set in the environment." |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Path to SSL certificates |
| 24 | +SSL_DIR="./ssl" # Suggest to place certificates in ./ssl directory |
| 25 | +SSL_ROOT_CERT="$SSL_DIR/ca-certificate.crt" |
| 26 | +SSL_CERT="$SSL_DIR/client-cert.pem" |
| 27 | +SSL_KEY="$SSL_DIR/client-key.pem" |
| 28 | + |
| 29 | +# ================================================================================================= |
| 30 | + |
| 31 | +# Extract components from DATABASE_URL using sed |
| 32 | +USERNAME=$(echo $DATABASE_URL | sed -n 's#.*//\([^:]*\):.*#\1#p') |
| 33 | +PASSWORD=$(echo $DATABASE_URL | sed -n 's#.*//[^:]*:\([^@]*\)@.*#\1#p') |
| 34 | +HOST=$(echo $DATABASE_URL | sed -n 's#.*@\(.*\):[0-9]*/.*#\1#p') |
| 35 | +PORT=$(echo $DATABASE_URL | sed -n 's#.*:\([0-9]*\)/.*#\1#p') |
| 36 | +DB_NAME=$(echo $DATABASE_URL | sed -n 's#.*/\([^?]*\).*#\1#p') |
| 37 | +SSL_MODE=$(echo $DATABASE_URL | sed -n 's#.*sslmode=\([^&]*\).*#\1#p') |
| 38 | + |
| 39 | +# Set PGPASSWORD environment variable for non-interactive password authentication |
| 40 | +export PGPASSWORD=$PASSWORD |
| 41 | + |
| 42 | +# Confirm action |
| 43 | +echo "You are about to clone schema '$SOURCE_SCHEMA' to '$TARGET_SCHEMA' and delete data for chain ID: $CHAIN_ID." |
| 44 | +read -p "Do you want to continue? (y/n): " CONFIRM |
| 45 | +if [ "$CONFIRM" != "y" ]; then |
| 46 | + echo "Operation cancelled." |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +# Function to handle errors |
| 51 | +handle_error() { |
| 52 | + echo "Error occurred. Exiting." |
| 53 | + unset PGPASSWORD |
| 54 | + exit 1 |
| 55 | +} |
| 56 | + |
| 57 | +# Trap errors and call handle_error function |
| 58 | +trap 'handle_error' ERR |
| 59 | + |
| 60 | +# Check SSL certificates if SSL mode is required |
| 61 | +if [ "$SSL_MODE" == "require" ]; then |
| 62 | + echo "SSL mode is enabled. Checking for SSL certificates in $SSL_DIR..." |
| 63 | + if [ ! -f "$SSL_ROOT_CERT" ]; then |
| 64 | + echo "Missing $SSL_ROOT_CERT. Please place the CA certificate in the $SSL_DIR directory." |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | + if [ ! -f "$SSL_CERT" ]; then |
| 68 | + echo "Missing $SSL_CERT. Please place the client certificate in the $SSL_DIR directory." |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + if [ ! -f "$SSL_KEY" ]; then |
| 72 | + echo "Missing $SSL_KEY. Please place the client key in the $SSL_DIR directory." |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | +fi |
| 76 | + |
| 77 | +# Connection string with SSL options if required |
| 78 | +if [ "$SSL_MODE" == "require" ]; then |
| 79 | + CONNECTION_STRING="sslmode=$SSL_MODE sslrootcert=$SSL_ROOT_CERT sslcert=$SSL_CERT sslkey=$SSL_KEY host=$HOST port=$PORT dbname=$DB_NAME user=$USERNAME password=$PASSWORD" |
| 80 | +else |
| 81 | + CONNECTION_STRING="host=$HOST port=$PORT dbname=$DB_NAME user=$USERNAME password=$PASSWORD" |
| 82 | +fi |
| 83 | + |
| 84 | +# Check if target schema exists |
| 85 | +SCHEMA_EXISTS=$(psql "$CONNECTION_STRING" -tAc "SELECT 1 FROM information_schema.schemata WHERE schema_name = '$TARGET_SCHEMA';") |
| 86 | + |
| 87 | +if [ "$SCHEMA_EXISTS" == "1" ]; then |
| 88 | + echo "Error: Target schema '$TARGET_SCHEMA' already exists. Exiting." |
| 89 | + unset PGPASSWORD |
| 90 | + exit 1 |
| 91 | +fi |
| 92 | + |
| 93 | +echo "Creating new target schema..." |
| 94 | +psql "$CONNECTION_STRING" -c "CREATE SCHEMA $TARGET_SCHEMA;" |
| 95 | + |
| 96 | +echo "Cloning schema and data..." |
| 97 | +pg_dump "$CONNECTION_STRING" -n $SOURCE_SCHEMA | sed "s/$SOURCE_SCHEMA/$TARGET_SCHEMA/g" | psql "$CONNECTION_STRING" |
| 98 | + |
| 99 | +# Deleting data for the specified chain ID |
| 100 | +echo "Deleting data for chain ID: $CHAIN_ID" |
| 101 | +psql "$CONNECTION_STRING" <<EOF |
| 102 | +DO \$\$ |
| 103 | +BEGIN |
| 104 | + -- Delete from pending_round_roles |
| 105 | + DELETE FROM $TARGET_SCHEMA.pending_round_roles WHERE chain_id = $CHAIN_ID; |
| 106 | +
|
| 107 | + -- Delete from round_roles |
| 108 | + DELETE FROM $TARGET_SCHEMA.round_roles WHERE chain_id = $CHAIN_ID; |
| 109 | +
|
| 110 | + -- Delete from pending_project_roles |
| 111 | + DELETE FROM $TARGET_SCHEMA.pending_project_roles WHERE chain_id = $CHAIN_ID; |
| 112 | +
|
| 113 | + -- Delete from project_roles |
| 114 | + DELETE FROM $TARGET_SCHEMA.project_roles WHERE chain_id = $CHAIN_ID; |
| 115 | +
|
| 116 | + -- Delete from applications |
| 117 | + DELETE FROM $TARGET_SCHEMA.applications WHERE chain_id = $CHAIN_ID; |
| 118 | +
|
| 119 | + -- Delete from donations |
| 120 | + DELETE FROM $TARGET_SCHEMA.donations WHERE chain_id = $CHAIN_ID; |
| 121 | +
|
| 122 | + -- Delete from rounds |
| 123 | + DELETE FROM $TARGET_SCHEMA.rounds WHERE chain_id = $CHAIN_ID; |
| 124 | +
|
| 125 | + -- Delete from projects |
| 126 | + DELETE FROM $TARGET_SCHEMA.projects WHERE chain_id = $CHAIN_ID; |
| 127 | +END |
| 128 | +\$\$; |
| 129 | +EOF |
| 130 | + |
| 131 | +# Unset PGPASSWORD |
| 132 | +unset PGPASSWORD |
| 133 | + |
| 134 | +echo "Schema $SOURCE_SCHEMA has been successfully cloned to $TARGET_SCHEMA in database $DB_NAME" |
| 135 | +echo "Deleted chain data for chain ID: $CHAIN_ID" |
0 commit comments