import { query } from './db'; import { migrateLegacyArticles } from './legacy-migrate'; // Schema "analysis": persistent, written through the API (pages/api/*), never touched by the data rebuild // (which replaces schema "live" on every deploy). Created on first use. const DDL = ` CREATE SCHEMA IF NOT EXISTS analysis; -- Page blocks (current content model): a page is a flat, ordered list of independent blocks ("stories"). -- page = 'paper//' (a paper's detail page) or 'content/' (an analysis page on /contents). -- type = markdown | list | callout | table | chart | mermaid | figure | code | links (static) -- stats | tool-table | paper-table | benchmark-table (filled from the database when rendered) CREATE TABLE IF NOT EXISTS analysis.page_block ( id bigserial PRIMARY KEY, page text NOT NULL, position integer NOT NULL, type text NOT NULL, data jsonb NOT NULL, revision integer NOT NULL DEFAULT 1, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS idx_page_block_page ON analysis.page_block (page, position); CREATE TABLE IF NOT EXISTS analysis.migration (name text PRIMARY KEY, done_at timestamptz NOT NULL DEFAULT now()); -- LEGACY (kept, no longer written or rendered): articles with sections. Copied once into page_block by lib/legacy-migrate.ts. -- Articles: one per (paper, topic); each has ordered, nestable sections; each section is an ordered list of typed blocks. CREATE TABLE IF NOT EXISTS analysis.article ( id bigserial PRIMARY KEY, slug text NOT NULL UNIQUE, subject_conf text NOT NULL, -- the paper this is about, by its stable key (conf, ext_id): subject_ext_id text NOT NULL, -- papers.id is not stable across data rebuilds title text NOT NULL, summary text, revision integer NOT NULL DEFAULT 1, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS idx_article_subject ON analysis.article (subject_conf, subject_ext_id); CREATE TABLE IF NOT EXISTS analysis.section ( id bigserial PRIMARY KEY, article_id bigint NOT NULL REFERENCES analysis.article(id) ON DELETE CASCADE, parent_id bigint REFERENCES analysis.section(id) ON DELETE CASCADE, key text NOT NULL, title text NOT NULL, level integer NOT NULL DEFAULT 2, position integer NOT NULL, UNIQUE (article_id, key) ); CREATE TABLE IF NOT EXISTS analysis.block ( id bigserial PRIMARY KEY, section_id bigint NOT NULL REFERENCES analysis.section(id) ON DELETE CASCADE, position integer NOT NULL, type text NOT NULL CHECK (type IN ('markdown','list','callout','table','chart','mermaid','figure','code')), data jsonb NOT NULL, UNIQUE (section_id, position) ); CREATE TABLE IF NOT EXISTS analysis.link ( id bigserial PRIMARY KEY, article_id bigint NOT NULL REFERENCES analysis.article(id) ON DELETE CASCADE, section_id bigint REFERENCES analysis.section(id) ON DELETE SET NULL, position integer NOT NULL, kind text NOT NULL CHECK (kind IN ('paper','code','project','demo','dataset','related','other')), title text NOT NULL, url text NOT NULL, note text ); -- Tool taxonomy: a tree of categories, and the tools that papers designed, each in one or more categories. CREATE TABLE IF NOT EXISTS analysis.tool_category ( slug text PRIMARY KEY, title text NOT NULL, parent_slug text REFERENCES analysis.tool_category(slug) DEFERRABLE INITIALLY DEFERRED, descr text, position integer NOT NULL DEFAULT 0 ); CREATE TABLE IF NOT EXISTS analysis.paper_tool ( id bigserial PRIMARY KEY, conf text NOT NULL, -- the paper, by its stable key (conf, ext_id) ext_id text NOT NULL, tool text NOT NULL, descr text, built_on text, ref text, -- where in the paper the tool is described position integer NOT NULL DEFAULT 0, UNIQUE (conf, ext_id, tool) ); CREATE TABLE IF NOT EXISTS analysis.paper_tool_category ( tool_id bigint NOT NULL REFERENCES analysis.paper_tool(id) ON DELETE CASCADE, category_slug text NOT NULL REFERENCES analysis.tool_category(slug) ON DELETE CASCADE, PRIMARY KEY (tool_id, category_slug) ); `; let ready: Promise | null = null; /** Create the analysis schema if it is missing (cached per process). */ export function ensureAnalysisSchema(): Promise { if (!ready) ready = query(DDL).then(() => migrateLegacyArticles()).catch((e) => { ready = null; throw e; }); return ready; }