-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcleanup-db.sh
executable file
·82 lines (67 loc) · 3.05 KB
/
cleanup-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
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
#!/bin/sh
# This script should be compatible with MSYS, the compatibility layer used by
# Git for Windows. Absolute paths which should not be converted to windows paths
# have to start with //, see https://github.com/git-for-windows/git/issues/1387
# On windows, docker cp does not like leading double / on the container path.
# As a workaround, use relative paths: container:tmp/foo instead of container://tmp/foo
set -e
# Check arguments
if [ "$#" -ne 0 ]; then
echo "usage: $0"
exit 1
fi
root_path=$(realpath "$(dirname "$0")/..")
# These variables are necessary to load the infra on the correct instance (the pr-infra or the dev one)
OSRD_POSTGRES="osrd-postgres"
OSRD_EDITOAST="osrd-editoast"
OSRD_VALKEY="osrd-valkey"
OSRD_VALKEY_VOLUME="osrd_valkey_data"
OSRD_POSTGRES_PORT=5432
OSRD_VALKEY_PORT=6379
if [ "$PR_TEST" -eq 1 ]; then
OSRD_POSTGRES="osrd-postgres-pr-tests"
OSRD_EDITOAST="osrd-editoast-pr-tests"
OSRD_VALKEY="osrd-valkey-pr-tests"
OSRD_VALKEY_VOLUME="osrd-pr-tests_valkey_data"
OSRD_POSTGRES_PORT=5433
OSRD_VALKEY_PORT=6380
fi
# Check if the database exists
echo "Checking database exists..."
DB_EXISTS="$(docker exec $OSRD_POSTGRES psql -p $OSRD_POSTGRES_PORT -c "SELECT EXISTS (SELECT FROM pg_stat_database WHERE datname = 'osrd');")"
DB_EXISTS="$(echo "$DB_EXISTS" | grep -o -E '[tf]$')"
if [ "$DB_EXISTS" = 't' ]; then
echo " Database 'osrd' found"
else
echo " Database 'osrd' not found"
fi
if [ "$DB_EXISTS" = 't' ]; then
# Check that no service is connected to the database
echo "Checking database availability..."
# if editoast is running, shut it down.
if [ "$(docker ps -q -f name=$OSRD_EDITOAST)" ]; then
echo "Stopping $OSRD_EDITOAST..."
docker stop "$OSRD_EDITOAST"
fi
DB_CONN="$(docker exec "$OSRD_POSTGRES" psql -p "$OSRD_POSTGRES_PORT" -c "SELECT numbackends FROM pg_stat_database WHERE datname = 'osrd';")"
DB_CONN="$(echo "$DB_CONN" | grep -o -E '[0-9]+$')"
if [ "$DB_CONN" -ne 0 ]; then
echo " The database can not be cleared."
echo " A process is connected to your database, please check it and close it."
echo " In doubt, you can shutdown the whole compose stack and only keep the database running."
exit 2
fi
# Drop database
echo "Deleting osrd database..."
docker exec "$OSRD_POSTGRES" psql -p "$OSRD_POSTGRES_PORT" -c 'DROP DATABASE osrd;' > /dev/null
fi
echo "Initialize new database (no migration)..."
# Here I remove the first line of the script cause the user already exists
docker exec "$OSRD_POSTGRES" sh -c 'cat /docker-entrypoint-initdb.d/init.sql | tail -n 1 > /tmp/init.sql'
docker exec "$OSRD_POSTGRES" psql -p "$OSRD_POSTGRES_PORT" -f //tmp/init.sql > /dev/null
# Clear Valkey Cache
echo "Deleting valkey cache..."
docker exec "$OSRD_VALKEY" valkey-cli -p "$OSRD_VALKEY_PORT" FLUSHALL > /dev/null 2>&1 || docker volume rm -f $OSRD_VALKEY_VOLUME > /dev/null
echo 'Cleanup done!'
echo "You may want to apply migrations if you don't load a backup:"
echo "'diesel migration run --migration-dir \"$root_path/editoast/migrations\"' # 'docker compose up editoast' does it automatically"