Open History to see the last commit
1import { OrchestrationV2AppThreadJson } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Schema from "effect/Schema";
5import * as SqlClient from "effect/sql/SqlClient";
6import ProjectionThreadSweepIndexes from "./065_ProjectionThreadSweepIndexes.ts";
7
8import {
9 compactThreadPullRequests,
10 encodeThreadReadPayload,
11} from "../../orchestration-v2/ThreadReadPayload.ts";
12
13// Startup also ensures this table when another build recorded migration 66 or above.
14// Triggers invalidate read rows when a build that does not maintain them writes a thread.
15const ensureInTransaction = (sql: SqlClient.SqlClient, rebuild: boolean) =>
16 Effect.gen(function* () {
17 const columns = yield* sql<{ readonly name: string }>`
18 SELECT name FROM pragma_table_info('orchestration_v2_projection_thread_reads')
19 `;
20 const current = columns.some((column) => column.name === "source_stale");
21 if (current && rebuild) {
22 yield* sql`DELETE FROM orchestration_v2_projection_thread_reads`;
23 } else if (!current) {
24 // Early builds lacked source_stale; the derived rows can be rebuilt.
25 yield* sql`DROP TABLE IF EXISTS orchestration_v2_projection_thread_reads`;
26 yield* sql`
27 CREATE TABLE orchestration_v2_projection_thread_reads (
28 thread_id TEXT PRIMARY KEY NOT NULL,
29 source_stale INTEGER NOT NULL DEFAULT 0,
30 compact_payload_json TEXT NOT NULL,
31 payload_json TEXT NOT NULL
32 )
33 `;
34 }
35 if (!current || rebuild) {
36 const decode = Schema.decodeUnknownEffect(
37 Schema.fromJsonString(OrchestrationV2AppThreadJson),
38 );
39 let after = "";
40 while (true) {
41 const rows = yield* sql<{ thread_id: string; payload_json: string }>`
42 SELECT thread_id, payload_json FROM orchestration_v2_projection_threads
43 WHERE thread_id > ${after} ORDER BY thread_id LIMIT 100
44 `;
45 if (rows.length === 0) break;
46 for (const row of rows) {
47 // A payload this build cannot decode keeps no read row; readers use the thread payload.
48 const encoded = yield* decode(row.payload_json).pipe(
49 Effect.flatMap((thread) =>
50 Effect.all([
51 encodeThreadReadPayload(thread),
52 encodeThreadReadPayload(compactThreadPullRequests(thread)),
53 ]),
54 ),
55 Effect.option,
56 );
57 if (Option.isNone(encoded)) continue;
58 const [payload, compact] = encoded.value;
59 yield* sql`
60 INSERT INTO orchestration_v2_projection_thread_reads (thread_id, payload_json, compact_payload_json)
61 VALUES (${row.thread_id}, ${payload}, ${compact})
62 `;
63 }
64 after = rows[rows.length - 1]!.thread_id;
65 }
66 }
67 yield* sql`
68 CREATE TRIGGER IF NOT EXISTS orchestration_v2_projection_thread_reads_stale_on_insert
69 AFTER INSERT ON orchestration_v2_projection_threads
70 BEGIN
71 UPDATE orchestration_v2_projection_thread_reads SET source_stale = 1
72 WHERE thread_id = NEW.thread_id;
73 END
74 `;
75 yield* sql`
76 CREATE TRIGGER IF NOT EXISTS orchestration_v2_projection_thread_reads_stale_on_update
77 AFTER UPDATE OF payload_json ON orchestration_v2_projection_threads
78 BEGIN
79 UPDATE orchestration_v2_projection_thread_reads SET source_stale = 1
80 WHERE thread_id = NEW.thread_id;
81 END
82 `;
83 });
84
85const ensureThreadReadProjection = Effect.gen(function* () {
86 const sql = yield* SqlClient.SqlClient;
87 yield* sql.withTransaction(ensureInTransaction(sql, false));
88});
89
90export default ensureThreadReadProjection;
91
92/**
93 * Migration 66 itself rebuilds every row. Early builds of this change ran it as 62, 63 or 64 and wrote
94 * compact rows in an older format that the triggers never mark stale.
95 */
96export const migrateThreadReadProjection = Effect.gen(function* () {
97 // An old wave-2 ledger at 63 skipped the sweep migration on upgrade.
98 yield* ProjectionThreadSweepIndexes;
99 const sql = yield* SqlClient.SqlClient;
100 yield* sql.withTransaction(ensureInTransaction(sql, true));
101});