Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as SqlClient from "effect/sql/SqlClient";
5
6import { migrationEntries, runMigrations } from "../Migrations.ts";
7import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
8
9const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layer({ filename: ":memory:" })));
10
11layer("055_OrchestrationV2", (it) => {
12 it.effect("keeps released migrations contiguous", () =>
13 Effect.sync(() => {
14 assert.deepStrictEqual(
15 migrationEntries.map(([id]) => id),
16 Array.from({ length: 60 }, (_, index) => index + 1),
17 );
18 }),
19 );
20
21 it.effect("upgrades released schema 53 through the latest migrations", () =>
22 Effect.gen(function* () {
23 const sql = yield* SqlClient.SqlClient;
24 yield* runMigrations({ toMigrationInclusive: 53 });
25
26 const executed = yield* runMigrations();
27 assert.deepStrictEqual(executed, [
28 [54, "ProjectionThreadsAutoSettleDisabledAt"],
29 [55, "OrchestrationV2"],
30 [56, "RemoveRedundantProjectionIndexes"],
31 [57, "ScheduledTaskWebhooks"],
32 [58, "WebhookRelayDeliveries"],
33 [59, "McpAppModelContext"],
34 [60, "ThreadSnapshotWindowIndexes"],
35 ]);
36 assert.deepStrictEqual(yield* runMigrations(), []);
37
38 const migrations = yield* sql<{
39 readonly migration_id: number;
40 readonly name: string;
41 }>`
42 SELECT migration_id, name
43 FROM effect_sql_migrations
44 WHERE migration_id >= 48
45 ORDER BY migration_id
46 `;
47 assert.deepStrictEqual(migrations, [
48 { migration_id: 48, name: "ProjectionThreadBranchPullRequest" },
49 { migration_id: 49, name: "ProjectionThreadsActiveOrderKey" },
50 { migration_id: 50, name: "ProjectionThreadPullRequests" },
51 { migration_id: 51, name: "ProjectionThreadMessageContext" },
52 { migration_id: 52, name: "ProjectionThreadTitleState" },
53 { migration_id: 53, name: "PullRequestFilesViewed" },
54 { migration_id: 54, name: "ProjectionThreadsAutoSettleDisabledAt" },
55 { migration_id: 55, name: "OrchestrationV2" },
56 { migration_id: 56, name: "RemoveRedundantProjectionIndexes" },
57 { migration_id: 57, name: "ScheduledTaskWebhooks" },
58 { migration_id: 58, name: "WebhookRelayDeliveries" },
59 { migration_id: 59, name: "McpAppModelContext" },
60 { migration_id: 60, name: "ThreadSnapshotWindowIndexes" },
61 ]);
62
63 const tables = yield* sql<{ readonly name: string }>`
64 SELECT name
65 FROM sqlite_master
66 WHERE type = 'table'
67 AND name IN (
68 'orchestration_v2_projection_threads',
69 'orchestration_v2_projection_subagents',
70 'orchestration_v2_effect_outbox',
71 'orchestration_v2_turn_item_positions',
72 'orchestration_v2_projection_metadata',
73 'orchestration_v2_projection_provider_session_bindings',
74 'orchestration_v2_thread_launch_workflows',
75 'orchestration_v2_legacy_imports',
76 'scheduled_tasks'
77 )
78 ORDER BY name
79 `;
80 assert.deepStrictEqual(
81 tables.map(({ name }) => name),
82 [
83 "orchestration_v2_effect_outbox",
84 "orchestration_v2_legacy_imports",
85 "orchestration_v2_projection_metadata",
86 "orchestration_v2_projection_provider_session_bindings",
87 "orchestration_v2_projection_subagents",
88 "orchestration_v2_projection_threads",
89 "orchestration_v2_thread_launch_workflows",
90 "orchestration_v2_turn_item_positions",
91 "scheduled_tasks",
92 ],
93 );
94
95 const eventColumns = yield* sql<{ readonly name: string }>`
96 PRAGMA table_info(orchestration_events)
97 `;
98 const receiptColumns = yield* sql<{ readonly name: string }>`
99 PRAGMA table_info(orchestration_command_receipts)
100 `;
101 const threadColumns = yield* sql<{ readonly name: string }>`
102 PRAGMA table_info(orchestration_v2_projection_threads)
103 `;
104 const subagentColumns = yield* sql<{ readonly name: string }>`
105 PRAGMA table_info(orchestration_v2_projection_subagents)
106 `;
107 assert.ok(eventColumns.some(({ name }) => name === "application_event_version"));
108 assert.ok(receiptColumns.some(({ name }) => name === "command_type"));
109 assert.ok(threadColumns.some(({ name }) => name === "provider_instance_id"));
110 assert.ok(subagentColumns.some(({ name }) => name === "driver"));
111 assert.ok(subagentColumns.some(({ name }) => name === "provider_instance_id"));
112
113 const indexes = yield* sql<{ readonly name: string }>`
114 SELECT name
115 FROM sqlite_master
116 WHERE type = 'index'
117 AND name IN (
118 'idx_orchestration_events_application_high_water',
119 'orchestration_events_v2_created_threads_idx',
120 'orchestration_v2_projection_turn_items_shell_pending_idx'
121 )
122 ORDER BY name
123 `;
124 assert.deepStrictEqual(
125 indexes.map(({ name }) => name),
126 [
127 "idx_orchestration_events_application_high_water",
128 "orchestration_events_v2_created_threads_idx",
129 "orchestration_v2_projection_turn_items_shell_pending_idx",
130 ],
131 );
132 }),
133 );
134});