@effect/sql-pg 4.0.0-beta.6 → 4.0.0-beta.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/PgMigrator.ts CHANGED
@@ -1,11 +1,17 @@
1
1
  /**
2
2
  * @since 1.0.0
3
3
  */
4
- import type * as Effect from "effect/Effect"
4
+ import * as Effect from "effect/Effect"
5
+ import * as FileSystem from "effect/FileSystem"
5
6
  import * as Layer from "effect/Layer"
7
+ import * as Path from "effect/Path"
8
+ import * as Redacted from "effect/Redacted"
9
+ import * as ChildProcess from "effect/unstable/process/ChildProcess"
10
+ import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"
6
11
  import * as Migrator from "effect/unstable/sql/Migrator"
7
- import type * as Client from "effect/unstable/sql/SqlClient"
12
+ import type { SqlClient } from "effect/unstable/sql/SqlClient"
8
13
  import type { SqlError } from "effect/unstable/sql/SqlError"
14
+ import { PgClient } from "./PgClient.ts"
9
15
 
10
16
  /**
11
17
  * @since 1.0.0
@@ -21,64 +27,67 @@ export const run: <R2 = never>(
21
27
  ) => Effect.Effect<
22
28
  ReadonlyArray<readonly [id: number, name: string]>,
23
29
  Migrator.MigrationError | SqlError,
24
- Client.SqlClient | R2
30
+ | SqlClient
31
+ | PgClient
32
+ | ChildProcessSpawner.ChildProcessSpawner
33
+ | FileSystem.FileSystem
34
+ | Path.Path
35
+ | R2
25
36
  > = Migrator.make({
26
- // TODO: Wait for Command module
27
- // dumpSchema(path, table) {
28
- // const pgDump = (args: Array<string>) =>
29
- // Effect.gen(function*() {
30
- // const sql = yield* PgClient
31
- // const dump = yield* pipe(
32
- // Command.make("pg_dump", ...args, "--no-owner", "--no-privileges"),
33
- // Command.env({
34
- // PATH: (globalThis as any).process?.env.PATH,
35
- // PGHOST: sql.config.host,
36
- // PGPORT: sql.config.port?.toString(),
37
- // PGUSER: sql.config.username,
38
- // PGPASSWORD: sql.config.password
39
- // ? Redacted.value(sql.config.password)
40
- // : undefined,
41
- // PGDATABASE: sql.config.database,
42
- // PGSSLMODE: sql.config.ssl ? "require" : "prefer"
43
- // }),
44
- // Command.string
45
- // )
46
- //
47
- // return dump.replace(/^--.*$/gm, "")
48
- // .replace(/^SET .*$/gm, "")
49
- // .replace(/^SELECT pg_catalog\..*$/gm, "")
50
- // .replace(/\n{2,}/gm, "\n\n")
51
- // .trim()
52
- // }).pipe(
53
- // Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
54
- // )
55
- //
56
- // const pgDumpSchema = pgDump(["--schema-only"])
57
- //
58
- // const pgDumpMigrations = pgDump([
59
- // "--column-inserts",
60
- // "--data-only",
61
- // `--table=${table}`
62
- // ])
63
- //
64
- // const pgDumpAll = Effect.map(
65
- // Effect.all([pgDumpSchema, pgDumpMigrations], { concurrency: 2 }),
66
- // ([schema, migrations]) => schema + "\n\n" + migrations
67
- // )
68
- //
69
- // const pgDumpFile = (path: string) =>
70
- // Effect.gen(function*() {
71
- // const fs = yield* FileSystem
72
- // const path_ = yield* Path
73
- // const dump = yield* pgDumpAll
74
- // yield* fs.makeDirectory(path_.dirname(path), { recursive: true })
75
- // yield* fs.writeFileString(path, dump)
76
- // }).pipe(
77
- // Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
78
- // )
79
- //
80
- // return pgDumpFile(path)
81
- // }
37
+ dumpSchema(path, table) {
38
+ const pgDump = (args: Array<string>) =>
39
+ Effect.gen(function*() {
40
+ const sql = yield* PgClient
41
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
42
+ const dump = yield* ChildProcess.make("pg_dump", [...args, "--no-owner", "--no-privileges"], {
43
+ env: {
44
+ PATH: (globalThis as any).process?.env.PATH,
45
+ PGHOST: sql.config.host,
46
+ PGPORT: sql.config.port?.toString(),
47
+ PGUSER: sql.config.username,
48
+ PGPASSWORD: sql.config.password
49
+ ? Redacted.value(sql.config.password)
50
+ : undefined,
51
+ PGDATABASE: sql.config.database,
52
+ PGSSLMODE: sql.config.ssl ? "require" : "prefer"
53
+ }
54
+ }).pipe(spawner.string)
55
+
56
+ return dump.replace(/^--.*$/gm, "")
57
+ .replace(/^SET .*$/gm, "")
58
+ .replace(/^SELECT pg_catalog\..*$/gm, "")
59
+ .replace(/\n{2,}/gm, "\n\n")
60
+ .trim();
61
+ }).pipe(
62
+ Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
63
+ )
64
+
65
+ const pgDumpSchema = pgDump(["--schema-only"])
66
+
67
+ const pgDumpMigrations = pgDump([
68
+ "--column-inserts",
69
+ "--data-only",
70
+ `--table=${table}`
71
+ ])
72
+
73
+ const pgDumpAll = Effect.map(
74
+ Effect.all([pgDumpSchema, pgDumpMigrations], { concurrency: 2 }),
75
+ ([schema, migrations]) => schema + "\n\n" + migrations
76
+ )
77
+
78
+ const pgDumpFile = (path: string) =>
79
+ Effect.gen(function*() {
80
+ const fs = yield* FileSystem.FileSystem
81
+ const path_ = yield* Path.Path
82
+ const dump = yield* pgDumpAll
83
+ yield* fs.makeDirectory(path_.dirname(path), { recursive: true })
84
+ yield* fs.writeFileString(path, dump)
85
+ }).pipe(
86
+ Effect.mapError((error) => new Migrator.MigrationError({ kind: "Failed", message: error.message }))
87
+ )
88
+
89
+ return pgDumpFile(path)
90
+ }
82
91
  })
83
92
 
84
93
  /**
@@ -90,5 +99,10 @@ export const layer = <R>(
90
99
  ): Layer.Layer<
91
100
  never,
92
101
  Migrator.MigrationError | SqlError,
93
- Client.SqlClient | R
102
+ | SqlClient
103
+ | PgClient
104
+ | ChildProcessSpawner.ChildProcessSpawner
105
+ | FileSystem.FileSystem
106
+ | Path.Path
107
+ | R
94
108
  > => Layer.effectDiscard(run(options))