mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: ensure default org always exists (#12412)
* chore: ensure default org always exists First user just joins the org created by the migration
This commit is contained in:
@@ -232,7 +232,7 @@ var (
|
||||
rbac.ResourceGroup.Type: {rbac.ActionCreate, rbac.ActionUpdate},
|
||||
rbac.ResourceRoleAssignment.Type: {rbac.ActionCreate, rbac.ActionDelete},
|
||||
rbac.ResourceSystem.Type: {rbac.WildcardSymbol},
|
||||
rbac.ResourceOrganization.Type: {rbac.ActionCreate},
|
||||
rbac.ResourceOrganization.Type: {rbac.ActionCreate, rbac.ActionRead},
|
||||
rbac.ResourceOrganizationMember.Type: {rbac.ActionCreate},
|
||||
rbac.ResourceOrgRoleAssignment.Type: {rbac.ActionCreate},
|
||||
rbac.ResourceProvisionerDaemon.Type: {rbac.ActionCreate, rbac.ActionUpdate},
|
||||
|
||||
@@ -568,7 +568,7 @@ func (s *MethodTestSuite) TestOrganization() {
|
||||
check.Args(o.ID).Asserts(o, rbac.ActionRead).Returns(o)
|
||||
}))
|
||||
s.Run("GetDefaultOrganization", s.Subtest(func(db database.Store, check *expects) {
|
||||
o := dbgen.Organization(s.T(), db, database.Organization{})
|
||||
o, _ := db.GetDefaultOrganization(context.Background())
|
||||
check.Args().Asserts(o, rbac.ActionRead).Returns(o)
|
||||
}))
|
||||
s.Run("GetOrganizationByName", s.Subtest(func(db database.Store, check *expects) {
|
||||
@@ -597,9 +597,10 @@ func (s *MethodTestSuite) TestOrganization() {
|
||||
check.Args(u.ID).Asserts(a, rbac.ActionRead, b, rbac.ActionRead).Returns(slice.New(a, b))
|
||||
}))
|
||||
s.Run("GetOrganizations", s.Subtest(func(db database.Store, check *expects) {
|
||||
def, _ := db.GetDefaultOrganization(context.Background())
|
||||
a := dbgen.Organization(s.T(), db, database.Organization{})
|
||||
b := dbgen.Organization(s.T(), db, database.Organization{})
|
||||
check.Args().Asserts(a, rbac.ActionRead, b, rbac.ActionRead).Returns(slice.New(a, b))
|
||||
check.Args().Asserts(def, rbac.ActionRead, a, rbac.ActionRead, b, rbac.ActionRead).Returns(slice.New(def, a, b))
|
||||
}))
|
||||
s.Run("GetOrganizationsByUserID", s.Subtest(func(db database.Store, check *expects) {
|
||||
u := dbgen.User(s.T(), db, database.User{})
|
||||
|
||||
@@ -77,6 +77,17 @@ func New() database.Store {
|
||||
locks: map[int64]struct{}{},
|
||||
},
|
||||
}
|
||||
// Always start with a default org. Matching migration 198.
|
||||
_, err := q.InsertOrganization(context.Background(), database.InsertOrganizationParams{
|
||||
ID: uuid.New(),
|
||||
Name: "first-organization",
|
||||
Description: "Builtin default organization.",
|
||||
CreatedAt: dbtime.Now(),
|
||||
UpdatedAt: dbtime.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to create default organization: %w", err))
|
||||
}
|
||||
q.defaultProxyDisplayName = "Default"
|
||||
q.defaultProxyIconURL = "/emojis/1f3e1.png"
|
||||
return q
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
-- There is no down. If the org is created, just let it be. Deleting an org feels dangerous in a migration.
|
||||
@@ -0,0 +1,16 @@
|
||||
-- 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);
|
||||
|
||||
@@ -506,20 +506,11 @@ func TestDefaultOrg(t *testing.T) {
|
||||
db := database.New(sqlDB)
|
||||
ctx := context.Background()
|
||||
|
||||
// Should start with 0 orgs
|
||||
// Should start with the default org
|
||||
all, err := db.GetOrganizations(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, all, 0)
|
||||
|
||||
org, err := db.InsertOrganization(ctx, database.InsertOrganizationParams{
|
||||
ID: uuid.New(),
|
||||
Name: "default",
|
||||
Description: "",
|
||||
CreatedAt: dbtime.Now(),
|
||||
UpdatedAt: dbtime.Now(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, org.IsDefault, "first org should always be default")
|
||||
require.Len(t, all, 1)
|
||||
require.True(t, all[0].IsDefault, "first org should always be default")
|
||||
}
|
||||
|
||||
type tvArgs struct {
|
||||
|
||||
Reference in New Issue
Block a user