Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

049_ApplicationEventSource.ts

281 lines8.0 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import * as SqlClient from "effect/unstable/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;
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;
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 // ProjectService wrote this projection directly before the application event
126 // boundary was restored. Re-baseline every current row into the shared log so
127 // projection rebuilds preserve the exact pre-migration project state.
128 const projectRows = yield* sql<ProjectProjectionRow>`
129 SELECT
130 project_id,
131 title,
132 workspace_root,
133 default_model_selection_json,
134 scripts_json,
135 created_at,
136 updated_at,
137 deleted_at
138 FROM projection_projects
139 ORDER BY created_at ASC, project_id ASC
140 `;
141 yield* Effect.forEach(
142 projectRows,
143 (project) =>
144 Effect.gen(function* () {
145 const createdEventId = `migration:39:project:${project.project_id}:baseline`;
146 const createdPayload = {
147 projectId: project.project_id,
148 title: project.title,
149 workspaceRoot: project.workspace_root,
150 defaultModelSelection:
151 project.default_model_selection_json === null
152 ? null
153 : yield* decodeJson(project.default_model_selection_json),
154 scripts: yield* decodeJson(project.scripts_json),
155 createdAt: project.created_at,
156 updatedAt: project.updated_at,
157 };
158 const createdPayloadJson = yield* encodeJson(createdPayload);
159 const migrationMetadataJson = yield* encodeJson({
160 applicationEventVersion: 2,
161 migration: 39,
162 });
163 yield* sql`
164 INSERT INTO orchestration_events (
165 event_id,
166 aggregate_kind,
167 stream_id,
168 stream_version,
169 event_type,
170 occurred_at,
171 command_id,
172 causation_event_id,
173 correlation_id,
174 actor_kind,
175 payload_json,
176 metadata_json,
177 application_event_version
178 )
179 VALUES (
180 ${createdEventId},
181 'project',
182 ${project.project_id},
183 COALESCE(
184 (
185 SELECT MAX(stream_version) + 1
186 FROM orchestration_events
187 WHERE aggregate_kind = 'project' AND stream_id = ${project.project_id}
188 ),
189 0
190 ),
191 'project.created',
192 ${project.updated_at},
193 NULL,
194 NULL,
195 NULL,
196 'server',
197 ${createdPayloadJson},
198 ${migrationMetadataJson},
199 2
200 )
201 `;
202​
203 if (project.deleted_at !== null) {
204 const deletedPayloadJson = yield* encodeJson({
205 projectId: project.project_id,
206 deletedAt: project.deleted_at,
207 });
208 yield* sql`
209 INSERT INTO orchestration_events (
210 event_id,
211 aggregate_kind,
212 stream_id,
213 stream_version,
214 event_type,
215 occurred_at,
216 command_id,
217 causation_event_id,
218 correlation_id,
219 actor_kind,
220 payload_json,
221 metadata_json,
222 application_event_version
223 )
224 VALUES (
225 ${`migration:39:project:${project.project_id}:deleted`},
226 'project',
227 ${project.project_id},
228 (
229 SELECT MAX(stream_version) + 1
230 FROM orchestration_events
231 WHERE aggregate_kind = 'project' AND stream_id = ${project.project_id}
232 ),
233 'project.deleted',
234 ${project.deleted_at},
235 NULL,
236 ${createdEventId},
237 NULL,
238 'server',
239 ${deletedPayloadJson},
240 ${migrationMetadataJson},
241 2
242 )
243 `;
244 }
245 }),
246 { concurrency: 1, discard: true },
247 );
248​
249 yield* sql`
250 INSERT INTO orchestration_command_receipts (
251 command_id,
252 aggregate_kind,
253 aggregate_id,
254 accepted_at,
255 result_sequence,
256 status,
257 error,
258 command_type
259 )
260 SELECT
261 command_id,
262 'thread',
263 thread_id,
264 accepted_at,
265 COALESCE(
266 (
267 SELECT MAX(events.sequence)
268 FROM orchestration_events events
269 WHERE events.application_event_version = 2
270 AND events.command_id = receipts.command_id
271 ),
272 0
273 ),
274 status,
275 error,
276 command_type
277 FROM orchestration_v2_command_receipts receipts
278 WHERE TRUE
279 ON CONFLICT(command_id) DO NOTHING
280 `;
281});