From 4d8a3b6d155d890103b46881a082b47a19a2190b Mon Sep 17 00:00:00 2001 From: Taksh Date: Wed, 29 Jul 2026 10:24:38 +0300 Subject: [PATCH] fix(scripts): derive seed PG* from DATABASE_URL Keep community seeding on the same Postgres migrations use when PGPORT in .env has drifted from DATABASE_URL. Signed-off-by: Taksh --- .env.example | 4 +++- scripts/seed-local-community.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 3dc54856e7e..58494b2d157 100644 --- a/.env.example +++ b/.env.example @@ -7,7 +7,7 @@ # All defaults here work with `docker compose up` out of the box. # # Service ports (defaults): -# Postgres → localhost:5432 +# Postgres → localhost:5432 (keep PGPORT in sync with DATABASE_URL) # Redis → localhost:6379 # Typesense → localhost:8108 # Adminer → localhost:8082 (DB browser UI) @@ -21,6 +21,8 @@ DATABASE_URL=postgres://buzz:buzz_dev@localhost:5432/buzz # Optional read-replica URL; unset/blank keeps all reads on the writer. # READ_DATABASE_URL=postgres://buzz:buzz_dev@localhost:5433/buzz +# scripts/seed-local-community.sh derives PG* from DATABASE_URL when set, so a +# stale PGPORT in this file cannot send seeding at the wrong Postgres. PGHOST=localhost PGPORT=5432 PGUSER=buzz diff --git a/scripts/seed-local-community.sh b/scripts/seed-local-community.sh index 41ff403d0f4..bfa267b66b4 100755 --- a/scripts/seed-local-community.sh +++ b/scripts/seed-local-community.sh @@ -17,6 +17,37 @@ if [[ -f ".env" ]]; then set +o allexport fi +# Prefer DATABASE_URL when set so PGHOST/PGPORT/… can't drift from the URL +# migrations already use (#2479). Explicit PG* still win only when DATABASE_URL +# is unset. +if [[ -n "${DATABASE_URL:-}" ]]; then + eval "$(python3 - <<'PY' +import os +from urllib.parse import unquote, urlparse + +url = os.environ["DATABASE_URL"] +parsed = urlparse(url) +if parsed.scheme not in {"postgres", "postgresql"}: + raise SystemExit(f"DATABASE_URL must be postgres://…, got {parsed.scheme!r}") + +def sh_export(name: str, value: str) -> None: + print(f"export {name}={value!r}") + +if parsed.hostname: + sh_export("PGHOST", parsed.hostname) +if parsed.port is not None: + sh_export("PGPORT", str(parsed.port)) +if parsed.username: + sh_export("PGUSER", unquote(parsed.username)) +if parsed.password is not None: + sh_export("PGPASSWORD", unquote(parsed.password)) +db = (parsed.path or "").lstrip("/") +if db: + sh_export("PGDATABASE", unquote(db)) +PY +)" +fi + export PGHOST="${PGHOST:-localhost}" export PGPORT="${PGPORT:-5432}" export PGUSER="${PGUSER:-buzz}"