36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
/**
|
|
* Adds the `dump_backlinks` table, recording which dumps, comments and
|
|
* descriptions link to a dump, and indexes `dumps.url`.
|
|
*
|
|
* Idempotent: safe to run against a fresh db (already created from schema.sql
|
|
* with these) or an existing one that predates them.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { DatabaseSync } from "node:sqlite";
|
|
|
|
/** Applies this migration to `db`. */
|
|
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);`,
|
|
);
|
|
}
|