PhoenixKit.Migrations.UUIDFKColumns (phoenix_kit v1.7.71)

Copy Markdown View Source

Adds UUID FK columns alongside integer FKs across PhoenixKit tables.

Called from V56 migration. This helper module keeps V56 manageable by extracting the UUID FK column creation logic (~80 columns across ~40 tables).

How It Works

For each FK column, three operations:

  1. ALTER TABLE ... ADD COLUMN IF NOT EXISTS {uuid_fk} UUID
  2. Backfill via JOIN: UPDATE t SET {uuid_fk} = s.uuid FROM source s WHERE s.id = t.{int_fk}
  3. CREATE INDEX IF NOT EXISTS ... ON table({uuid_fk})

Large tables use batched backfills (PL/pgSQL DO block, 10k rows/batch) to avoid long-running transactions.

After columns are created and backfilled, add_constraints/1 adds:

  • NOT NULL constraints where the integer FK is NOT NULL
  • FK constraints where the integer FK has an explicit DB-level FK constraint

Safety

  • All operations wrapped in table_exists? + column_exists? checks
  • FK constraint creation uses pg_constraint existence check (idempotent)
  • NOT NULL uses ALTER COLUMN SET NOT NULL (idempotent in PostgreSQL)
  • Idempotent — safe to run multiple times

Summary

Functions

Adds NOT NULL constraints and FK constraints to UUID FK columns.

Drops FK constraints and NOT NULL from UUID FK columns.

Functions

add_constraints(opts)

Adds NOT NULL constraints and FK constraints to UUID FK columns.

Must be called AFTER up/1 so that columns exist and are backfilled.

Order: NOT NULL first (data already backfilled), then ensure unique indexes on all FK-target tables, then FK constraints.

down(opts)

drop_constraints(opts)

Drops FK constraints and NOT NULL from UUID FK columns.

Must be called BEFORE down/1 so constraints are removed before columns are dropped.

Order: FK constraints first (unblocks column removal), then NOT NULL.

up(opts)