Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: add database constraint to enforce minimum username length#19453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
cstyan merged 3 commits intomainfromcallum-username-constraint
Aug 21, 2025

Conversation

cstyan
Copy link
Contributor

Username length and format, via regex, are already enforced at the application layer, but we have some code paths with database queries where we could optimize away many of the DB query calls if we could be sure at the database level that the username is never an empty string.

For example:#19395

length and regex are already enforced at the application layer, but wehave some code paths with database queries where we could optimize awaymany of the DB query calls if we could be sure at the database levelthat the username is never an empty string.Signed-off-by: Callum Styan <callumstyan@gmail.com>
@johnstcn
Copy link
Member

=== FAIL: coderd/database TestUpdateSystemUser (0.11s)    t.go:106: 2025-08-20 17:44:28.486 [debu]  pubsub: pubsub dialing postgres  network=tcp  address=127.0.0.1:5432  timeout_ms=0    t.go:106: 2025-08-20 17:44:28.486 [debu]  pubsub: pubsub postgres TCP connection established  network=tcp  address=127.0.0.1:5432  timeout_ms=0  elapsed_ms=0    t.go:106: 2025-08-20 17:44:28.496 [debu]  pubsub: pubsub connected to postgres    t.go:106: 2025-08-20 17:44:28.496 [debu]  pubsub: pubsub has started    querier_test.go:1560:         Error Trace:/home/runner/work/coder/coder/coderd/database/querier_test.go:1560        Error:      Received unexpected error:                    pq: new row for relation "users" violates check constraint "users_username_min_length"        Test:       TestUpdateSystemUser    t.go:106: 2025-08-20 17:44:28.510 [info]  pubsub: pubsub is closing    t.go:106: 2025-08-20 17:44:28.510 [info]  pubsub: pubsub listen stopped receiving notify    t.go:106: 2025-08-20 17:44:28.510 [debu]  pubsub: pubsub closed

The failing tests are actually catching this!

Signed-off-by: Callum Styan <callumstyan@gmail.com>
Signed-off-by: Callum Styan <callumstyan@gmail.com>
@cstyancstyan changed the titlefix: Add database constraint to enforce minimum username lengthfix: add database constraint to enforce minimum username lengthAug 21, 2025
@@ -0,0 +1,3 @@
ALTER TABLE users
ADD CONSTRAINT users_username_min_length
CHECK (length(username) >= 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should expand this constraint to take the other clauses into account:
https://github.com/coder/coder/blob/main/codersdk/name.go#L41-L60

In any case, enforcing the minimum solves our immediate problem so I'm approving, but we might be overfitting.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need to duplicate the entire logic here.

Copy link
Contributor

@dannykoppingdannykoppingAug 21, 2025
edited
Loading

Choose a reason for hiding this comment

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

I'm not advocating for duplicating so much as replacing the logic; should've been more explicit.

@cstyancstyan merged commitbcdade7 intomainAug 21, 2025
28 checks passed
@cstyancstyan deleted the callum-username-constraint branchAugust 21, 2025 14:56
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsAug 21, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@dannykoppingdannykoppingdannykopping approved these changes

@johnstcnjohnstcnjohnstcn approved these changes

Assignees

@cstyancstyan

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@cstyan@johnstcn@dannykopping

[8]ページ先頭

©2009-2025 Movatter.jp