Skip to content

chore: move default everyone group to a migration #12435

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func New() database.Store {
},
}
// Always start with a default org. Matching migration 198.
_, err := q.InsertOrganization(context.Background(), database.InsertOrganizationParams{
defaultOrg, err := q.InsertOrganization(context.Background(), database.InsertOrganizationParams{
ID: uuid.New(),
Name: "first-organization",
Description: "Builtin default organization.",
Expand All @@ -88,6 +88,12 @@ func New() database.Store {
if err != nil {
panic(fmt.Errorf("failed to create default organization: %w", err))
}

_, err = q.InsertAllUsersGroup(context.Background(), defaultOrg.ID)
if err != nil {
panic(fmt.Errorf("failed to create default group: %w", err))
}

Comment on lines +91 to +96
Copy link
Member Author

Choose a reason for hiding this comment

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

dbmem doesn't have the concept of "migrations". I wonder if we should have something similar rather than chucking them here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think "migrations" is the right concept, since we don't ever change an old dbmem into a new one, but it might make sense to have an abstraction that populates the initial contents of the database, which is what this does...

q.defaultProxyDisplayName = "Default"
q.defaultProxyIconURL = "/emojis/1f3e1.png"
return q
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Nothing to do. If the group exists, this is ok.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- This ensures a default everyone group exists for default org.
INSERT INTO
groups(name, id, organization_id)
SELECT
-- This is a special keyword that must be exactly this.
'Everyone',
-- Org ID and group ID must match.
(SELECT id FROM organizations WHERE is_default = true LIMIT 1),
(SELECT id FROM organizations WHERE is_default = true LIMIT 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is LIMIT 1 required for SQL to correctly cast a rowset into a scaler? The unique index on is_default should ensure there is exactly one organization where it is true.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not required, the unique index should protect this.

I added the LIMIT 1 for defensive programming in our migrations since they can prevent boot of Coderd.

-- It might already exist
ON CONFLICT DO NOTHING;
12 changes: 0 additions & 12 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,6 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
return
}

//nolint:gocritic // ensure everyone group
_, err = api.Database.InsertAllUsersGroup(dbauthz.AsSystemRestricted(ctx), defaultOrg.ID)
// A unique constraint violation just means the group already exists.
// This should not happen, but is ok if it does.
if err != nil && !database.IsUniqueViolation(err) {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error creating all users group.",
Detail: err.Error(),
})
return
}

//nolint:gocritic // needed to create first user
user, organizationID, err := api.CreateUser(dbauthz.AsSystemRestricted(ctx), api.Database, CreateUserRequest{
CreateUserRequest: codersdk.CreateUserRequest{
Expand Down