Skip to content

chore: ensure default org always exists #12412

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 12 commits into from
Mar 5, 2024
Next Next commit
chore: ensure default org always exists
First user just joins the org created by the migration
  • Loading branch information
Emyrk committed Mar 4, 2024
commit e4b1bb884cd03bae542ccea9c0ea9f8419cb3be2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- There is no down. If the org is created, just let it be. Deleting an org feels dangerous in a migration.
15 changes: 15 additions & 0 deletions coderd/database/migrations/000198_ensure_default_org.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- This ensures a default organization always exists.
INSERT INTO
organizations(id, name, description, created_at, updated_at, is_default)
SELECT
-- Avoid calling it "default" as we are reserving that word as a keyword to fetch
-- the default org regardless of the name.
gen_random_uuid(),
'first-organization',
'Builtin default organization.',
now(),
now(),
true
WHERE
-- Only insert if no organizations exist.
NOT EXISTS (SELECT * FROM organizations)
13 changes: 11 additions & 2 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,25 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
}
}

defaultOrg, err := api.Database.GetDefaultOrganization(ctx)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching default organization. If you are encountering this error, you will have to restart the Coder deployment.",
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{
Email: createUser.Email,
Username: createUser.Username,
Password: createUser.Password,
// Create an org for the first user.
OrganizationID: uuid.Nil,
OrganizationID: defaultOrg.ID,
},
CreateOrganization: true,
CreateOrganization: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can remove this field, which also allows cleaning up a lot of cruft in userauth.go

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh you are right. I think we can drop the CreateOrganization behavior entirely. I'll make a new PR that will look into dropping it.

LoginType: database.LoginTypePassword,
})
if err != nil {
Expand Down