Skip to content

Commit

Permalink
Merge pull request #386 from MerginMaps/fix-duplicate-sql
Browse files Browse the repository at this point in the history
Fix sql with '' regexp issue in similar to
  • Loading branch information
MarcelGeo authored Feb 26, 2025
2 parents 9666044 + a6c0178 commit 4f8f558
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions server/mergin/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,22 @@ def generate_username(cls, email: str) -> Optional[str]:
# additional check for reserved words
username = f"{username}0" if is_reserved_word(username) else username
# check if we already do not have existing usernames
suffix = db.session.execute(
query = db.session.execute(
text(
"""
SELECT
replace(username, :username, '0')::int AS suffix
FROM "user"
WHERE
username = :username OR
username SIMILAR TO :username'\d+'
username SIMILAR TO :username_like
ORDER BY replace(username, :username, '0')::int DESC
LIMIT 1;
"""
),
{"username": username},
).scalar()
{"username": username, "username_like": f"{username}\d+"},
)
suffix = query.scalar()
return username if suffix is None else username + str(int(suffix) + 1)

@classmethod
Expand Down

0 comments on commit 4f8f558

Please sign in to comment.