check-sqlite-schema.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env -S pnpm exec tsx
  2. // 基线是实际迁移产物;校验旧列/主键/索引兼容,并要求 schema 变更显式更新可审阅基线。
  3. import { execFile } from "node:child_process";
  4. import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  5. import { tmpdir } from "node:os";
  6. import { join } from "node:path";
  7. import { DatabaseSync } from "node:sqlite";
  8. import { promisify } from "node:util";
  9. import { fileURLToPath } from "node:url";
  10. import { TaskIndexRepo } from "../packages/services/src/session/taskIndexRepo.js";
  11. import { AutomationRepo } from "../packages/services/src/session/automationRepo.js";
  12. import { OffPeakTaskRepo } from "../packages/services/src/session/offPeakTaskRepo.js";
  13. import { getTasksIndexDatabasePath, setDataBaseDir } from "../packages/services/src/paths.js";
  14. import { SQLITE_MIGRATIONS } from "../apps/zcode-cli/packages/adapters/src/storage/session-store/migrations.js";
  15. import { sqliteMigrations } from "../packages/shared/src/sqlite-migrations/index.js";
  16. import { assertAdditiveSchema, dumpSqliteSchema } from "./sqlite-schema-contract.mjs";
  17. process.chdir(fileURLToPath(new URL("..", import.meta.url)));
  18. const update = process.argv.includes("--update");
  19. const base =
  20. process.env.CI_MERGE_REQUEST_DIFF_BASE_SHA || process.env.SQLITE_SCHEMA_BASE_REF || "HEAD";
  21. const exec = promisify(execFile);
  22. await exec("git", ["rev-parse", "--verify", `${base}^{commit}`]);
  23. const directory = await mkdtemp(join(tmpdir(), "zcode-schema-check-"));
  24. const cli = new DatabaseSync(":memory:");
  25. setDataBaseDir(directory);
  26. const repos = [new AutomationRepo(), new OffPeakTaskRepo(), new TaskIndexRepo()];
  27. try {
  28. for (const repo of repos) await repo.ensureReady();
  29. const app = new DatabaseSync(getTasksIndexDatabasePath(), { readOnly: true });
  30. let appSql: string;
  31. try {
  32. appSql = dumpSqliteSchema(app);
  33. } finally {
  34. app.close();
  35. }
  36. await sqliteMigrations.migrate(cli, SQLITE_MIGRATIONS, {
  37. database: "cli",
  38. backupDirectories: [directory],
  39. appliedAtColumn: "time_applied",
  40. });
  41. const schemas = [
  42. ["packages/services/src/project-storage/schema.baseline.sql", appSql],
  43. [
  44. "apps/zcode-cli/packages/adapters/src/storage/session-store/schema.baseline.sql",
  45. dumpSqliteSchema(cli),
  46. ],
  47. ];
  48. for (const [path, current] of schemas) {
  49. // 旧基线来自目标 git 状态;修改本地基线不能绕过只增约束。
  50. let previous: string | undefined;
  51. try {
  52. previous = (await exec("git", ["show", `${base}:${path}`])).stdout;
  53. } catch {
  54. /* 首次引入该库的基线时没有历史文件。 */
  55. }
  56. if (previous) assertAdditiveSchema(previous, current);
  57. if (update) await writeFile(path, current);
  58. else {
  59. const baseline = await readFile(path, "utf8");
  60. assertAdditiveSchema(baseline, current);
  61. assertAdditiveSchema(current, baseline);
  62. if (baseline !== current)
  63. throw new Error(`Schema baseline differs: ${path}; review then run pnpm db:schema:update`);
  64. }
  65. console.log(`SQLite schema compatible: ${path}`);
  66. }
  67. } finally {
  68. for (const repo of repos.toReversed()) repo.close();
  69. cli.close();
  70. setDataBaseDir(null);
  71. await rm(directory, { recursive: true, force: true });
  72. }