-- 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 (<entity_key> for whole-order
-- rows, <entity_key>#f<fact_id> 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';
