fix(purchases): sort new item insertions ascending to prevent unique index deadlock #116

Merged
yuris merged 1 commit from fix/83-purchase-deadlock into main 2026-09-28 20:42:59 +00:00
Owner

Resolves #83.

Summary

  • Sorts new item insertions ascending by normalized item name in createPurchase and updatePurchase before the items loop to ensure deterministic index lock acquisition and prevent SQLSTATE 40P01 deadlocks.
  • Preserves invoice line order for purchaseItems while resolving IDs from sorted inventory inserts.
  • Rejects duplicate new item names within the same purchase.
  • Adds unit tests in service.test.ts covering ascending insertion order, line order preservation, and name collision invariants.
Resolves #83. ### Summary - Sorts new item insertions ascending by normalized item name in `createPurchase` and `updatePurchase` before the items loop to ensure deterministic index lock acquisition and prevent SQLSTATE 40P01 deadlocks. - Preserves invoice line order for `purchaseItems` while resolving IDs from sorted inventory inserts. - Rejects duplicate new item names within the same purchase. - Adds unit tests in `service.test.ts` covering ascending insertion order, line order preservation, and name collision invariants.
fix(purchases): sort new item insertions ascending to prevent unique index deadlock (#83)
All checks were successful
Checks / lint (biome) (pull_request) Successful in 11s
Checks / test @garage/catalog (pull_request) Successful in 1m14s
Checks / test garage (pull_request) Successful in 1m21s
Checks / test garage-store (pull_request) Successful in 1m17s
Checks / typecheck apps/management (pull_request) Successful in 1m36s
Checks / typecheck apps/store (pull_request) Successful in 1m20s
c45cf50c75
yuris left a comment

VERDICT: approve

  • Correctly sorts new-item insertions in ascending normalized alphabetical order prior to insertion, acquiring unique index locks deterministically.
  • Symmetrically applied to both createPurchase and updatePurchase.
  • Preserves original purchase item line order and references newly resolved inventory IDs.
  • Verified with unit tests in service.test.ts and pnpm verify:management.
VERDICT: approve - Correctly sorts new-item insertions in ascending normalized alphabetical order prior to insertion, acquiring unique index locks deterministically. - Symmetrically applied to both `createPurchase` and `updatePurchase`. - Preserves original purchase item line order and references newly resolved inventory IDs. - Verified with unit tests in `service.test.ts` and `pnpm verify:management`.
yuris merged commit 9304861cde into main 2026-09-28 20:42:59 +00:00
yuris changed title from fix(purchases): sort new item insertions ascending to prevent unique index deadlock (#83) to fix(purchases): sort new item insertions ascending to prevent unique index deadlock 2026-09-28 20:45:55 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
yuris/garage!116
No description provided.