-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgenerate.pglite.test.ts
More file actions
94 lines (80 loc) · 2.6 KB
/
Copy pathgenerate.pglite.test.ts
File metadata and controls
94 lines (80 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { test, expect } from "bun:test"
import fs from "fs"
import os from "os"
import path from "path"
import { generate } from "../src/generate"
const migrationFile = `
exports.up = async (pgm) => {
pgm.createTable('foo', { id: 'id' })
}
exports.down = async (pgm) => {
pgm.dropTable('foo')
}
`
test("generate defaults to pglite and dumps structure without postgres", async () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pgstrap-generate-"))
const migrationsDir = path.join(tmp, "migrations")
const previousDatabaseUrl = process.env.DATABASE_URL
fs.mkdirSync(migrationsDir, { recursive: true })
fs.writeFileSync(
path.join(migrationsDir, "001_create_table.js"),
migrationFile,
)
process.env.DATABASE_URL = "postgres://postgres:postgres@127.0.0.1:1/missing"
try {
await generate({
schemas: ["public"],
defaultDatabase: "postgres",
dbDir: path.join(tmp, "db"),
migrationsDir,
})
const zapatosFile = path.join(tmp, "db", "zapatos", "schema.d.ts")
const structureDir = path.join(
tmp,
"db",
"structure",
"public",
"tables",
"foo",
)
expect(fs.existsSync(zapatosFile)).toBe(true)
expect(fs.existsSync(path.join(structureDir, "table.sql"))).toBe(true)
expect(process.env.DATABASE_URL).toBe(
"postgres://postgres:postgres@127.0.0.1:1/missing",
)
} finally {
if (previousDatabaseUrl === undefined) delete process.env.DATABASE_URL
else process.env.DATABASE_URL = previousDatabaseUrl
fs.rmSync(tmp, { recursive: true, force: true })
}
})
test("generate with pglite restores DATABASE_URL after failure", async () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pgstrap-generate-"))
const migrationsDir = path.join(tmp, "migrations")
const dbDir = path.join(tmp, "db")
const previousDatabaseUrl = process.env.DATABASE_URL
fs.mkdirSync(migrationsDir, { recursive: true })
fs.writeFileSync(
path.join(migrationsDir, "001_create_table.js"),
migrationFile,
)
fs.writeFileSync(dbDir, "not a directory")
process.env.DATABASE_URL = "postgres://existing:secret@localhost:5432/app"
try {
await expect(
generate({
schemas: ["public"],
defaultDatabase: "postgres",
dbDir,
migrationsDir,
}),
).rejects.toThrow()
expect(process.env.DATABASE_URL).toBe(
"postgres://existing:secret@localhost:5432/app",
)
} finally {
if (previousDatabaseUrl === undefined) delete process.env.DATABASE_URL
else process.env.DATABASE_URL = previousDatabaseUrl
fs.rmSync(tmp, { recursive: true, force: true })
}
})