state-hub/tests/test_schema_state.py
tegwick 97c8762a71
All checks were successful
CI Smoke / host-smoke (push) Successful in 0s
CI Smoke / container-smoke (push) Successful in 1s
Build and Publish Multi-Context Image / build-and-push (push) Successful in 27s
feat(deploy): run migrations as part of the release, and report schema state
Central was serving two revisions behind the code it shipped: review_contracts
did not exist there although its migration was inside the running image. There
was no migration mechanism at all — bare uvicorn CMD, nothing chart-declared —
and nothing surfaced the mismatch. The API starts happily against a schema it
was not built for and only fails when a request touches a missing table.

Adds a chart-managed Helm pre-install/pre-upgrade hook running alembic upgrade
head, weighted to complete before the API rolls. A hook rather than an init
container: init containers run per pod, so more than one replica means
concurrent alembic upgrade with no locking. Failed jobs are deliberately
retained — a migration that fails and vanishes is how this drifted in the first
place.

/state/health now reports applied and expected revisions. "unknown" is
deliberately not "ok": an instance that cannot establish agreement must not
claim it, the same principle as instance_role defaulting to unknown.

Refs STATE-WP-0083-T07

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Assistant: claude-code
Assistant-Model: opus
Assistant-Process: 2583210@bnt-lap001
Assistant-Session: f2bff2d5-e9b2-4338-92ca-10282a927006
2026-08-26 00:00:09 +02:00

56 lines
1.8 KiB
Python

"""Schema/code agreement must be observable (STATE-WP-0083-T07).
Central served two revisions behind the code with no signal at all. The API
started fine and would only have failed when a request touched a missing table.
"""
from __future__ import annotations
import pytest
from api.services import schema_state as ss
class _Session:
def __init__(self, revision=None, raises=False):
self._revision = revision
self._raises = raises
async def execute(self, *_a, **_k):
if self._raises:
raise RuntimeError("no connection")
rev = self._revision
class R:
def scalar(self_inner):
return rev
return R()
def test_code_head_is_readable_from_the_shipped_migrations():
"""Must not require a database: it is what the code expects, not what ran."""
assert ss.code_head_revision()
@pytest.mark.asyncio
async def test_matching_revision_reports_ok(monkeypatch):
monkeypatch.setattr(ss, "code_head_revision", lambda: "abc123")
assert (await ss.schema_state(_Session("abc123")))["status"] == "ok"
@pytest.mark.asyncio
async def test_behind_revision_is_reported_with_both_values(monkeypatch):
monkeypatch.setattr(ss, "code_head_revision", lambda: "newrev")
state = await ss.schema_state(_Session("oldrev"))
assert state["status"] == "behind"
assert state["applied"] == "oldrev" and state["expected"] == "newrev"
@pytest.mark.asyncio
async def test_unknown_is_not_reported_as_ok(monkeypatch):
"""An instance that cannot establish agreement must not claim it."""
monkeypatch.setattr(ss, "code_head_revision", lambda: "newrev")
assert (await ss.schema_state(_Session(raises=True)))["status"] == "unknown"
monkeypatch.setattr(ss, "code_head_revision", lambda: None)
assert (await ss.schema_state(_Session("oldrev")))["status"] == "unknown"