Open History to see the last commit
1/**
2 * Runs database migrations for Bun SQLite projects that use Effect SQL.
3 *
4 * This module re-exports the shared migration loaders and errors, then provides
5 * `run` and `layer` helpers that apply pending migration files with the current
6 * `SqlClient`. It does not add Bun-specific schema dump support; migration
7 * execution is handled by the shared SQL migrator.
8 *
9 * @stability unstable
10 * @since 4.0.0
11 */
12import type * as Effect from "effect/Effect"
13import * as Layer from "effect/Layer"
14import * as Migrator from "effect/sql/Migrator"
15import type * as Client from "effect/sql/SqlClient"
16import type { SqlError } from "effect/sql/SqlError"
17
18/**
19 * @since 4.0.0
20 */
21export * from "effect/sql/Migrator"
22export {
23 /**
24 * @stability unstable
25 * @category loaders
26 * @since 4.0.0
27 */
28 fromFileSystem
29} from "effect/sql/MigratorFileSystem"
30
31/**
32 * Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
33 *
34 * @stability unstable
35 * @category running
36 * @since 4.0.0
37 */
38export const run: <R2 = never>(
39 options: Migrator.MigratorOptions<R2>
40) => Effect.Effect<
41 ReadonlyArray<readonly [id: number, name: string]>,
42 Migrator.MigrationError | SqlError,
43 Client.SqlClient | R2
44> = Migrator.make({
45 // dumpSchema(path, table) {
46 // const dump = (args: Array<string>) =>
47 // Effect.gen(function*() {
48 // const sql = yield* SqliteClient
49 // const dump = yield* pipe(
50 // Command.make("sqlite3", (sql as SqliteClient).config.filename, ...args),
51 // Command.string
52 // )
53 // return dump.replace(/^create table sqlite_sequence\(.*$/im, "")
54 // .replace(/\n{2,}/gm, "\n\n")
55 // .trim()
56 // }).pipe(
57 // Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
58 // )
59 //
60 // const dumpSchema = dump([".schema"])
61 //
62 // const dumpMigrations = dump([
63 // "--cmd",
64 // `.mode insert ${table}`,
65 // `select * from ${table}`
66 // ])
67 //
68 // const dumpAll = Effect.map(
69 // Effect.all([dumpSchema, dumpMigrations], { concurrency: 2 }),
70 // ([schema, migrations]) => schema + "\n\n" + migrations
71 // )
72 //
73 // const dumpFile = (file: string) =>
74 // Effect.gen(function*() {
75 // const fs = yield* FileSystem
76 // const path = yield* Path
77 // const dump = yield* dumpAll
78 // yield* fs.makeDirectory(path.dirname(file), { recursive: true })
79 // yield* fs.writeFileString(file, dump)
80 // }).pipe(
81 // Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
82 // )
83 //
84 // return dumpFile(path)
85 // }
86})
87
88/**
89 * Creates a layer that runs the configured SQL migrations during layer construction.
90 *
91 * @stability unstable
92 * @category layers
93 * @since 4.0.0
94 */
95export const layer = <R>(
96 options: Migrator.MigratorOptions<R>
97): Layer.Layer<
98 never,
99 SqlError | Migrator.MigrationError,
100 Client.SqlClient | R
101> => Layer.effectDiscard(run(options))