Skip to content
Closed
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
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ npm install pgstrap --save-dev
npm run db:migrate
```

6. Generate types and structure:
6. Generate types and structure without starting a separate PostgreSQL server:
```bash
npm run db:generate
```
Expand All @@ -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` - Run migrations against in-memory PGlite and generate types and structure dumps. Use `pgstrap generate --no-pglite` to use an externally running PostgreSQL database instead.
- `npm run db:create-migration` - Create a new migration file

### Configuration
Expand Down Expand Up @@ -96,7 +96,7 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
}

export async function down(pgm: MigrationBuilder): Promise<void> {
// Code to revert your migration (optional)
// Code to revert the migration (optional)
}
```

Expand All @@ -116,7 +116,6 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
default: pgm.func("current_timestamp"),
},
})

pgm.createIndex("users", "username")
}

Expand All @@ -142,4 +141,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file

If you encounter any issues or have questions, please file an issue on the [GitHub repository](https://github.com/seveibar/pgstrap/issues).

Happy coding with pgstrap! 🚀
Happy coding with pgstrap!
11 changes: 9 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ 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",
describe:
"run migrations in an in-memory PGlite database (enabled by default; use --no-pglite for an external Postgres database)",
})
},
async (argv) => {
generate({ ...(await getProjectContext()), pglite: !!argv.pglite })
generate({
...(await getProjectContext()),
pglite: argv.pglite as boolean | undefined,
})
},
)
.parse()
6 changes: 4 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { dumpTree } from "pg-schema-dump"
import path from "path"
import { migrate } from "./migrate"

export const shouldUsePglite = (pglite?: boolean) => pglite !== false

export const generate = async ({
schemas,
defaultDatabase,
dbDir,
pglite = false,
pglite,
migrationsDir,
}: Pick<Context, "schemas" | "defaultDatabase" | "dbDir"> & {
pglite?: boolean
Expand All @@ -21,7 +23,7 @@ export const generate = async ({
dbDir = dbDir ?? "./src/db"
migrationsDir = migrationsDir ?? path.join(dbDir, "migrations")

if (pglite) {
if (shouldUsePglite(pglite)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore PGlite state when generation fails

When pglite is omitted, this now sends normal generate() calls into the PGlite branch, but that branch mutates process.env.DATABASE_URL and opens a TCP server and only restores/closes them after both zg.generate and dumpTree succeed. In a programmatic caller or test runner that catches a generation error, such as a schema dump failure, the process is left pointed at the transient PGlite URL with the server still listening, so later DB work can hit the wrong database or the process can hang; wrap the PGlite work in try/finally before making it the default.

Useful? React with 👍 / 👎.

const { PGlite } = await import("@electric-sql/pglite")
const { fromNodeSocket } = await import("pg-gateway/node")
const net = await import("node:net")
Expand Down
8 changes: 8 additions & 0 deletions tests/generate-mode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from "bun:test"
import { shouldUsePglite } from "../src/generate"

test("type generation uses PGlite unless external Postgres is explicitly requested", () => {
expect(shouldUsePglite()).toBe(true)
expect(shouldUsePglite(true)).toBe(true)
expect(shouldUsePglite(false)).toBe(false)
})