gabriel / musehub public
0040_repair_schema_drift_idempotent.py python
104 lines 3.8 KB
Raw
sha256:73f24973678ceefd56628ee17c6d0535d86e33533828af6c63348f50941515ad Merge branch 'fix/smoke-test-tag-label' into dev Human 15 days ago
1 """Repair schema drift — idempotent retry of 0039 changes.
2
3 Revision ID: 0040
4 Revises: 0039
5 Create Date: 2026-05-07
6
7 Context
8 -------
9 Migration 0039 dropped ix_musehub_coord_reservations_repo_id but on some
10 environments that index was absent, causing the migration to fail. The
11 deploy script stamped the alembic_version to 0039 and continued, leaving
12 all 0039 changes unapplied on those environments.
13
14 This migration re-applies all 0039 changes using IF EXISTS / IF NOT EXISTS
15 guards so it is safe to run regardless of which state the DB is in.
16 """
17 from __future__ import annotations
18
19 from alembic import op
20 import sqlalchemy as sa
21
22
23 revision: str = "0040"
24 down_revision: str | None = "0039"
25 branch_labels = None
26 depends_on = None
27
28
29 def upgrade() -> None:
30 # muse_tags.tag_id — widen VARCHAR(36) to VARCHAR(128) if not already done
31 bind = op.get_bind()
32 cols = {c["name"]: c for c in sa.inspect(bind).get_columns("muse_tags")}
33 tag_id_col = cols.get("tag_id")
34 if tag_id_col and isinstance(tag_id_col["type"], sa.VARCHAR) and tag_id_col["type"].length == 36:
35 op.alter_column(
36 "muse_tags", "tag_id",
37 existing_type=sa.VARCHAR(length=36),
38 type_=sa.String(length=128),
39 existing_nullable=False,
40 )
41
42 # coord_reservations — drop wrong-named index; ensure correct name exists
43 op.execute("DROP INDEX IF EXISTS ix_musehub_coord_reservations_repo_id")
44 op.execute(
45 "CREATE INDEX IF NOT EXISTS ix_coord_reservations_repo_id "
46 "ON musehub_coord_reservations(repo_id)"
47 )
48
49 # breakage_issues — add missing index
50 op.execute(
51 "CREATE INDEX IF NOT EXISTS ix_musehub_intel_breakage_issues_repo_id "
52 "ON musehub_intel_breakage_issues(repo_id)"
53 )
54
55 # refactor_events — add missing index
56 op.execute(
57 "CREATE INDEX IF NOT EXISTS ix_musehub_intel_refactor_events_repo_id "
58 "ON musehub_intel_refactor_events(repo_id)"
59 )
60
61 # velocity — drop stale composite index
62 op.execute("DROP INDEX IF EXISTS ix_intel_velocity_repo_active")
63
64 # symbol_intel — drop stale DESC-expression blast index
65 op.execute("DROP INDEX IF EXISTS ix_symbol_intel_repo_blast")
66
67 # symbol_intel gravity_pct — rebuild without DESC expression
68 op.execute("DROP INDEX IF EXISTS ix_symbol_intel_repo_gravity_pct")
69 op.execute(
70 "CREATE INDEX IF NOT EXISTS ix_symbol_intel_repo_gravity_pct "
71 "ON musehub_symbol_intel(repo_id, gravity_pct)"
72 )
73
74
75 def downgrade() -> None:
76 op.execute("DROP INDEX IF EXISTS ix_symbol_intel_repo_gravity_pct")
77 op.execute(
78 "CREATE INDEX IF NOT EXISTS ix_symbol_intel_repo_gravity_pct "
79 "ON musehub_symbol_intel(repo_id, gravity_pct DESC NULLS LAST)"
80 )
81 op.execute(
82 "CREATE INDEX IF NOT EXISTS ix_symbol_intel_repo_blast "
83 "ON musehub_symbol_intel(repo_id, blast DESC NULLS LAST)"
84 )
85 op.execute(
86 "CREATE INDEX IF NOT EXISTS ix_intel_velocity_repo_active "
87 "ON musehub_intel_velocity(repo_id, active_commits)"
88 )
89 op.execute("DROP INDEX IF EXISTS ix_musehub_intel_refactor_events_repo_id")
90 op.execute("DROP INDEX IF EXISTS ix_musehub_intel_breakage_issues_repo_id")
91 op.execute("DROP INDEX IF EXISTS ix_coord_reservations_repo_id")
92 op.execute(
93 "CREATE INDEX IF NOT EXISTS ix_musehub_coord_reservations_repo_id "
94 "ON musehub_coord_reservations(repo_id)"
95 )
96 cols = {c["name"]: c for c in sa.inspect(op.get_bind()).get_columns("muse_tags")}
97 tag_id_col = cols.get("tag_id")
98 if tag_id_col and isinstance(tag_id_col["type"], sa.VARCHAR) and tag_id_col["type"].length == 128:
99 op.alter_column(
100 "muse_tags", "tag_id",
101 existing_type=sa.String(length=128),
102 type_=sa.VARCHAR(length=36),
103 existing_nullable=False,
104 )
File History 12 commits
sha256:cfefc25a166c3c3eed8ea3529aee19ea350bc05f2954d007420e924133b7d8ce chore: pivot to nightly channel — bump version to 0.2.0.dev… Sonnet 5 patch 13 days ago
sha256:d035733f21ccff27735fddebfbbe0ed24565a32a22db8de5885402262671ecd2 chore: bump version to 0.2.0rc15 for musehub#113 fix release Sonnet 4.6 patch 15 days ago
sha256:0032d6cfa33bc3c8367436ad768e7dd0e339b4332153160247da8266cb5fa352 Merge branch 'task/version-tags-phase3-server' into dev Human 17 days ago
sha256:4669620efda9ff41c55bdefd1f7bfe1c239d468428744c84ead9957e5a003a53 merge: rescue snapshot-recovery hardening (c00aa21d) into d… Opus 4.8 minor 30 days ago
sha256:a59da49c4611b970fc4b6ae48678ce4943261c213a07ddbd73ce9201df869b4a fix: remove false-positive proposal_comments index drop fro… Sonnet 4.6 patch 34 days ago
sha256:0a240d6dbff234f07d98a28a4a9a68db702f3f9ff9260196f24219bdb1c0b6f3 feat: render markdown mists as HTML with heading anchor links Sonnet 4.6 patch 35 days ago
sha256:24a7d47486ebc4ebd1832830580e177ec6f877b48dced8c000e198cdec4ce9d6 Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump … Human 36 days ago
sha256:b9ff931d147e0114a1f17060f415b89ed551c170a91ff226c70437aa5c85f9ee Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump … Human 36 days ago
sha256:d1122d21e73471879b460037b22c0b50fded7c423444a176f248428f75dac39c Merge 'task/fix-issue-pagination-cursor' into 'dev' — propo… Human 36 days ago
sha256:01e18975e73d2b3cd5b6db7929c895bef9aa6e0d4391dc5b2adfc548b41318dd Merge 'feat/adding-debug-logs-to-staging' into 'dev' — prop… Human 36 days ago
sha256:6b1949fc2797ca4c1936a637a4cbfec828ef56cf52398a2e74ca3c4f494e728f fix: use wire_bytes not mpack_bytes_raw in compute_object_b… Sonnet 4.6 patch 48 days ago
sha256:b99f2455dc346966d040133f5203297e6e3ef5803a93728a2c30568d0a0f7583 rename: delta_add → delta_upsert across wire format, models… Sonnet 4.6 patch 50 days ago