From c79a3bd84bf2db3d802def1575c1f257692ef24e Mon Sep 17 00:00:00 2001 From: breken-ai <312387581+breken-ai@users.noreply.github.com> Date: Fri, 25 Sep 2026 21:26:17 -0700 Subject: [PATCH] fix(start-server): stop and exit when a setup step fails Every failed setup step called `'stop_postgres' 1`, which neither exits nor stops a daemonized server: stop_postgres ignores its argument and, in --daemonize mode, only prints a message. A failing init script or migration therefore let the script carry on with the remaining scripts, restart the server and exit 0 with a partially initialized database. Add abort_setup, which stops the setup server with pg_ctl and exits 1, and use it for every failure path. --- nix/tools/run-server.sh.in | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/nix/tools/run-server.sh.in b/nix/tools/run-server.sh.in index 6090cab8f0..ae536bee22 100644 --- a/nix/tools/run-server.sh.in +++ b/nix/tools/run-server.sh.in @@ -63,6 +63,14 @@ stop_postgres() { fi } +# Setup failed: stop the server that was started for setup (in either mode) and +# exit non-zero, instead of carrying on with a partially initialized database. +abort_setup() { + echo "Error: database setup failed, stopping PostgreSQL" >&2 + pg_ctl stop -D "$DATDIR" -m fast || true + exit 1 +} + trap 'stop_postgres' SIGINT SIGTERM # Parse arguments @@ -311,7 +319,7 @@ for i in {1..60}; do sleep 1 if [ $i -eq 60 ]; then echo "PostgreSQL failed to start" - 'stop_postgres' 1 + abort_setup fi done @@ -328,47 +336,47 @@ if [ "$SKIP_MIGRATIONS" = false ]; then alter database postgres owner to postgres; EOSQL then - 'stop_postgres' 1 + abort_setup fi if [ -n "$MIGRATION_FILE" ]; then echo "Running user-provided migration file $MIGRATION_FILE" if ! psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PGSQL_SUPERUSER" -p "$PORTNO" -h localhost -f "$MIGRATION_FILE" postgres; then - 'stop_postgres' 1 + abort_setup fi else # Run default init scripts for sql in "$MIGRATIONS_DIR"/init-scripts/*.sql; do echo "Running $sql" if ! psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PSQL_USER" -p "$PORTNO" -h localhost -f "$sql" postgres; then - 'stop_postgres' 1 + abort_setup fi done # Set superuser password if ! psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PSQL_USER" -p "$PORTNO" -h localhost -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"; then - 'stop_postgres' 1 + abort_setup fi # Run additional schema files if ! psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PSQL_USER" -p "$PORTNO" -h localhost -d postgres -f "$PGBOUNCER_AUTH_SCHEMA_SQL"; then - 'stop_postgres' 1 + abort_setup fi if ! psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PSQL_USER" -p "$PORTNO" -h localhost -d postgres -f "$STAT_EXTENSION_SQL"; then - 'stop_postgres' 1 + abort_setup fi # Run migrations as superuser for sql in "$MIGRATIONS_DIR"/migrations/*.sql; do echo "Running $sql" if ! psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PGSQL_SUPERUSER" -p "$PORTNO" -h localhost -f "$sql" postgres; then - 'stop_postgres' 1 + abort_setup fi done # Run PostgreSQL schema if ! psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PGSQL_SUPERUSER" -p "$PORTNO" -h localhost -f "$POSTGRESQL_SCHEMA_SQL" postgres; then - 'stop_postgres' 1 + abort_setup fi fi fi