Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import * as SqlClient from "effect/sql/SqlClient";
4
5interface LegacyV2EventRow {
6 readonly event_id: string;
7 readonly command_id: string | null;
8 readonly thread_id: string;
9 readonly run_id: string | null;
10 readonly node_id: string | null;
11 readonly driver: string | null;
12 readonly provider_instance_id: string | null;
13 readonly raw_event_id: string | null;
14 readonly event_type: string;
15 readonly occurred_at: string;
16 readonly payload_json: string;
17}
18
19interface ProjectProjectionRow {
20 readonly project_id: string;
21 readonly title: string;
22 readonly workspace_root: string;
23 readonly default_model_selection_json: string | null;
24 readonly scripts_json: string;
25 readonly created_at: string;
26 readonly updated_at: string;
27 readonly deleted_at: string | null;
28}
29
30const decodeJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown));
31const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
32
33export default Effect.gen(function* () {
34 const sql = yield* SqlClient.SqlClient;
35
36 yield* sql`
37 ALTER TABLE orchestration_events
38 ADD COLUMN application_event_version INTEGER NOT NULL DEFAULT 1
39 `;
40 yield* sql`
41 CREATE INDEX idx_orchestration_events_application_sequence
42 ON orchestration_events(application_event_version, sequence)
43 `;
44 yield* sql`
45 ALTER TABLE orchestration_command_receipts
46 ADD COLUMN command_type TEXT NOT NULL DEFAULT 'legacy'
47 `;
48
49 const legacyV2Events = yield* sql<LegacyV2EventRow>`
50 SELECT
51 event_id,
52 command_id,
53 thread_id,
54 run_id,
55 node_id,
56 driver,
57 provider_instance_id,
58 raw_event_id,
59 event_type,
60 occurred_at,
61 payload_json
62 FROM orchestration_v2_events
63 ORDER BY sequence ASC
64 `;
65
66 yield* Effect.forEach(
67 legacyV2Events,
68 (event) =>
69 Effect.gen(function* () {
70 const metadata = yield* encodeJson({
71 applicationEventVersion: 2,
72 ...(event.run_id === null ? {} : { runId: event.run_id }),
73 ...(event.node_id === null ? {} : { nodeId: event.node_id }),
74 ...(event.driver === null ? {} : { driver: event.driver }),
75 ...(event.provider_instance_id === null
76 ? {}
77 : { providerInstanceId: event.provider_instance_id }),
78 ...(event.raw_event_id === null ? {} : { rawEventId: event.raw_event_id }),
79 });
80 yield* sql`
81 INSERT INTO orchestration_events (
82 event_id,
83 aggregate_kind,
84 stream_id,
85 stream_version,
86 event_type,
87 occurred_at,
88 command_id,
89 causation_event_id,
90 correlation_id,
91 actor_kind,
92 payload_json,
93 metadata_json,
94 application_event_version
95 )
96 SELECT
97 ${event.event_id},
98 'thread',
99 ${event.thread_id},
100 COALESCE(
101 (
102 SELECT MAX(stream_version) + 1
103 FROM orchestration_events
104 WHERE aggregate_kind = 'thread' AND stream_id = ${event.thread_id}
105 ),
106 0
107 ),
108 ${event.event_type},
109 ${event.occurred_at},
110 ${event.command_id},
111 NULL,
112 ${event.command_id},
113 ${event.raw_event_id === null ? "server" : "provider"},
114 ${event.payload_json},
115 ${metadata},
116 2
117 WHERE NOT EXISTS (
118 SELECT 1 FROM orchestration_events WHERE event_id = ${event.event_id}
119 )
120 `;
121 }),
122 { concurrency: 1, discard: true },
123 );
124
125 // Re-baseline every current row into the shared log so projection rebuilds
126 // preserve the exact pre-migration project state.
127 const projectRows = yield* sql<ProjectProjectionRow>`
128 SELECT
129 project_id,
130 title,
131 workspace_root,
132 default_model_selection_json,
133 scripts_json,
134 created_at,
135 updated_at,
136 deleted_at
137 FROM projection_projects
138 ORDER BY created_at ASC, project_id ASC
139 `;
140 yield* Effect.forEach(
141 projectRows,
142 (project) =>
143 Effect.gen(function* () {
144 const createdEventId = `migration:39:project:${project.project_id}:baseline`;
145 const createdPayload = {
146 projectId: project.project_id,
147 title: project.title,
148 workspaceRoot: project.workspace_root,
149 defaultModelSelection:
150 project.default_model_selection_json === null
151 ? null
152 : yield* decodeJson(project.default_model_selection_json),
153 scripts: yield* decodeJson(project.scripts_json),
154 createdAt: project.created_at,
155 updatedAt: project.updated_at,
156 };
157 const createdPayloadJson = yield* encodeJson(createdPayload);
158 const migrationMetadataJson = yield* encodeJson({
159 applicationEventVersion: 2,
160 migration: 39,
161 });
162 yield* sql`
163 INSERT INTO orchestration_events (
164 event_id,
165 aggregate_kind,
166 stream_id,
167 stream_version,
168 event_type,
169 occurred_at,
170 command_id,
171 causation_event_id,
172 correlation_id,
173 actor_kind,
174 payload_json,
175 metadata_json,
176 application_event_version
177 )
178 VALUES (
179 ${createdEventId},
180 'project',
181 ${project.project_id},
182 COALESCE(
183 (
184 SELECT MAX(stream_version) + 1
185 FROM orchestration_events
186 WHERE aggregate_kind = 'project' AND stream_id = ${project.project_id}
187 ),
188 0
189 ),
190 'project.created',
191 ${project.updated_at},
192 NULL,
193 NULL,
194 NULL,
195 'server',
196 ${createdPayloadJson},
197 ${migrationMetadataJson},
198 2
199 )
200 `;
201
202 if (project.deleted_at !== null) {
203 const deletedPayloadJson = yield* encodeJson({
204 projectId: project.project_id,
205 deletedAt: project.deleted_at,
206 });
207 yield* sql`
208 INSERT INTO orchestration_events (
209 event_id,
210 aggregate_kind,
211 stream_id,
212 stream_version,
213 event_type,
214 occurred_at,
215 command_id,
216 causation_event_id,
217 correlation_id,
218 actor_kind,
219 payload_json,
220 metadata_json,
221 application_event_version
222 )
223 VALUES (
224 ${`migration:39:project:${project.project_id}:deleted`},
225 'project',
226 ${project.project_id},
227 (
228 SELECT MAX(stream_version) + 1
229 FROM orchestration_events
230 WHERE aggregate_kind = 'project' AND stream_id = ${project.project_id}
231 ),
232 'project.deleted',
233 ${project.deleted_at},
234 NULL,
235 ${createdEventId},
236 NULL,
237 'server',
238 ${deletedPayloadJson},
239 ${migrationMetadataJson},
240 2
241 )
242 `;
243 }
244 }),
245 { concurrency: 1, discard: true },
246 );
247
248 yield* sql`
249 INSERT INTO orchestration_command_receipts (
250 command_id,
251 aggregate_kind,
252 aggregate_id,
253 accepted_at,
254 result_sequence,
255 status,
256 error,
257 command_type
258 )
259 SELECT
260 command_id,
261 'thread',
262 thread_id,
263 accepted_at,
264 COALESCE(
265 (
266 SELECT MAX(events.sequence)
267 FROM orchestration_events events
268 WHERE events.application_event_version = 2
269 AND events.command_id = receipts.command_id
270 ),
271 0
272 ),
273 status,
274 error,
275 command_type
276 FROM orchestration_v2_command_receipts receipts
277 WHERE TRUE
278 ON CONFLICT(command_id) DO NOTHING
279 `;
280});