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.

final changes

Gaurav Bhattdf87113ad068
diff --git a/packages/database/supabase/migrations/20260521120000_supplier-customer-readable-id.sql b/packages/database/supabase/migrations/20260521120000_supplier-customer-readable-id.sql
index 74a63d2ad9..3bb468f663 100644
--- a/packages/database/supabase/migrations/20260521120000_supplier-customer-readable-id.sql
+++ b/packages/database/supabase/migrations/20260521120000_supplier-customer-readable-id.sql
@@ -1,27 +1,15 @@
-- Add a first-class human-readable identifier to supplier and customer.
--
--- Existing rows keep readableId = NULL until they're either re-imported via
--- CSV (importer sets readableId = CSV Unique ID) or edited in-app (form has
--- a SequenceOrCustomId input). The csv mapping rows in
--- externalIntegrationMapping remain the dedup key for re-imports.
+-- Every supplier/customer gets a readableId — either user-supplied (CSV import
+-- "Unique ID" column or the SequenceOrCustomId form field) or auto-generated
+-- from a per-company sequence (SUP000001 / CUS000001 style).
+-- 1. Add the column (initially nullable so the backfill can run).
ALTER TABLE "supplier" ADD COLUMN IF NOT EXISTS "readableId" TEXT;
ALTER TABLE "customer" ADD COLUMN IF NOT EXISTS "readableId" TEXT;
--- Partial unique index: NULL allowed (in-app-created entities have no
--- natural readableId), but non-NULL values must be unique per company.
-CREATE UNIQUE INDEX IF NOT EXISTS "supplier_readableId_companyId_unique"
- ON "supplier" ("companyId", "readableId")
- WHERE "readableId" IS NOT NULL;
-
-CREATE UNIQUE INDEX IF NOT EXISTS "customer_readableId_companyId_unique"
- ON "customer" ("companyId", "readableId")
- WHERE "readableId" IS NOT NULL;
-
--- Seed default sequences for every existing company so that supplier and
--- customer get auto-generated readableIds (SUP-000001, CUS-000001, etc.)
--- matching the convention used by purchaseOrder/quote/salesOrder.
--- Note: get_next_sequence() formats as: prefix + zero-padded(size) + suffix.
+-- 2. Seed default sequences for every existing company.
+-- get_next_sequence() formats as: prefix + zero-padded(size) + suffix.
INSERT INTO "sequence" ("table", "name", "prefix", "suffix", "next", "size", "step", "companyId", "updatedBy")
SELECT 'supplier', 'Supplier', 'SUP', NULL, 0, 6, 1, c.id, 'system'
FROM "company" c
@@ -32,16 +20,34 @@ SELECT 'customer', 'Customer', 'CUS', NULL, 0, 6, 1, c.id, 'system'
FROM "company" c
ON CONFLICT ("table", "companyId") DO NOTHING;
--- BEFORE INSERT trigger: auto-fill readableId via get_next_sequence when
--- the caller didn't provide one. This means ALL insertion paths (in-app
--- creation, CSV import where the user didn't map Unique ID, future code)
--- get a readableId for free without per-call boilerplate.
+-- 3. Backfill existing rows so the column can be tightened to NOT NULL.
+-- Each call to get_next_sequence() atomically increments the per-company
+-- counter, so two rows in the same company get distinct ids.
+UPDATE "supplier"
+SET "readableId" = get_next_sequence('supplier', "companyId")
+WHERE "readableId" IS NULL;
+
+UPDATE "customer"
+SET "readableId" = get_next_sequence('customer', "companyId")
+WHERE "readableId" IS NULL;
+
+-- 4. Require the column and enforce per-company uniqueness.
+ALTER TABLE "supplier" ALTER COLUMN "readableId" SET NOT NULL;
+ALTER TABLE "customer" ALTER COLUMN "readableId" SET NOT NULL;
+
+CREATE UNIQUE INDEX IF NOT EXISTS "supplier_readableId_companyId_unique"
+ ON "supplier" ("companyId", "readableId");
+
+CREATE UNIQUE INDEX IF NOT EXISTS "customer_readableId_companyId_unique"
+ ON "customer" ("companyId", "readableId");
+
+-- 5. BEFORE INSERT trigger: auto-fill readableId via get_next_sequence when
+-- the caller didn't provide one (e.g., in-app create with blank ID field).
+-- Trigger no-ops if readableId is already set, so explicit values win.
CREATE OR REPLACE FUNCTION set_supplier_readable_id_on_insert()
RETURNS TRIGGER AS $$
BEGIN
IF NEW."readableId" IS NULL OR NEW."readableId" = '' THEN
- -- Only fire if a sequence row exists for this company. If not, leave
- -- readableId NULL — that's the safe default.
IF EXISTS (
SELECT 1 FROM "sequence"
WHERE "table" = 'supplier' AND "companyId" = NEW."companyId"
@@ -80,7 +86,7 @@ CREATE TRIGGER set_customer_readable_id
FOR EACH ROW
EXECUTE PROCEDURE set_customer_readable_id_on_insert();
--- Recreate the suppliers and customers views to expose readableId.
+-- 6. Recreate the suppliers and customers views to expose readableId.
-- Postgres views freeze their column manifest at CREATE time, so adding a
-- column to the base table does not propagate without a DROP/CREATE cycle.
diff --git a/packages/database/supabase/migrations/20260605120000_supplier-customer-readable-id-required.sql b/packages/database/supabase/migrations/20260605120000_supplier-customer-readable-id-required.sql
deleted file mode 100644
index 29b81c6501..0000000000
--- a/packages/database/supabase/migrations/20260605120000_supplier-customer-readable-id-required.sql
+++ /dev/null
@@ -1,46 +0,0 @@
--- Tighten supplier.readableId and customer.readableId to NOT NULL.
---
--- Follow-up to 20260521120000_supplier-customer-readable-id.sql, which
--- introduced the column as nullable so the deploy could land without
--- disturbing existing rows. By the time this migration runs, the trigger +
--- form + importer paths have had time to populate readableId for any rows
--- users care about. Anything still NULL gets a sequence-generated id here
--- so we can lock the column down to NOT NULL.
---
--- Run only after verifying:
--- 1. The auto-fill trigger on supplier/customer is firing for new inserts
--- (check a few recent rows: SELECT "readableId" FROM supplier ORDER
--- BY "createdAt" DESC LIMIT 10 — should all be non-NULL).
--- 2. The SequenceOrCustomId form input is wired up in production.
--- 3. CSV import is setting readableId on new entities.
-
--- Defensive: re-seed any missing sequences. The 20260521120000 migration
--- already did this for companies present at that time, but a company
--- created via some bypass path between then and now might be missing the
--- sequence row, which would cause get_next_sequence() below to raise.
-INSERT INTO "sequence" ("table", "name", "prefix", "suffix", "next", "size", "step", "companyId", "updatedBy")
-SELECT 'supplier', 'Supplier', 'SUP', NULL, 0, 6, 1, c.id, 'system'
-FROM "company" c
-ON CONFLICT ("table", "companyId") DO NOTHING;
-
-INSERT INTO "sequence" ("table", "name", "prefix", "suffix", "next", "size", "step", "companyId", "updatedBy")
-SELECT 'customer', 'Customer', 'CUS', NULL, 0, 6, 1, c.id, 'system'
-FROM "company" c
-ON CONFLICT ("table", "companyId") DO NOTHING;
-
--- Backfill any remaining NULL readableIds using the per-company sequence.
--- Each row's UPDATE triggers one get_next_sequence() call, which atomically
--- increments the sequence's "next" counter and returns the formatted value.
--- The partial unique index on (companyId, readableId) WHERE readableId IS
--- NOT NULL guarantees no collisions with values already present.
-UPDATE "supplier"
-SET "readableId" = get_next_sequence('supplier', "companyId")
-WHERE "readableId" IS NULL;
-
-UPDATE "customer"
-SET "readableId" = get_next_sequence('customer', "companyId")
-WHERE "readableId" IS NULL;
-
--- All rows now have a value. Lock the column down.
-ALTER TABLE "supplier" ALTER COLUMN "readableId" SET NOT NULL;
-ALTER TABLE "customer" ALTER COLUMN "readableId" SET NOT NULL;