-- Which statement line settled an order's card leg.
--
-- Without this, reconcileCardLeg has no way to know a charge has already been
-- consumed, so two orders on the same card inside the match window both bind to
-- it and each books its own credits remainder -- double-counting spend.
ALTER TABLE expense_metadata
  ADD COLUMN IF NOT EXISTS matched_transaction_id INTEGER
  REFERENCES transactions(id) ON DELETE SET NULL;

-- One statement line settles at most one order.
CREATE UNIQUE INDEX IF NOT EXISTS uq_expense_matched_txn
  ON expense_metadata (matched_transaction_id)
  WHERE matched_transaction_id IS NOT NULL;
