Files
accounted/supabase/migrations/20260904021000_rot_rut_payout_voucher_unique.sql
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

21 lines
1.1 KiB
SQL

-- One live settlement voucher per ROT/RUT begäran, enforced at the journal.
--
-- Two concurrent settles (two same-amount bank rows, or a headless call racing
-- a bank-row match) can both pass the application's "not yet settled" read and
-- both book debit 19xx / credit 1513 before the request row's compare-and-set
-- picks a winner. The loser's voucher would stand (posted entries are
-- immutable) and 1513 would be credited twice for one payout. This index makes
-- the second entry fail at insert, before it can ever be committed.
--
-- draft is included on purpose: the engine inserts a draft and commits it in a
-- second step, so a loser must fail at the draft insert and leave nothing
-- behind. Reversed/cancelled entries fall outside the predicate, so a storno
-- of a settlement never blocks a later re-settle.
-- pg-test: tests/pg/rot-rut-payout-voucher-unique.pg.test.ts
CREATE UNIQUE INDEX IF NOT EXISTS journal_entries_rot_rut_payout_live_unique
ON public.journal_entries (company_id, source_id)
WHERE source_type = 'rot_rut_payout'
AND source_id IS NOT NULL
AND status IN ('draft', 'posted');