| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as SqlClient from "effect/sql/SqlClient"; |
| 6 | |
| 7 | import { runMigrations } from "../Migrations.ts"; |
| 8 | |
| 9 | const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layer({ filename: ":memory:" }))); |
| 10 | |
| 11 | layer("056_RemoveRedundantProjectionIndexes", (it) => { |
| 12 | it.effect("keeps the covering indexes and removes their prefix indexes", () => |
| 13 | Effect.gen(function* () { |
| 14 | const sql = yield* SqlClient.SqlClient; |
| 15 | yield* runMigrations({ toMigrationInclusive: 55 }); |
| 16 | yield* runMigrations({ toMigrationInclusive: 56 }); |
| 17 | |
| 18 | const rows = yield* sql<{ readonly name: string }>` |
| 19 | SELECT name |
| 20 | FROM sqlite_master |
| 21 | WHERE type = 'index' |
| 22 | AND name LIKE 'idx_projection_%' |
| 23 | `; |
| 24 | const indexes = new Set(rows.map((row) => row.name)); |
| 25 | |
| 26 | for (const removed of [ |
| 27 | "idx_projection_threads_project_id", |
| 28 | "idx_projection_thread_messages_thread_created", |
| 29 | "idx_projection_thread_activities_thread_sequence", |
| 30 | "idx_projection_turns_thread_requested", |
| 31 | ]) { |
| 32 | assert.isFalse(indexes.has(removed), removed); |
| 33 | } |
| 34 | for (const covering of [ |
| 35 | "idx_projection_threads_project_deleted_created", |
| 36 | "idx_projection_thread_messages_thread_created_id", |
| 37 | "idx_projection_thread_activities_thread_sequence_created_id", |
| 38 | "idx_projection_turns_thread_keyset", |
| 39 | ]) { |
| 40 | assert.isTrue(indexes.has(covering), covering); |
| 41 | } |
| 42 | }), |
| 43 | ); |
| 44 | }); |