Files
Hello/server/tests/test_schema_matches_models.py
Thomas KarnerandClaude Opus 5 5d16ded553 Server: verify Phase 1 end to end against PostgreSQL 17
Set up the dev environment (uv-managed CPython 3.14.7, postgres:17 in a
Podman container) and ran the skeleton for real: migrations apply, the
API serves /api/v1/health and /healthz, the worker claims a job and
drains it, 12/12 tests pass with nothing skipped.

The schema-drift test failed on its first real run and found a genuine
problem: Postgres reflects a varchar default as 'x'::character varying
while the model renders 'x', so all ten string defaults read as
permanent drift. Left alone that is the failure mode where you learn to
ignore the test and then miss the one real change. Add schema_compare.py,
a normaliser shared by migrations/env.py and the test so autogenerate and
the test can never disagree about what counts as a difference. A real
default change still fails it.

Also: ruff import-order fix in captures.py, and the README gains the
uv + Podman dev setup, the expected test count, and the CREATE DATABASE
privilege the drift test needs.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-21 00:30:52 +02:00

69 lines
2.6 KiB
Python

"""Guard the baseline migration against the models.
Migration 0001 builds the schema from ``Base.metadata`` (see its docstring).
That is only safe if something checks it, so: migrate a scratch database, then
ask Alembic to autogenerate against it and assert the diff is empty.
This test keeps working after 0001 is replaced by explicit DDL, and it is what
catches "added a column to the model, forgot the migration" from Phase 2 on.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from conftest import needs_db
pytestmark = [pytest.mark.db, needs_db]
def test_migrated_schema_matches_the_models():
from alembic.autogenerate import compare_metadata
from alembic.migration import MigrationContext
from sqlalchemy import create_engine, text
from poorbird.config import get_settings
from poorbird.db import engine
from poorbird.models import Base
from poorbird.schema_compare import COMPARE_OPTS
scratch = "poorbird_schema_check"
# CREATE DATABASE cannot run inside a transaction block.
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as conn:
conn.execute(text(f'DROP DATABASE IF EXISTS "{scratch}"'))
conn.execute(text(f'CREATE DATABASE "{scratch}"'))
url = get_settings().database_url
scratch_url = url.rsplit("/", 1)[0] + "/" + scratch
scratch_engine = create_engine(scratch_url)
try:
# Apply the real migration chain, exactly as `poorbird migrate` would.
with scratch_engine.begin() as conn:
from alembic import command
from alembic.config import Config
cfg = Config()
cfg.set_main_option(
"script_location", str(Path(__file__).resolve().parents[1] / "migrations")
)
cfg.set_main_option("sqlalchemy.url", scratch_url)
cfg.attributes["connection"] = conn
command.upgrade(cfg, "head")
with scratch_engine.connect() as conn:
# The same options autogenerate uses, so the test and the tool can
# never disagree about what counts as a difference.
ctx = MigrationContext.configure(conn, opts=dict(COMPARE_OPTS))
diff = compare_metadata(ctx, Base.metadata)
# alembic_version is Alembic's own bookkeeping, not part of the models.
diff = [d for d in diff if "alembic_version" not in str(d)]
assert diff == [], f"schema has drifted from the models:\n{diff}"
finally:
scratch_engine.dispose()
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as conn:
conn.execute(text(f'DROP DATABASE IF EXISTS "{scratch}"'))