@effect/sql-pg 4.0.0-beta.5 → 4.0.0-beta.51
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/README.md +1 -1
- package/dist/PgClient.d.ts +59 -19
- package/dist/PgClient.d.ts.map +1 -1
- package/dist/PgClient.js +340 -184
- package/dist/PgClient.js.map +1 -1
- package/dist/PgMigrator.d.ts +8 -4
- package/dist/PgMigrator.d.ts.map +1 -1
- package/dist/PgMigrator.js +49 -56
- package/dist/PgMigrator.js.map +1 -1
- package/package.json +9 -9
- package/src/PgClient.ts +536 -252
- package/src/PgMigrator.ts +74 -60
package/src/PgMigrator.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
import
|
|
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
|
|
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
|
-
|
|
30
|
+
| SqlClient
|
|
31
|
+
| PgClient
|
|
32
|
+
| ChildProcessSpawner.ChildProcessSpawner
|
|
33
|
+
| FileSystem.FileSystem
|
|
34
|
+
| Path.Path
|
|
35
|
+
| R2
|
|
25
36
|
> = Migrator.make({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
102
|
+
| SqlClient
|
|
103
|
+
| PgClient
|
|
104
|
+
| ChildProcessSpawner.ChildProcessSpawner
|
|
105
|
+
| FileSystem.FileSystem
|
|
106
|
+
| Path.Path
|
|
107
|
+
| R
|
|
94
108
|
> => Layer.effectDiscard(run(options))
|