f2a12f9fed
v0.4 (Operator Tier — Cohort Dashboard + Auth + Postgres) milestone complete. Phases: ✓ P0 pre-execution (planning) → v0.1.6 ✓ P1 operator foundation (Postgres+auth+VC migration) → v0.1.7 ✓ P2 cohort dashboard + aggregation → v0.1.8 ✓ P3 final review + ship → v0.1.9 (= v0.4 milestone release) Requirements covered (8/8): REQ-MT-01 (Postgres store), REQ-MT-02 (aggregation pipeline), REQ-AUTH-01 (operator auth), REQ-DASH-01 (cohort dashboard), REQ-NFR-AUTH-01 (auth NFRs), REQ-NFR-MT-01 (Postgres-in-LXC), REQ-NFR-DASH-01 (k-anonymity ≥10), REQ-NFR-DASH-02 (freshness ≤24h) Grill MUSTs honored (6/6): G-008, G-011, G-027, G-031, G-038, G-041 Tests: 317 pytest pass, 36 skip (Postgres-requiring), 0 fail; 17/17 vitest pass Review: APPROVE_WITH_NOTES (6/6 personas, 0 P0, 8 P1+ carry-forward) Audit: HEALTHY (reconstruction PASS, 8/8 REQ, 6/6 grill) ---ci--- project: praxis phase: 3 milestone: v0.4 status: complete phase_role: final milestone_complete: true milestone_merged_to_main: true tag: v0.1.9 requirements: covered: [REQ-MT-01, REQ-MT-02, REQ-AUTH-01, REQ-DASH-01, REQ-NFR-AUTH-01, REQ-NFR-MT-01, REQ-NFR-DASH-01, REQ-NFR-DASH-02] partial: [] ---/ci---
71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
"""Postgres migration runner — applies db/pg_migrations/*.sql in order.
|
|
|
|
Mirrors db/migrate.py: ordered .sql files tracked in a `_pg_migrations`
|
|
table so re-running is idempotent. Uses an asyncpg pool. Retries on
|
|
connection failure (3 attempts, 2s backoff — R-MT-02 mitigation).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import datetime as _dt
|
|
from pathlib import Path
|
|
|
|
import asyncpg
|
|
|
|
_DEFAULT_MIGRATIONS_DIR = Path(__file__).resolve().parent / "pg_migrations"
|
|
_RETRY_ATTEMPTS = 3
|
|
_RETRY_BACKOFF_S = 2.0
|
|
|
|
|
|
async def apply_pg_migrations(
|
|
pool: asyncpg.Pool,
|
|
migrations_dir: Path | None = None,
|
|
) -> list[str]:
|
|
"""Apply all pending Postgres migrations in order. Returns applied names.
|
|
|
|
Idempotent — no-op if all migrations are already applied. Each migration
|
|
runs within a transaction; the `_pg_migrations` tracking row is inserted
|
|
in the same transaction so a failure rolls back cleanly.
|
|
"""
|
|
mdir = migrations_dir or _DEFAULT_MIGRATIONS_DIR
|
|
if not mdir.exists():
|
|
return []
|
|
|
|
async def _run() -> list[str]:
|
|
async with pool.acquire() as conn:
|
|
await conn.execute(
|
|
"CREATE TABLE IF NOT EXISTS _pg_migrations ("
|
|
"id TEXT PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT now()"
|
|
")"
|
|
)
|
|
rows = await conn.fetch("SELECT id FROM _pg_migrations")
|
|
applied_ids = {r["id"] for r in rows}
|
|
applied: list[str] = []
|
|
for sql_path in sorted(mdir.glob("*.sql")):
|
|
mid = sql_path.stem
|
|
if mid in applied_ids:
|
|
continue
|
|
sql = sql_path.read_text(encoding="utf-8")
|
|
async with conn.transaction():
|
|
await conn.execute(sql)
|
|
await conn.execute(
|
|
"INSERT INTO _pg_migrations (id) VALUES ($1)", mid
|
|
)
|
|
applied.append(mid)
|
|
return applied
|
|
|
|
last_exc: Exception | None = None
|
|
for attempt in range(1, _RETRY_ATTEMPTS + 1):
|
|
try:
|
|
return await _run()
|
|
except (asyncpg.PostgresConnectionError, ConnectionError, OSError) as exc:
|
|
last_exc = exc
|
|
if attempt < _RETRY_ATTEMPTS:
|
|
await asyncio.sleep(_RETRY_BACKOFF_S)
|
|
continue
|
|
assert last_exc is not None
|
|
raise last_exc
|
|
|
|
|
|
__all__ = ["apply_pg_migrations"] |