Skip to content

Improve CLI error handling for missing database credentials - #6

Merged
jalexw merged 5 commits into
mainfrom
claude/cli-migration-env-errors-pqmlru
Jul 28, 2026
Merged

jalexw merged 5 commits into
mainfrom
claude/cli-migration-env-errors-pqmlru

Conversation

@jalexw

@jalexw jalexw commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Refactored database credential validation to provide clear, actionable error messages when required environment variables are missing. Instead of failing on the first missing variable with a generic error, the CLI now reports all missing credentials at once and guides users on how to provide them.

Key Changes

  • New error class (MissingDatabaseCredentialsError): Captures all missing required credentials at once rather than failing on the first one. Includes a missing property listing every absent key.

  • Refactored credential parsing (parseDatabaseCredentials): Collects all required credentials before validation, then throws MissingDatabaseCredentialsError with the complete list of missing keys if any are absent.

  • Enhanced CLI error handling:

    • New createCliDbhOrExit() function catches MissingDatabaseCredentialsError and prints a formatted table showing which credentials are missing vs. set, along with guidance on how to provide them (environment variables or --env-file).
    • New loadCliEnvFileOrExit() function wraps env file loading with clean error messages instead of stack traces.
    • Both migrate and reverse commands now use these helpers.
  • Comprehensive test coverage:

    • Unit tests for parseDatabaseCredentials validating error behavior with partial/complete missing credentials.
    • Integration tests for CLI commands verifying graceful failure, helpful error messages, and proper env file handling.

Implementation Details

  • The REQUIRED_POSTGRES_CREDENTIAL_KEYS constant defines the four mandatory credentials: POSTGRES_HOST, POSTGRES_DATABASE, POSTGRES_USER, POSTGRES_PASSWORD.
  • Optional credentials (POSTGRES_PORT, POSTGRES_URL, POSTGRES_URL_NON_POOLING) are handled separately and do not trigger errors when absent.
  • Error messages are exported from the public API so library consumers can also handle MissingDatabaseCredentialsError programmatically.
  • CLI output avoids stack traces and instead provides a structured list of missing variables with hints for resolution.

https://claude.ai/code/session_01RDMoz2eWFyzx5rry6i739k

claude and others added 4 commits July 23, 2026 19:32
Previously, running 'dbh migrate' or 'dbh reverse' without the required
POSTGRES_* environment variables dumped a raw stack trace that only
mentioned the first missing variable, forcing users to fix them one at
a time.

- parseDatabaseCredentials now collects every missing required credential
  (POSTGRES_HOST, POSTGRES_DATABASE, POSTGRES_USER, POSTGRES_PASSWORD)
  and throws a single MissingDatabaseCredentialsError listing all of them
- The CLI's migrate/reverse commands catch adapter-initialization errors
  and print a clean, actionable message: which variables are missing vs
  set, which are optional, and how to provide them (env vars or
  --env-file), with no stack trace
- A missing --env-file path now also exits with a clean error message
- MissingDatabaseCredentialsError and REQUIRED_POSTGRES_CREDENTIAL_KEYS
  are exported from the package root so consumers can catch it
- Add unit tests for the aggregated error and the CLI failure modes

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RDMoz2eWFyzx5rry6i739k
Make POSTGRES_URL required
Credential parsing previously required POSTGRES_HOST, POSTGRES_DATABASE,
POSTGRES_USER, and POSTGRES_PASSWORD even when a full POSTGRES_URL
connection string was provided. Credentials are now satisfied by EITHER
a POSTGRES_URL (host, database, user, password, and port are derived
from it for any component not set individually) OR the four component
variables (from which POSTGRES_URL is constructed, as before).

- Add parsePostgresUrl util: extracts components from postgres:// or
  postgresql:// URLs, decoding percent-encoded userinfo; throws a
  descriptive error for malformed URLs, but only when the URL is
  actually needed to fill missing components (a pass-through connection
  string with fully-specified component variables still works)
- Individually-set variables take precedence over URL components; port
  precedence is POSTGRES_PORT > port in POSTGRES_URL > 5432, and a
  numeric POSTGRES_PORT in a credentials object is now honored
- Fix unit tests broken by adding POSTGRES_URL to
  REQUIRED_POSTGRES_CREDENTIAL_KEYS: parseDatabaseCredentials now
  resolves that key, and MissingDatabaseCredentialsError explains the
  either/or requirement while still listing every unresolved key
- Update the CLI missing-credentials message to present both options
  with per-variable (set)/(missing) status
- Relax BaseInitializablePostgresDatabaseCredentials so URL-only
  credentials objects typecheck; export the new component-keys constant
- Extend unit tests: URL-only parsing, precedence, incomplete URLs,
  malformed URLs, scheme validation, and adapter init from URL only

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RDMoz2eWFyzx5rry6i739k
@jalexw jalexw self-assigned this Jul 28, 2026
- package.json: 0.12.1 -> 0.13.0
- README: update the postgres-ws-proxy image version example to match

Also correct the README's "Required Environment Variables" section,
which still described all POSTGRES_* component variables as required.
Credentials are now satisfied by either POSTGRES_URL alone or the four
component variables, so document both options, how they merge, and move
POSTGRES_PORT / POSTGRES_URL_NON_POOLING into the optional section
(merging the two optional-variable blocks into one).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RDMoz2eWFyzx5rry6i739k
@jalexw
jalexw merged commit b1d815b into main Jul 28, 2026
9 checks passed
@jalexw
jalexw deleted the claude/cli-migration-env-errors-pqmlru branch July 28, 2026 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants