Files
accounted/supabase/migrations/20260807103000_mail_hunt_dedupe_per_attachment.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

34 lines
1.5 KiB
SQL

-- The unit of an underlag is an attachment, not a message.
--
-- 20260807090100 made (company_id, mail_message_id) unique for hunted mail, on
-- the assumption that one mail carries one receipt. A provkörning against a
-- real mailbox disproved it: receipts reach these inboxes by being forwarded in
-- batches, and a single message routinely carries several receipts for
-- different purchases ("Fwd: Kvitton februari" with five attachments, "Fwd:
-- Anthropic receipts" with two covering two different months).
--
-- Under the old index the first attachment filed would block every other
-- receipt in the same forward, silently and permanently. The key becomes
-- message + attachment.
--
-- Backfill first, so the new index can be created on existing rows: anything
-- already ingested was filed one-per-message, and its file key is derived from
-- the attachment id captured at the time.
UPDATE public.invoice_inbox_items
SET channel_context = channel_context || jsonb_build_object(
'mail_file_key',
(channel_context->>'mail_message_id') || '::' ||
COALESCE(channel_context->>'mail_attachment_id', '')
)
WHERE source = 'mail_hunt'
AND channel_context ? 'mail_message_id'
AND NOT (channel_context ? 'mail_file_key');
DROP INDEX IF EXISTS public.idx_invoice_inbox_mail_message_unique;
CREATE UNIQUE INDEX IF NOT EXISTS idx_invoice_inbox_mail_file_unique
ON public.invoice_inbox_items (company_id, ((channel_context->>'mail_file_key')))
WHERE source = 'mail_hunt';
NOTIFY pgrst, 'reload schema';