-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathclear-db.sh
36 lines (28 loc) · 1.26 KB
/
clear-db.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
#!/usr/bin/env bash
. ./scripts/devcontainer/_assert-in-container "$0" "$@"
set -euo pipefail
# Clear DBs
DBLOG="${DARK_CONFIG_RUNDIR}/clear-db.log"
echo "Clearing old DB data (logs in ${DBLOG})"
DB="${DARK_CONFIG_DB_DBNAME}"
function run_sql { psql -d "$DB" -c "$@" >> "$DBLOG" ; }
function fetch_sql { psql -d "$DB" -t -c "$@"; }
CANVASES=$(fetch_sql "SELECT id FROM canvases WHERE substring(name, 0, 6)
= 'test-';")
SCRIPT=""
for cid in $CANVASES; do
SCRIPT+="DELETE FROM scheduling_rules_v0 WHERE canvas_id = '$cid';";
SCRIPT+="DELETE FROM traces_v0 WHERE canvas_id = '$cid';";
SCRIPT+="DELETE FROM user_data_v0 WHERE canvas_id = '$cid';";
SCRIPT+="DELETE FROM cron_records_v0 WHERE canvas_id = '$cid';";
SCRIPT+="DELETE FROM toplevels_v0 WHERE canvas_id = '$cid';";
SCRIPT+="DELETE FROM canvases_v0 WHERE id = '$cid';";
SCRIPT+="DELETE FROM secrets_v0 WHERE canvas_id = '$cid';";
done
SCRIPT+="DELETE FROM package_functions_v0 WHERE author_id IN (SELECT id FROM accounts_v0
WHERE username = 'test_admin');";
SCRIPT+="DELETE FROM package_types_v0 WHERE author_id IN (SELECT id FROM accounts_v0
WHERE username = 'test_admin');";
SCRIPT+="DELETE FROM package_constants_v0 WHERE author_id IN (SELECT id FROM accounts_v0
WHERE username = 'test_admin');";
run_sql "$SCRIPT";