資料改存 SQLite(leadboard.db):建表 migration+可重跑匯入+逐筆核對 #33

Merged
walkdoggo.yeh merged 13 commits from feature/sqlite-storage into main 2026-11-23 08:34:01 +00:00
Owner

做了什麼

把 /app/data 底下原本的 JSON 存檔,改存進 /app/data/leadboard.db(SQLite,Node 內建的 node:sqlite,沒有新增套件)。

  • 建表 migration:db.js(schema_migrations、documents、import_log;之後要改表就新增版本)。
  • 可重跑的匯入程式:import-json.js(node import-json.js --dir /app/data [--dry-run | --verify-only | --force])。JSON 內容原樣存入,讀回後逐筆核對;重跑時相同的略過,資料庫裡內容不同的不覆蓋(要 --force);任何一個檔案不是合法 JSON 就整批不寫。
  • 各存檔改接同一個存檔介面(storage.js):成本覆寫、外包、核心參數、英文名、待生效設定、目前年度、各年在職月份、定錨、封存、手動權限、預排異動、登入狀態。
  • 第一次啟動(data-store.js):資料庫還沒有成功的匯入紀錄時,自動把資料夾裡的 JSON 搬進去並逐筆核對;核對不過就不啟動(寧可不上線,也不顯示空資料)。
  • STORAGE_BACKEND=json 可退回舊的 JSON 檔存法;/api/health 多一個 storage 欄位(sqlite / file / memory)。
  • Dockerfile 的 COPY 清單加入新檔案。vibe.yaml 的 persistence 沒有動。
  • JSON 檔一律不刪、不改。

切換後要知道

  • 部署後,資料庫是正式存放處;JSON 檔留在原地但不再更新(會越來越舊)。要退回舊版程式前,請先回報,不要直接退版(舊版會讀到過期的 JSON)。
  • 還原點 tag:before-sqlite-storage。
  • 移除 JSON 另開 PR,等平台備份、核對資料一致之後再做。

測試

新增 tests/sqlite-storage.test.mjs(migration、原樣存取、重跑、衝突不覆蓋、整批失敗、首次啟動匯入、各 store 走資料庫);tests/server.test.mjs 改成從 leadboard.db 讀存檔。CI 全綠(測試、建置映像、容器 healthy)。

🤖 Generated with Claude Code

https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ

## 做了什麼 把 /app/data 底下原本的 JSON 存檔,改存進 /app/data/leadboard.db(SQLite,Node 內建的 `node:sqlite`,沒有新增套件)。 - **建表 migration**:`db.js`(`schema_migrations`、`documents`、`import_log`;之後要改表就新增版本)。 - **可重跑的匯入程式**:`import-json.js`(`node import-json.js --dir /app/data [--dry-run | --verify-only | --force]`)。JSON 內容原樣存入,讀回後逐筆核對;重跑時相同的略過,資料庫裡內容不同的**不覆蓋**(要 `--force`);任何一個檔案不是合法 JSON 就整批不寫。 - **各存檔改接同一個存檔介面**(`storage.js`):成本覆寫、外包、核心參數、英文名、待生效設定、目前年度、各年在職月份、定錨、封存、手動權限、預排異動、登入狀態。 - **第一次啟動**(`data-store.js`):資料庫還沒有成功的匯入紀錄時,自動把資料夾裡的 JSON 搬進去並逐筆核對;核對不過就不啟動(寧可不上線,也不顯示空資料)。 - `STORAGE_BACKEND=json` 可退回舊的 JSON 檔存法;`/api/health` 多一個 `storage` 欄位(sqlite / file / memory)。 - Dockerfile 的 COPY 清單加入新檔案。`vibe.yaml` 的 persistence 沒有動。 - **JSON 檔一律不刪、不改。** ## 切換後要知道 - 部署後,資料庫是正式存放處;JSON 檔留在原地但不再更新(會越來越舊)。要退回舊版程式前,請先回報,不要直接退版(舊版會讀到過期的 JSON)。 - 還原點 tag:`before-sqlite-storage`。 - 移除 JSON 另開 PR,等平台備份、核對資料一致之後再做。 ## 測試 新增 `tests/sqlite-storage.test.mjs`(migration、原樣存取、重跑、衝突不覆蓋、整批失敗、首次啟動匯入、各 store 走資料庫);`tests/server.test.mjs` 改成從 leadboard.db 讀存檔。CI 全綠(測試、建置映像、容器 healthy)。 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
walkdoggo.yeh added 13 commits 2026-11-23 08:06:24 +00:00
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
新增測試:SQLite 存檔、migration、匯入與核對
CI / build (push) Successful in 32s
CI / build (pull_request) Successful in 33s
091c93993f
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
walkdoggo.yeh merged commit f6621fc61c into main 2026-11-23 08:34:01 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: walkdoggo.yeh/leadboard#33