rag-service/migrations/006_ocr_page_content_selection.sql

22 lines
1.4 KiB
SQL

-- NULL preserves the semantics of historical candidates and approved artifacts.
ALTER TABLE rag_document_pages ADD COLUMN IF NOT EXISTS content_selection jsonb NULL;
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_constraint
WHERE conrelid = 'rag_document_pages'::regclass
AND conname = 'rag_document_pages_content_selection_check') THEN
ALTER TABLE rag_document_pages ADD CONSTRAINT rag_document_pages_content_selection_check
CHECK (content_selection IS NULL OR (
jsonb_typeof(content_selection) = 'object'
AND content_selection->>'policyVersion' = 'page-best-effort-v1'
AND content_selection->>'source' IN ('native', 'ocr', 'none')
AND content_selection->>'reason' IN ('native_only', 'ocr_accepted', 'native_fallback', 'ocr_warning', 'ocr_blocked', 'blank_discrepancy', 'verified_blank')
AND jsonb_typeof(content_selection->'contentUnrecovered') = 'boolean'
AND ((content_selection->>'contentUnrecovered')::boolean = (content_selection->>'reason' = 'ocr_blocked'))
AND (content_selection->'issue' = 'null'::jsonb OR content_selection->>'issue'
IN ('visual_content_not_incorporated', 'ocr_warning', 'content_not_recovered', 'blank_native_discrepancy'))
AND content_selection ?& ARRAY['policyVersion', 'source', 'reason', 'contentUnrecovered', 'issue']
) IS TRUE);
END IF;
END $$;