controllerRemoteIdentityMigration.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { DatabaseSync } from "node:sqlite";
  2. import { mkdtemp, rm } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { describe, expect, it } from "vitest";
  6. import { migrateControllerRemoteWorkspaceIdentity } from "../src/session/controllerRemoteIdentityMigration.js";
  7. import { TASK_GROUP_ADMISSIONS_SQL } from "#src/task-groups/migration-v6.js";
  8. import { runTasksDatabaseMigrations } from "#src/session/tasksDatabase/migrations.js";
  9. describe("controller remote identity migration inventory", () => {
  10. it("returns the original failure even when the failed audit cannot be written", async () => {
  11. const root = await mkdtemp(join(tmpdir(), "zcode-controller-audit-failure-"));
  12. const dbPath = join(root, "tasks-index.sqlite");
  13. const db = new DatabaseSync(dbPath);
  14. try {
  15. await runTasksDatabaseMigrations(db, "controller-remote");
  16. db.exec(`
  17. CREATE TABLE tasks (workspace_key TEXT, workspace_identity TEXT, workspace_path TEXT, task_id TEXT,
  18. title TEXT, created_at INTEGER, updated_at INTEGER, meta_json TEXT DEFAULT '{}',
  19. PRIMARY KEY (workspace_key, task_id));
  20. INSERT INTO tasks (workspace_key, workspace_path, task_id, title, created_at, updated_at)
  21. VALUES ('old', '/repo', 'task-1', 'Task', 1, 1);
  22. CREATE TRIGGER fail_migration BEFORE UPDATE ON tasks
  23. BEGIN SELECT RAISE(ABORT, 'original migration failure'); END;
  24. CREATE TRIGGER fail_audit BEFORE INSERT ON controller_remote_identity_migrations
  25. WHEN NEW.status = 'failed'
  26. BEGIN SELECT RAISE(ABORT, 'audit unavailable'); END;
  27. `);
  28. await expect(
  29. migrateControllerRemoteWorkspaceIdentity({
  30. dbPath,
  31. oldWorkspaceKey: "old",
  32. newWorkspaceKey: "new",
  33. migrationId: "failed-audit",
  34. }),
  35. ).resolves.toMatchObject({
  36. migrationId: "failed-audit",
  37. status: "failed",
  38. error: "original migration failure",
  39. before: { tasks: 1 },
  40. after: { tasks: 0 },
  41. });
  42. expect(db.prepare("SELECT workspace_key FROM tasks").all()).toEqual([
  43. { workspace_key: "old" },
  44. ]);
  45. expect(db.prepare("SELECT * FROM controller_remote_identity_migrations").all()).toEqual([]);
  46. } finally {
  47. db.close();
  48. await rm(root, { recursive: true, force: true });
  49. }
  50. });
  51. it("rekeys every inventoried workspace table in one transaction", async () => {
  52. const root = await mkdtemp(join(tmpdir(), "zcode-r3-controller-identity-"));
  53. const dbPath = join(root, "tasks-index.sqlite");
  54. const oldKey = "remote:ssh:old.example:22:alice:/srv/project";
  55. const newKey = "remote:server:3cd4f9da-cd79-426c-9d70-75f36e9f6a87:/srv/project";
  56. const db = new DatabaseSync(dbPath);
  57. db.exec(`
  58. CREATE TABLE tasks (workspace_key TEXT, workspace_identity TEXT, workspace_path TEXT, task_id TEXT, updated_at INTEGER, meta_json TEXT, PRIMARY KEY(workspace_key, task_id));
  59. CREATE TABLE task_group_members (workspace_key TEXT, workspace_identity TEXT, task_id TEXT, PRIMARY KEY(workspace_key, task_id));
  60. CREATE TABLE task_group_workspace_bootstraps (workspace_key TEXT PRIMARY KEY, group_id TEXT);
  61. CREATE TABLE task_group_view_node_orders (node_type TEXT, node_key TEXT, sort_order INTEGER, PRIMARY KEY(node_type, node_key));
  62. CREATE TABLE automations (automation_id TEXT PRIMARY KEY, workspace_key TEXT, workspace_identity TEXT);
  63. CREATE TABLE automation_runs (run_id TEXT PRIMARY KEY, workspace_key TEXT);
  64. CREATE TABLE off_peak_tasks (off_peak_task_id TEXT PRIMARY KEY, workspace_key TEXT, workspace_identity TEXT);
  65. `);
  66. db.prepare("INSERT INTO tasks VALUES (?, ?, '/srv/project', 'task-1', 10, ?)").run(
  67. oldKey,
  68. oldKey,
  69. JSON.stringify({ taskId: "task-1", workspaceIdentity: oldKey }),
  70. );
  71. db.exec(TASK_GROUP_ADMISSIONS_SQL);
  72. db.prepare(
  73. "INSERT INTO task_group_admissions(workspace_key,task_id,user_organized,creation_id) VALUES (?, 'task-1', 1, 'created')",
  74. ).run(oldKey);
  75. db.prepare("INSERT INTO task_group_members VALUES (?, ?, 'task-1')").run(oldKey, oldKey);
  76. db.prepare("INSERT INTO task_group_workspace_bootstraps VALUES (?, 'group-1')").run(oldKey);
  77. db.prepare("INSERT INTO task_group_view_node_orders VALUES ('task', ?, 1)").run(
  78. JSON.stringify([oldKey, "task-1"]),
  79. );
  80. db.prepare("INSERT INTO automations VALUES ('automation-1', ?, ?)").run(oldKey, oldKey);
  81. db.prepare("INSERT INTO automation_runs VALUES ('run-1', ?)").run(oldKey);
  82. db.prepare("INSERT INTO off_peak_tasks VALUES ('off-peak-1', ?, ?)").run(oldKey, oldKey);
  83. db.close();
  84. const report = await migrateControllerRemoteWorkspaceIdentity({
  85. dbPath,
  86. oldWorkspaceKey: oldKey,
  87. newWorkspaceKey: newKey,
  88. migrationId: "migration-1",
  89. now: () => 100,
  90. });
  91. expect(report.status).toBe("completed");
  92. expect(Object.values(report.before)).toEqual([1, 1, 1, 1, 1, 1, 1, 1]);
  93. const verify = new DatabaseSync(dbPath);
  94. for (const table of [
  95. "tasks",
  96. "task_group_members",
  97. "task_group_admissions",
  98. "task_group_workspace_bootstraps",
  99. "automations",
  100. "automation_runs",
  101. "off_peak_tasks",
  102. ]) {
  103. expect(
  104. verify
  105. .prepare(`SELECT COUNT(*) AS count FROM ${table} WHERE workspace_key = ?`)
  106. .get(oldKey),
  107. ).toEqual({ count: 0 });
  108. expect(
  109. verify
  110. .prepare(`SELECT COUNT(*) AS count FROM ${table} WHERE workspace_key = ?`)
  111. .get(newKey),
  112. ).toEqual({ count: 1 });
  113. }
  114. expect(
  115. verify
  116. .prepare("SELECT node_key FROM task_group_view_node_orders WHERE node_type = 'task'")
  117. .get(),
  118. ).toEqual({ node_key: JSON.stringify([newKey, "task-1"]) });
  119. const task = verify.prepare("SELECT workspace_identity, meta_json FROM tasks").get() as {
  120. workspace_identity: string;
  121. meta_json: string;
  122. };
  123. expect(task.workspace_identity).toBe(newKey);
  124. expect(JSON.parse(task.meta_json)).toMatchObject({ workspaceIdentity: newKey });
  125. expect(
  126. verify.prepare("SELECT status FROM controller_remote_identity_migrations").get(),
  127. ).toEqual({ status: "completed" });
  128. verify.close();
  129. const repeated = await migrateControllerRemoteWorkspaceIdentity({
  130. dbPath,
  131. oldWorkspaceKey: oldKey,
  132. newWorkspaceKey: newKey,
  133. migrationId: "migration-2",
  134. now: () => 200,
  135. });
  136. expect(Object.values(repeated.before)).toEqual([0, 0, 0, 0, 0, 0, 0, 0]);
  137. expect(Object.values(repeated.after)).toEqual([1, 1, 1, 1, 1, 1, 1, 1]);
  138. const repeatedVerify = new DatabaseSync(dbPath);
  139. expect(
  140. repeatedVerify
  141. .prepare("SELECT COUNT(*) AS count FROM controller_remote_identity_migrations")
  142. .get(),
  143. ).toEqual({ count: 1 });
  144. repeatedVerify.close();
  145. });
  146. });