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

065_ProjectionThreadSweepIndexes.ts

51 lines2.4 KB

1import * as Effect from "effect/Effect";
2import * as SqlClient from "effect/sql/SqlClient";
3import DropUnusedV1Projections from "./063_DropUnusedV1Projections.ts";
4import CanonicalizeThreadPullRequestLinks from "./064_CanonicalizeThreadPullRequestLinks.ts";
5​
6/**
7 * Background sweeps (settlement, pull request sync, the shell reads behind pull request
8 * discovery) filtered every thread with `json_extract` on its payload, which holds every linked
9 * pull request's snapshot: tens of KB parsed per thread per sweep. This index stores the values
10 * they filter on, so SQLite reads them from the index instead of parsing payloads.
11 *
12 * SQLite computes the entries from `payload_json` on every write, so they stay correct for any
13 * writer, including an older build sharing this database. ProjectionStore's sweep queries must
14 * spell each expression exactly as it is here, or SQLite parses the payload again.
15 */
16export default Effect.gen(function* () {
17 // Published perf builds used ids 63/64, so the migrator skips main's effects there.
18 yield* DropUnusedV1Projections;
19 yield* CanonicalizeThreadPullRequestLinks;
20 const sql = yield* SqlClient.SqlClient;
21 // Development builds of this change created an index of the same name over copied columns.
22 // Replace it so every database gets this definition.
23 yield* sql`DROP INDEX IF EXISTS orchestration_v2_projection_threads_active_idx`;
24 yield* sql`
25 CREATE INDEX IF NOT EXISTS orchestration_v2_projection_threads_active_idx
26 ON orchestration_v2_projection_threads(
27 updated_at,
28 thread_id,
29 json_extract(payload_json, '$.settledAt'),
30 json_extract(payload_json, '$.settledOverride'),
31 json_extract(payload_json, '$.pinnedAt'),
32 json_extract(payload_json, '$.autoSettleDisabledAt'),
33 json_array_length(payload_json, '$.pullRequests'),
34 CASE
35 WHEN json_extract(payload_json, '$.forkedFrom.type') = 'run'
36 THEN json_extract(payload_json, '$.forkedFrom.threadId')
37 ELSE NULL
38 END,
39 provider_instance_id
40 )
41 WHERE deleted_at IS NULL AND archived_at IS NULL
42 `;
43 // Older sweep builds recorded id 62, so they skip main's failed-error index migration.
44 yield* sql`
45 CREATE INDEX IF NOT EXISTS orchestration_v2_projection_turn_items_failed_error_idx
46 ON orchestration_v2_projection_turn_items(
47 thread_id, run_id, node_id, updated_at DESC, ordinal DESC, turn_item_id DESC
48 )
49 WHERE type = 'error' AND status = 'failed'
50 `;
51});