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(items): imports of one part wait for each other before creating a version
Raulfe5a3c714ca1
diff --git a/.ai/plans/2026-10-21-method-import-append.md b/.ai/plans/2026-10-21-method-import-append.mdindex 30000a3f18..58b4579ed6 100644--- a/.ai/plans/2026-10-21-method-import-append.md+++ b/.ai/plans/2026-10-21-method-import-append.md@@ -78,6 +78,7 @@ import match the BOM and Bill of Process editors.| CodeRabbit: a non-numeric Op No never matched an existing operation, so a re-run added it again, and its fallback order could collide with a numeric Op No. | Op No must be a number. Op Nos compare by value. || CodeRabbit: the change notice approval check ran a separate request while a part's transaction was open. | The import reads every change notice lock before any transaction. || CodeRabbit: the status check read the version before the row lock, so a Draft activated in that gap was still written. | The refusals read the locked row. |+| CodeRabbit: two imports that created the same new version at once both inserted it, and the second failed on the unique constraint. | `writeGroup` locks the part's item row (`FOR NO KEY UPDATE`) before it reads the versions. || The BOM wizard preset EA as the Unit of Measure, so a blank unit never reached the import's fallback to the material's unit. | The wizard presets no BOM unit. || The results said "4 updated" for 4 parts and 6 BOM lines, and counted skipped rows in the same sentence. | Method imports also return the rows added by kind (`added`). The results dialog and the toast name the parts, then the rows. |diff --git a/.claude/rules/csv-import-system.md b/.claude/rules/csv-import-system.mdindex 087f03d43c..d758713431 100644--- a/.claude/rules/csv-import-system.md+++ b/.claude/rules/csv-import-system.md@@ -308,7 +308,10 @@ continue.number. An Inspection operation is refused, because`methodOperationValidator` requires an Inspection Plan and a CSV has nocolumn for one. Pinned by `method-import-guards.test.ts`.-- **Concurrency.** `writeGroup` locks the target `makeMethod` row+- **Concurrency.** `writeGroup` locks the part's `item` row (`FOR NO KEY+ UPDATE`, so rows that reference the item can still be written) before it+ reads the versions, so two imports that create the same new version create+ it once. It then locks the target `makeMethod` row(`FOR UPDATE`) before it reads the version's lines, so two imports of onefile write each line once. The refusals read the status and change noticeof the locked row, so a Draft an editor activates or links to a noticediff --git a/apps/erp/app/modules/shared/shared.service.ts b/apps/erp/app/modules/shared/shared.service.tsindex 148030a55b..925a154044 100644--- a/apps/erp/app/modules/shared/shared.service.ts+++ b/apps/erp/app/modules/shared/shared.service.ts@@ -4090,6 +4090,18 @@ async function writeGroup(revisionStatus = parent.revisionStatus;}+ // Imports of one part run one at a time from here, so a second import+ // reads the version the first created instead of creating it again.+ // NO KEY UPDATE leaves rows that reference the item (jobs, quote lines,+ // other BOMs) free to be written meanwhile.+ await trx+ .selectFrom("item")+ .select("id")+ .where("id", "=", parentItemId)+ .where("companyId", "=", companyId)+ .forNoKeyUpdate()+ .execute();+const methods: MakeMethodVersion[] =g.versions.size === 0? []diff --git a/apps/erp/test/database/method-import-guards.test.ts b/apps/erp/test/database/method-import-guards.test.tsindex dbcbf39d65..156abadde2 100644--- a/apps/erp/test/database/method-import-guards.test.ts+++ b/apps/erp/test/database/method-import-guards.test.ts@@ -319,6 +319,38 @@ describe("method import guards", () => {});+ databaseTest(+ "two imports that create the same version at the same time create it once",+ async () => {+ f = await methodImportFixture();+ const second = await connectLocalTestDatabase();+ try {+ const rows = [+ bom("ASSY", "COMP-1", "1", { makeMethodVersion: "2" }),+ bom("ASSY", "COMP-2", "2", { makeMethodVersion: "2" })+ ];+ const [a, b] = await Promise.all([+ f.run("bom", rows),+ f.run("bom", rows, second)+ ]);+ expect([...a.errors, ...b.errors]).toEqual([]);+ expect(a.updated + b.updated).toBe(1);+ expect(a.skipped.length + b.skipped.length).toBe(2);+ const versions = await f.db+ .selectFrom("makeMethod")+ .select("version")+ .where("itemId", "=", f.itemId("ASSY"))+ .where("companyId", "=", f.companyId)+ .orderBy("version")+ .execute();+ expect(versions.map((v) => Number(v.version))).toEqual([1, 2]);+ expect((await rowCounts()).materials).toBe(2);+ } finally {+ await second.destroy();+ }+ }+ );+databaseTest("two imports of one file at the same time write each line once",async () => {