Open History to see the last commit
1import { ThreadLinkedPullRequest, ThreadPullRequestLink } from "@t3tools/contracts";
2import { legacyThreadPullRequestKey } from "@t3tools/shared/threadPullRequests";
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5import * as SqlClient from "effect/sql/SqlClient";
6
7const decodeLegacyLink = Schema.decodeEffect(Schema.fromJsonString(ThreadLinkedPullRequest));
8const encodeLinks = Schema.encodeSync(Schema.fromJsonString(Schema.Array(ThreadPullRequestLink)));
9const linksJson = Effect.fnUntraced(function* (linkedPullRequestJson: string, linkedAt: string) {
10 const linked = yield* decodeLegacyLink(linkedPullRequestJson);
11 return encodeLinks([
12 {
13 ...legacyThreadPullRequestKey(linked),
14 url: linked.url,
15 source: "manual",
16 linkedAt,
17 snapshot: null,
18 stack: null,
19 },
20 ]);
21});
22
23export default Effect.gen(function* () {
24 const sql = yield* SqlClient.SqlClient;
25 const pageSize = 500;
26 let afterThreadId = "";
27 while (true) {
28 const rows = yield* sql<{
29 readonly thread_id: string;
30 readonly linked_pull_request_json: string;
31 readonly updated_at: string;
32 }>`
33 SELECT thread_id, json_extract(payload_json, '$.linkedPullRequest') AS linked_pull_request_json,
34 updated_at
35 FROM orchestration_v2_projection_threads
36 WHERE thread_id > ${afterThreadId}
37 AND json_type(payload_json, '$.pullRequests') IS NULL
38 AND json_type(payload_json, '$.linkedPullRequest') = 'object'
39 ORDER BY thread_id
40 LIMIT ${pageSize}
41 `;
42 for (const row of rows) {
43 const pullRequests = yield* linksJson(row.linked_pull_request_json, row.updated_at);
44 yield* sql`
45 UPDATE orchestration_v2_projection_threads
46 SET payload_json = json_set(payload_json, '$.pullRequests', json(${pullRequests}))
47 WHERE thread_id = ${row.thread_id}
48 `;
49 }
50 if (rows.length < pageSize) break;
51 afterThreadId = rows.at(-1)!.thread_id;
52 }
53
54 // Normalize the source events too, so rebuilding projections cannot restore the old shape.
55 let afterSequence = 0;
56 while (true) {
57 const rows = yield* sql<{
58 readonly sequence: number;
59 readonly linked_pull_request_json: string;
60 readonly updated_at: string;
61 }>`
62 SELECT sequence, json_extract(payload_json, '$.linkedPullRequest') AS linked_pull_request_json,
63 json_extract(payload_json, '$.updatedAt') AS updated_at
64 FROM orchestration_events
65 WHERE sequence > ${afterSequence}
66 AND application_event_version = 2 AND aggregate_kind = 'thread'
67 AND event_type LIKE 'thread.%'
68 AND json_type(payload_json, '$.pullRequests') IS NULL
69 AND json_type(payload_json, '$.linkedPullRequest') = 'object'
70 ORDER BY sequence
71 LIMIT ${pageSize}
72 `;
73 for (const row of rows) {
74 const pullRequests = yield* linksJson(row.linked_pull_request_json, row.updated_at);
75 yield* sql`
76 UPDATE orchestration_events
77 SET payload_json = json_set(payload_json, '$.pullRequests', json(${pullRequests}))
78 WHERE sequence = ${row.sequence}
79 `;
80 }
81 if (rows.length < pageSize) break;
82 afterSequence = rows.at(-1)!.sequence;
83 }
84});