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

Merged
JA
2mo ago
+42-1

Closes #168.

The defect

config/database.yml used the key user: where Rails' key is username:.

The PostgreSQL adapter passes keys it does not recognise straight through to libpq, and libpq does understand user — so Rails connected fine and nothing looked broken. But configuration_hash carried :user and no :username:

username=nil  user="postgres"

RecurringTransaction::PipelineTest opens a genuinely separate PG session — it must, because a session can always re-take its own advisory lock and transactional tests share one connection — and reads config[:username], the documented key. That was nil, .compact dropped it, and libpq fell back to the OS user. In the dev container that is root:

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

CI never saw it: the workflow sets PGUSER, which libpq reads before it reaches the fallback. That is why this survived — it is invisible everywhere except a local run.

Why it was worth fixing now

Not the one error. A full local run never came back clean, which teaches everyone to read "1 errors" as normal — precisely the state in which a real regression goes unnoticed. I have spent this session reporting "0 failures, 1 error — environmental" on five different branches.

Full suite after the fix: 9892 runs, 41177 assertions, 0 failures, 0 errors.

The one-line fix, and the guard that is deliberately not belt-and-braces

-  user: <%= ENV.fetch("POSTGRES_USER") { nil } %>
+  username: <%= ENV.fetch("POSTGRES_USER") { nil } %>

The issue suggested optionally hardening the test with config[:username] || config[:user]. I have deliberately not done that. It would make the test pass again if the key regressed — which is exactly the failure mode that hid this for months. A fallback that masks a config defect is how the defect stays hidden.

Instead a new test asserts the config directly:

assert config.key?(:username), "database.yml must use `username:`, not `user:` ..."
assert_nil config[:user], "`user:` is not a Rails config key; use `username:`"

So a revert fails with a sentence saying why, rather than with a confusing authentication error three files away.

Evidence

  • Before: bin/rails test test/models/recurring_transaction/pipeline_test.rb in the dev container — 4 runs, 1 error.
  • After: 5 runs, 0 errors (4 original plus the new guard).
  • Mutation: reverting the key to user: reproduces the original FATAL: password authentication failed for user "root" and fails the new guard — so both the symptom and the explanation come back together.
  • Full suite: 0 failures, 0 errors.

Blast radius — confirmed, not assumed

The issue and the Gatekeeper review both flagged the single-reader claim as the one unverified item. Checked:

  • configuration_hash has exactly one reader in the repository — the test above.
  • Nothing templates database.yml. The Helm chart, compose.example.yml, the .env examples and the hosting docs all set POSTGRES_USER as an environment variable and never read this key.

The Rails connection itself is unaffected either way: it already worked, via libpq's pass-through, and now works via the adapter's own mapping.