MCPcopy Index your code
hub / github.com/bytebase/bytebase / CreateWorkspace

Method CreateWorkspace

backend/store/workspace.go:91–199  ·  view source on GitHub ↗

CreateWorkspace creates a new workspace and initializes its default settings and IAM policy.

(ctx context.Context, create *WorkspaceMessage, adminEmail string)

Source from the content-addressed store, hash-verified

89
90// CreateWorkspace creates a new workspace and initializes its default settings and IAM policy.
91func (s *Store) CreateWorkspace(ctx context.Context, create *WorkspaceMessage, adminEmail string) (*WorkspaceMessage, error) {
92 tx, err := s.GetDB().BeginTx(ctx, nil)
93 if err != nil {
94 return nil, errors.Wrap(err, "failed to begin transaction")
95 }
96 defer tx.Rollback()
97
98 // Create workspace.
99 if create.Payload == nil {
100 create.Payload = &storepb.WorkspacePayload{}
101 }
102 payloadBytes, err := protojson.Marshal(create.Payload)
103 if err != nil {
104 return nil, errors.Wrap(err, "failed to marshal workspace payload")
105 }
106 if _, err := tx.ExecContext(ctx,
107 `INSERT INTO workspace (resource_id, payload) VALUES ($1, $2)`,
108 create.ResourceID, payloadBytes,
109 ); err != nil {
110 return nil, errors.Wrap(err, "failed to create workspace")
111 }
112
113 // Initialize default settings for the workspace.
114 type defaultSetting struct {
115 name storepb.SettingName
116 payload proto.Message
117 }
118 settings := []defaultSetting{
119 {storepb.SettingName_SYSTEM, &storepb.SystemSetting{}},
120 {storepb.SettingName_APP_IM, &storepb.AppIMSetting{}},
121 {storepb.SettingName_DATA_CLASSIFICATION, &storepb.DataClassificationSetting{}},
122 {storepb.SettingName_WORKSPACE_APPROVAL, &storepb.WorkspaceApprovalSetting{
123 Rules: []*storepb.WorkspaceApprovalSetting_Rule{
124 {
125 Template: &storepb.ApprovalTemplate{
126 Flow: &storepb.ApprovalFlow{Roles: []string{"roles/projectOwner"}},
127 Title: "Fallback Rule",
128 Description: "Requires project owner approval when no other rules match.",
129 },
130 Condition: &expr.Expr{Expression: "true"},
131 },
132 },
133 }},
134 {storepb.SettingName_WORKSPACE_PROFILE, &storepb.WorkspaceProfileSetting{
135 EnableMetricCollection: true,
136 DirectorySyncToken: uuid.New().String(),
137 DisallowSignup: false,
138 DisallowPasswordSignin: false,
139 PasswordRestriction: &storepb.WorkspaceProfileSetting_PasswordRestriction{MinLength: 8},
140 }},
141 {storepb.SettingName_ENVIRONMENT, &storepb.EnvironmentSetting{
142 Environments: []*storepb.EnvironmentSetting_Environment{
143 {Title: "Test", Id: "test", Color: defaultEnvironmentColor()},
144 {Title: "Prod", Id: "prod", Color: defaultEnvironmentColor()},
145 },
146 }},
147 }
148 // Append additional settings from the caller (e.g., AI config in SaaS mode).

Callers 2

Calls 4

GetDBMethod · 0.95
DefaultProjectIDFunction · 0.92
defaultEnvironmentColorFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected