From 731cc1ba1495035643554a81b89efbbfef0252ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?walkdoggo=2Eyeh=5F=E8=91=89=E5=82=B3=E8=80=80?= Date: Mon, 23 Nov 2026 08:03:05 +0000 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20db.js=EF=BC=9Aleadboard.db?= =?UTF-8?q?=20=E8=88=87=E5=BB=BA=E8=A1=A8=20migration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ --- db.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 db.js diff --git a/db.js b/db.js new file mode 100644 index 0000000..bf1e347 --- /dev/null +++ b/db.js @@ -0,0 +1,79 @@ +// 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; +}