Use the config key Rails documents, so a local run comes back clean

Merged
MI
2mo ago
+42-1

Credit: this is @jaysbeekay's fix, ported unchanged from their fork: jaysbeekay/sure#196 (commit f7c5bb573), which resolved jaysbeekay/sure#168. It never reached this repository, so upstream main still has the bug. The commit keeps @jaysbeekay as author and their original message.

Problem

config/database.yml sets user:, but Rails' key is username:. The PostgreSQL adapter passes unrecognised keys through to libpq, which understands user, so the app connects fine and nothing looks wrong. But configuration_hash has :user and no :username.

RecurringTransaction::PipelineTest "run_with_lock! refuses to stack on a held family lock" opens a separate PG session and reads config[:username]. That's nil, gets dropped by .compact, and libpq falls back to the OS user. In the devcontainer that's root:

PG::ConnectionBad: ... FATAL:  password authentication failed for user "root"

CI is green only because ci.yml sets PGUSER (and DATABASE_URL). Other contributors have been noting this error as "environmental" in their PRs (e.g. #3608, #3916).

Change

  • config/database.yml: user: → username:. The adapter maps it to libpq's user itself, so the app's connection is unchanged. configuration_hash has one reader in the repo (this test), and nothing templates database.yml; compose files, the chart and the docs set POSTGRES_USER as an env var.
  • Two guard tests in pipeline_test.rb:
    • one asserts the database.yml file itself uses username: (CI's DATABASE_URL hides the key in the resolved config);
    • one checks the resolved config, skipped when DATABASE_URL is set.
  • As jaysbeekay explains, this deliberately does not fall back to config[:username] || config[:user] in the test. That fallback would hide a regression of the key, which is how this bug went unnoticed. An earlier revision of this PR did use that fallback; it's replaced by this fix.

Validation (devcontainer, no DATABASE_URL)

  • pipeline_test.rb: 6 runs, 0 failures, 0 errors.
  • Temporarily reverting the key to user:: both guard tests fail ("database.yml must use username: …") and the lock test errors as before.
  • Full bin/rails test: 10431 runs, 0 failures, 1 error. The one error is the date-dependent ReportsControllerTest failure from #3908, which #3902 fixes; this PR doesn't touch it.
  • bin/rubocop on the test file: no offenses.

Refs #3908. Together with #3902, this resolves it.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Configuration
    • PostgreSQL connections now use the username setting, sourced from POSTGRES_USER and defaulting to nil. The previous user setting is no longer used in the default database configuration.
  • Tests
    • Added checks to confirm the default configuration and resolved connection settings use username rather than user. The resolved-settings check is skipped when DATABASE_URL is set.