Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli)!: ensure env vars are set before migrating #1222

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .github/workflows/postgres-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ jobs:
- name: Integration Test
run: pnpm run migrate up -m test/migrations && pnpm run migrate down 0 -m test/migrations --timestamps
env:
PGHOST: localhost
PGUSER: ubuntu
PGPASSWORD: 123456abcdefghABCDEFGH~\`\!@#$%^\&*-_=+{}[]\(\)\<\>,.\;:\"\'?\|/\\
PGDATABASE: integration_test
Expand Down
7 changes: 5 additions & 2 deletions bin/node-pg-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,12 @@ if (action === 'create') {
if (!DB_CONNECTION) {
const cp = new ConnectionParameters();

if (!cp.host && !cp.port && !cp.database) {
if (
!process.env[argv[databaseUrlVarArg]] &&
(!process.env.PGHOST || !cp.user || !cp.database)
Copy link
Collaborator

Choose a reason for hiding this comment

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

question (if-minor): you removed the check for !cp.host && !cp.port, but added !cp.user and used || (or). Can you explain a bit why you did it like that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cp.host and cp.port are unlikely to be undefined since we read pg defaults, which set host to 'localhost' and port to '5432' if missing.
When running pg locally without any env vars, node-pg-migrate would run but give an error due to an undefined password string/function. Adding a check for the pw alone isn't enough because it would still cause a connection error if pg isn't running locally (also when pw is set but others vars aren't).
so, if there's no dburl, I check if any required variable or process.env.PGHOST is missing and stop it

) {
console.error(
`The $${argv[databaseUrlVarArg]} environment variable is not set.`
`The ${argv[databaseUrlVarArg]} environment variable is not set or incomplete connection parameters are provided.`
);
process.exit(1);
}
Expand Down