From ff6d7998c3f92410016578859c9bc4fb8aa86130 Mon Sep 17 00:00:00 2001 From: nessi Date: Thu, 12 Feb 2026 14:36:07 +0100 Subject: [PATCH] Add support for multiple PostgreSQL DSN candidates This update introduces `PG_DSN_CANDIDATES` for specifying multiple DSN options, improving compatibility and CI portability. The script now attempts connections sequentially using the provided candidates before falling back to single DSN or raising an error. Relevant updates to documentation and workflow configuration have also been made. --- .github/workflows/pg-compat-matrix.yml | 2 +- README.md | 7 ++++++ backend/scripts/pg_compat_smoke.py | 32 +++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/.github/workflows/pg-compat-matrix.yml b/.github/workflows/pg-compat-matrix.yml index 71c4dbd..55c4d14 100644 --- a/.github/workflows/pg-compat-matrix.yml +++ b/.github/workflows/pg-compat-matrix.yml @@ -43,5 +43,5 @@ jobs: - name: Run PostgreSQL compatibility smoke checks env: - PG_DSN: postgresql://postgres:postgres@127.0.0.1:5432/compatdb?sslmode=disable + PG_DSN_CANDIDATES: postgresql://postgres:postgres@postgres:5432/compatdb?sslmode=disable,postgresql://postgres:postgres@127.0.0.1:5432/compatdb?sslmode=disable run: python backend/scripts/pg_compat_smoke.py diff --git a/README.md b/README.md index ff4bc85..dccac4f 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,13 @@ PG_DSN='postgresql://postgres:postgres@127.0.0.1:5432/compatdb?sslmode=disable' python backend/scripts/pg_compat_smoke.py ``` +CI/runner-safe variant (tries multiple hosts): + +```bash +PG_DSN_CANDIDATES='postgresql://postgres:postgres@postgres:5432/compatdb?sslmode=disable,postgresql://postgres:postgres@127.0.0.1:5432/compatdb?sslmode=disable' \ +python backend/scripts/pg_compat_smoke.py +``` + ## Environment variables reference All variables are defined in `.env.example`. diff --git a/backend/scripts/pg_compat_smoke.py b/backend/scripts/pg_compat_smoke.py index 14facab..8d953d3 100644 --- a/backend/scripts/pg_compat_smoke.py +++ b/backend/scripts/pg_compat_smoke.py @@ -31,13 +31,39 @@ async def _fetchrow_required(conn: asyncpg.Connection, query: str, label: str) - return dict(row) +def _dsn_candidates() -> list[str]: + # Preferred: explicit candidate list for CI portability (Gitea/GitHub runners). + raw_candidates = os.getenv("PG_DSN_CANDIDATES", "").strip() + if raw_candidates: + values = [item.strip() for item in raw_candidates.split(",") if item.strip()] + if values: + return values + # Backward compatible single DSN. + raw_single = os.getenv("PG_DSN", "").strip() + if raw_single: + return [raw_single] + raise RuntimeError("Missing PG_DSN or PG_DSN_CANDIDATES") + + async def run() -> None: - dsn = _required_env("PG_DSN") - conn = await _connect_with_retry(dsn) + candidates = _dsn_candidates() + last_error: Exception | None = None + conn: asyncpg.Connection | None = None + used_dsn = "" + for dsn in candidates: + try: + conn = await _connect_with_retry(dsn) + used_dsn = dsn + break + except Exception as exc: + last_error = exc + if conn is None: + raise RuntimeError(f"Could not connect to PostgreSQL using candidates: {last_error}") + try: version = await conn.fetchval("SHOW server_version") current_db = await conn.fetchval("SELECT current_database()") - print(f"[compat] Connected: version={version} db={current_db}") + print(f"[compat] Connected: version={version} db={current_db} dsn={used_dsn}") # Core collector queries await _fetchrow_required(