| 1 | import { PgliteClient, PgliteMigrator } from "@effect/sql-pglite" |
| 2 | import { assert, describe, layer } from "@effect/vitest" |
| 3 | import { Effect, Layer } from "effect" |
| 4 | import { SqlClient } from "effect/sql/SqlClient" |
| 5 | |
| 6 | const ClientLayer = PgliteClient.layer({}) |
| 7 | |
| 8 | const loader = Effect.succeed([ |
| 9 | [ |
| 10 | 1, |
| 11 | "init", |
| 12 | Effect.succeed(Effect.gen(function*() { |
| 13 | const sql = yield* SqlClient |
| 14 | yield* sql`CREATE TABLE migrator_test (id SERIAL PRIMARY KEY, value TEXT)` |
| 15 | })) |
| 16 | ] as const, |
| 17 | [ |
| 18 | 2, |
| 19 | "insert", |
| 20 | Effect.succeed(Effect.gen(function*() { |
| 21 | const sql = yield* SqlClient |
| 22 | yield* sql`INSERT INTO migrator_test (value) VALUES ('hello')` |
| 23 | })) |
| 24 | ] as const |
| 25 | ]) |
| 26 | |
| 27 | const MigratorLayer = PgliteMigrator.layer({ loader }).pipe(Layer.provide(ClientLayer)) |
| 28 | |
| 29 | describe("PgliteMigrator", () => { |
| 30 | layer(Layer.merge(ClientLayer, MigratorLayer), { timeout: "30 seconds" })((it) => { |
| 31 | it.effect("runs migrations and records them", () => |
| 32 | Effect.gen(function*() { |
| 33 | const sql = yield* PgliteClient.PgliteClient |
| 34 | const rows = yield* sql<{ value: string }>`SELECT value FROM migrator_test` |
| 35 | assert.deepStrictEqual(rows, [{ value: "hello" }]) |
| 36 | const migrations = yield* sql< |
| 37 | { migration_id: number; name: string } |
| 38 | >`SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id` |
| 39 | assert.deepStrictEqual( |
| 40 | migrations.map((m) => [m.migration_id, m.name]), |
| 41 | [[1, "init"], [2, "insert"]] |
| 42 | ) |
| 43 | })) |
| 44 | }) |
| 45 | }) |