Skip to content

refactor(setup/pgsql): improve db setup credential handling and code organization#58391

Draft
joshtrichards wants to merge 8 commits intomasterfrom
jtr/refactor-setup-pgsql
Draft

refactor(setup/pgsql): improve db setup credential handling and code organization#58391
joshtrichards wants to merge 8 commits intomasterfrom
jtr/refactor-setup-pgsql

Conversation

@joshtrichards
Copy link
Member

@joshtrichards joshtrichards commented Feb 17, 2026

Summary

Original code:

  • Had weird coupling for $this->dbUser / $this->dbPassword that made logic harder to follow, troubleshoot, or change without introducing problems (e.g., userExists() implicitly checked whatever was in $this->dbUser)
  • Used same code path for manual (pre-created by admin) credentials versus automatically generated credential mode
  • Didn't use any helpers making it harder to read/isolate

Improvements:

  1. Credential Management
    • Admin credentials now saved in local variables before mutation
    • Credentials generated entirely inside createDBUser() and only assigned to $this-> after success
    • userExists() now accepts explicit "dbUser" parameter
  2. Code Organization
    • Separates automatic vs. manual setup paths (new performAutomaticSetup() introduced + less conditional logic depth)
    • Extracted key bits of existing logic into new helpers: checkCanCreateRoles(), verifyDatabaseConnection(), grantCreateOnPublicSchema()
    • Single $connection object/variable; less confusion between connection scenarios and more explicit lifecycle management
  3. Observability
    • Better error context (user, database, exception details)
    • Some additional logging scenarios and clarity (e.g. when alternate username chosen)
    • Added 'app' => 'pgsql.setup' to all log contexts

In short:

  • Clearer separation of concerns and elimination of hidden state dependencies. Behavior preserved; credentials saved to config remain identical.
  • 172 lines -> 284 lines, but significantly more maintainable

Easiest to review probably by commit

TODO

  • ...

Checklist

Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
And consolidate credential generation logic in a single place

Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
…le admin credentials

Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant