@effect/sql-pg 4.0.0-beta.1 → 4.0.0-beta.100

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,94 +1,120 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * Runs database migrations for PostgreSQL projects that use Effect SQL.
3
+ *
4
+ * This module reuses the shared SQL migrator and connects it to PostgreSQL. It
5
+ * exposes the common migration helpers and adds `run` and `layer` functions
6
+ * that apply pending migration files with the current SQL client. When schema
7
+ * dumps are requested, it uses `pg_dump` and the usual process and filesystem
8
+ * services.
9
+ *
10
+ * @since 4.0.0
3
11
  */
4
- import type * as Effect from "effect/Effect"
12
+ import * as Effect from "effect/Effect"
13
+ import * as FileSystem from "effect/FileSystem"
5
14
  import * as Layer from "effect/Layer"
15
+ import * as Path from "effect/Path"
16
+ import * as Redacted from "effect/Redacted"
17
+ import * as ChildProcess from "effect/unstable/process/ChildProcess"
18
+ import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"
6
19
  import * as Migrator from "effect/unstable/sql/Migrator"
7
- import type * as Client from "effect/unstable/sql/SqlClient"
20
+ import type { SqlClient } from "effect/unstable/sql/SqlClient"
8
21
  import type { SqlError } from "effect/unstable/sql/SqlError"
22
+ import { PgClient } from "./PgClient.ts"
9
23
 
10
24
  /**
11
- * @since 1.0.0
25
+ * @since 4.0.0
12
26
  */
13
27
  export * from "effect/unstable/sql/Migrator"
14
28
 
15
29
  /**
16
- * @category constructor
17
- * @since 1.0.0
30
+ * Runs PostgreSQL SQL migrations using the configured clients. Schema dumps use `pg_dump` and require child process, filesystem, and path services.
31
+ *
32
+ * @category constructors
33
+ * @since 4.0.0
18
34
  */
19
35
  export const run: <R2 = never>(
20
36
  options: Migrator.MigratorOptions<R2>
21
37
  ) => Effect.Effect<
22
38
  ReadonlyArray<readonly [id: number, name: string]>,
23
39
  Migrator.MigrationError | SqlError,
24
- Client.SqlClient | R2
40
+ | SqlClient
41
+ | PgClient
42
+ | ChildProcessSpawner.ChildProcessSpawner
43
+ | FileSystem.FileSystem
44
+ | Path.Path
45
+ | R2
25
46
  > = 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
- // }
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
+ }
82
101
  })
83
102
 
84
103
  /**
104
+ * Creates a layer that runs PostgreSQL migrations during layer construction, including `pg_dump`-based schema dump support when requested.
105
+ *
85
106
  * @category layers
86
- * @since 1.0.0
107
+ * @since 4.0.0
87
108
  */
88
109
  export const layer = <R>(
89
110
  options: Migrator.MigratorOptions<R>
90
111
  ): Layer.Layer<
91
112
  never,
92
113
  Migrator.MigrationError | SqlError,
93
- Client.SqlClient | R
114
+ | SqlClient
115
+ | PgClient
116
+ | ChildProcessSpawner.ChildProcessSpawner
117
+ | FileSystem.FileSystem
118
+ | Path.Path
119
+ | R
94
120
  > => Layer.effectDiscard(run(options))
package/src/index.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * @since 4.0.0
3
3
  */
4
4
 
5
5
  // @barrel: Auto-generated exports. Do not edit manually.
6
6
 
7
7
  /**
8
- * @since 1.0.0
8
+ * @since 4.0.0
9
9
  */
10
10
  export * as PgClient from "./PgClient.ts"
11
11
 
12
12
  /**
13
- * @since 1.0.0
13
+ * @since 4.0.0
14
14
  */
15
15
  export * as PgMigrator from "./PgMigrator.ts"