-
Notifications
You must be signed in to change notification settings - Fork 6
/
resetTables.sh
executable file
·98 lines (82 loc) · 2.96 KB
/
resetTables.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /usr/bin/env bash
#
# Resets application state by dropping blockchain related table data.
# Assumes an .env file neighbour with an authenticated postgres URI present
set -euxo pipefail
# Load psql auth vars to env
POSTGRES_USER=$(grep "POSTGRES_USER" .env | cut -d"=" -f2)
POSTGRES_PASSWORD=$(grep "POSTGRES_PASSWORD" .env | cut -d"=" -f2)
# Try to get ahold of a running postgres container to execute in,
# and if that doesn't exist we start the service from our compose cluster
started_by_script=0
if ! container=$(docker ps | grep db_boilerplate | cut -d' ' -f1); then
started_by_script=1
echo "Postgres container not alive, starting..."
container=$(docker compose \
--file docker-compose.yml \
--file docker-compose.dev.yml \
--compatibility \
run \
--no-deps \
--detach \
db_postgres)
# docker is a lot slower on MacOS due to running in a Linux VM
if [ "$(uname)" == "Darwin" ]; then
sleep 8
else
sleep 3
fi
fi
drop_schema() {
local schema=$1
echo "[resetTables] Dropping schema $schema..."
docker exec $container bash -c \
"psql -U $POSTGRES_USER -d postgres --echo-all -c 'drop schema if exists $schema cascade;'"
# Courtesy pause between calls to allow postgres to breathe
sleep 1
}
drop_tables_in_schema() {
local schema=$1
local query="$(table_drop_query $schema)"
echo "[resetTables] Dropping tables in $schema..."
# The reason we don't drop the schema and recreate it is because
# the graph puts stuff in the public schema, which if dropped cascades
# to stored functions, pg extensions, and other important things.
#
# So instead we drop tables individually, by using psql to generate
# drop statement strings that we pipe back into psql
#
# The '$query' variable expands even if it's inside single quotes
# because the heredoc decides.
#
# --tuples-only removes all fuzz from psql output, leaving only
# the tabular results (newline separated strings)
#
# --echo-all in the pipe call emits all statements psql receives
docker exec $container bash -c "psql -U $POSTGRES_USER -d postgres --tuples-only -c \"$query\" | psql -U $POSTGRES_USER -d postgres --echo-all"
# Courtesy pause between calls to allow postgres to breathe
sleep 3
}
# Generate drop table statements scoped for a particular schema.
# Heads up: the quotation here is a bit fragile
table_drop_query() {
local schema=$1
# Can't indent this, heredoc token needs to be first in a line
cat <<EOF
select 'drop table if exists \"' || schemaname || '\".\"' || tablename || '\" cascade;'
from pg_tables
where schemaname = '$schema';
EOF
}
# graph node crashes if the schemas are there, even if empty
drop_schema "chain1"
drop_schema "subgraphs"
drop_schema "sgd1"
# we can't drop the public schema since it contains other goodies
drop_tables_in_schema "public"
if [ "$started_by_script" -gt 0 ]; then
sleep 1
echo "Killing postgres container..."
docker stop "$container" > /dev/null
fi
echo "[resetTables] table cleanup done"