@supatype/cli 0.3.4 → 0.3.6
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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +168 -158
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +36 -0
- package/dist/adopt-walkthrough.d.ts +40 -0
- package/dist/adopt-walkthrough.d.ts.map +1 -0
- package/dist/adopt-walkthrough.js +113 -0
- package/dist/adopt-walkthrough.js.map +1 -0
- package/dist/cli-version-embedded.js +1 -1
- package/dist/commands/admin.d.ts.map +1 -1
- package/dist/commands/admin.js +7 -16
- package/dist/commands/admin.js.map +1 -1
- package/dist/commands/adopt.d.ts.map +1 -1
- package/dist/commands/adopt.js +5 -6
- package/dist/commands/adopt.js.map +1 -1
- package/dist/commands/db.js +3 -3
- package/dist/commands/db.js.map +1 -1
- package/dist/commands/deploy.d.ts.map +1 -1
- package/dist/commands/deploy.js +7 -4
- package/dist/commands/deploy.js.map +1 -1
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/dev.js +20 -5
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +5 -7
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/init.js +1 -1
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/migrate.d.ts.map +1 -1
- package/dist/commands/migrate.js +6 -3
- package/dist/commands/migrate.js.map +1 -1
- package/dist/commands/push.d.ts.map +1 -1
- package/dist/commands/push.js +10 -6
- package/dist/commands/push.js.map +1 -1
- package/dist/commands/seed.js +2 -2
- package/dist/commands/seed.js.map +1 -1
- package/dist/dev-compose.d.ts +42 -0
- package/dist/dev-compose.d.ts.map +1 -1
- package/dist/dev-compose.js +116 -71
- package/dist/dev-compose.js.map +1 -1
- package/dist/dev-db-reset.d.ts +19 -0
- package/dist/dev-db-reset.d.ts.map +1 -0
- package/dist/dev-db-reset.js +75 -0
- package/dist/dev-db-reset.js.map +1 -0
- package/dist/dev-initial-push.d.ts +37 -0
- package/dist/dev-initial-push.d.ts.map +1 -0
- package/dist/dev-initial-push.js +91 -0
- package/dist/dev-initial-push.js.map +1 -0
- package/dist/dev-ports.d.ts.map +1 -1
- package/dist/dev-ports.js +1 -1
- package/dist/dev-ports.js.map +1 -1
- package/dist/diff-output.d.ts +2 -2
- package/dist/diff-output.d.ts.map +1 -1
- package/dist/diff-output.js +1 -1
- package/dist/diff-output.js.map +1 -1
- package/dist/engine-client.d.ts +5 -1
- package/dist/engine-client.d.ts.map +1 -1
- package/dist/engine-client.js +8 -6
- package/dist/engine-client.js.map +1 -1
- package/dist/engine-push-output.d.ts +10 -0
- package/dist/engine-push-output.d.ts.map +1 -1
- package/dist/engine-push-output.js +12 -0
- package/dist/engine-push-output.js.map +1 -1
- package/dist/{seed-connection.d.ts → host-database.d.ts} +20 -10
- package/dist/host-database.d.ts.map +1 -0
- package/dist/{seed-connection.js → host-database.js} +40 -14
- package/dist/host-database.js.map +1 -0
- package/dist/resolve-target.d.ts +2 -2
- package/dist/resolve-target.d.ts.map +1 -1
- package/dist/resolve-target.js +15 -11
- package/dist/resolve-target.js.map +1 -1
- package/dist/retired-no-cache.d.ts +18 -0
- package/dist/retired-no-cache.d.ts.map +1 -0
- package/dist/retired-no-cache.js +21 -0
- package/dist/retired-no-cache.js.map +1 -0
- package/dist/self-host-compose.d.ts +6 -0
- package/dist/self-host-compose.d.ts.map +1 -1
- package/dist/self-host-compose.js +9 -3
- package/dist/self-host-compose.js.map +1 -1
- package/dist/type-extractor.d.ts.map +1 -1
- package/dist/type-extractor.js +31 -25
- package/dist/type-extractor.js.map +1 -1
- package/package.json +2 -2
- package/src/adopt-walkthrough.ts +142 -0
- package/src/cli-version-embedded.ts +1 -1
- package/src/commands/admin.ts +7 -20
- package/src/commands/adopt.ts +5 -6
- package/src/commands/db.ts +3 -3
- package/src/commands/deploy.ts +7 -4
- package/src/commands/dev.ts +22 -6
- package/src/commands/doctor.ts +5 -7
- package/src/commands/init.ts +1 -1
- package/src/commands/migrate.ts +7 -4
- package/src/commands/push.ts +28 -10
- package/src/commands/seed.ts +2 -2
- package/src/dev-compose.ts +166 -77
- package/src/dev-db-reset.ts +95 -0
- package/src/dev-initial-push.ts +134 -0
- package/src/dev-ports.ts +1 -1
- package/src/diff-output.ts +2 -2
- package/src/engine-client.ts +5 -4
- package/src/engine-push-output.ts +19 -0
- package/src/{seed-connection.ts → host-database.ts} +42 -12
- package/src/resolve-target.ts +20 -14
- package/src/retired-no-cache.ts +27 -0
- package/src/self-host-compose.ts +10 -3
- package/src/type-extractor.ts +39 -42
- package/tests/adopt-result.test.ts +27 -0
- package/tests/adopt-walkthrough.test.ts +129 -0
- package/tests/dev-db-reset.test.ts +162 -0
- package/tests/dev-initial-push.test.ts +142 -0
- package/tests/engine-push-output.test.ts +30 -1
- package/tests/host-connection-targets.test.ts +84 -0
- package/tests/{seed-connection.test.ts → host-database.test.ts} +33 -15
- package/tests/retired-no-cache.test.ts +46 -0
- package/tests/type-extractor.test.ts +80 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/seed-connection.d.ts.map +0 -1
- package/dist/seed-connection.js.map +0 -1
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { Command } from "commander"
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
|
|
3
|
+
import type { SpawnSyncReturns } from "node:child_process"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `supatype dev --reset-db` is the only way `dev` removes local data, and it removes exactly one
|
|
7
|
+
* volume: Postgres's. Storage and the cache volume are the developer's too, and nothing about a
|
|
8
|
+
* database reset is a reason to lose uploaded files.
|
|
9
|
+
*
|
|
10
|
+
* It is opt-in and confirmed. Without a TTY there is nobody to confirm, so it is refused unless
|
|
11
|
+
* `--yes` says the caller already decided.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const spawnSync = vi.fn<(cmd: string, args: readonly string[]) => Partial<SpawnSyncReturns<string>>>()
|
|
15
|
+
vi.mock("node:child_process", async (importOriginal) => ({
|
|
16
|
+
...(await importOriginal<typeof import("node:child_process")>()),
|
|
17
|
+
spawnSync: (cmd: string, args: readonly string[]) => spawnSync(cmd, args),
|
|
18
|
+
}))
|
|
19
|
+
|
|
20
|
+
const runDockerCompose = vi.fn<(composePath: string, args: string[]) => number>(() => 0)
|
|
21
|
+
vi.mock("../src/self-host-compose.js", async (importOriginal) => ({
|
|
22
|
+
...(await importOriginal<typeof import("../src/self-host-compose.js")>()),
|
|
23
|
+
runDockerCompose: (composePath: string, args: string[]) => runDockerCompose(composePath, args),
|
|
24
|
+
}))
|
|
25
|
+
|
|
26
|
+
const isInteractive = vi.fn(() => true)
|
|
27
|
+
vi.mock("../src/ui/interactive.js", () => ({ isInteractive: () => isInteractive() }))
|
|
28
|
+
|
|
29
|
+
const confirm = vi.fn(async (_message: string) => false)
|
|
30
|
+
vi.mock("../src/ui/confirm.js", () => ({ confirm: (message: string) => confirm(message) }))
|
|
31
|
+
|
|
32
|
+
const lines: string[] = []
|
|
33
|
+
vi.mock("../src/ui/messages.js", () => ({
|
|
34
|
+
error: (line: string) => lines.push(`error: ${line}`),
|
|
35
|
+
plain: (line?: string) => lines.push(line ?? ""),
|
|
36
|
+
}))
|
|
37
|
+
|
|
38
|
+
const { devDbVolumeName, resetDevDatabase } = await import("../src/dev-db-reset.js")
|
|
39
|
+
const { DB_VOLUME_KEY, renderSelfHostCompose } = await import("../src/self-host-compose.js")
|
|
40
|
+
const { validateProjectConfig } = await import("../src/project-config.js")
|
|
41
|
+
|
|
42
|
+
const target = {
|
|
43
|
+
composePath: "/proj/.supatype/self-host/docker-compose.yml",
|
|
44
|
+
cwd: "/proj",
|
|
45
|
+
project: "supatype-demo",
|
|
46
|
+
external: false,
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Every `docker volume rm` the code ran, by volume name. */
|
|
50
|
+
const removedVolumes = (): string[] =>
|
|
51
|
+
spawnSync.mock.calls
|
|
52
|
+
.filter(([, args]) => args[0] === "volume" && args[1] === "rm")
|
|
53
|
+
.map(([, args]) => String(args[2]))
|
|
54
|
+
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
lines.length = 0
|
|
57
|
+
spawnSync.mockReset()
|
|
58
|
+
spawnSync.mockReturnValue({ status: 0, stdout: "", stderr: "" })
|
|
59
|
+
runDockerCompose.mockClear()
|
|
60
|
+
confirm.mockReset()
|
|
61
|
+
confirm.mockResolvedValue(false)
|
|
62
|
+
isInteractive.mockReturnValue(true)
|
|
63
|
+
vi.spyOn(console, "log").mockImplementation(() => {})
|
|
64
|
+
vi.spyOn(process, "exit").mockImplementation((code) => {
|
|
65
|
+
throw new Error(`exit ${String(code)}`)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
afterEach(() => {
|
|
69
|
+
vi.restoreAllMocks()
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
describe("the volume --reset-db removes", () => {
|
|
73
|
+
it("is the one the generated compose file mounts at Postgres's data directory", () => {
|
|
74
|
+
const config = validateProjectConfig(
|
|
75
|
+
{ project: { name: "demo" }, server: { mode: "dev" }, app: { mode: "none" }, database: { provider: "docker" } },
|
|
76
|
+
"supatype.config.ts",
|
|
77
|
+
)
|
|
78
|
+
const compose = renderSelfHostCompose(config)
|
|
79
|
+
expect(compose).toContain(`- ${DB_VOLUME_KEY}:/var/lib/postgresql/data`)
|
|
80
|
+
expect(compose).toMatch(new RegExp(`^volumes:[\\s\\S]*^ {2}${DB_VOLUME_KEY}:$`, "m"))
|
|
81
|
+
expect(devDbVolumeName("supatype-demo")).toBe(`supatype-demo_${DB_VOLUME_KEY}`)
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
describe("--reset-db without confirmation", () => {
|
|
86
|
+
it("removes nothing when the prompt is declined", async () => {
|
|
87
|
+
confirm.mockResolvedValue(false)
|
|
88
|
+
|
|
89
|
+
await expect(resetDevDatabase(target, { yes: false })).rejects.toThrow("exit 1")
|
|
90
|
+
|
|
91
|
+
expect(confirm).toHaveBeenCalledTimes(1)
|
|
92
|
+
expect(confirm.mock.calls[0]![0]).toContain("supatype-demo_db-data")
|
|
93
|
+
expect(removedVolumes()).toEqual([])
|
|
94
|
+
expect(runDockerCompose).not.toHaveBeenCalled()
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it("is refused without a TTY unless --yes is passed, and removes nothing", async () => {
|
|
98
|
+
isInteractive.mockReturnValue(false)
|
|
99
|
+
|
|
100
|
+
await expect(resetDevDatabase(target, { yes: false })).rejects.toThrow("exit 1")
|
|
101
|
+
|
|
102
|
+
expect(confirm).not.toHaveBeenCalled()
|
|
103
|
+
expect(removedVolumes()).toEqual([])
|
|
104
|
+
expect(runDockerCompose).not.toHaveBeenCalled()
|
|
105
|
+
expect(lines.join("\n")).toContain("--yes")
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it("is refused for a database Supatype does not run", async () => {
|
|
109
|
+
await expect(
|
|
110
|
+
resetDevDatabase({ ...target, external: true }, { yes: true }),
|
|
111
|
+
).rejects.toThrow("exit 1")
|
|
112
|
+
|
|
113
|
+
expect(removedVolumes()).toEqual([])
|
|
114
|
+
expect(runDockerCompose).not.toHaveBeenCalled()
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
describe("--reset-db once confirmed", () => {
|
|
119
|
+
it("removes only the Postgres data volume", async () => {
|
|
120
|
+
confirm.mockResolvedValue(true)
|
|
121
|
+
|
|
122
|
+
await resetDevDatabase(target, { yes: false })
|
|
123
|
+
|
|
124
|
+
expect(removedVolumes()).toEqual(["supatype-demo_db-data"])
|
|
125
|
+
// The db container has to go before its volume can, and nothing else may go with it.
|
|
126
|
+
expect(runDockerCompose.mock.calls.map(([, args]) => args)).toEqual([["rm", "--stop", "--force", "db"]])
|
|
127
|
+
for (const [, args] of runDockerCompose.mock.calls) {
|
|
128
|
+
expect(args).not.toContain("-v")
|
|
129
|
+
expect(args).not.toContain("down")
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it("skips the prompt with --yes, even without a TTY", async () => {
|
|
134
|
+
isInteractive.mockReturnValue(false)
|
|
135
|
+
|
|
136
|
+
await resetDevDatabase(target, { yes: true })
|
|
137
|
+
|
|
138
|
+
expect(confirm).not.toHaveBeenCalled()
|
|
139
|
+
expect(removedVolumes()).toEqual(["supatype-demo_db-data"])
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it("does nothing when there is no volume yet", async () => {
|
|
143
|
+
spawnSync.mockReturnValue({ status: 1, stdout: "", stderr: "Error: No such volume" })
|
|
144
|
+
|
|
145
|
+
await resetDevDatabase(target, { yes: true })
|
|
146
|
+
|
|
147
|
+
expect(removedVolumes()).toEqual([])
|
|
148
|
+
expect(runDockerCompose).not.toHaveBeenCalled()
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
describe("supatype dev", () => {
|
|
153
|
+
it("offers --reset-db and --yes", async () => {
|
|
154
|
+
const { registerDev } = await import("../src/commands/dev.js")
|
|
155
|
+
const program = new Command()
|
|
156
|
+
registerDev(program)
|
|
157
|
+
const dev = program.commands.find((c) => c.name() === "dev")
|
|
158
|
+
const flags = dev?.options.map((o) => o.long) ?? []
|
|
159
|
+
expect(flags).toContain("--reset-db")
|
|
160
|
+
expect(flags).toContain("--yes")
|
|
161
|
+
})
|
|
162
|
+
})
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { readdirSync, readFileSync } from "node:fs"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { fileURLToPath } from "node:url"
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A failed first push in `supatype dev` must never cost the developer their database.
|
|
8
|
+
*
|
|
9
|
+
* The loop caught every failure, ran `docker compose down -v`, started Postgres again and pushed
|
|
10
|
+
* again. An engine refusal is deterministic, so the reset was the only thing that changed between
|
|
11
|
+
* attempts: the second push "succeeded" against an empty database and the developer's rows and
|
|
12
|
+
* uploaded files were gone. These tests pin the three answers a failure can get: retried (Postgres
|
|
13
|
+
* was not reachable yet), refused (the engine said no, and `dev` stops with what it said), and
|
|
14
|
+
* exhausted.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const lines: string[] = []
|
|
18
|
+
vi.mock("../src/ui/messages.js", () => ({
|
|
19
|
+
error: (line: string) => lines.push(`error: ${line}`),
|
|
20
|
+
plain: (line?: string) => lines.push(line ?? ""),
|
|
21
|
+
}))
|
|
22
|
+
|
|
23
|
+
const { exitInitialPushFailed, pushInitialSchema } = await import("../src/dev-initial-push.js")
|
|
24
|
+
|
|
25
|
+
const UNMANAGED = [
|
|
26
|
+
"Error: Your schema declares model(s) for table(s) Supatype does not manage: public.orders.",
|
|
27
|
+
"",
|
|
28
|
+
"Nothing was applied. These tables already exist without Supatype's ownership marker.",
|
|
29
|
+
"What to do:",
|
|
30
|
+
" Run `supatype adopt` to bring them under management (it shows the SQL first)",
|
|
31
|
+
].join("\n")
|
|
32
|
+
|
|
33
|
+
const DESTRUCTIVE = "Error: 2 destructive operation(s) detected. Use --force to proceed."
|
|
34
|
+
|
|
35
|
+
const REFUSED = "error communicating with database: Connection refused (os error 111)"
|
|
36
|
+
|
|
37
|
+
function steps(failures: string[]) {
|
|
38
|
+
const queue = [...failures]
|
|
39
|
+
return {
|
|
40
|
+
push: vi.fn(async () => {
|
|
41
|
+
const next = queue.shift()
|
|
42
|
+
if (next !== undefined) throw new Error(next)
|
|
43
|
+
}),
|
|
44
|
+
recoverDatabase: vi.fn(async () => {}),
|
|
45
|
+
dumpLogs: vi.fn(),
|
|
46
|
+
sleep: vi.fn(async () => {}),
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
beforeEach(() => {
|
|
51
|
+
lines.length = 0
|
|
52
|
+
vi.spyOn(console, "error").mockImplementation(() => {})
|
|
53
|
+
vi.spyOn(console, "log").mockImplementation(() => {})
|
|
54
|
+
})
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
vi.restoreAllMocks()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe("an engine refusal on the first attempt", () => {
|
|
60
|
+
it("is returned as a refusal without a second attempt or any recovery", async () => {
|
|
61
|
+
const s = steps([UNMANAGED, UNMANAGED, UNMANAGED])
|
|
62
|
+
|
|
63
|
+
const outcome = await pushInitialSchema(s)
|
|
64
|
+
|
|
65
|
+
expect(outcome).toEqual({ kind: "refused", reason: "unmanaged-tables", message: UNMANAGED })
|
|
66
|
+
expect(s.push).toHaveBeenCalledTimes(1)
|
|
67
|
+
expect(s.recoverDatabase).not.toHaveBeenCalled()
|
|
68
|
+
expect(s.sleep).not.toHaveBeenCalled()
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it("is recognised by the engine's JSON reason, not only by its wording", async () => {
|
|
72
|
+
// The engine prints the refusal machine-readably on stdout. Matching the prose alone would
|
|
73
|
+
// turn any rewording of the message back into an "engine" refusal with no adopt offer.
|
|
74
|
+
const json = '{"status":"refused","reason":"unmanaged_model_tables","tables":["orders"]}'
|
|
75
|
+
const s = steps([json])
|
|
76
|
+
|
|
77
|
+
const outcome = await pushInitialSchema(s)
|
|
78
|
+
|
|
79
|
+
expect(outcome).toMatchObject({ kind: "refused", reason: "unmanaged-tables" })
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it("treats any other engine error as a refusal too", async () => {
|
|
83
|
+
const s = steps([DESTRUCTIVE, DESTRUCTIVE, DESTRUCTIVE])
|
|
84
|
+
|
|
85
|
+
const outcome = await pushInitialSchema(s)
|
|
86
|
+
|
|
87
|
+
expect(outcome).toEqual({ kind: "refused", reason: "engine", message: DESTRUCTIVE })
|
|
88
|
+
expect(s.push).toHaveBeenCalledTimes(1)
|
|
89
|
+
expect(s.recoverDatabase).not.toHaveBeenCalled()
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it("ends dev non-zero with the engine's message and the adopt remedy", () => {
|
|
93
|
+
const exit = vi.spyOn(process, "exit").mockImplementation((code) => {
|
|
94
|
+
throw new Error(`exit ${String(code)}`)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
expect(() =>
|
|
98
|
+
exitInitialPushFailed({ kind: "refused", reason: "unmanaged-tables", message: UNMANAGED }),
|
|
99
|
+
).toThrow("exit 1")
|
|
100
|
+
|
|
101
|
+
expect(exit).toHaveBeenCalledWith(1)
|
|
102
|
+
const out = lines.join("\n")
|
|
103
|
+
expect(out).toContain("Supatype does not manage: public.orders")
|
|
104
|
+
expect(out).toContain("supatype adopt")
|
|
105
|
+
expect(out).toContain("left as it was")
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
describe("a database that is not reachable yet", () => {
|
|
110
|
+
it("is retried, and recovered without removing anything", async () => {
|
|
111
|
+
const s = steps([REFUSED])
|
|
112
|
+
|
|
113
|
+
const outcome = await pushInitialSchema(s)
|
|
114
|
+
|
|
115
|
+
expect(outcome).toEqual({ kind: "applied" })
|
|
116
|
+
expect(s.push).toHaveBeenCalledTimes(2)
|
|
117
|
+
expect(s.recoverDatabase).toHaveBeenCalledTimes(1)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it("is reported as exhausted once the attempts run out", async () => {
|
|
121
|
+
const starting = "error returned from database: the database system is starting up"
|
|
122
|
+
const s = steps([starting, starting, starting])
|
|
123
|
+
|
|
124
|
+
const outcome = await pushInitialSchema(s)
|
|
125
|
+
|
|
126
|
+
expect(outcome).toEqual({ kind: "exhausted", attempts: 3, message: starting })
|
|
127
|
+
expect(s.push).toHaveBeenCalledTimes(3)
|
|
128
|
+
expect(s.recoverDatabase).toHaveBeenCalledTimes(2)
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
describe("the CLI source", () => {
|
|
133
|
+
// The recovery a retry runs is wired up in dev-compose.ts, so the tests above cannot see what it
|
|
134
|
+
// does. This one can: no code path in the CLI may tear the stack down with its volumes.
|
|
135
|
+
it("never runs `docker compose down -v`", () => {
|
|
136
|
+
const src = fileURLToPath(new URL("../src", import.meta.url))
|
|
137
|
+
const offenders = readdirSync(src, { recursive: true, encoding: "utf8" })
|
|
138
|
+
.filter((rel) => rel.endsWith(".ts"))
|
|
139
|
+
.filter((rel) => /\[\s*"down"\s*,\s*"-v"/.test(readFileSync(join(src, rel), "utf8")))
|
|
140
|
+
expect(offenders).toEqual([])
|
|
141
|
+
})
|
|
142
|
+
})
|
|
@@ -1,10 +1,39 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest"
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest"
|
|
2
2
|
import {
|
|
3
3
|
filterComposeNoise,
|
|
4
4
|
formatEnginePushMessage,
|
|
5
5
|
parseEnginePushOutput,
|
|
6
|
+
printPushWarnings,
|
|
6
7
|
} from "../src/engine-push-output.js"
|
|
7
8
|
|
|
9
|
+
describe("printPushWarnings()", () => {
|
|
10
|
+
afterEach(() => vi.restoreAllMocks())
|
|
11
|
+
|
|
12
|
+
function printed(run: () => void): string {
|
|
13
|
+
const log = vi.spyOn(console, "log").mockImplementation(() => undefined)
|
|
14
|
+
run()
|
|
15
|
+
return log.mock.calls.map((call) => call.join(" ")).join("\n")
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
it("prints what a push warned about", () => {
|
|
19
|
+
const out = printed(() =>
|
|
20
|
+
printPushWarnings(parseEnginePushOutput('{"status":"up_to_date","warnings":["Bucket \\"a\\" declares no delete rule"]}')!),
|
|
21
|
+
)
|
|
22
|
+
expect(out).toContain('[!] Bucket "a" declares no delete rule')
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it("leaves out what the diff before it already showed", () => {
|
|
26
|
+
const out = printed(() => printPushWarnings({ warnings: ["shown", "found after applying"] }, ["shown"]))
|
|
27
|
+
expect(out).toContain("found after applying")
|
|
28
|
+
expect(out).not.toContain("shown\n")
|
|
29
|
+
expect(out).toContain("1 warning(s)")
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it("prints nothing when there is nothing new", () => {
|
|
33
|
+
expect(printed(() => printPushWarnings({ warnings: ["shown"] }, ["shown"]))).toBe("")
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
8
37
|
describe("parseEnginePushOutput()", () => {
|
|
9
38
|
it("parses JSON on its own line amid docker progress", () => {
|
|
10
39
|
const output = [
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest"
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
|
|
3
|
+
import { join } from "node:path"
|
|
4
|
+
import { tmpdir } from "node:os"
|
|
5
|
+
import { resolveTarget } from "../src/resolve-target.js"
|
|
6
|
+
import { resolveHostEngineDatabaseUrl } from "../src/dev-compose.js"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A command that runs the engine on the host has to reach the database at an address the host can
|
|
10
|
+
* resolve.
|
|
11
|
+
*
|
|
12
|
+
* `supatype adopt` on a docker project failed looking up the host `db` (supatype#85): direct mode
|
|
13
|
+
* took `.supatype/environment.json`'s URL, which describes the `db` *container*. `doctor` and
|
|
14
|
+
* `pull` read `process.env` and never the project's `.env`, where `supatype dev` writes the
|
|
15
|
+
* host-published URL. Every one now goes through the resolver seed already used.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const CONTAINER_URL = "postgresql://supatype_admin:pw@db:5432/supatype?sslmode=disable"
|
|
19
|
+
const HOST_URL = "postgresql://supatype_admin:pw@localhost:54329/supatype?sslmode=disable"
|
|
20
|
+
|
|
21
|
+
let tmp: string
|
|
22
|
+
let previousDatabaseUrl: string | undefined
|
|
23
|
+
|
|
24
|
+
function dockerProject(dir: string): void {
|
|
25
|
+
writeFileSync(
|
|
26
|
+
join(dir, "supatype.config.ts"),
|
|
27
|
+
`export default ${JSON.stringify({
|
|
28
|
+
project: { name: "demo" },
|
|
29
|
+
database: { provider: "docker" },
|
|
30
|
+
server: { mode: "dev" },
|
|
31
|
+
app: { mode: "none" },
|
|
32
|
+
schema: { path: "schema/index.ts", pg_schema: "public" },
|
|
33
|
+
})}`,
|
|
34
|
+
)
|
|
35
|
+
mkdirSync(join(dir, ".supatype"), { recursive: true })
|
|
36
|
+
// What `supatype dev` leaves behind: the container URL for containers, the host URL in `.env`.
|
|
37
|
+
writeFileSync(
|
|
38
|
+
join(dir, ".supatype", "environment.json"),
|
|
39
|
+
JSON.stringify({
|
|
40
|
+
target: "local",
|
|
41
|
+
apiUrl: "http://127.0.0.1:18473",
|
|
42
|
+
databaseUrl: CONTAINER_URL,
|
|
43
|
+
projectRef: "demo",
|
|
44
|
+
kongPort: 18473,
|
|
45
|
+
provider: "docker",
|
|
46
|
+
}),
|
|
47
|
+
)
|
|
48
|
+
writeFileSync(join(dir, ".env"), `DATABASE_URL=${HOST_URL}\n`)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
tmp = mkdtempSync(join(tmpdir(), "supatype-host-db-"))
|
|
53
|
+
previousDatabaseUrl = process.env["DATABASE_URL"]
|
|
54
|
+
delete process.env["DATABASE_URL"]
|
|
55
|
+
dockerProject(tmp)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
afterEach(() => {
|
|
59
|
+
if (previousDatabaseUrl === undefined) delete process.env["DATABASE_URL"]
|
|
60
|
+
else process.env["DATABASE_URL"] = previousDatabaseUrl
|
|
61
|
+
rmSync(tmp, { recursive: true, force: true })
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
describe("a host-side command on a docker project", () => {
|
|
65
|
+
it("connects direct mode to the host-published database, not the container (#85)", () => {
|
|
66
|
+
const target = resolveTarget(tmp, { direct: true })
|
|
67
|
+
expect(target.databaseUrl).toBe(HOST_URL)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it("does the same for the local target used when there is no service key", () => {
|
|
71
|
+
const target = resolveTarget(tmp)
|
|
72
|
+
expect(target.mode).toBe("local")
|
|
73
|
+
expect(target.databaseUrl).toBe(HOST_URL)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it("reads the project's .env for doctor and pull, not only the process environment", async () => {
|
|
77
|
+
expect(await resolveHostEngineDatabaseUrl(tmp, (await import("../src/config.js")).loadConfig(tmp))).toBe(HOST_URL)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it("still takes --connection over everything", () => {
|
|
81
|
+
const explicit = "postgresql://someone:else@example.test:5432/app"
|
|
82
|
+
expect(resolveTarget(tmp, { direct: true, connection: explicit }).databaseUrl).toBe(explicit)
|
|
83
|
+
})
|
|
84
|
+
})
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Which database a seed
|
|
2
|
+
* Which database a host-side command (seed, adopt, doctor, migrate, ...) connects to, and what it
|
|
3
|
+
* says when it cannot tell.
|
|
3
4
|
*
|
|
4
5
|
* The error is the reason this is a file of its own. The old seed template carried a
|
|
5
6
|
* hardcoded fallback DSN, so a project with no `.env` did not fail: it tried
|
|
@@ -13,7 +14,7 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest"
|
|
|
13
14
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
|
|
14
15
|
import { tmpdir } from "node:os"
|
|
15
16
|
import { join } from "node:path"
|
|
16
|
-
import { DsnNotFound, redact,
|
|
17
|
+
import { DsnNotFound, redact, resolveHostDatabaseUrl } from "../src/host-database.js"
|
|
17
18
|
import type { SupatypeProjectConfig } from "../src/project-config.js"
|
|
18
19
|
|
|
19
20
|
const SECRET = "postgres://user:s3cret@db.example.test:5432/app"
|
|
@@ -45,7 +46,7 @@ afterEach(() => {
|
|
|
45
46
|
describe("where the connection string comes from", () => {
|
|
46
47
|
it("takes --connection first, because it was typed just now", () => {
|
|
47
48
|
process.env["DATABASE_URL"] = "postgres://env/one"
|
|
48
|
-
const resolved =
|
|
49
|
+
const resolved = resolveHostDatabaseUrl(cwd, config(), { connection: SECRET })
|
|
49
50
|
expect(resolved.dsn).toBe(SECRET)
|
|
50
51
|
expect(resolved.source).toBe("--connection")
|
|
51
52
|
})
|
|
@@ -56,7 +57,7 @@ describe("where the connection string comes from", () => {
|
|
|
56
57
|
*/
|
|
57
58
|
it("prefers a declared external database over DATABASE_URL", () => {
|
|
58
59
|
process.env["DATABASE_URL"] = "postgres://env/one"
|
|
59
|
-
const resolved =
|
|
60
|
+
const resolved = resolveHostDatabaseUrl(
|
|
60
61
|
cwd,
|
|
61
62
|
// `provider` is omitted when `external` is set: there is no backend to choose.
|
|
62
63
|
config({ database: { external: { url: SECRET } } } as Partial<SupatypeProjectConfig>),
|
|
@@ -66,20 +67,20 @@ describe("where the connection string comes from", () => {
|
|
|
66
67
|
|
|
67
68
|
it("takes `connection` from the config before the environment", () => {
|
|
68
69
|
process.env["DATABASE_URL"] = "postgres://env/one"
|
|
69
|
-
const resolved =
|
|
70
|
+
const resolved = resolveHostDatabaseUrl(cwd, config({ connection: SECRET } as Partial<SupatypeProjectConfig>))
|
|
70
71
|
expect(resolved.dsn).toBe(SECRET)
|
|
71
72
|
})
|
|
72
73
|
|
|
73
74
|
it("takes DATABASE_URL from the environment", () => {
|
|
74
75
|
process.env["DATABASE_URL"] = SECRET
|
|
75
|
-
const resolved =
|
|
76
|
+
const resolved = resolveHostDatabaseUrl(cwd, config())
|
|
76
77
|
expect(resolved.dsn).toBe(SECRET)
|
|
77
78
|
expect(resolved.source).toBe("DATABASE_URL in the environment")
|
|
78
79
|
})
|
|
79
80
|
|
|
80
81
|
it("falls back to the project's .env", () => {
|
|
81
82
|
writeFileSync(join(cwd, ".env"), `DATABASE_URL=${SECRET}\n`, "utf8")
|
|
82
|
-
const resolved =
|
|
83
|
+
const resolved = resolveHostDatabaseUrl(cwd, config())
|
|
83
84
|
expect(resolved.dsn).toBe(SECRET)
|
|
84
85
|
expect(resolved.source).toContain(".env")
|
|
85
86
|
})
|
|
@@ -88,25 +89,42 @@ describe("where the connection string comes from", () => {
|
|
|
88
89
|
it("lets a real environment variable beat the file", () => {
|
|
89
90
|
writeFileSync(join(cwd, ".env"), "DATABASE_URL=postgres://file/one\n", "utf8")
|
|
90
91
|
process.env["DATABASE_URL"] = "postgres://env/two"
|
|
91
|
-
expect(
|
|
92
|
+
expect(resolveHostDatabaseUrl(cwd, config()).dsn).toBe("postgres://env/two")
|
|
92
93
|
})
|
|
93
94
|
|
|
94
|
-
it("derives
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
it("derives the published compose database for a docker project", () => {
|
|
96
|
+
// Not the native layout: a docker stack has no `launch-test` database on 5432. It runs
|
|
97
|
+
// `supatype` as `supatype_admin` on the port `supatype dev` publishes.
|
|
98
|
+
const resolved = resolveHostDatabaseUrl(cwd, config(), { allowDerived: true })
|
|
99
|
+
expect(resolved.dsn).toMatch(/^postgresql:\/\/supatype_admin:[^@]+@127\.0\.0\.1:54329\/supatype\?sslmode=disable$/)
|
|
97
100
|
expect(resolved.source).toBe("the project's own local database")
|
|
98
101
|
})
|
|
102
|
+
|
|
103
|
+
it("reads the published port and credentials the stack was created with", () => {
|
|
104
|
+
writeFileSync(
|
|
105
|
+
join(cwd, ".env"),
|
|
106
|
+
"SUPATYPE_DEV_DB_PORT=55555\nPOSTGRES_USER=owner\nPOSTGRES_PASSWORD=pw\nPOSTGRES_DB=app\n",
|
|
107
|
+
)
|
|
108
|
+
const resolved = resolveHostDatabaseUrl(cwd, config(), { allowDerived: true })
|
|
109
|
+
expect(resolved.dsn).toBe("postgresql://owner:pw@127.0.0.1:55555/app?sslmode=disable")
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it("derives the native layout for a native project", () => {
|
|
113
|
+
const native = config({ database: { provider: "native" } } as Partial<SupatypeProjectConfig>)
|
|
114
|
+
const resolved = resolveHostDatabaseUrl(cwd, native, { allowDerived: true })
|
|
115
|
+
expect(resolved.dsn).toContain("launch-test")
|
|
116
|
+
})
|
|
99
117
|
})
|
|
100
118
|
|
|
101
119
|
describe("when nothing names a database", () => {
|
|
102
120
|
it("refuses rather than guessing", () => {
|
|
103
|
-
expect(() =>
|
|
121
|
+
expect(() => resolveHostDatabaseUrl(cwd, config())).toThrow(DsnNotFound)
|
|
104
122
|
})
|
|
105
123
|
|
|
106
124
|
it("names every source it tried, and what was at each", () => {
|
|
107
125
|
let message = ""
|
|
108
126
|
try {
|
|
109
|
-
|
|
127
|
+
resolveHostDatabaseUrl(cwd, config())
|
|
110
128
|
} catch (e) {
|
|
111
129
|
message = e instanceof Error ? e.message : String(e)
|
|
112
130
|
}
|
|
@@ -127,7 +145,7 @@ describe("when nothing names a database", () => {
|
|
|
127
145
|
writeFileSync(join(cwd, ".env"), "SOMETHING_ELSE=1\n", "utf8")
|
|
128
146
|
let message = ""
|
|
129
147
|
try {
|
|
130
|
-
|
|
148
|
+
resolveHostDatabaseUrl(cwd, config())
|
|
131
149
|
} catch (e) {
|
|
132
150
|
message = e instanceof Error ? e.message : String(e)
|
|
133
151
|
}
|
|
@@ -151,7 +169,7 @@ describe("what reaches the terminal", () => {
|
|
|
151
169
|
|
|
152
170
|
it("does not print the password while explaining where it looked", () => {
|
|
153
171
|
process.env["DATABASE_URL"] = SECRET
|
|
154
|
-
const resolved =
|
|
172
|
+
const resolved = resolveHostDatabaseUrl(cwd, config())
|
|
155
173
|
const printed = resolved.tried.map((source) => source.detail).join("\n")
|
|
156
174
|
expect(printed).not.toContain("s3cret")
|
|
157
175
|
})
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Command } from "commander"
|
|
2
|
+
import { describe, expect, it, vi } from "vitest"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `--no-cache` on `adopt` and `doctor` does nothing now the database is always read live. A script
|
|
6
|
+
* that passes it must keep working, be told, and not see the flag offered in help.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const warned: string[] = []
|
|
10
|
+
vi.mock("../src/ui/messages.js", () => ({ warn: (line: string) => warned.push(line) }))
|
|
11
|
+
|
|
12
|
+
const { addRetiredNoCacheOption, warnIfRetiredNoCache } = await import("../src/retired-no-cache.js")
|
|
13
|
+
|
|
14
|
+
function parse(args: string[]): { cache?: boolean } {
|
|
15
|
+
const program = new Command().exitOverride()
|
|
16
|
+
let seen: { cache?: boolean } = {}
|
|
17
|
+
addRetiredNoCacheOption(program.command("adopt")).action((opts: { cache?: boolean }) => {
|
|
18
|
+
seen = opts
|
|
19
|
+
})
|
|
20
|
+
program.parse(["node", "supatype", "adopt", ...args])
|
|
21
|
+
return seen
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("the retired --no-cache flag", () => {
|
|
25
|
+
it("is still accepted, so a script passing it does not break", () => {
|
|
26
|
+
expect(() => parse(["--no-cache"])).not.toThrow()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it("says it is no longer needed, read the way Commander stores it", () => {
|
|
30
|
+
warned.length = 0
|
|
31
|
+
warnIfRetiredNoCache(parse(["--no-cache"]))
|
|
32
|
+
expect(warned.join("\n")).toContain("--no-cache is no longer needed")
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it("says nothing when it was not passed", () => {
|
|
36
|
+
warned.length = 0
|
|
37
|
+
warnIfRetiredNoCache(parse([]))
|
|
38
|
+
expect(warned).toEqual([])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it("is not offered in help", () => {
|
|
42
|
+
const program = new Command()
|
|
43
|
+
const adopt = addRetiredNoCacheOption(program.command("adopt"))
|
|
44
|
+
expect(adopt.helpInformation()).not.toContain("--no-cache")
|
|
45
|
+
})
|
|
46
|
+
})
|