Files
praxis/db/pg_migrate.py
T
Praxis CI b0cb6280d7 feat(P01): TASK-01-04..07 Postgres DB foundation — migrate + schema + PgStore + tests
- db/pg_migrate.py: asyncpg migration runner with _pg_migrations tracking
  table, ordered .sql, transactional, 3x retry on connection failure (R-MT-02).
- db/pg_schema.sql + db/pg_migrations/0001_operator_tier.sql: 5 operator-tier
  tables (operators, issued_credentials, mastery_gate_events,
  cohort_aggregates, issuer_keys) using gen_random_uuid() (PG16 core, no
  extension). cohort_aggregates is a plain table, NOT partitioned (D-050).
- db/pg_store.py: PgStore class implementing the IssuerKeyStore protocol
  (init/get_active/get_public_key_row/set_superseded) plus operator CRUD,
  cohort aggregate read/write, credential methods, gate events.
  get_public_key_row queries by id (not status) → finds superseded keys
  (R-VC-MIG-01 verification fallback, D-051). No cross-DB FKs (D-031).
- tests/test_pg_store.py: 13 integration tests (skip if PRAXIS_PG_DSN unset).

---ci---
project: praxis
phase: 1
milestone: v0.4
status: execute
persona: data-engineer
task: 01-04,01-05,01-06,01-07
requirements:
  covered: [REQ-MT-01, REQ-NFR-MT-01, REQ-MT-02]
---/ci---
2026-08-04 00:47:14 +00:00

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"]