2026-08-19 14:42:01 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from tenant_engine.postgres_store import PostgresTenantStore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clean_postgres_store(tmp_path: Path) -> PostgresTenantStore:
|
|
|
|
|
dsn = os.getenv("TENANT_ENGINE_TEST_DATABASE_URL", "").strip()
|
|
|
|
|
if not dsn:
|
|
|
|
|
pytest.skip("set TENANT_ENGINE_TEST_DATABASE_URL to exercise PostgreSQL conformance")
|
|
|
|
|
try:
|
|
|
|
|
import psycopg
|
|
|
|
|
except ImportError:
|
|
|
|
|
pytest.skip("install tenant-engine[postgres] to exercise PostgreSQL conformance")
|
|
|
|
|
|
2026-08-29 13:02:51 +02:00
|
|
|
migrations = Path(__file__).parents[1] / "migrations/postgres"
|
2026-08-19 14:42:01 +02:00
|
|
|
with psycopg.connect(dsn, autocommit=True) as connection:
|
2026-08-29 13:02:51 +02:00
|
|
|
for path in sorted(migrations.glob("*.sql")):
|
|
|
|
|
connection.execute(path.read_text(encoding="utf-8"))
|
2026-08-19 14:42:01 +02:00
|
|
|
connection.execute(
|
2026-08-29 13:02:51 +02:00
|
|
|
"""TRUNCATE audit_outbox, authz_records, guardrail_changes, guardrail_overrides,
|
2026-08-19 14:42:01 +02:00
|
|
|
idempotency_receipts, events, plans, grants, tenants
|
|
|
|
|
RESTART IDENTITY CASCADE"""
|
|
|
|
|
)
|
|
|
|
|
url_file = tmp_path / "postgres-url"
|
|
|
|
|
url_file.write_text(dsn, encoding="utf-8")
|
|
|
|
|
return PostgresTenantStore(str(url_file), min_pool_size=1, max_pool_size=4)
|