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

Function dumpSchema

packages/sql/pg/src/PgMigrator.ts:90–133  ·  view source on GitHub ↗
(path, table)

Source from the content-addressed store, hash-verified

88 | R2
89> = Migrator.make({
90 dumpSchema(path, table) {
91 const pgDump = Effect.fnUntraced(function*(connection: DumpConnection, args: Array<string>) {
92 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
93 const dump = yield* ChildProcess.make("pg_dump", [
94 ...args,
95 "--no-owner",
96 "--no-privileges",
97 "--no-password",
98 `--dbname=${connection.conninfo}`
99 ], {
100 env: { PATH: process.env.PATH, PGPASSWORD: connection.password },
101 extendEnv: false
102 }).pipe(spawner.string)
103
104 return dump.replace(/^--.*$/gm, "")
105 .replace(/^SET .*$/gm, "")
106 .replace(/^SELECT pg_catalog\..*$/gm, "")
107 .replace(/\n{2,}/gm, "\n\n")
108 .trim()
109 })
110
111 const pgDumpAll = Effect.gen(function*() {
112 const sql = yield* PgClient
113 const connection = yield* dumpConnection(sql.config)
114 const [schema, migrations] = yield* Effect.all([
115 pgDump(connection, ["--schema-only"]),
116 pgDump(connection, ["--column-inserts", "--data-only", `--table=${table}`])
117 ], { concurrency: 2 })
118 return schema + "\n\n" + migrations
119 }).pipe(Effect.scoped)
120
121 const pgDumpFile = Effect.fnUntraced(
122 function*(path: string) {
123 const fs = yield* FileSystem.FileSystem
124 const path_ = yield* Path.Path
125 const dump = yield* pgDumpAll
126 yield* fs.makeDirectory(path_.dirname(path), { recursive: true })
127 yield* fs.writeFileString(path, dump)
128 },
129 Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message, cause: error }))
130 )
131
132 return pgDumpFile(path)
133 }
134})
135
136interface DumpConnection {

Callers 1

makeFunction · 0.85

Calls 6

dumpConnectionFunction · 0.85
trimMethod · 0.80
pipeMethod · 0.65
makeMethod · 0.65
allMethod · 0.65
replaceMethod · 0.45

Tested by

no test coverage detected