1import * as Effect from "effect/Effect";
2import * as SqlClient from "effect/sql/SqlClient";
3
4export default Effect.gen(function* () {
5 const sql = yield* SqlClient.SqlClient;
6
7 // Project creation never exposed a model choice. A later metadata event
8 // containing this field is the evidence that the user set or reset one.
9 yield* sql`
10 WITH automatically_seeded_projects AS (
11 SELECT created.stream_id AS project_id
12 FROM orchestration_events AS created
13 WHERE created.aggregate_kind = 'project'
14 AND created.event_type = 'project.created'
15 AND json_type(created.payload_json, '$.defaultModelSelection') IS NOT NULL
16 AND json_type(created.payload_json, '$.defaultModelSelection') <> 'null'
17 AND NOT EXISTS (
18 SELECT 1
19 FROM orchestration_events AS configured
20 WHERE configured.aggregate_kind = 'project'
21 AND configured.stream_id = created.stream_id
22 AND configured.event_type = 'project.meta-updated'
23 AND json_type(configured.payload_json, '$.defaultModelSelection') IS NOT NULL
24 )
25 )
26 UPDATE projection_projects
27 SET default_model_selection_json = NULL
28 WHERE project_id IN (SELECT project_id FROM automatically_seeded_projects)
29 `;
30
31 yield* sql`
32 UPDATE orchestration_events AS created
33 SET payload_json = json_set(
34 created.payload_json,
35 '$.defaultModelSelection',
36 json('null')
37 )
38 WHERE created.aggregate_kind = 'project'
39 AND created.event_type = 'project.created'
40 AND json_type(created.payload_json, '$.defaultModelSelection') IS NOT NULL
41 AND json_type(created.payload_json, '$.defaultModelSelection') <> 'null'
42 AND NOT EXISTS (
43 SELECT 1
44 FROM orchestration_events AS configured
45 WHERE configured.aggregate_kind = 'project'
46 AND configured.stream_id = created.stream_id
47 AND configured.event_type = 'project.meta-updated'
48 AND json_type(configured.payload_json, '$.defaultModelSelection') IS NOT NULL
49 )
50 `;
51});