db:generate works without an external Postgres via auto PGlite (#2)#32
Open
quantamShade0337 wants to merge 2 commits into
Open
db:generate works without an external Postgres via auto PGlite (#2)#32quantamShade0337 wants to merge 2 commits into
quantamShade0337 wants to merge 2 commits into
Conversation
…eveibar#2) When no external DB env is configured, generate now starts an in-process PGlite, runs migrations, serves it over a local pg-gateway wire port, and points Zapatos + pg-schema-dump at it (then tears down). External Postgres path preserved when DB env vars are present. Adds the missing db:generate script and awaits the async CLI calls. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates generate to automatically fall back to an in-memory PGlite database when no Postgres connection environment is configured, and aligns CLI/docs/scripts with this behavior.
Changes:
- Add detection of external Postgres env config and auto-run PGlite migrations/generation when absent.
- Improve resource cleanup in PGlite generation and ensure CLI subcommands
awaitasync work. - Update tests, README, and package scripts to reflect the new default behavior.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/generate.pglite.test.ts | Adjusts the generation test to verify implicit PGlite usage when Postgres env vars are absent, with env restore logic. |
| src/generate.ts | Adds env-based fallback to PGlite and refactors PGlite generation into a dedicated function with cleanup. |
| src/cli.ts | Ensures migrate and generate commands await async operations. |
| package.json | Adds a db:generate script for running the CLI generate command. |
| bun.lock | Updates lockfile metadata (configVersion). |
| README.md | Documents the new automatic PGlite fallback behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
69
to
71
| await new Promise<void>((resolve) => server.listen(0, resolve)) | ||
| const port = (server.address() as any).port | ||
| const connectionString = `postgres://postgres:postgres@127.0.0.1:${port}/postgres` |
| }) | ||
|
|
||
| const db = new PGlite() | ||
| const prevDbUrl = process.env.DATABASE_URL |
| const connectionString = `postgres://postgres:postgres@127.0.0.1:${port}/postgres` | ||
|
|
||
| const prevDbUrl = process.env.DATABASE_URL | ||
| process.env.DATABASE_URL = connectionString |
Comment on lines
91
to
92
| if (prevDbUrl === undefined) delete process.env.DATABASE_URL | ||
| else process.env.DATABASE_URL = prevDbUrl |
Comment on lines
+8
to
+15
| const postgresEnvKeys = [ | ||
| "DATABASE_URL", | ||
| "PGHOST", | ||
| "PGPORT", | ||
| "PGDATABASE", | ||
| "PGUSER", | ||
| "PGPASSWORD", | ||
| ] |
Comment on lines
+46
to
+54
| async onMessage(data: Uint8Array, { isAuthenticated }: any) { | ||
| if (!isAuthenticated) return | ||
| try { | ||
| const { data: responseData } = await (db as any).execProtocol(data) | ||
| return responseData | ||
| } catch { | ||
| return undefined | ||
| } | ||
| }, |
Comment on lines
26
to
27
| const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pgstrap-generate-")) | ||
| const migrationsDir = path.join(tmp, "migrations") |
Comment on lines
34
to
+40
| fs.mkdirSync(migrationsDir, { recursive: true }) | ||
| fs.writeFileSync( | ||
| path.join(migrationsDir, "001_create_table.js"), | ||
| migrationFile, | ||
| ) | ||
|
|
||
| await generate({ | ||
| schemas: ["public"], | ||
| defaultDatabase: "postgres", | ||
| dbDir: path.join(tmp, "db"), | ||
| migrationsDir, | ||
| pglite: true, | ||
| }) | ||
|
|
||
| const zapatosFile = path.join(tmp, "db", "zapatos", "schema.d.ts") | ||
| const structureDir = path.join( | ||
| tmp, | ||
| "db", | ||
| "structure", | ||
| "public", | ||
| "tables", | ||
| "foo", | ||
| ) | ||
| try { |
Comment on lines
+118
to
126
| if (pglite || !hasExternalDatabaseConfig()) { | ||
| await generateWithPglite({ | ||
| schemas, | ||
| defaultDatabase, | ||
| dbDir, | ||
| migrationsDir, | ||
| }) | ||
| return | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2 —
bun run db:generateno longer requires a running Postgres.When no external DB connection is configured (no
DATABASE_URL/PGHOST/PGPORT/PGDATABASE/PGUSER/PGPASSWORD),generatenow automatically:pg-schema-dumpat it,DATABASE_URL, closes the server + PGlite).The existing external-Postgres path is preserved when DB env vars are present. Also added the missing
db:generatescript (bun ./src/cli.ts generate) and awaited the asyncgenerate/migratecalls in the CLI.Verified:
bun install && bun test→ 3 pass / 0 fail, including the updatedtests/generate.pglite.test.tswhich clears Postgres env vars and runsgenerate()end-to-end against in-process PGlite./claim #2