@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 86 lines 3.22 KB
Raw
1 import { ThreadLinkedPullRequest, ThreadPullRequestLink } from "@t3tools/contracts";
2 import { legacyThreadPullRequestKey } from "./legacyThreadPullRequestKey.ts";
3 import * as Effect from "effect/Effect";
4 import * as Schema from "effect/Schema";
5 import * as SqlClient from "effect/sql/SqlClient";
6
7 const decodeLegacyLink = Schema.decodeEffect(Schema.fromJsonString(ThreadLinkedPullRequest));
8 const encodeLinks = Schema.encodeSync(Schema.fromJsonString(Schema.Array(ThreadPullRequestLink)));
9 const 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
23 export 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_valid(payload_json)
38 AND json_type(payload_json, '$.pullRequests') IS NULL
39 AND json_type(payload_json, '$.linkedPullRequest') = 'object'
40 ORDER BY thread_id
41 LIMIT ${pageSize}
42 `;
43 for (const row of rows) {
44 const pullRequests = yield* linksJson(row.linked_pull_request_json, row.updated_at);
45 yield* sql`
46 UPDATE orchestration_v2_projection_threads
47 SET payload_json = json_set(payload_json, '$.pullRequests', json(${pullRequests}))
48 WHERE thread_id = ${row.thread_id}
49 `;
50 }
51 if (rows.length < pageSize) break;
52 afterThreadId = rows.at(-1)!.thread_id;
53 }
54
55 // Normalize the source events too, so rebuilding projections cannot restore the old shape.
56 let afterSequence = 0;
57 while (true) {
58 const rows = yield* sql<{
59 readonly sequence: number;
60 readonly linked_pull_request_json: string;
61 readonly updated_at: string;
62 }>`
63 SELECT sequence, json_extract(payload_json, '$.linkedPullRequest') AS linked_pull_request_json,
64 json_extract(payload_json, '$.updatedAt') AS updated_at
65 FROM orchestration_events
66 WHERE sequence > ${afterSequence}
67 AND application_event_version = 2 AND aggregate_kind = 'thread'
68 AND event_type LIKE 'thread.%'
69 AND json_valid(payload_json)
70 AND json_type(payload_json, '$.pullRequests') IS NULL
71 AND json_type(payload_json, '$.linkedPullRequest') = 'object'
72 ORDER BY sequence
73 LIMIT ${pageSize}
74 `;
75 for (const row of rows) {
76 const pullRequests = yield* linksJson(row.linked_pull_request_json, row.updated_at);
77 yield* sql`
78 UPDATE orchestration_events
79 SET payload_json = json_set(payload_json, '$.pullRequests', json(${pullRequests}))
80 WHERE sequence = ${row.sequence}
81 `;
82 }
83 if (rows.length < pageSize) break;
84 afterSequence = rows.at(-1)!.sequence;
85 }
86 });