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 * @since 4.0.0
10 */
11import type * as Effect from "effect/Effect"
12import * as Layer from "effect/Layer"
13import * as Migrator from "effect/sql/Migrator"
14import type * as Client from "effect/sql/SqlClient"
15import type { SqlError } from "effect/sql/SqlError"
16
17/**
18 * @since 4.0.0
19 */
20export * from "effect/sql/Migrator"
21
22/**
23 * Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
24 *
25 * @category running
26 * @since 4.0.0
27 */
28export const run: <R2 = never>(
29 options: Migrator.MigratorOptions<R2>
30) => Effect.Effect<
31 ReadonlyArray<readonly [id: number, name: string]>,
32 Migrator.MigrationError | SqlError,
33 Client.SqlClient | R2
34> = Migrator.make({
35 // dumpSchema(path, table) {
36 // const dump = (args: Array<string>) =>
37 // Effect.gen(function*() {
38 // const sql = yield* SqliteClient
39 // const dump = yield* pipe(
40 // Command.make("sqlite3", (sql as SqliteClient).config.filename, ...args),
41 // Command.string
42 // )
43 // return dump.replace(/^create table sqlite_sequence\(.*$/im, "")
44 // .replace(/\n{2,}/gm, "\n\n")
45 // .trim()
46 // }).pipe(
47 // Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
48 // )
49 //
50 // const dumpSchema = dump([".schema"])
51 //
52 // const dumpMigrations = dump([
53 // "--cmd",
54 // `.mode insert ${table}`,
55 // `select * from ${table}`
56 // ])
57 //
58 // const dumpAll = Effect.map(
59 // Effect.all([dumpSchema, dumpMigrations], { concurrency: 2 }),
60 // ([schema, migrations]) => schema + "\n\n" + migrations
61 // )
62 //
63 // const dumpFile = (file: string) =>
64 // Effect.gen(function*() {
65 // const fs = yield* FileSystem
66 // const path = yield* Path
67 // const dump = yield* dumpAll
68 // yield* fs.makeDirectory(path.dirname(file), { recursive: true })
69 // yield* fs.writeFileString(file, dump)
70 // }).pipe(
71 // Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
72 // )
73 //
74 // return dumpFile(path)
75 // }
76})
77
78/**
79 * Creates a layer that runs the configured SQL migrations during layer construction.
80 *
81 * @category layers
82 * @since 4.0.0
83 */
84export const layer = <R>(
85 options: Migrator.MigratorOptions<R>
86): Layer.Layer<
87 never,
88 SqlError | Migrator.MigrationError,
89 Client.SqlClient | R
90> => Layer.effectDiscard(run(options))