Open History to see the last commit
1/**
2 * MySQL adapter for the shared Effect SQL migration runner.
3 *
4 * This module re-exports the shared `Migrator` loaders and error types, then
5 * provides `run` and `layer` helpers that apply ordered migrations through the
6 * current mysql2-backed `SqlClient`. `run` returns the applied migration IDs
7 * and names, while `layer` runs migrations during layer construction and
8 * provides no services.
9 *
10 * @stability unstable
11 * @since 4.0.0
12 */
13import type * as Effect from "effect/Effect"
14import * as Layer from "effect/Layer"
15import * as Migrator from "effect/sql/Migrator"
16import type * as Client from "effect/sql/SqlClient"
17import type { SqlError } from "effect/sql/SqlError"
18
19/**
20 * @since 4.0.0
21 */
22export * from "effect/sql/Migrator"
23export {
24 /**
25 * @stability unstable
26 * @category loaders
27 * @since 4.0.0
28 */
29 fromFileSystem
30} from "effect/sql/MigratorFileSystem"
31
32/**
33 * Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
34 *
35 * @stability unstable
36 * @category running
37 * @since 4.0.0
38 */
39export const run: <R2 = never>(
40 { loader, schemaDirectory, table }: Migrator.MigratorOptions<R2>
41) => Effect.Effect<
42 ReadonlyArray<readonly [id: number, name: string]>,
43 Migrator.MigrationError | SqlError,
44 Client.SqlClient | R2
45> = Migrator.make({
46 // TODO: re-add when Command module is available
47 // dumpSchema(path, table) {
48 // const mysqlDump = (args: Array<string>) =>
49 // Effect.gen(function*() {
50 // const sql = yield* MysqlClient
51 // const dump = yield* pipe(
52 // Command.make(
53 // "mysqldump",
54 // ...(sql.config.host ? ["-h", sql.config.host] : []),
55 // ...(sql.config.port ? ["-P", sql.config.port.toString()] : []),
56 // ...(sql.config.username ? ["-u", sql.config.username] : []),
57 // ...(sql.config.password ? [`-p${Redacted.value(sql.config.password)}`] : []),
58 // ...(sql.config.database ? [sql.config.database] : []),
59 // "--skip-comments",
60 // "--compact",
61 // ...args
62 // ),
63 // Command.env({
64 // PATH: (globalThis as any).process?.env.PATH
65 // }),
66 // Command.string
67 // )
68 //
69 // return dump.replace(/^\/\*.*$/gm, "")
70 // .replace(/\n{2,}/gm, "\n\n")
71 // .trim()
72 // }).pipe(
73 // Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
74 // )
75 //
76 // const dumpSchema = mysqlDump(["--no-data"])
77 //
78 // const dumpMigrations = mysqlDump(["--no-create-info", "--tables", table])
79 //
80 // const dumpAll = Effect.map(
81 // Effect.all([dumpSchema, dumpMigrations], { concurrency: 2 }),
82 // ([schema, migrations]) => schema + "\n\n" + migrations
83 // )
84 //
85 // const dumpFile = (file: string) =>
86 // Effect.gen(function*() {
87 // const fs = yield* FileSystem
88 // const path = yield* Path
89 // const dump = yield* dumpAll
90 // yield* fs.makeDirectory(path.dirname(file), { recursive: true })
91 // yield* fs.writeFileString(file, dump)
92 // }).pipe(
93 // Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
94 // )
95 //
96 // return dumpFile(path)
97 // }
98})
99
100/**
101 * Creates a layer that runs the configured SQL migrations during layer construction.
102 *
103 * @stability unstable
104 * @category layers
105 * @since 4.0.0
106 */
107export const layer = <R>(
108 options: Migrator.MigratorOptions<R>
109): Layer.Layer<
110 never,
111 Migrator.MigrationError | SqlError,
112 Client.SqlClient | R
113> => Layer.effectDiscard(run(options))