@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 187 lines 7.09 KB
Raw
1 import { PgClient, PgMigrator } from "@effect/sql-pg"
2 import { assert, describe, it } from "@effect/vitest"
3 import { Effect, FileSystem, Path, Redacted, Stream } from "effect"
4 import { ChildProcess, ChildProcessSpawner } from "effect/process"
5 import { Reactivity } from "effect/reactivity"
6 import * as SqlClient from "effect/sql/SqlClient"
7 import * as Fs from "node:fs"
8 import * as NodePath from "node:path"
9 import { PassThrough } from "node:stream"
10
11 const makeMigrator = Effect.fnUntraced(function*(
12 config: PgClient.PgClientConfig,
13 onSpawn?: (command: ChildProcess.StandardCommand) => void
14 ) {
15 const commands: Array<ChildProcess.StandardCommand> = []
16 const files: Array<string> = []
17 const pg = yield* PgClient.make(config)
18 const sql = yield* SqlClient.make({
19 driver: "test",
20 acquirer: Effect.succeed({
21 execute: () => Effect.succeed([]),
22 executeRaw: () => Effect.succeed([]),
23 executeValues: () => Effect.succeed([]),
24 executeUnprepared: () => Effect.succeed([]),
25 executeValuesUnprepared: () => Effect.succeed([]),
26 executeStream: () => Stream.empty
27 }),
28 compiler: PgClient.makeCompiler(),
29 spanAttributes: []
30 })
31 const run = PgMigrator.run({
32 loader: PgMigrator.fromRecord({ "1_init": Effect.void }),
33 schemaDirectory: "migrations"
34 }).pipe(
35 Effect.provideService(PgClient.PgClient, pg),
36 Effect.provideService(SqlClient.SqlClient, sql),
37 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, {
38 ...ChildProcessSpawner.make(() => Effect.die("unexpected spawn")),
39 string: (command) =>
40 Effect.sync(() => {
41 assert(ChildProcess.isStandardCommand(command))
42 commands.push(command)
43 onSpawn?.(command)
44 return "SELECT 1;"
45 })
46 }),
47 Effect.provide(FileSystem.layerNoop({
48 makeDirectory: () => Effect.void,
49 writeFileString: (path) =>
50 Effect.sync(() => {
51 files.push(path)
52 })
53 })),
54 Effect.provide(Path.layer)
55 )
56 return { commands, files, run }
57 }, Effect.provide(Reactivity.layer))
58
59 // Parses the libpq conninfo passed as `pg_dump --dbname=<conninfo>`.
60 const dumpConnection = (command: ChildProcess.StandardCommand) => {
61 const dbname = command.args.find((arg): arg is string => typeof arg === "string" && arg.startsWith("--dbname="))
62 assert(dbname !== undefined, "pg_dump was not given --dbname")
63 const conninfo: Record<string, string> = {}
64 for (const [, key, quoted, plain] of dbname.slice(9).matchAll(/(\w+)\s*=\s*(?:'((?:[^'\\]|\\.)*)'|(\S*))/g)) {
65 conninfo[key] = quoted !== undefined ? quoted.replace(/\\(.)/g, "$1") : plain
66 }
67 return conninfo
68 }
69
70 describe("PgMigrator", () => {
71 it.effect("shares one password per schema dump and refreshes it for the next dump", () =>
72 Effect.gen(function*() {
73 let calls = 0
74 const { commands, files, run } = yield* makeMigrator({
75 password: Effect.map(Effect.yieldNow, () => Redacted.make(`secret-${++calls}`))
76 })
77
78 yield* run
79
80 assert.strictEqual(calls, 1)
81 assert.deepStrictEqual(commands.map((command) => command.command), ["pg_dump", "pg_dump"])
82 assert.deepStrictEqual(commands.map((command) => command.options.env?.PGPASSWORD), ["secret-1", "secret-1"])
83
84 yield* run
85
86 assert.strictEqual(calls, 2)
87 assert.deepStrictEqual(commands.map((command) => command.options.env?.PGPASSWORD), [
88 "secret-1",
89 "secret-1",
90 "secret-2",
91 "secret-2"
92 ])
93 assert.deepStrictEqual(files, ["migrations/_schema.sql", "migrations/_schema.sql"])
94 }))
95
96 it.effect("passes a static password to both dump processes", () =>
97 Effect.gen(function*() {
98 const { commands, files, run } = yield* makeMigrator({ password: Redacted.make("static") })
99 yield* run
100
101 assert.deepStrictEqual(commands.map((command) => command.options.env?.PGPASSWORD), ["static", "static"])
102 assert.deepStrictEqual(files, ["migrations/_schema.sql"])
103 }))
104
105 it.effect("dumps through the connection resolved from the url and explicit fields", () =>
106 Effect.gen(function*() {
107 const { commands, run } = yield* makeMigrator({
108 url: Redacted.make("postgres://alice:s3cret@db.example.com:6543/app?sslmode=prefer"),
109 database: "reports"
110 })
111 yield* run
112
113 assert.strictEqual(commands.length, 2)
114 for (const command of commands) {
115 assert.containsSubset(dumpConnection(command), {
116 host: "db.example.com",
117 port: "6543",
118 user: "alice",
119 dbname: "reports",
120 // The client verifies the certificate whenever the server accepts TLS
121 sslmode: "verify-full"
122 })
123 assert.isFalse(
124 command.args.some((arg) => (Redacted.isRedacted(arg) ? Redacted.value(arg) : arg).includes("s3cret"))
125 )
126 assert.strictEqual(command.options.extendEnv, false)
127 assert.deepStrictEqual(command.options.env, { PATH: process.env.PATH, PGPASSWORD: "s3cret" })
128 }
129 }))
130
131 it.effect("matches the client's socket and unverified TLS settings", () =>
132 Effect.gen(function*() {
133 const cases: ReadonlyArray<readonly [PgClient.PgClientConfig, Record<string, string>]> = [
134 [{ path: "/var/run/postgresql/.s.PGSQL.5433" }, {
135 host: "/var/run/postgresql",
136 port: "5433",
137 sslmode: "disable"
138 }],
139 [{ host: "db.example.com", ssl: { rejectUnauthorized: false } }, { sslmode: "require" }]
140 ]
141 for (const [config, expected] of cases) {
142 const { commands, run } = yield* makeMigrator({ ...config, username: "alice" })
143 yield* run
144 assert.strictEqual(commands.length, 2)
145 for (const command of commands) {
146 assert.containsSubset(dumpConnection(command), expected)
147 }
148 }
149 }))
150
151 it.effect("gives pg_dump the client's CA only while the dump runs", () =>
152 Effect.gen(function*() {
153 const ca = "-----BEGIN CERTIFICATE-----\nY2E=\n-----END CERTIFICATE-----"
154 const dumps: Array<{ sslmode: string; rootcert: string; directory: string }> = []
155 const { run } = yield* makeMigrator({ host: "db.example.com", username: "alice", ssl: { ca } }, (command) => {
156 const { sslmode, sslrootcert } = dumpConnection(command)
157 dumps.push({
158 sslmode,
159 rootcert: Fs.readFileSync(sslrootcert, "utf8"),
160 directory: NodePath.dirname(sslrootcert)
161 })
162 })
163 yield* run
164
165 assert.strictEqual(dumps.length, 2)
166 for (const dump of dumps) {
167 assert.strictEqual(dump.sslmode, "verify-full")
168 assert.include(dump.rootcert, ca)
169 assert.isFalse(Fs.existsSync(dump.directory))
170 }
171 }))
172
173 it.effect("does not dump connections that libpq cannot reproduce", () =>
174 Effect.gen(function*() {
175 const cases: ReadonlyArray<PgClient.PgClientConfig> = [
176 { stream: () => new PassThrough() },
177 { path: "/var/run/postgresql/.s.PGSQL.5432", ssl: true }
178 ]
179 for (const config of cases) {
180 const { commands, files, run } = yield* makeMigrator({ ...config, username: "alice" })
181 yield* run
182
183 assert.deepStrictEqual(commands, [])
184 assert.deepStrictEqual(files, [])
185 }
186 }))
187 })