legacy-selection-store.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { mkdtemp, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { DatabaseSync } from "node:sqlite";
  5. import { createSqliteSessionStore } from "@zcode/adapters/storage";
  6. import { SESSION_ENTRY_MODEL_SELECTION, type SessionId } from "@zcode/contracts";
  7. /** 旧数据必须从磁盘播种;新版 writer 会包装新字段,不能拿它伪造升级前状态。 */
  8. export async function createLegacySelectionStoreFixture() {
  9. const root = await mkdtemp(join(tmpdir(), "app-legacy-selection-"));
  10. const dbPath = join(root, "sessions.sqlite");
  11. let store = await createSqliteSessionStore({ dbPath });
  12. const db = new DatabaseSync(dbPath);
  13. return {
  14. get store() {
  15. return store;
  16. },
  17. async seedLegacy(sessionID: SessionId, data: unknown) {
  18. db.prepare(
  19. "insert into session_entry(id, session_id, type, time_created, time_updated, data) values(?,?,?,?,?,?)",
  20. ).run(
  21. `${sessionID}:runtime-model-selection`,
  22. sessionID,
  23. SESSION_ENTRY_MODEL_SELECTION,
  24. 2,
  25. 2,
  26. JSON.stringify(data),
  27. );
  28. // Todo109:播种旧格式后关闭并重开,迁移必须由启动门禁触发,不再由 resume 补迁。
  29. store.close();
  30. db.exec("DELETE FROM schema_migration WHERE id='0020_provider_model_selection'");
  31. store = await createSqliteSessionStore({ dbPath });
  32. },
  33. async dispose() {
  34. db.close();
  35. store.close();
  36. await rm(root, { recursive: true, force: true });
  37. },
  38. };
  39. }