Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(items): validate itemLedger FK in a separate migration

sidgaikwadb2e62f8c009b
diff --git a/packages/database/supabase/migrations/20260827112750_prevent-item-delete-with-inventory.sql b/packages/database/supabase/migrations/20260827112750_prevent-item-delete-with-inventory.sql
index 247a1ae79b..72e69905e0 100644
--- a/packages/database/supabase/migrations/20260827112750_prevent-item-delete-with-inventory.sql
+++ b/packages/database/supabase/migrations/20260827112750_prevent-item-delete-with-inventory.sql
@@ -47,14 +47,23 @@
-- 1. itemLedger.itemId: CASCADE -> NO ACTION. Re-created under the same name so
-- the app's error mapper keeps matching "itemLedger_itemId_fkey".
--- Added NOT VALID then VALIDATEd separately (repo convention, see
--- 20260703143904_composite-tenant-fks.sql): the NOT VALID add enforces the
--- ON DELETE NO ACTION action and checks new/changed rows immediately while
--- holding the write-blocking ShareRowExclusive lock only momentarily; the
--- separate VALIDATE scans existing rows under the lighter ShareUpdateExclusive
--- lock, which does not block reads or writes. Every itemLedger row already
--- references a live item (the dropped constraint was ON DELETE CASCADE, so
--- orphans were structurally impossible), so this VALIDATE is expected to pass.
+--
+-- The DROP + ADD run together in this migration's transaction (the Supabase
+-- CLI wraps each migration file in one txn -- which is why enum ADD VALUE
+-- cannot share a file with statements that use it), so there is never a
+-- window where itemLedger has no FK to item.
+--
+-- Added NOT VALID here, and VALIDATEd in a SEPARATE later migration
+-- (20260827115500_validate-item-ledger-item-fk). Splitting the validation
+-- into its own transaction is what makes it cheap: the NOT VALID add takes
+-- the write-blocking ShareRowExclusive lock only momentarily (no scan) and
+-- already enforces ON DELETE NO ACTION plus the check on new/changed rows;
+-- the follow-up VALIDATE scans existing rows under the lighter
+-- ShareUpdateExclusive lock, which does not block reads or writes. In the
+-- same file the ShareRowExclusive lock would be held until COMMIT, so the
+-- scan would still block writes -- the split is pointless unless VALIDATE
+-- runs after this migration commits. (Repo convention, cf.
+-- 20260703143904_composite-tenant-fks.sql.)
ALTER TABLE "itemLedger"
DROP CONSTRAINT "itemLedger_itemId_fkey";
@@ -64,9 +73,6 @@ ALTER TABLE "itemLedger"
ON DELETE NO ACTION ON UPDATE CASCADE
NOT VALID;
-ALTER TABLE "itemLedger"
- VALIDATE CONSTRAINT "itemLedger_itemId_fkey";
-
-- 2. costLedger.itemId: add the missing FK. NOT VALID skips the scan of
-- existing rows (past deletions left orphans with itemId pointing at a
-- now-deleted item), but the ON DELETE NO ACTION action and the check on
diff --git a/packages/database/supabase/migrations/20260827115500_validate-item-ledger-item-fk.sql b/packages/database/supabase/migrations/20260827115500_validate-item-ledger-item-fk.sql
new file mode 100644
index 0000000000..e0cdb432d8
--- /dev/null
+++ b/packages/database/supabase/migrations/20260827115500_validate-item-ledger-item-fk.sql
@@ -0,0 +1,25 @@
+-- ============================================================================
+-- Migration: validate-item-ledger-item-fk
+--
+-- Follow-up to 20260827112750_prevent-item-delete-with-inventory, which recreated
+-- "itemLedger_itemId_fkey" (CASCADE -> NO ACTION) as NOT VALID. That constraint
+-- already enforces ON DELETE NO ACTION and checks every new/changed row; this
+-- migration validates the pre-existing rows.
+--
+-- Kept in its OWN migration on purpose. The Supabase CLI runs each migration
+-- file in a single transaction, so the ADD ... NOT VALID above holds a
+-- write-blocking ShareRowExclusive lock on itemLedger until that file commits.
+-- Running VALIDATE in a separate transaction lets its full-table scan take the
+-- lighter ShareUpdateExclusive lock instead, which does not block reads or
+-- writes -- the point of adding the constraint NOT VALID in the first place.
+-- Same deploy, separate transactions: the CLI commits each file before the next.
+--
+-- Every itemLedger row already references a live item -- the constraint it
+-- replaced was ON DELETE CASCADE, so orphaned ledger rows were structurally
+-- impossible -- so this VALIDATE is expected to pass without touching any row.
+-- (costLedger's FK is deliberately left NOT VALID: past deletions left orphan
+-- cost rows, so validating it would fail; it is not validated here.)
+-- ============================================================================
+
+ALTER TABLE "itemLedger"
+ VALIDATE CONSTRAINT "itemLedger_itemId_fkey";