Skip to content

Commit 6757ba0

Browse files
oliricethebengeu
andauthored
Support restoring a user defined backup (#1254)
* allow user defined init script for restoring paused db dumps * make start-client CLI more erganomic with named args and help * update description in cli * Update nix/tools/run-client.sh.in Co-authored-by: Beng Eu <[email protected]> --------- Co-authored-by: Beng Eu <[email protected]>
1 parent 7295fc0 commit 6757ba0

File tree

1 file changed

+106
-20
lines changed

1 file changed

+106
-20
lines changed

nix/tools/run-client.sh.in

Lines changed: 106 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,97 @@
33

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

6-
# first argument should be '15' or '16' for the version
7-
if [ "$1" == "15" ]; then
6+
# Default values
7+
PSQL_VERSION="15"
8+
MIGRATION_FILE=""
9+
PORTNO="@PGSQL_DEFAULT_PORT@"
10+
11+
# Function to display help
12+
print_help() {
13+
echo "Usage: nix run .#start-client -- [options]"
14+
echo
15+
echo "Options:"
16+
echo " -v, --version [15|16|orioledb-16] Specify the PostgreSQL version to use (required)"
17+
echo " -f, --file FILE Provide a custom migration script"
18+
echo " -h, --help Show this help message"
19+
echo
20+
echo "Description:"
21+
echo " Starts an interactive 'psql' session connecting to a Postgres database started with the"
22+
echo " 'nix run .#start-server' command. If a migration file is not provided, the client"
23+
echo " initializes the database with the default migrations for a new Supabase project."
24+
echo " If a migrations file is provided, default migrations are skipped"
25+
echo " If no migration file is provided, it runs the default Supabase migrations."
26+
echo
27+
echo "Examples:"
28+
echo " nix run .#start-client"
29+
echo " nix run .#start-client -- --version 15"
30+
echo " nix run .#start-client -- --version 16 --file custom_migration.sql"
31+
echo " nix run .#start-client -- --version 16 --port 5433"
32+
}
33+
34+
# Parse arguments
35+
while [[ "$#" -gt 0 ]]; do
36+
case "$1" in
37+
-v|--version)
38+
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
39+
PSQL_VERSION="$2"
40+
shift 2
41+
else
42+
echo "Error: --version requires an argument (15, 16, or orioledb-16)"
43+
exit 1
44+
fi
45+
;;
46+
-f|--file)
47+
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
48+
MIGRATION_FILE="$2"
49+
shift 2
50+
else
51+
echo "Error: --file requires a filename"
52+
exit 1
53+
fi
54+
;;
55+
-h|--help)
56+
print_help
57+
exit 0
58+
;;
59+
*)
60+
echo "Unknown option: $1"
61+
print_help
62+
exit 1
63+
;;
64+
esac
65+
done
66+
67+
# Check if version is provided
68+
if [[ -z "$PSQL_VERSION" ]]; then
69+
echo "Error: PostgreSQL version is required."
70+
print_help
71+
exit 1
72+
fi
73+
74+
# Determine PostgreSQL version
75+
if [ "$PSQL_VERSION" == "15" ]; then
876
echo "Starting client for PSQL 15"
977
PSQL15=@PSQL15_BINDIR@
1078
BINDIR="$PSQL15"
11-
elif [ "$1" == "16" ]; then
79+
elif [ "$PSQL_VERSION" == "16" ]; then
1280
echo "Starting client for PSQL 16"
1381
PSQL16=@PSQL16_BINDIR@
1482
BINDIR="$PSQL16"
15-
elif [ "$1" == "orioledb-16" ]; then
83+
elif [ "$PSQL_VERSION" == "orioledb-16" ]; then
1684
echo "Starting client for PSQL ORIOLEDB 16"
1785
PSQLORIOLEDB16=@PSQLORIOLEDB16_BINDIR@
1886
BINDIR="$PSQLORIOLEDB16"
1987
else
2088
echo "Please provide a valid Postgres version (15, 16, or orioledb-16)"
2189
exit 1
2290
fi
91+
2392
#vars for migration.sh
2493
export PATH=$BINDIR/bin:$PATH
2594
export POSTGRES_DB=postgres
2695
export POSTGRES_HOST=localhost
27-
export POSTGRES_PORT=@PGSQL_DEFAULT_PORT@
96+
2897
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"
2998
PGSQL_SUPERUSER=@PGSQL_SUPERUSER@
3099
MIGRATIONS_DIR=@MIGRATIONS_DIR@
@@ -35,20 +104,37 @@ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PGSQL_SUPERUSER" -p "$POR
35104
create role postgres superuser login password '$PGPASSWORD';
36105
alter database postgres owner to postgres;
37106
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
53107

108+
# Use custom migration script if provided
109+
if [ -n "$MIGRATION_FILE" ]; then
110+
echo "$0: running user-provided migration file $MIGRATION_FILE"
111+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PGSQL_SUPERUSER" -p "$PORTNO" -h localhost -f "$MIGRATION_FILE" postgres
112+
else
113+
# Run default init scripts
114+
for sql in "$MIGRATIONS_DIR"/init-scripts/*.sql; do
115+
echo "$0: running $sql"
116+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -f "$sql" postgres
117+
done
118+
119+
# Alter user password
120+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"
121+
122+
# Run additional schema files
123+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$PGBOUNCER_AUTH_SCHEMA_SQL"
124+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$STAT_EXTENSION_SQL"
125+
126+
# Run migrations as superuser
127+
for sql in "$MIGRATIONS_DIR"/migrations/*.sql; do
128+
echo "$0: running $sql"
129+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -f "$sql" postgres
130+
done
131+
132+
# Run PostgreSQL schema
133+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -f "$POSTGRESQL_SCHEMA_SQL" postgres
134+
fi
135+
136+
# Optional: Reset stats if needed
137+
# 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
138+
139+
# Start interactive psql session
54140
exec psql -U postgres -p "$PORTNO" -h localhost postgres

0 commit comments

Comments
 (0)