130 lines
6.1 KiB
Python
130 lines
6.1 KiB
Python
"""The migration must stay in lockstep with the models — and refuse the wrong migrating role first.
|
|
|
|
Tables *and* enum types now both come from `Base.metadata`: the migration calls
|
|
`create_enum_types(conn)` — which derives every native enum type from the metadata — and then
|
|
`Base.metadata.create_all(conn)`. The old failure this file guarded (a hand-maintained `ENUMS` map
|
|
drifting from the models) is therefore **gone by construction**, and there is no map left to check.
|
|
|
|
What can still break is a model that is never imported in `app/models/__init__.py`: it silently
|
|
gets no table and no enum type, and the failure surfaces much later as a missing relation at
|
|
runtime, usually on someone else's machine. That is what these tests guard.
|
|
|
|
Deliberately DB-free: they read the metadata as data, so they run everywhere.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dredd_core.schema import enum_types
|
|
from sqlalchemy.dialects.postgresql import ENUM as PGEnum
|
|
|
|
|
|
def _metadata():
|
|
from app.models import Base
|
|
|
|
return Base.metadata
|
|
|
|
|
|
def test_core_tables_are_registered():
|
|
"""Importing `dredd_core` registers the shared identity tables on the one metadata."""
|
|
assert {"app_user", "session", "app_setting", "audit_log"} <= set(_metadata().tables)
|
|
|
|
|
|
SKILL_TABLES = ("skill", "skill_version", "skill_file", "agent", "agent_version",
|
|
"agent_version_skill", "publish_request")
|
|
|
|
|
|
def test_the_apps_own_tables_are_registered():
|
|
"""A model missing from `app/models/__init__.py` silently gets no table."""
|
|
assert {"module", "module_assignment", "access_request"} <= set(_metadata().tables)
|
|
assert set(SKILL_TABLES) <= set(_metadata().tables)
|
|
assert "record" not in _metadata().tables
|
|
|
|
|
|
def test_every_model_enum_is_derived_for_the_migration():
|
|
"""`create_enum_types()` emits exactly the types this derivation finds — so anything a column
|
|
references must appear here, or the migration will not create it."""
|
|
derived = enum_types(_metadata())
|
|
for table in _metadata().tables.values():
|
|
for column in table.columns:
|
|
if isinstance(column.type, PGEnum) and column.type.name:
|
|
assert column.type.name in derived, (
|
|
f"enum {column.type.name} is used by a column but is not derived for the "
|
|
"migration — is its model imported in app/models/__init__.py?"
|
|
)
|
|
|
|
|
|
def test_marketplace_vocabularies_are_check_constraints_not_enum_types():
|
|
"""Atrium's own vocabularies are Text + CHECK (see app/enums.py), so they add no native type —
|
|
and each CHECK lists exactly the StrEnum's values, so the code and the database agree."""
|
|
from sqlalchemy import CheckConstraint
|
|
|
|
from app.enums import (
|
|
AccessRequestStatus,
|
|
AgentKind,
|
|
Availability,
|
|
CatalogStatus,
|
|
ModuleStatus,
|
|
PublishKind,
|
|
PublishRequestStatus,
|
|
SkillOrigin,
|
|
SkillScope,
|
|
)
|
|
|
|
tables = _metadata().tables
|
|
for name in ("module", "module_assignment", "access_request", *SKILL_TABLES):
|
|
assert not [c for c in tables[name].columns if isinstance(c.type, PGEnum)], name
|
|
assert "record_status" not in enum_types(_metadata())
|
|
expected = {("module", "status"): ModuleStatus, ("module", "availability"): Availability,
|
|
("access_request", "status"): AccessRequestStatus,
|
|
("skill", "scope"): SkillScope, ("skill", "status"): CatalogStatus,
|
|
("skill", "origin"): SkillOrigin,
|
|
("agent", "scope"): SkillScope, ("agent", "status"): CatalogStatus,
|
|
("agent", "kind"): AgentKind,
|
|
("publish_request", "kind"): PublishKind,
|
|
("publish_request", "status"): PublishRequestStatus}
|
|
for (table, column), enum in expected.items():
|
|
sql = [str(c.sqltext) for c in tables[table].constraints
|
|
if isinstance(c, CheckConstraint) and str(c.sqltext).startswith(f"{column} IN")]
|
|
assert sql, f"no CHECK on {table}.{column}"
|
|
for member in enum:
|
|
assert repr(member.value) in sql[0], f"{table}.{column} CHECK is missing {member.value}"
|
|
|
|
|
|
# ── who is allowed to run it at all ───────────────────────────────────────────
|
|
# 0003's runtime grants make the same two role checks, and it is tempting to call that the guard. It
|
|
# is not: 0003 is only reached if 0001 and 0002 succeeded, so as the platform admin the hub creates
|
|
# the whole family's accounts owned by that role — on a managed provider, open to its auto-generated
|
|
# table API — and only Alembic's transaction takes them away again. A check that fires after the
|
|
# damage would have been done is a check placed in the wrong revision.
|
|
|
|
def _first_revision():
|
|
import importlib.util
|
|
from pathlib import Path
|
|
|
|
path = Path(__file__).resolve().parents[1] / "alembic" / "versions" / "0001_initial.py"
|
|
spec = importlib.util.spec_from_file_location("atrium_rev_0001", path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
def test_the_first_revision_runs_the_preflight_before_anything_else(monkeypatch):
|
|
from types import SimpleNamespace
|
|
|
|
from dredd_core.schema import migration_preflight_sql
|
|
|
|
rev = _first_revision()
|
|
steps: list[str] = []
|
|
monkeypatch.setattr(rev, "op", SimpleNamespace(execute=steps.append, get_bind=lambda: object()))
|
|
monkeypatch.setattr(rev, "get_settings", lambda: SimpleNamespace(db_app_role="atrium_app"))
|
|
monkeypatch.setattr(rev, "create_enum_types", lambda bind: steps.append("<create_enum_types>"))
|
|
monkeypatch.setattr(rev.Base.metadata, "create_all", lambda **kw: steps.append("<create_all>"))
|
|
rev.upgrade()
|
|
|
|
assert steps[0] == migration_preflight_sql("atrium_app")
|
|
# The phrase a deploy log is grepped for, and the role it has to name.
|
|
assert "refusing to migrate as" in steps[0]
|
|
assert "'atrium_app'" in steps[0]
|
|
# "First" only means something if the revision then goes on to build the schema.
|
|
assert any("CREATE EXTENSION" in step for step in steps[1:])
|
|
assert steps[1:].count("<create_enum_types>") == 1 and steps[1:].count("<create_all>") == 1
|