MCPcopy Create free account
hub / github.com/Effect-TS/effect / dumpSchema

Function dumpSchema

packages/sql-mysql2/src/MysqlMigrator.ts:38–97  ·  view source on GitHub ↗
(path, table)

Source from the content-addressed store, hash-verified

36 FileSystem | Path | MysqlClient | Client.SqlClient | CommandExecutor | R2
37> = Migrator.make({
38 dumpSchema(path, table) {
39 const mysqlDump = (args: Array<string>) =>
40 Effect.gen(function*() {
41 const sql = yield* MysqlClient
42
43 const url = sql.config.url ? new URL(Redacted.value(sql.config.url)) : undefined
44
45 const host = url?.hostname ?? sql.config.host
46 const port = url?.port ?? sql.config.port?.toString()
47 const username = url?.username ?? sql.config.username
48 const password = url?.password ? Redacted.make(url.password) : sql.config.password
49 const database = url?.pathname?.slice(1) ?? sql.config.database
50
51 const dump = yield* pipe(
52 Command.make(
53 "mysqldump",
54 ...(host ? ["-h", host] : []),
55 ...(port ? ["-P", port] : []),
56 ...(username ? ["-u", username] : []),
57 ...(password ? [`-p${Redacted.value(password)}`] : []),
58 ...(database ? [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({ reason: "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({ reason: "failed", message: error.message }))
94 )
95

Callers

nothing calls this directly

Calls 3

mysqlDumpFunction · 0.85
dumpFileFunction · 0.70
mapMethod · 0.65

Tested by

no test coverage detected