Skip to content

refactor(setup): extract DB user collision handling and normalize PostgreSQL suffixing#59094

Open
joshtrichards wants to merge 7 commits intomasterfrom
jtr/refactor-setup-collission-avoidance
Open

refactor(setup): extract DB user collision handling and normalize PostgreSQL suffixing#59094
joshtrichards wants to merge 7 commits intomasterfrom
jtr/refactor-setup-collission-avoidance

Conversation

@joshtrichards
Copy link
Copy Markdown
Member

@joshtrichards joshtrichards commented Mar 20, 2026

Summary

This refactor extracts the database user/role collision handling into dedicated helpers in the MySQL and PostgreSQL setup classes, reduces hidden mutation, and makes DB-specific naming constraints explicit.

What changed

  • extract userExists() helpers so existence checks no longer rely on implicit reads from $this->dbUser
  • extract findAvailableUsername() helpers to isolate collision-avoidance logic
  • make MySQL’s username-length limit explicit via a named constant
  • add explicit PostgreSQL identifier-length handling
  • move $this->dbUser assignment out of collision loops for better readability and less mutation
  • normalize PostgreSQL suffixing to use base, base1, base2, ... when collisions occur

Why

This makes the setup flow easier to follow, reduces duplicated loop logic, and makes future refactors in the database setup classes safer and easier to reason about.

Notes

  • MySQL/MariaDB username truncation and suffixing behavior is preserved
  • PostgreSQL collision handling is now implemented in a dedicated helper
  • PostgreSQL collision suffixing now starts at 1 instead of skipping to 2

Related: #59092

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

…() helpers

Signed-off-by: Josh <josh.t.richards@gmail.com>
…ame() helper

Signed-off-by: Josh <josh.t.richards@gmail.com>
@joshtrichards joshtrichards added this to the Nextcloud 34 milestone Mar 20, 2026
@joshtrichards joshtrichards added feature: install and update technical debt 🧱 🤔🚀 ♻️ refactor Refactor code (not a bug fix, not a feature just refactoring) feature: database Database related DB 3. to review Waiting for reviews labels Mar 20, 2026
@joshtrichards joshtrichards marked this pull request as ready for review March 20, 2026 14:32
@joshtrichards joshtrichards requested a review from a team as a code owner March 20, 2026 14:32
@joshtrichards joshtrichards requested review from ArtificialOwl, CarlSchwan, leftybournes, nickvergessen and provokateurin and removed request for a team March 20, 2026 14:32
@joshtrichards joshtrichards changed the title refactor(setup): extract userExists() and findAvailableUsername() helpers for MySQL and PostgreSQL refactor(setup): Untangles the collision-avoidance loops for database user creation (MySQL and PostgreSQL) Mar 21, 2026
@joshtrichards joshtrichards changed the title refactor(setup): Untangles the collision-avoidance loops for database user creation (MySQL and PostgreSQL) refactor(setup): Untangle the collision-avoidance loops for database user creation (MySQL and PostgreSQL) Mar 21, 2026
@joshtrichards joshtrichards changed the title refactor(setup): Untangle the collision-avoidance loops for database user creation (MySQL and PostgreSQL) refactor(setup): extract database-user collision handling helpers for MySQL and PostgreSQL Apr 13, 2026
PostgreSQL role collision handling now uses base, base1, base2, ... instead of base, base2, base3, ... . This aligns the suffixing scheme with the MySQL helper.

Signed-off-by: Josh <josh.t.richards@gmail.com>
Right now PostgreSQL appends suffixes without enforcing a maximum identifier length. This makes enforcement explicit for  robustness and symmetry with the MySQL implementation.

Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Match MySQL implementation

Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
@joshtrichards joshtrichards changed the title refactor(setup): extract database-user collision handling helpers for MySQL and PostgreSQL refactor(setup): extract DB user collision handling and normalize PostgreSQL suffixing Apr 13, 2026
$adminUser = substr('oc_' . $username, 0, 16 - $length) . $i;
$i++;
}
$this->dbUser = $this->findAvailableUsername($connection, 'oc_' . $username);
Copy link
Copy Markdown
Member

@MichaIng MichaIng Apr 24, 2026

Choose a reason for hiding this comment

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

Shall we fix #59514 right here?

Suggested change
$this->dbUser = $this->findAvailableUsername($connection, 'oc_' . $username);
$this->dbUser = $this->findAvailableUsername($connection, $username);

setupDatabase calls this with hardcoded oc_admin, so the prefix is doubled, resulting in a oc_oc_admin database user. Compare with PostgreSQL, where no additional oc_ is prepended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review Waiting for reviews feature: database Database related DB feature: install and update ♻️ refactor Refactor code (not a bug fix, not a feature just refactoring) technical debt 🧱 🤔🚀

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants