/** * Adds `dump_backlinks`, which records the links from a dump's note or * comments to other dumps, so a dump page can list the dumps related to it. * backlink-service fills it, and the index on `dumps.url` lets it find the * dumps an external link was posted as. * * @module */ import type { DatabaseSync } from "node:sqlite"; /** * Run once by `runMigrations`. It doesn't scan the notes and comments already * posted, so their links are recorded only once they are edited. */ export function up(db: DatabaseSync): void { db.exec(`CREATE TABLE IF NOT EXISTS dump_backlinks ( source_type TEXT NOT NULL, source_id TEXT NOT NULL, from_dump_id TEXT NOT NULL, to_dump_id TEXT NOT NULL, created_at TEXT NOT NULL, PRIMARY KEY (source_type, source_id, to_dump_id), FOREIGN KEY (from_dump_id) REFERENCES dumps(id) ON DELETE CASCADE, FOREIGN KEY (to_dump_id) REFERENCES dumps(id) ON DELETE CASCADE );`); db.exec( `CREATE INDEX IF NOT EXISTS idx_dump_backlinks_to ON dump_backlinks(to_dump_id);`, ); db.exec( `CREATE INDEX IF NOT EXISTS idx_dump_backlinks_from ON dump_backlinks(from_dump_id);`, ); db.exec( `CREATE INDEX IF NOT EXISTS idx_dumps_url ON dumps(url);`, ); }