Add support for multiple PostgreSQL DSN candidates
All checks were successful
PostgreSQL Compatibility Matrix / PG14 smoke (push) Successful in 8s
PostgreSQL Compatibility Matrix / PG15 smoke (push) Successful in 7s
PostgreSQL Compatibility Matrix / PG16 smoke (push) Successful in 17s
PostgreSQL Compatibility Matrix / PG17 smoke (push) Successful in 19s
PostgreSQL Compatibility Matrix / PG18 smoke (push) Successful in 18s
All checks were successful
PostgreSQL Compatibility Matrix / PG14 smoke (push) Successful in 8s
PostgreSQL Compatibility Matrix / PG15 smoke (push) Successful in 7s
PostgreSQL Compatibility Matrix / PG16 smoke (push) Successful in 17s
PostgreSQL Compatibility Matrix / PG17 smoke (push) Successful in 19s
PostgreSQL Compatibility Matrix / PG18 smoke (push) Successful in 18s
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.
This commit is contained in:
2
.github/workflows/pg-compat-matrix.yml
vendored
2
.github/workflows/pg-compat-matrix.yml
vendored
@@ -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
|
||||
|
||||
@@ -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`.
|
||||
|
||||
@@ -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")
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user