|
| 1 | +"""environment_settings_single_json |
| 2 | +
|
| 3 | +Revision ID: env_settings_single_json |
| 4 | +Revises: add_idp_user_social |
| 5 | +Create Date: 2026-03-08 |
| 6 | +
|
| 7 | +Transform settings table: one row per environment, settings as JSON array. |
| 8 | +Remove key, description; rename value -> settings. No default settings injected. |
| 9 | +""" |
| 10 | +from typing import Sequence, Union |
| 11 | +import json |
| 12 | +import uuid as uuid_module |
| 13 | +from alembic import op |
| 14 | +import sqlalchemy as sa |
| 15 | +from sqlalchemy.dialects import postgresql |
| 16 | + |
| 17 | +revision: str = "env_settings_single_json" |
| 18 | +down_revision: Union[str, None] = "add_idp_user_social" |
| 19 | +branch_labels: Union[str, Sequence[str], None] = None |
| 20 | +depends_on: Union[str, Sequence[str], None] = None |
| 21 | + |
| 22 | + |
| 23 | +def _json_type(val): |
| 24 | + """Infer type string for a value.""" |
| 25 | + if val is None: |
| 26 | + return "string" |
| 27 | + if isinstance(val, bool): |
| 28 | + return "boolean" |
| 29 | + if isinstance(val, (int, float)) and not isinstance(val, bool): |
| 30 | + return "number" |
| 31 | + if isinstance(val, list): |
| 32 | + return "list" |
| 33 | + if isinstance(val, dict): |
| 34 | + return "object" |
| 35 | + return "string" |
| 36 | + |
| 37 | + |
| 38 | +def upgrade() -> None: |
| 39 | + conn = op.get_bind() |
| 40 | + |
| 41 | + op.create_table( |
| 42 | + "settings_new", |
| 43 | + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), |
| 44 | + sa.Column("uuid", postgresql.UUID(as_uuid=True), nullable=False), |
| 45 | + sa.Column("environment_id", sa.Integer(), nullable=False), |
| 46 | + sa.Column("organization_id", sa.Integer(), nullable=False), |
| 47 | + sa.Column("settings", postgresql.JSONB(astext_type=sa.Text()), nullable=False), |
| 48 | + sa.ForeignKeyConstraint(["environment_id"], ["environments.id"], ondelete="CASCADE"), |
| 49 | + sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"], ondelete="CASCADE"), |
| 50 | + sa.PrimaryKeyConstraint("id"), |
| 51 | + sa.UniqueConstraint("environment_id", name="uq_settings_environment_id"), |
| 52 | + sa.UniqueConstraint("uuid", name="uq_settings_new_uuid"), |
| 53 | + ) |
| 54 | + op.create_index("ix_settings_new_organization_id", "settings_new", ["organization_id"], unique=False) |
| 55 | + |
| 56 | + env_rows = conn.execute(sa.text("SELECT id, organization_id FROM environments")).fetchall() |
| 57 | + for seq_id, (env_id, org_id) in enumerate(env_rows, start=1): |
| 58 | + existing = conn.execute( |
| 59 | + sa.text("SELECT key, value, description FROM settings WHERE environment_id = :eid"), |
| 60 | + {"eid": env_id}, |
| 61 | + ).fetchall() |
| 62 | + if existing: |
| 63 | + items = [ |
| 64 | + { |
| 65 | + "key": r[0], |
| 66 | + "value": r[1], |
| 67 | + "description": r[2] or "", |
| 68 | + "type": _json_type(r[1]), |
| 69 | + } |
| 70 | + for r in existing |
| 71 | + ] |
| 72 | + settings_json = json.dumps(items) |
| 73 | + else: |
| 74 | + settings_json = "[]" |
| 75 | + new_uuid = str(uuid_module.uuid4()) |
| 76 | + conn.execute( |
| 77 | + sa.text( |
| 78 | + "INSERT INTO settings_new (id, uuid, environment_id, organization_id, settings) " |
| 79 | + "VALUES (:id, CAST(:uuid AS uuid), :eid, :oid, CAST(:settings AS jsonb))" |
| 80 | + ), |
| 81 | + {"id": seq_id, "uuid": new_uuid, "eid": env_id, "oid": org_id, "settings": settings_json}, |
| 82 | + ) |
| 83 | + |
| 84 | + op.drop_table("settings") |
| 85 | + op.rename_table("settings_new", "settings") |
| 86 | + op.create_index(op.f("ix_settings_organization_id"), "settings", ["organization_id"], unique=False) |
| 87 | + |
| 88 | + |
| 89 | +def downgrade() -> None: |
| 90 | + op.drop_table("settings") |
| 91 | + op.create_table( |
| 92 | + "settings", |
| 93 | + sa.Column("id", sa.Integer(), nullable=False), |
| 94 | + sa.Column("uuid", postgresql.UUID(as_uuid=True), nullable=False), |
| 95 | + sa.Column("key", sa.String(), nullable=False), |
| 96 | + sa.Column("value", postgresql.JSON(), nullable=False), |
| 97 | + sa.Column("description", sa.String(), nullable=True), |
| 98 | + sa.Column("environment_id", sa.Integer(), nullable=False), |
| 99 | + sa.Column("organization_id", sa.Integer(), nullable=False), |
| 100 | + sa.ForeignKeyConstraint(["environment_id"], ["environments.id"]), |
| 101 | + sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]), |
| 102 | + sa.PrimaryKeyConstraint("id"), |
| 103 | + sa.UniqueConstraint("key", "environment_id", name="uq_key_environment"), |
| 104 | + sa.UniqueConstraint("uuid"), |
| 105 | + ) |
| 106 | + op.create_index(op.f("ix_settings_organization_id"), "settings", ["organization_id"], unique=False) |
0 commit comments