CI / build (push) Successful in 34s
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
80 lines
3.3 KiB
JavaScript
80 lines
3.3 KiB
JavaScript
// leadboard.db:用 Node 內建的 node:sqlite,不另外裝套件。
|
|
// 資料表由 MIGRATIONS 建立;每個版本只跑一次,記在 schema_migrations。要改資料表就「新增」一個版本,不要改舊的。
|
|
import { chmodSync, existsSync, mkdirSync } from 'node:fs';
|
|
import { dirname } from 'node:path';
|
|
import { DatabaseSync } from 'node:sqlite';
|
|
|
|
export const MIGRATIONS = Object.freeze([
|
|
{
|
|
version: 1,
|
|
name: 'documents-and-import-log',
|
|
sql: `
|
|
-- 原本 /app/data 裡的每個 JSON 檔對應一列;name 就是原來的相對檔名(例如 archives/2027.json),
|
|
-- body 是檔案內容原樣的文字,sha256 / bytes 供核對。
|
|
CREATE TABLE documents (
|
|
name TEXT PRIMARY KEY NOT NULL,
|
|
body TEXT NOT NULL,
|
|
sha256 TEXT NOT NULL,
|
|
bytes INTEGER NOT NULL,
|
|
updated_at TEXT NOT NULL
|
|
) STRICT;
|
|
-- 每次匯入/核對的紀錄。
|
|
CREATE TABLE import_log (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
started_at TEXT NOT NULL,
|
|
finished_at TEXT NOT NULL,
|
|
source_dir TEXT NOT NULL,
|
|
mode TEXT NOT NULL,
|
|
imported INTEGER NOT NULL,
|
|
unchanged INTEGER NOT NULL,
|
|
conflicts INTEGER NOT NULL,
|
|
records_checked INTEGER NOT NULL,
|
|
ok INTEGER NOT NULL,
|
|
detail TEXT NOT NULL
|
|
) STRICT;
|
|
`,
|
|
},
|
|
]);
|
|
|
|
export function migrate(db, migrations = MIGRATIONS, now = () => new Date().toISOString()) {
|
|
db.exec(`CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
version INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, applied_at TEXT NOT NULL) STRICT`);
|
|
const applied = new Map(db.prepare('SELECT version, name FROM schema_migrations').all().map(row => [row.version, row.name]));
|
|
const known = new Set(migrations.map(item => item.version));
|
|
for (const version of applied.keys()) {
|
|
if (!known.has(version)) throw new Error(`資料庫有這個程式不認得的版本 ${version}(程式比資料庫舊?)`);
|
|
}
|
|
const ran = [];
|
|
for (const item of [...migrations].sort((a, b) => a.version - b.version)) {
|
|
if (applied.has(item.version)) continue;
|
|
db.exec('BEGIN IMMEDIATE');
|
|
try {
|
|
db.exec(item.sql);
|
|
db.prepare('INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)').run(item.version, item.name, now());
|
|
db.exec('COMMIT');
|
|
} catch (error) {
|
|
db.exec('ROLLBACK');
|
|
throw new Error(`migration ${item.version} (${item.name}) 失敗:${error.message}`);
|
|
}
|
|
ran.push(item.version);
|
|
}
|
|
return ran;
|
|
}
|
|
|
|
// 開啟(沒有就建立)並套用 migration。日誌模式維持預設的 DELETE:整個資料庫只有 leadboard.db 一個檔案,
|
|
// 平台備份複製這一個檔案就是完整的(WAL 會多出 -wal / -shm,單檔備份可能漏掉最近的寫入)。
|
|
export function openDatabase(path, options = {}) {
|
|
if (path !== ':memory:') mkdirSync(dirname(path), { recursive: true });
|
|
const fresh = path !== ':memory:' && !existsSync(path);
|
|
const db = new DatabaseSync(path);
|
|
if (fresh) {
|
|
try { chmodSync(path, 0o600); } catch {
|
|
// 權限改不了就維持預設
|
|
}
|
|
}
|
|
db.exec('PRAGMA busy_timeout = 5000');
|
|
db.exec('PRAGMA synchronous = FULL');
|
|
migrate(db, options.migrations, options.now);
|
|
return db;
|
|
}
|