|
| 1 | +"""convert app_config_fragments.scope_id to a nullable UUID |
| 2 | +
|
| 3 | +``scope_id`` held a ``VARCHAR(255)`` that was never really a string: a domain |
| 4 | +fragment stores a domain id and a user fragment a user id, both UUIDs, while a |
| 5 | +public fragment has no owner at all and stored ``''`` only because the column |
| 6 | +was ``NOT NULL``. Store the real type instead — ``UUID NULL``, with ``NULL`` |
| 7 | +meaning "public, no owner". |
| 8 | +
|
| 9 | +The uniqueness of ``(config_name, scope_type, scope_id)`` needs help: Postgres |
| 10 | +treats ``NULL``s as distinct in a unique constraint, so the existing constraint |
| 11 | +stops rejecting a second public fragment for the same config name once public |
| 12 | +rows hold ``NULL``. A partial unique index over the ``NULL`` rows restores the |
| 13 | +guarantee the ``''`` sentinel used to provide. (``UNIQUE NULLS NOT DISTINCT`` |
| 14 | +would say the same thing in one constraint, but it needs Postgres 15+ and the |
| 15 | +test fixture runs 13.) |
| 16 | +
|
| 17 | +Public rows are forced to ``NULL`` regardless of what they stored, since public |
| 18 | +has no owner by definition. Domain and user rows are cast, and a value that is |
| 19 | +not a UUID fails the migration on purpose — nulling it would silently drop the |
| 20 | +scope binding that decides who can see the fragment. |
| 21 | +
|
| 22 | +Revision ID: e5b71c94d2a8 |
| 23 | +Revises: c7e2b48a15d9 |
| 24 | +Create Date: 2026-07-21 |
| 25 | +
|
| 26 | +""" |
| 27 | + |
| 28 | +import sqlalchemy as sa |
| 29 | +from alembic import op |
| 30 | + |
| 31 | +# revision identifiers, used by Alembic. |
| 32 | +revision = "e5b71c94d2a8" |
| 33 | +down_revision = "c7e2b48a15d9" |
| 34 | +# Part of: NEXT_RELEASE_VERSION |
| 35 | +branch_labels = None |
| 36 | +depends_on = None |
| 37 | + |
| 38 | +_PUBLIC_INDEX = "uq_app_config_fragments_public_config_name" |
| 39 | +_TABLE = "app_config_fragments" |
| 40 | + |
| 41 | + |
| 42 | +def upgrade() -> None: |
| 43 | + op.execute( |
| 44 | + sa.text(""" |
| 45 | + ALTER TABLE app_config_fragments |
| 46 | + ALTER COLUMN scope_id DROP NOT NULL, |
| 47 | + ALTER COLUMN scope_id TYPE UUID |
| 48 | + USING ( |
| 49 | + CASE |
| 50 | + WHEN scope_type = 'public' THEN NULL |
| 51 | + ELSE NULLIF(scope_id, '')::uuid |
| 52 | + END |
| 53 | + ) |
| 54 | + """) |
| 55 | + ) |
| 56 | + op.create_index( |
| 57 | + _PUBLIC_INDEX, |
| 58 | + _TABLE, |
| 59 | + ["config_name", "scope_type"], |
| 60 | + unique=True, |
| 61 | + postgresql_where=sa.text("scope_id IS NULL"), |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def downgrade() -> None: |
| 66 | + op.drop_index(_PUBLIC_INDEX, table_name=_TABLE) |
| 67 | + # Public rows go back to the empty sentinel the NOT NULL column used to require, which |
| 68 | + # the plain constraint can compare like any other value. |
| 69 | + op.execute( |
| 70 | + sa.text(""" |
| 71 | + ALTER TABLE app_config_fragments |
| 72 | + ALTER COLUMN scope_id TYPE VARCHAR(255) |
| 73 | + USING (COALESCE(scope_id::text, '')) |
| 74 | + """) |
| 75 | + ) |
| 76 | + op.alter_column(_TABLE, "scope_id", nullable=False) |
0 commit comments