gabriel / musehub public
0037_widen_id_columns_repair.py python
64 lines 1.9 KB
Raw
sha256:ab9eda7b6479e1c35cdba9a54f62bacd2825de8faacec3ba67a9a8ef45914b7d fix: migration and wire protocol alignment Sonnet 4.6 minor ⚠ breaking 20 days ago
1 """Widen varchar(36) ID columns to varchar(128) — idempotent repair.
2
3 Migration 0030 was stamped before it ran on staging (same stamp-and-retry
4 escape hatch issue as 0035/0036). This migration re-applies those ALTERs
5 using DO $$ conditional blocks that are no-ops when the column is already
6 the right size.
7
8 Also widens musehub_background_jobs.repo_id which 0030 missed.
9
10 Revision ID: 0037
11 Revises: 0036
12 """
13 from __future__ import annotations
14
15 from alembic import op
16 from sqlalchemy import text
17
18 revision = "0037"
19 down_revision = "0036"
20 branch_labels = None
21 depends_on = None
22
23 _WIDEN = [
24 ("musehub_background_jobs", "job_id"),
25 ("musehub_background_jobs", "repo_id"),
26 ("musehub_domain_installs", "install_id"),
27 ("musehub_issue_events", "event_id"),
28 ("musehub_webhook_deliveries", "delivery_id"),
29 ]
30
31
32 def upgrade() -> None:
33 for table, col in _WIDEN:
34 op.execute(text(f"""
35 DO $$
36 BEGIN
37 IF EXISTS (
38 SELECT 1 FROM information_schema.columns
39 WHERE table_name = '{table}'
40 AND column_name = '{col}'
41 AND character_maximum_length < 128
42 ) THEN
43 ALTER TABLE {table} ALTER COLUMN {col} TYPE VARCHAR(128);
44 END IF;
45 END $$
46 """))
47
48
49 def downgrade() -> None:
50 # Narrow columns back to VARCHAR(36) — the original width before the repair.
51 for table, col in _WIDEN:
52 op.execute(text(f"""
53 DO $$
54 BEGIN
55 IF EXISTS (
56 SELECT 1 FROM information_schema.columns
57 WHERE table_name = '{table}'
58 AND column_name = '{col}'
59 AND character_maximum_length >= 128
60 ) THEN
61 ALTER TABLE {table} ALTER COLUMN {col} TYPE VARCHAR(36);
62 END IF;
63 END $$
64 """))
File History 1 commit
sha256:ab9eda7b6479e1c35cdba9a54f62bacd2825de8faacec3ba67a9a8ef45914b7d fix: migration and wire protocol alignment Sonnet 4.6 minor 20 days ago