From 502e3f563c33b029eda0854f6aacfa83190391de Mon Sep 17 00:00:00 2001 From: siddharthd Date: Mon, 10 Aug 2026 17:42:45 +1000 Subject: [PATCH] 0029: rescope uq_expense_source_order for per-shipment bridge rows Split-shipment orders are charged per shipment; the spine bridge now writes one row per shipment sharing (source, order_reference). Meal-lane idempotency semantics unchanged (index scope excludes only source='order-bridge'); bridge rows get their own unique index on (source, source_message_id). Applied 2026-08-10. --- .../0029_bridge_shipment_rows/migration.sql | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 prisma/migrations/0029_bridge_shipment_rows/migration.sql diff --git a/prisma/migrations/0029_bridge_shipment_rows/migration.sql b/prisma/migrations/0029_bridge_shipment_rows/migration.sql new file mode 100644 index 0000000..3a82a65 --- /dev/null +++ b/prisma/migrations/0029_bridge_shipment_rows/migration.sql @@ -0,0 +1,25 @@ +-- 0029: let the spine bridge write one row PER SHIPMENT of a split order. +-- +-- An Amazon order that ships in two boxes is charged per shipment, so its +-- order_total matches no statement line — the bridge (ingestion-engine +-- jobs/order_transaction_bridge.py) matches each shipment's own amount and +-- items instead. Those rows share (source='order-bridge', order_reference), +-- which uq_expense_source_order forbade. +-- +-- The index exists as the MEAL lane's idempotency key (0018 I7), and that +-- lane's semantics are untouched: the scope simply excludes bridge rows, +-- whose idempotency key is source_message_id ( for whole-order +-- rows, #f per shipment) — now enforced with its own +-- unique index instead of by convention. No app code does ON CONFLICT +-- against either index; ingest idempotency is SELECT-based +-- (lib/order-ingestion.ts). + +DROP INDEX IF EXISTS uq_expense_source_order; + +CREATE UNIQUE INDEX uq_expense_source_order + ON expense_metadata (source, order_reference) + WHERE order_reference IS NOT NULL AND source <> 'order-bridge'; + +CREATE UNIQUE INDEX uq_expense_bridge_message + ON expense_metadata (source, source_message_id) + WHERE source = 'order-bridge';