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

Function dumpSchema

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

Source from the content-addressed store, hash-verified

45 | R2
46> = Migrator.make({
47 dumpSchema(path, table) {
48 const pgDump = (args: Array<string>) =>
49 Effect.gen(function*() {
50 const sql = yield* PgClient
51 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
52 const dump = yield* ChildProcess.make("pg_dump", [...args, "--no-owner", "--no-privileges"], {
53 env: {
54 PATH: (globalThis as any).process?.env.PATH,
55 PGHOST: sql.config.host,
56 PGPORT: sql.config.port?.toString(),
57 PGUSER: sql.config.username,
58 PGPASSWORD: sql.config.password
59 ? Redacted.value(sql.config.password)
60 : undefined,
61 PGDATABASE: sql.config.database,
62 PGSSLMODE: sql.config.ssl ? "require" : "prefer"
63 }
64 }).pipe(spawner.string)
65
66 return dump.replace(/^--.*$/gm, "")
67 .replace(/^SET .*$/gm, "")
68 .replace(/^SELECT pg_catalog\..*$/gm, "")
69 .replace(/\n{2,}/gm, "\n\n")
70 .trim()
71 }).pipe(
72 Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
73 )
74
75 const pgDumpSchema = pgDump(["--schema-only"])
76
77 const pgDumpMigrations = pgDump([
78 "--column-inserts",
79 "--data-only",
80 `--table=${table}`
81 ])
82
83 const pgDumpAll = Effect.map(
84 Effect.all([pgDumpSchema, pgDumpMigrations], { concurrency: 2 }),
85 ([schema, migrations]) => schema + "\n\n" + migrations
86 )
87
88 const pgDumpFile = (path: string) =>
89 Effect.gen(function*() {
90 const fs = yield* FileSystem.FileSystem
91 const path_ = yield* Path.Path
92 const dump = yield* pgDumpAll
93 yield* fs.makeDirectory(path_.dirname(path), { recursive: true })
94 yield* fs.writeFileString(path, dump)
95 }).pipe(
96 Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
97 )
98
99 return pgDumpFile(path)
100 }
101})
102
103/**

Callers 1

makeFunction · 0.85

Calls 4

pgDumpFunction · 0.85
pgDumpFileFunction · 0.85
allMethod · 0.80
mapMethod · 0.45

Tested by

no test coverage detected