| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as SqlClient from "effect/sql/SqlClient"; |
| 5 | |
| 6 | import { runMigrations } from "../Migrations.ts"; |
| 7 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 8 | |
| 9 | const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layer({ filename: ":memory:" }))); |
| 10 | |
| 11 | layer("019_ProjectionSnapshotLookupIndexes", (it) => { |
| 12 | it.effect("creates indexes for targeted projection lookup filters", () => |
| 13 | Effect.gen(function* () { |
| 14 | const sql = yield* SqlClient.SqlClient; |
| 15 | |
| 16 | yield* runMigrations({ toMigrationInclusive: 18 }); |
| 17 | yield* runMigrations({ toMigrationInclusive: 19 }); |
| 18 | |
| 19 | const projectIndexes = yield* sql<{ |
| 20 | readonly seq: number; |
| 21 | readonly name: string; |
| 22 | readonly unique: number; |
| 23 | readonly origin: string; |
| 24 | readonly partial: number; |
| 25 | }>` |
| 26 | PRAGMA index_list(projection_projects) |
| 27 | `; |
| 28 | assert.ok( |
| 29 | projectIndexes.some( |
| 30 | (index) => index.name === "idx_projection_projects_workspace_root_deleted_at", |
| 31 | ), |
| 32 | ); |
| 33 | |
| 34 | const projectIndexColumns = yield* sql<{ |
| 35 | readonly seqno: number; |
| 36 | readonly cid: number; |
| 37 | readonly name: string; |
| 38 | }>` |
| 39 | PRAGMA index_info('idx_projection_projects_workspace_root_deleted_at') |
| 40 | `; |
| 41 | assert.deepStrictEqual( |
| 42 | projectIndexColumns.map((column) => column.name), |
| 43 | ["workspace_root", "deleted_at"], |
| 44 | ); |
| 45 | |
| 46 | const threadIndexes = yield* sql<{ |
| 47 | readonly seq: number; |
| 48 | readonly name: string; |
| 49 | readonly unique: number; |
| 50 | readonly origin: string; |
| 51 | readonly partial: number; |
| 52 | }>` |
| 53 | PRAGMA index_list(projection_threads) |
| 54 | `; |
| 55 | assert.ok( |
| 56 | threadIndexes.some( |
| 57 | (index) => index.name === "idx_projection_threads_project_deleted_created", |
| 58 | ), |
| 59 | ); |
| 60 | |
| 61 | const threadIndexColumns = yield* sql<{ |
| 62 | readonly seqno: number; |
| 63 | readonly cid: number; |
| 64 | readonly name: string; |
| 65 | }>` |
| 66 | PRAGMA index_info('idx_projection_threads_project_deleted_created') |
| 67 | `; |
| 68 | assert.deepStrictEqual( |
| 69 | threadIndexColumns.map((column) => column.name), |
| 70 | ["project_id", "deleted_at", "created_at"], |
| 71 | ); |
| 72 | }), |
| 73 | ); |
| 74 | }); |