gabriel / musehub public
0028_fix_musehub_commits_array_cols.py python
67 lines 2.1 KB
Raw
sha256:94ef169c149a452bff7c604ded8b280b19bd477c2dabcb56972780b0b784c7aa Merge 'fix/assignee-sigil-inline' into 'dev' — proposal: As… Human 2 days ago
1 """Fix musehub_commits array columns missed by migration 0024.
2
3 Migration 0024 intended to convert parent_ids, breaking_changes, and
4 reviewed_by in musehub_commits from JSON to PostgreSQL ARRAY, but the
5 column list incorrectly referenced muse_commits (a different table with
6 no such columns). This migration applies the correct conversions.
7
8 Revision ID: 0028
9 Revises: 0027
10 """
11 from __future__ import annotations
12
13 from alembic import op
14 import sqlalchemy as sa
15
16
17 revision = "0028"
18 down_revision = "0027"
19 branch_labels = None
20 depends_on = None
21
22
23 def upgrade() -> None:
24 conn = op.get_bind()
25
26 conn.execute(sa.text("""
27 CREATE OR REPLACE FUNCTION _muse_json_to_text_array(v text)
28 RETURNS text[]
29 LANGUAGE sql IMMUTABLE AS $$
30 SELECT CASE WHEN v IS NULL THEN NULL
31 ELSE ARRAY(SELECT jsonb_array_elements_text(v::jsonb))
32 END
33 $$
34 """))
35
36 conn.execute(sa.text(
37 "ALTER TABLE musehub_commits ALTER COLUMN breaking_changes TYPE text[] "
38 "USING _muse_json_to_text_array(breaking_changes::text)"
39 ))
40 conn.execute(sa.text(
41 "ALTER TABLE musehub_commits ALTER COLUMN reviewed_by TYPE text[] "
42 "USING _muse_json_to_text_array(reviewed_by::text)"
43 ))
44 conn.execute(sa.text(
45 "ALTER TABLE musehub_commits ALTER COLUMN parent_ids TYPE varchar(128)[] "
46 "USING _muse_json_to_text_array(parent_ids::text)::varchar(128)[]"
47 ))
48
49 conn.execute(sa.text("DROP FUNCTION _muse_json_to_text_array(text)"))
50
51
52 def downgrade() -> None:
53 conn = op.get_bind()
54
55 def _has_column(table: str, column: str) -> bool:
56 r = conn.execute(sa.text(
57 "SELECT 1 FROM information_schema.columns "
58 "WHERE table_schema='public' AND table_name=:t AND column_name=:c"
59 ), {"t": table, "c": column})
60 return r.fetchone() is not None
61
62 for col in ("breaking_changes", "reviewed_by", "parent_ids"):
63 if _has_column("musehub_commits", col):
64 conn.execute(sa.text(
65 f"ALTER TABLE musehub_commits ALTER COLUMN {col} TYPE json "
66 f"USING to_json({col})"
67 ))
File History 3 commits
sha256:94ef169c149a452bff7c604ded8b280b19bd477c2dabcb56972780b0b784c7aa Merge 'fix/assignee-sigil-inline' into 'dev' — proposal: As… Human 2 days ago
sha256:6b1949fc2797ca4c1936a637a4cbfec828ef56cf52398a2e74ca3c4f494e728f fix: use wire_bytes not mpack_bytes_raw in compute_object_b… Sonnet 4.6 patch 10 days ago
sha256:4aed3d8601c8dd3ed37074de35f11f4a9699a0a4b99d43727048fd3f8e6fd13d chore: doc sweep, ignore wrangler build state, misc fixes Sonnet 4.6 minor 13 days ago