gabriel / musehub public
test_musehub_alembic.py python
137 lines 4.8 KB Hotspot Cold Blast
Raw
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3 feat(intel): every grammar on the hub, and every intel job … Opus 5.5 minor ⚠ breaking 1 day ago
1 """Alembic migration structural tests.
2
3 Validates the migration chain without executing SQL against a live database.
4 Uses ``ScriptDirectory`` inspection to check the revision graph, head revision,
5 module imports, and CI migration table definitions — all without connecting to
6 a database engine.
7
8 Structural inspection is used (not live SQL) because some migrations use
9 PostgreSQL-specific SQL (``now()``, JSON casts, etc.) and editing applied
10 migrations is forbidden.
11 """
12 from __future__ import annotations
13
14 import importlib
15 import pathlib
16 import types
17
18 from alembic.config import Config
19 from alembic.script import ScriptDirectory
20
21
22 # ---------------------------------------------------------------------------
23 # Constants
24 # ---------------------------------------------------------------------------
25
26 _REPO_ROOT = pathlib.Path(__file__).parent.parent
27 _EXPECTED_HEAD = "0084"
28 _EXPECTED_MIGRATION_COUNT = 84
29
30 # Core tables that the consolidated schema migration (0001) must create
31 _CORE_TABLES = {
32 "musehub_repos",
33 "musehub_identities",
34 }
35
36
37 # ---------------------------------------------------------------------------
38 # Helpers
39 # ---------------------------------------------------------------------------
40
41 def _script_dir() -> ScriptDirectory:
42 """Return a ScriptDirectory for the project migrations."""
43 cfg = Config(str(_REPO_ROOT / "alembic.ini"))
44 cfg.set_main_option("script_location", str(_REPO_ROOT / "alembic"))
45 return ScriptDirectory.from_config(cfg)
46
47
48 # ---------------------------------------------------------------------------
49 # Tests
50 # ---------------------------------------------------------------------------
51
52
53 def test_alembic_migration_chain_has_single_head() -> None:
54 """The migration graph must have exactly one head revision (linear chain)."""
55 heads = _script_dir().get_heads()
56 assert len(heads) == 1, (
57 f"Expected a single-head migration chain, found {len(heads)} heads: {heads}. "
58 "Branching migrations require manual resolution before merging."
59 )
60
61
62 def test_alembic_migration_count() -> None:
63 """The migration chain must contain exactly the expected number of revisions."""
64 revisions = list(_script_dir().walk_revisions())
65 assert len(revisions) == _EXPECTED_MIGRATION_COUNT, (
66 f"Expected {_EXPECTED_MIGRATION_COUNT} migrations, "
67 f"found {len(revisions)}: {[r.revision for r in revisions]}. "
68 "Update _EXPECTED_MIGRATION_COUNT when adding a new migration."
69 )
70
71
72 def test_alembic_head_revision_is_latest() -> None:
73 """The current head revision must match the expected head."""
74 heads = _script_dir().get_heads()
75 assert len(heads) == 1
76 assert heads[0].startswith(_EXPECTED_HEAD), (
77 f"Expected head to start with '{_EXPECTED_HEAD}', got '{heads[0]}'. "
78 "Update _EXPECTED_HEAD when a new migration is added."
79 )
80
81
82 def test_alembic_all_revisions_importable() -> None:
83 """Every migration module can be imported; no NameError or ImportError."""
84 script_dir = _script_dir()
85 revisions = list(script_dir.walk_revisions())
86 assert len(revisions) > 0, "No revisions found — check alembic script_location."
87 for rev in revisions:
88 assert rev.revision is not None
89 assert rev.module is not None, (
90 f"Revision {rev.revision} has no module — the migration file may be missing."
91 )
92
93
94 def test_alembic_schema_migration_defines_core_tables() -> None:
95 """Migration 0001 upgrade() must reference core tables."""
96 script_dir = _script_dir()
97 revisions = list(script_dir.walk_revisions())
98
99 rev = next(
100 (r for r in revisions if r.revision.startswith("0001")),
101 None,
102 )
103 assert rev is not None, "Revision 0001 not found — consolidated schema migration is missing."
104 assert rev.module is not None
105
106 mod: types.ModuleType = rev.module
107 import inspect
108 upgrade_src = inspect.getsource(getattr(mod, "upgrade"))
109
110 missing = {t for t in _CORE_TABLES if t not in upgrade_src}
111 assert not missing, (
112 f"Schema migration 0001 upgrade() does not reference: {missing}. "
113 "Ensure all core tables are created in the migration."
114 )
115
116
117 def test_alembic_schema_migration_downgrade_drops_core_tables() -> None:
118 """Migration 0001 downgrade() must drop core tables."""
119 script_dir = _script_dir()
120 revisions = list(script_dir.walk_revisions())
121
122 rev = next(
123 (r for r in revisions if r.revision.startswith("0001")),
124 None,
125 )
126 assert rev is not None, "Revision 0001 not found."
127 assert rev.module is not None
128
129 mod: types.ModuleType = rev.module
130 import inspect
131 downgrade_src = inspect.getsource(getattr(mod, "downgrade"))
132
133 missing = {t for t in _CORE_TABLES if t not in downgrade_src}
134 assert not missing, (
135 f"Schema migration 0001 downgrade() does not reference: {missing}. "
136 "Ensure all core tables are dropped in the downgrade."
137 )
File History 20 commits
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3 feat(intel): every grammar on the hub, and every intel job … Opus 5.5 minor ⚠ 1 day ago
sha256:e6c07a0f17d2b30516cfe406c094c493c3aeaf8ea72db9ca098d24013bcc3bb9 Merge branch 'task/wave-hub-mcp-289' into dev Opus 5.5 minor ⚠ 1 day ago
sha256:5e59815d34816a1aa12717d0ac82362d6a9919e8b6e096e66ed49c47a028d76f fix(mcp): share Streamable HTTP sessions across worker proc… Opus 5.5 minor ⚠ 1 day ago
sha256:89c7a8268be75aeb33144acf863ad3ebefe7fd3bdfd679faa768199fd789b376 merge: REST wire wave — #296 #297 #298 #299 (Tier A review … Opus 5.5 minor ⚠ 1 day ago
sha256:bcf1d3c0486f7022d47cd24eed38fad1b1c50879b58cfe173231d76f0c9a1432 feat(wire): keep each pushed commit's metadata — commit_met… Opus 5.5 minor ⚠ 1 day ago
sha256:c3c5945f4af49c55c1e625b8d7d33065ceca41cd6ac991101c44fd61ca6a09c4 test: land rc22 tests on dev's parallel harness (psycopg2 d… Opus 5.5 2 days ago
sha256:5ea41f687b567512d786a8bff6f949aa626b4d52c7cdcebfa9e950d18b93e5a5 feat: defer large pushes to a background job (musehub#233) Sonnet 5 minor ⚠ 17 days ago
sha256:0a67e14158797a33cb9047f2658354ca4834888c027805b7cfd873ff7b9f534c test: update stale alembic migration-count/head constants (… Sonnet 5 18 days ago
sha256:62dd2e53c948b1f169c6c353b0791b8da11e941895e9fc8d89accddac96778bf feat: wire a repo to a real registered marketplace domain (… Sonnet 4.6 minor ⚠ 137 days ago
sha256:061a77af58afabc6ed959786a1084a62622ffbed5f8e8582af41de27f3900ea3 fix: full test-suite green — fetch cache gate, mock scope, … Sonnet 4.6 137 days ago
sha256:dc28fb2384d12a52d4b4fea7743873940b89d9d08ce298f96d0fdc8d694724d4 test+types: green the musehub suite and ratchet typing audi… Opus 4.8 minor ⚠ 153 days ago
sha256:d8cbca3a06f39f82f66be6c29de3f41c3dec5f367722958fb5454dcbc007cc15 fix: rc11 test fixes — event→verdict rename, migration coun… Sonnet 4.6 patch 165 days ago
sha256:ad616c6113d6c00f4efed6b2993734ca46d3e9b5bee25addd4ce8ae6b57136e5 chore: bump version to 0.2.0rc11; typing audit clean + all … Sonnet 4.6 minor ⚠ 170 days ago
sha256:b8884c81af5e135510894a537784c16158ee9c2c05d074f978611bbcb611ca10 fix: port security checks inline, remove repo_id from Museh… Sonnet 4.6 minor ⚠ 181 days ago
sha256:81c275295dab8d63641f74e3d9d5f4c563f2a9f5212504d40cb6d7e8a22e8eb6 perf: add composite (repo_id, name) index on musehub_branches Sonnet 4.6 minor ⚠ 188 days ago
sha256:45a8a521ce7cc99c6c43c3ddb4995e64f2aed03f01bb4a988a9249fdbf98e593 feat: Phase 2 dead table removal — drop muse_objects and mu… Sonnet 4.6 minor ⚠ 188 days ago
sha256:b7e6fa389958e44b2b2ada6801bf263a799231d346cbd13520a680bc9f4b9f6c test: add S3 alembic-check gate and fix stale migration constants Sonnet 4.6 188 days ago
sha256:9eb2c154221421e3f7fff17a04956ee6f1fef4eea59691931bfa6931a9f4074c fix: migration 0051 IF EXISTS, alembic test constants, ghos… Sonnet 4.6 patch 191 days ago
sha256:d3e70ae9c7a7d8c43904a471fd7b8826c42bbe319a326651ce4e9419b4e95877 fix: repair test suite after BlobBackend migration Sonnet 4.6 minor ⚠ 191 days ago
sha256:763eb2cb8675073b84c19345b27586d2ed939a9aee97c5479b69f502f1a70eff fix(tests): update test suite to match current implementation Sonnet 4.6 patch 193 days ago