Files
accounted/supabase/migrations/20260423140200_journal_entries_ai_provenance.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

48 lines
2.0 KiB
SQL

-- AI provenance on journal entries.
--
-- Adds two columns to journal_entries so that entries posted via the AI
-- agent flow carry a BFL-defensible audit trail: who created this entry,
-- and which AI proposal did the user approve to produce it?
--
-- `created_via` is informational — it describes the *method* of creation,
-- not the business event. The existing `source_type` column still describes
-- the business event (bank_transaction, invoice_created, supplier_invoice_
-- registered, etc.). An AI-proposed booking for a bank transaction will
-- have source_type='bank_transaction' AND created_via='ai_proposed'.
--
-- The existing immutability trigger (migration 017) prevents changes to
-- posted entries. These new columns are set while the entry is still in
-- draft status and frozen at commit — consistent with how the trigger
-- already treats other fields.
ALTER TABLE public.journal_entries
ADD COLUMN IF NOT EXISTS created_via text NOT NULL DEFAULT 'manual';
ALTER TABLE public.journal_entries
ALTER COLUMN created_via SET DEFAULT 'manual';
-- Re-apply NOT NULL for environments where the column was added out-of-band
UPDATE public.journal_entries SET created_via = 'manual' WHERE created_via IS NULL;
ALTER TABLE public.journal_entries
ALTER COLUMN created_via SET NOT NULL;
ALTER TABLE public.journal_entries
DROP CONSTRAINT IF EXISTS journal_entries_created_via_check;
ALTER TABLE public.journal_entries
ADD CONSTRAINT journal_entries_created_via_check
CHECK (created_via IN ('manual', 'ai_proposed', 'imported', 'system'));
-- Nullable FK — only AI-proposed entries link back to a proposal
ALTER TABLE public.journal_entries
ADD COLUMN IF NOT EXISTS source_proposal_id uuid
REFERENCES public.ai_proposals(id) ON DELETE SET NULL;
-- Audit lookup: "show me all AI-proposed entries from last month"
CREATE INDEX IF NOT EXISTS idx_journal_entries_created_via_ai
ON public.journal_entries (company_id, created_at DESC)
WHERE created_via = 'ai_proposed';
NOTIFY pgrst, 'reload schema';