Skip to content

Commit b115aff

Browse files
committed
clone schema and clean chain
prompt input changes script updates clear subscriptions add readme Update src/config.ts
1 parent 43ff8de commit b115aff

File tree

3 files changed

+187
-1
lines changed

3 files changed

+187
-1
lines changed

READ_REINDEX_SINGLE_CHAIN.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Overview
2+
3+
This guide provides instructions on how to clone a source PostgreSQL schema to a target schema, delete specific data for a given chain ID, and subsequently restart the indexer to reindex the removed chain.
4+
5+
## Prerequisites
6+
7+
1. **Node.js and npm installed** (to run the script and manage packages).
8+
2. **PostgreSQL installed and configured**.
9+
10+
## Configuration
11+
12+
Before running the script, ensure you have the following configurations in place:
13+
14+
**Database URL** in the `.env` file:
15+
16+
```
17+
DATABASE_URL=postgres://user:password@localhost:5432/your_database?sslmode=no-verify
18+
```
19+
20+
## Running the Script
21+
22+
1. **Make the script executable**:
23+
```bash
24+
chmod +x cloneSchemaAndCleanChain.sh
25+
```
26+
27+
2. **Run the script**:
28+
```bash
29+
./cloneSchemaAndCleanChain.sh
30+
```
31+
32+
You will be prompted to enter:
33+
- Source schema number
34+
- Target schema number
35+
- Chain ID to delete data for
36+
37+
Example:
38+
```bash
39+
Enter source schema number: 1
40+
Enter target schema number: 2
41+
Enter chain ID to delete data for: 1329
42+
```
43+
44+
## Reindexing the Chain
45+
46+
- Open `config.ts` and modify the configuration for the chain data to point to the new target schema.
47+
48+
- After the script has successfully cloned the schema and deleted the data for the specified chain ID, you can start the indexer as usual. The indexer will detect the missing data for the chain ID and begin reindexing it.

cloneSchemaAndCleanChain.sh

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
128+
-- Update subscriptions
129+
UPDATE $TARGET_SCHEMA.subscriptions SET indexed_to_block = 0::bigint WHERE chain_id = $CHAIN_ID;
130+
END
131+
\$\$;
132+
EOF
133+
134+
# Unset PGPASSWORD
135+
unset PGPASSWORD
136+
137+
echo "Schema $SOURCE_SCHEMA has been successfully cloned to $TARGET_SCHEMA in database $DB_NAME"
138+
echo "Deleted chain data for chain ID: $CHAIN_ID"

src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ const CHAINS: Chain[] = [
15021502
.default("https://evm-rpc.sei-apis.com")
15031503
.parse(process.env.SEI_MAINNET_RPC_URL),
15041504
pricesFromTimestamp: Date.UTC(2024, 0, 1, 0, 0, 0),
1505-
maxGetLogsRange: 10000,
1505+
maxGetLogsRange: 1000,
15061506
tokens: [
15071507
{
15081508
code: "SEI",

0 commit comments

Comments
 (0)