From e11d3ca0ee0b95c72b1abb511dbcc20177f4fffb Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 12 Mar 2024 09:27:36 -0500 Subject: [PATCH] chore: move default everyone group to a migration (#12435) --- coderd/database/dbmem/dbmem.go | 8 +++++++- .../000199_ensure_default_everyone_group.down.sql | 1 + .../000199_ensure_default_everyone_group.up.sql | 11 +++++++++++ coderd/users.go | 12 ------------ 4 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 coderd/database/migrations/000199_ensure_default_everyone_group.down.sql create mode 100644 coderd/database/migrations/000199_ensure_default_everyone_group.up.sql diff --git a/coderd/database/dbmem/dbmem.go b/coderd/database/dbmem/dbmem.go index 4fd3a80f03..898d257790 100644 --- a/coderd/database/dbmem/dbmem.go +++ b/coderd/database/dbmem/dbmem.go @@ -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.", @@ -88,6 +88,12 @@ func New() database.Store { if err != nil { panic(xerrors.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)) + } + q.defaultProxyDisplayName = "Default" q.defaultProxyIconURL = "/emojis/1f3e1.png" return q diff --git a/coderd/database/migrations/000199_ensure_default_everyone_group.down.sql b/coderd/database/migrations/000199_ensure_default_everyone_group.down.sql new file mode 100644 index 0000000000..b98e23defc --- /dev/null +++ b/coderd/database/migrations/000199_ensure_default_everyone_group.down.sql @@ -0,0 +1 @@ +-- Nothing to do. If the group exists, this is ok. diff --git a/coderd/database/migrations/000199_ensure_default_everyone_group.up.sql b/coderd/database/migrations/000199_ensure_default_everyone_group.up.sql new file mode 100644 index 0000000000..bb35ba22b2 --- /dev/null +++ b/coderd/database/migrations/000199_ensure_default_everyone_group.up.sql @@ -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) +-- It might already exist +ON CONFLICT DO NOTHING; diff --git a/coderd/users.go b/coderd/users.go index cd5ee35037..a0dc128fc9 100644 --- a/coderd/users.go +++ b/coderd/users.go @@ -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{