Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions scripts/seed-local-community.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down