Files
ai-taxonomy-literature-anal…/lib/analysis-schema.ts
T
claude-bot 5407bf4dff
Deploy / deploy (push) Successful in 8m0s
Page blocks: papers and content pages as flat lists of story blocks in the database
A page is now an ordered list of independent blocks (analysis.page_block), no sections and no
headings; every block has an id shown on the page so it can be pointed at. Paper detail pages
use page "paper/<conf>/<ext_id>", analysis pages on /contents use "content/<node id>".

- /api/blocks (list, replace a page, add) and /api/blocks/:id (get, patch, move, delete)
- dynamic block types (stats, tool-table, paper-table, benchmark-table) are filled from the
  database at render time; the two content pages are rendered from their blocks (seeded with
  defaults on first view)
- one-time migration copies the legacy articles into blocks (section titles dropped, links
  become a links block); legacy tables are kept untouched
- old /api/articles, ArticleView and PaperGroup removed

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbMbJPadPzVwvt9rU9GBNo
2026-10-23 22:51:54 +08:00

107 lines
4.8 KiB
TypeScript

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/<conf>/<ext_id>' (a paper's detail page) or 'content/<node id>' (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<unknown> | null = null;
/** Create the analysis schema if it is missing (cached per process). */
export function ensureAnalysisSchema(): Promise<unknown> {
if (!ready) ready = query(DDL).then(() => migrateLegacyArticles()).catch((e) => { ready = null; throw e; });
return ready;
}