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
This commit is contained in:
parent
89ff2b2ea3
commit
97c8762a71
5 changed files with 178 additions and 0 deletions
56
tests/test_schema_state.py
Normal file
56
tests/test_schema_state.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
"""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"
|
||||
Loading…
Add table
Add a link
Reference in a new issue