Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ npm install pgstrap --save-dev

- `npm run db:migrate` - Run pending migrations
- `npm run db:reset` - Drop and recreate the database, then run all migrations
- `npm run db:generate` - Generate types and structure dumps. Use `pgstrap generate --pglite` to run migrations against an in-memory PGlite instance.
- `npm run db:generate` - Generate types and structure dumps from migrations using an in-memory PGlite instance, without a running PostgreSQL server. Use `pgstrap generate --no-pglite` to generate from your configured PostgreSQL database instead.
- `npm run db:create-migration` - Create a new migration file

### Configuration
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { getProjectContext } from "./get-project-context"
"generate",
"generate types and sql documentation from database",
(yargs) => {
yargs.option("pglite", { type: "boolean", default: false })
yargs.option("pglite", { type: "boolean", default: true })
},
async (argv) => {
generate({ ...(await getProjectContext()), pglite: !!argv.pglite })
Expand Down
2 changes: 1 addition & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const generate = async ({
})
})

await new Promise<void>((resolve) => server.listen(0, resolve))
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve))
const port = (server.address() as any).port
const connectionString = `postgres://postgres:postgres@127.0.0.1:${port}/postgres`

Expand Down
5 changes: 4 additions & 1 deletion src/get-project-context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "fs"
import path from "path"
import { pathToFileURL } from "url"
import { PgstrapConfig } from "./define-config"

export interface Context extends PgstrapConfig {
Expand All @@ -13,7 +14,9 @@ export const getProjectContext = async (): Promise<Context> => {
)
}

const config = await import(path.join(process.cwd(), "pgstrap.config.js"))
const config = await import(
pathToFileURL(path.join(process.cwd(), "pgstrap.config.js")).href
)

return {
cwd: process.cwd(),
Expand Down
87 changes: 87 additions & 0 deletions tests/generate.cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { test, expect } from "bun:test"
import fs from "fs"
import os from "os"
import path from "path"

test("generate defaults to PGlite without a running PostgreSQL server", async () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pgstrap-cli-"))
const dbDir = path.join(tmp, "db")
const migrationsDir = path.join(dbDir, "migrations")
fs.mkdirSync(migrationsDir, { recursive: true })
fs.writeFileSync(
path.join(tmp, "pgstrap.config.js"),
`module.exports = ${JSON.stringify({ schemas: ["public"], defaultDatabase: "postgres", dbDir })}`,
)
fs.writeFileSync(
path.join(migrationsDir, "001_create_table.js"),
`exports.up = (pgm) => pgm.createTable("cli_items", { id: "id" })\nexports.down = (pgm) => pgm.dropTable("cli_items")`,
)

const child = Bun.spawn(
[
process.execPath,
"run",
path.join(import.meta.dir, "../src/cli.ts"),
"generate",
],
{
cwd: tmp,
env: {
...process.env,
DATABASE_URL: "postgres://postgres:postgres@127.0.0.1:1/postgres",
},
stdout: "pipe",
stderr: "pipe",
},
)

const timeout = setTimeout(() => child.kill(), 45_000)
try {
const [exitCode, stdout, stderr] = await Promise.all([
child.exited,
new Response(child.stdout).text(),
new Response(child.stderr).text(),
])
expect({ exitCode, stdout, stderr }).toMatchObject({ exitCode: 0 })
expect(fs.existsSync(path.join(dbDir, "zapatos", "schema.d.ts"))).toBe(true)
expect(
fs.readFileSync(path.join(dbDir, "zapatos", "schema.d.ts"), "utf8"),
).toContain("cli_items")
expect(
fs.existsSync(
path.join(
dbDir,
"structure",
"public",
"tables",
"cli_items",
"table.sql",
),
),
).toBe(true)
expect(
fs.readFileSync(
path.join(
dbDir,
"structure",
"public",
"tables",
"cli_items",
"table.sql",
),
"utf8",
),
).toContain("cli_items")
} finally {
clearTimeout(timeout)
child.kill()
const resolved = path.resolve(tmp)
if (
!resolved.startsWith(path.resolve(os.tmpdir()) + path.sep) ||
!path.basename(resolved).startsWith("pgstrap-cli-")
) {
throw new Error("Unexpected temporary directory")
}
fs.rmSync(resolved, { recursive: true, force: true })
}
}, 60_000)