fc5: strip SQLAlchemy +psycopg/+asyncpg driver suffix when passing URL to pg_dump/psql
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,19 @@ from typing import Any
|
|||||||
_BACKUPS_DIRNAME = "_backups"
|
_BACKUPS_DIRNAME = "_backups"
|
||||||
|
|
||||||
|
|
||||||
|
def _libpq_url(sa_url: str) -> str:
|
||||||
|
"""Strip SQLAlchemy driver suffix so pg_dump/psql accept the URL.
|
||||||
|
|
||||||
|
SQLAlchemy uses URLs like `postgresql+psycopg://...` or
|
||||||
|
`postgresql+asyncpg://...`. libpq tools (pg_dump, psql) only know
|
||||||
|
the plain `postgresql://` scheme.
|
||||||
|
"""
|
||||||
|
for driver in ("postgresql+psycopg", "postgresql+asyncpg", "postgresql+psycopg2"):
|
||||||
|
if sa_url.startswith(driver + "://"):
|
||||||
|
return "postgresql://" + sa_url[len(driver) + 3:]
|
||||||
|
return sa_url
|
||||||
|
|
||||||
|
|
||||||
def _backups_dir(images_root: Path | None = None) -> Path:
|
def _backups_dir(images_root: Path | None = None) -> Path:
|
||||||
# Overridable for tests via monkeypatch.
|
# Overridable for tests via monkeypatch.
|
||||||
root = images_root if images_root is not None else Path("/images")
|
root = images_root if images_root is not None else Path("/images")
|
||||||
@@ -49,7 +62,7 @@ def create_backup(
|
|||||||
manifest_path = out_dir / f"fc_{ts}.json"
|
manifest_path = out_dir / f"fc_{ts}.json"
|
||||||
|
|
||||||
_run_subprocess(
|
_run_subprocess(
|
||||||
["pg_dump", "--no-owner", "--no-acl", "-f", str(sql_path), db_url],
|
["pg_dump", "--no-owner", "--no-acl", "-f", str(sql_path), _libpq_url(db_url)],
|
||||||
_test_ts=ts,
|
_test_ts=ts,
|
||||||
)
|
)
|
||||||
_run_subprocess(
|
_run_subprocess(
|
||||||
@@ -104,7 +117,7 @@ def restore_backup(
|
|||||||
tar_path = Path(manifest["tar_path"])
|
tar_path = Path(manifest["tar_path"])
|
||||||
|
|
||||||
_run_subprocess(
|
_run_subprocess(
|
||||||
["psql", "-d", db_url, "-f", str(sql_path)],
|
["psql", "-d", _libpq_url(db_url), "-f", str(sql_path)],
|
||||||
)
|
)
|
||||||
|
|
||||||
# Wipe everything in images_root EXCEPT _backups/ (we'd delete the backup
|
# Wipe everything in images_root EXCEPT _backups/ (we'd delete the backup
|
||||||
|
|||||||
Reference in New Issue
Block a user