-- Base RAG contract. Mirrors the live ccc-digital-staging tables (inspected 2026-11-21) -- so local runs and staging share one schema. Idempotent; never drops anything. create extension if not exists vector; create extension if not exists pgcrypto; create table if not exists public.rag_documents ( id uuid primary key default gen_random_uuid(), source_key text not null unique, source_filename text not null, source_type text not null, title text, canonical_markdown text not null, content_sha256 text not null, metadata jsonb not null default '{}'::jsonb, extraction_status text not null default 'pending' check (extraction_status in ('pending','validated','indexed','rejected','failed')), created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); create index if not exists rag_documents_content_sha256_idx on public.rag_documents (content_sha256); create table if not exists public.rag_ingestion_runs ( id uuid primary key default gen_random_uuid(), document_id uuid references public.rag_documents(id) on delete set null, source_key text not null, status text not null check (status in ('pending','extracting','validated','embedding','indexed','failed','rejected')), pipeline_version text not null default 'v1', input_bytes bigint, extracted_characters bigint, useful_text_ratio numeric, chunks_created integer not null default 0, chunks_deduplicated integer not null default 0, chunks_embedded integer not null default 0, error_message text, routing_log jsonb not null default '{}'::jsonb, metrics jsonb not null default '{}'::jsonb, started_at timestamptz not null default now(), completed_at timestamptz, created_at timestamptz not null default now() ); create index if not exists rag_ingestion_runs_source_key_idx on public.rag_ingestion_runs (source_key, created_at desc); create table if not exists public.rag_chunks ( id uuid primary key default gen_random_uuid(), document_id uuid not null references public.rag_documents(id) on delete cascade, ingestion_run_id uuid references public.rag_ingestion_runs(id) on delete set null, ordinal integer not null, heading_path text, content text not null, content_sha256 text not null, token_estimate integer, char_start integer, char_end integer, metadata jsonb not null default '{}'::jsonb, embedding vector(384), embedding_model text not null default 'BAAI/bge-small-en-v1.5', embedding_created_at timestamptz, created_at timestamptz not null default now(), updated_at timestamptz not null default now(), unique (document_id, ordinal), unique (document_id, content_sha256) ); create index if not exists rag_chunks_document_id_idx on public.rag_chunks (document_id); create index if not exists rag_chunks_content_sha256_idx on public.rag_chunks (content_sha256); create index if not exists rag_chunks_metadata_gin_idx on public.rag_chunks using gin (metadata); create index if not exists rag_chunks_embedding_hnsw_idx on public.rag_chunks using hnsw (embedding vector_cosine_ops) with (m = 16, ef_construction = 64); create or replace function public.match_rag_chunks(query_embedding vector, match_count integer default 10, filter_metadata jsonb default '{}'::jsonb) returns table(id uuid, document_id uuid, content text, heading_path text, metadata jsonb, similarity double precision) language sql stable as $$ select c.id, c.document_id, c.content, c.heading_path, c.metadata, 1 - (c.embedding <=> query_embedding) from public.rag_chunks c where c.embedding is not null and c.metadata @> filter_metadata order by c.embedding <=> query_embedding limit greatest(match_count, 1); $$;