Skip to content

Commit f54cd73

Browse files
committed
allow user defined init script for restoring paused db dumps
1 parent 074a2ec commit f54cd73

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

nix/tools/run-client.sh.in

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[ ! -z "$DEBUG" ] && set -x
55

6-
# first argument should be '15' or '16' for the version
6+
# Determine PostgreSQL version
77
if [ "$1" == "15" ]; then
88
echo "Starting client for PSQL 15"
99
PSQL15=@PSQL15_BINDIR@
@@ -20,11 +20,22 @@ else
2020
echo "Please provide a valid Postgres version (15, 16, or orioledb-16)"
2121
exit 1
2222
fi
23+
24+
shift 1
25+
2326
#vars for migration.sh
2427
export PATH=$BINDIR/bin:$PATH
2528
export POSTGRES_DB=postgres
2629
export POSTGRES_HOST=localhost
2730
export POSTGRES_PORT=@PGSQL_DEFAULT_PORT@
31+
32+
# Optional second argument: path to custom migration script
33+
if [ -n "$1" ]; then
34+
MIGRATION_FILE="$1"
35+
else
36+
MIGRATION_FILE=""
37+
fi
38+
2839
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"
2940
PGSQL_SUPERUSER=@PGSQL_SUPERUSER@
3041
MIGRATIONS_DIR=@MIGRATIONS_DIR@
@@ -35,20 +46,37 @@ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PGSQL_SUPERUSER" -p "$POR
3546
create role postgres superuser login password '$PGPASSWORD';
3647
alter database postgres owner to postgres;
3748
EOSQL
38-
for sql in "$MIGRATIONS_DIR"/init-scripts/*.sql; do
39-
echo "$0: running $sql"
40-
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -f "$sql" postgres
41-
done
42-
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"
43-
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$PGBOUNCER_AUTH_SCHEMA_SQL"
44-
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$STAT_EXTENSION_SQL"
45-
# run migrations as super user - postgres user demoted in post-setup
46-
for sql in "$MIGRATIONS_DIR"/migrations/*.sql; do
47-
echo "$0: running $sql"
48-
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -f "$sql" postgres
49-
done
50-
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -f "$POSTGRESQL_SCHEMA_SQL" postgres
51-
# TODO Do we need to reset stats when running migrations locally?
52-
#psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -c 'SELECT extensions.pg_stat_statements_reset(); SELECT pg_stat_reset();' postgres || true
5349

50+
# Use custom migration script if provided
51+
if [ -n "$MIGRATION_FILE" ]; then
52+
echo "$0: running user-provided migration file $MIGRATION_FILE"
53+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PGSQL_SUPERUSER" -p "$PORTNO" -h localhost -f "$MIGRATION_FILE" postgres
54+
else
55+
# Run default init scripts
56+
for sql in "$MIGRATIONS_DIR"/init-scripts/*.sql; do
57+
echo "$0: running $sql"
58+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -f "$sql" postgres
59+
done
60+
61+
# Alter user password
62+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"
63+
64+
# Run additional schema files
65+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$PGBOUNCER_AUTH_SCHEMA_SQL"
66+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$STAT_EXTENSION_SQL"
67+
68+
# Run migrations as superuser
69+
for sql in "$MIGRATIONS_DIR"/migrations/*.sql; do
70+
echo "$0: running $sql"
71+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -f "$sql" postgres
72+
done
73+
74+
# Run PostgreSQL schema
75+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -f "$POSTGRESQL_SCHEMA_SQL" postgres
76+
fi
77+
78+
# Optional: Reset stats if needed
79+
# psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -c 'SELECT extensions.pg_stat_statements_reset(); SELECT pg_stat_reset();' postgres || true
80+
81+
# Start interactive psql session
5482
exec psql -U postgres -p "$PORTNO" -h localhost postgres

0 commit comments

Comments
 (0)