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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix: handle possiblity of duplicate external ids in migraiton
Brad Barbinc33100c9eeda
diff --git a/packages/database/supabase/migrations/20260130005853_external-id-migration.sql b/packages/database/supabase/migrations/20260130005853_external-id-migration.sqlindex 2e88bce08a..9e9ae5d0f5 100644--- a/packages/database/supabase/migrations/20260130005853_external-id-migration.sql+++ b/packages/database/supabase/migrations/20260130005853_external-id-migration.sql@@ -57,7 +57,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- supplierINSERT INTO "externalIntegrationMapping" (@@ -85,7 +85,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- contactINSERT INTO "externalIntegrationMapping" (@@ -113,7 +113,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- itemINSERT INTO "externalIntegrationMapping" (@@ -141,7 +141,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- partINSERT INTO "externalIntegrationMapping" (@@ -169,7 +169,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- materialINSERT INTO "externalIntegrationMapping" (@@ -197,7 +197,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- toolINSERT INTO "externalIntegrationMapping" (@@ -225,7 +225,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- fixtureINSERT INTO "externalIntegrationMapping" (@@ -253,7 +253,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- consumableINSERT INTO "externalIntegrationMapping" (@@ -281,7 +281,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- addressINSERT INTO "externalIntegrationMapping" (@@ -309,7 +309,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- salesOrderINSERT INTO "externalIntegrationMapping" (@@ -337,7 +337,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- purchaseOrderINSERT INTO "externalIntegrationMapping" (@@ -365,7 +365,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- quoteINSERT INTO "externalIntegrationMapping" (@@ -393,7 +393,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- salesInvoiceINSERT INTO "externalIntegrationMapping" (@@ -421,7 +421,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- nonConformanceActionTask (JSON type, cast to JSONB)INSERT INTO "externalIntegrationMapping" (@@ -449,7 +449,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- ============================================================================-- STEP 2: Migrate tables requiring JOINs for companyId@@ -482,7 +482,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- supplierLocation (join supplier for companyId)INSERT INTO "externalIntegrationMapping" (@@ -511,7 +511,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- ============================================================================-- STEP 3: Migrate special tables@@ -543,7 +543,7 @@ WHERE t."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- user (many-to-many via userToCompany, one mapping per company membership)INSERT INTO "externalIntegrationMapping" (@@ -572,7 +572,7 @@ WHERE u."externalId" IS NOT NULLELSE kv.value->>'id'END) IS NOT NULL-ON CONFLICT ("entityType", "entityId", "integration", "companyId") DO NOTHING;+ON CONFLICT ("integration", "externalId", "entityType", "companyId") WHERE "allowDuplicateExternalId" = false DO NOTHING;-- ============================================================================-- STEP 4: Drop old indexes and columns