| 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("029_ProjectionThreadDetailOrderingIndexes", (it) => { |
| 12 | it.effect("creates indexes matching thread detail ordering queries", () => |
| 13 | Effect.gen(function* () { |
| 14 | const sql = yield* SqlClient.SqlClient; |
| 15 | |
| 16 | yield* runMigrations({ toMigrationInclusive: 28 }); |
| 17 | yield* runMigrations({ toMigrationInclusive: 29 }); |
| 18 | |
| 19 | const activityIndexes = 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_thread_activities) |
| 27 | `; |
| 28 | assert.ok( |
| 29 | activityIndexes.some( |
| 30 | (index) => index.name === "idx_projection_thread_activities_thread_sequence_created_id", |
| 31 | ), |
| 32 | ); |
| 33 | |
| 34 | const activityIndexColumns = yield* sql<{ |
| 35 | readonly seqno: number; |
| 36 | readonly cid: number; |
| 37 | readonly name: string; |
| 38 | }>` |
| 39 | PRAGMA index_info('idx_projection_thread_activities_thread_sequence_created_id') |
| 40 | `; |
| 41 | assert.deepStrictEqual( |
| 42 | activityIndexColumns.map((column) => column.name), |
| 43 | ["thread_id", "sequence", "created_at", "activity_id"], |
| 44 | ); |
| 45 | |
| 46 | const messageIndexes = 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_thread_messages) |
| 54 | `; |
| 55 | assert.ok( |
| 56 | messageIndexes.some( |
| 57 | (index) => index.name === "idx_projection_thread_messages_thread_created_id", |
| 58 | ), |
| 59 | ); |
| 60 | |
| 61 | const messageIndexColumns = yield* sql<{ |
| 62 | readonly seqno: number; |
| 63 | readonly cid: number; |
| 64 | readonly name: string; |
| 65 | }>` |
| 66 | PRAGMA index_info('idx_projection_thread_messages_thread_created_id') |
| 67 | `; |
| 68 | assert.deepStrictEqual( |
| 69 | messageIndexColumns.map((column) => column.name), |
| 70 | ["thread_id", "created_at", "message_id"], |
| 71 | ); |
| 72 | }), |
| 73 | ); |
| 74 | }); |