Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: add workspace table and workspace column to root tables (#19642)
ecmadaoc5e75a54a9dd
diff --git a/backend/api/directory-sync/webhook.go b/backend/api/directory-sync/webhook.goindex 712920b8a9..5184b9fb06 100644--- a/backend/api/directory-sync/webhook.go+++ b/backend/api/directory-sync/webhook.go@@ -481,10 +481,15 @@ func (s *Service) RegisterDirectorySyncRoutes(g *echo.Group) {if email == "" && strings.Contains(scimGroup.ExternalID, "@") {email = scimGroup.ExternalID}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to get workspace, error %v", err))+ }newGroup, err := s.store.CreateGroup(ctx, &store.GroupMessage{- ID: scimGroup.ExternalID,- Email: email,- Title: scimGroup.DisplayName,+ Workspace: workspace.ResourceID,+ ID: scimGroup.ExternalID,+ Email: email,+ Title: scimGroup.DisplayName,Payload: &storepb.GroupPayload{Source: source,Members: members,@@ -841,13 +846,12 @@ func (s *Service) validRequestURL(ctx context.Context, c *echo.Context) error {}workspaceID := c.Param("workspaceID")- systemSetting, err := s.store.GetSystemSetting(ctx)+ workspace, err := s.store.GetWorkspace(ctx)if err != nil {- return errors.Wrap(err, "failed to get system setting")+ return errors.Wrap(err, "failed to get workspace")}- myWorkspaceID := systemSetting.WorkspaceId- if myWorkspaceID != workspaceID {- return errors.Errorf("invalid workspace id %q, my ID %q", workspaceID, myWorkspaceID)+ if workspace.ResourceID != workspaceID {+ return errors.Errorf("invalid workspace id %q, my ID %q", workspaceID, workspace.ResourceID)}workspaceProfileSetting, err := s.store.GetWorkspaceProfileSetting(ctx)diff --git a/backend/api/v1/actuator_service.go b/backend/api/v1/actuator_service.goindex dae4e61554..faaf3f811c 100644--- a/backend/api/v1/actuator_service.go+++ b/backend/api/v1/actuator_service.go@@ -115,11 +115,11 @@ func (s *ActuatorService) getServerInfo(ctx context.Context) (*v1pb.ActuatorInforeturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find workspace setting"))}- systemSetting, err := s.store.GetSystemSetting(ctx)+ workspace, err := s.store.GetWorkspace(ctx)if err != nil {- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get system setting"))+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get workspace"))}- workspaceID := systemSetting.WorkspaceId+ workspaceID := workspace.ResourceIDusedFeatures, err := s.getUsedFeatures(ctx)if err != nil {diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.goindex 1f4af50277..cd339cff39 100644--- a/backend/api/v1/audit.go+++ b/backend/api/v1/audit.go@@ -175,12 +175,11 @@ func createAuditLogConnect(ctx context.Context, request, response any, method stvar parents []stringif authContext.HasWorkspaceResource() {- systemSetting, err := storage.GetSystemSetting(ctx)+ workspace, err := storage.GetWorkspace(ctx)if err != nil {- return errors.Wrapf(err, "failed to get system setting")+ return errors.Wrapf(err, "failed to get workspace")}- workspaceID := systemSetting.WorkspaceId- parents = append(parents, common.FormatWorkspace(workspaceID))+ parents = append(parents, common.FormatWorkspace(workspace.ResourceID))} else {for _, projectID := range authContext.GetProjectResources() {parents = append(parents, common.FormatProject(projectID))@@ -216,7 +215,11 @@ func createAuditLogConnect(ctx context.Context, request, response any, method stServiceData: serviceData,RequestMetadata: requestMetadata,}- if err := storage.CreateAuditLog(createAuditLogCtx, p); err != nil {+ workspaceForAudit, err := storage.GetWorkspace(createAuditLogCtx)+ if err != nil {+ return errors.Wrapf(err, "failed to get workspace for audit log")+ }+ if err := storage.CreateAuditLog(createAuditLogCtx, workspaceForAudit.ResourceID, p); err != nil {return err}diff --git a/backend/api/v1/group_service.go b/backend/api/v1/group_service.goindex 6349650b65..4023a1639f 100644--- a/backend/api/v1/group_service.go+++ b/backend/api/v1/group_service.go@@ -134,6 +134,12 @@ func (s *GroupService) CreateGroup(ctx context.Context, req *connect.Request[v1preturn nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "invalid email %q", groupMessage.Email))}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }+ groupMessage.Workspace = workspace.ResourceID+group, err := s.store.CreateGroup(ctx, groupMessage)if err != nil {return nil, connect.NewError(connect.CodeInternal, err)diff --git a/backend/api/v1/idp_service.go b/backend/api/v1/idp_service.goindex ffb3a6f1ba..5aaf4b88e6 100644--- a/backend/api/v1/idp_service.go+++ b/backend/api/v1/idp_service.go@@ -88,7 +88,12 @@ func (s *IdentityProviderService) CreateIdentityProvider(ctx context.Context, reif err := validIdentityProviderConfig(req.Msg.IdentityProvider.Type, req.Msg.IdentityProvider.Config); err != nil {return nil, connect.NewError(connect.CodeInvalidArgument, err)}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }identityProviderMessage := &store.IdentityProviderMessage{+ Workspace: workspace.ResourceID,ResourceID: req.Msg.IdentityProviderId,Title: req.Msg.IdentityProvider.Title,Domain: req.Msg.IdentityProvider.Domain,@@ -100,7 +105,7 @@ func (s *IdentityProviderService) CreateIdentityProvider(ctx context.Context, rereturn connect.NewResponse(identityProvider), nil}- identityProviderMessage, err := s.store.CreateIdentityProvider(ctx, identityProviderMessage)+ identityProviderMessage, err = s.store.CreateIdentityProvider(ctx, identityProviderMessage)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to create identity provider"))}diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.goindex bd55537464..e9c06a991b 100644--- a/backend/api/v1/instance_service.go+++ b/backend/api/v1/instance_service.go@@ -208,6 +208,12 @@ func (s *InstanceService) CreateInstance(ctx context.Context, req *connect.Requereturn nil, err}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }+ instanceMessage.Workspace = workspace.ResourceID+instance, err := s.store.CreateInstance(ctx, instanceMessage)if err != nil {return nil, connect.NewError(connect.CodeInternal, err)diff --git a/backend/api/v1/org_policy_service.go b/backend/api/v1/org_policy_service.goindex 7379183b17..16281f2aad 100644--- a/backend/api/v1/org_policy_service.go+++ b/backend/api/v1/org_policy_service.go@@ -415,7 +415,12 @@ func (s *OrgPolicyService) createPolicyMessage(ctx context.Context, req *connectreturn nil, err}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }create := &store.PolicyMessage{+ Workspace: workspace.ResourceID,ResourceType: resourceType,Resource: parent,Payload: payloadStr,diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.goindex e63b164ec6..406bc51e78 100644--- a/backend/api/v1/project_service.go+++ b/backend/api/v1/project_service.go@@ -217,6 +217,12 @@ func (s *ProjectService) CreateProject(ctx context.Context, req *connect.RequestprojectMessage := convertToProjectMessage(req.Msg.ProjectId, req.Msg.Project)+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }+ projectMessage.Workspace = workspace.ResourceID+setting, err := s.store.GetDataClassificationSetting(ctx)if err != nil {slog.Error("failed to find classification setting", log.BBError(err))@@ -590,7 +596,12 @@ func (s *ProjectService) SetIamPolicy(ctx context.Context, req *connect.Request[if err != nil {return nil, connect.NewError(connect.CodeInternal, err)}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }if _, err := s.store.CreatePolicy(ctx, &store.PolicyMessage{+ Workspace: workspace.ResourceID,Resource: common.FormatProject(project.ResourceID),ResourceType: storepb.Policy_PROJECT,Payload: string(policyPayload),diff --git a/backend/api/v1/review_config_service.go b/backend/api/v1/review_config_service.goindex 9233951b0a..07425b0669 100644--- a/backend/api/v1/review_config_service.go+++ b/backend/api/v1/review_config_service.go@@ -41,6 +41,12 @@ func (s *ReviewConfigService) CreateReviewConfig(ctx context.Context, req *connereturn nil, err}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }+ reviewConfigMessage.Workspace = workspace.ResourceID+created, err := s.store.CreateReviewConfig(ctx, reviewConfigMessage)if err != nil {return nil, connect.NewError(connect.CodeInternal, err)diff --git a/backend/api/v1/role_service.go b/backend/api/v1/role_service.goindex 2a95bba8ea..fc6c8400f0 100644--- a/backend/api/v1/role_service.go+++ b/backend/api/v1/role_service.go@@ -89,7 +89,12 @@ func (s *RoleService) CreateRole(ctx context.Context, req *connect.Request[v1pb.for _, v := range req.Msg.GetRole().GetPermissions() {permissions[permission.Permission(v)] = true}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }create := &store.RoleMessage{+ Workspace: workspace.ResourceID,ResourceID: req.Msg.RoleId,Name: req.Msg.Role.Title,Description: req.Msg.Role.Description,diff --git a/backend/api/v1/service_account_service.go b/backend/api/v1/service_account_service.goindex fa98522396..1edfc935f6 100644--- a/backend/api/v1/service_account_service.go+++ b/backend/api/v1/service_account_service.go@@ -91,7 +91,12 @@ func (s *ServiceAccountService) CreateServiceAccount(ctx context.Context, requesreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to hash service key"))}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }createdSA, err := s.store.CreateServiceAccount(ctx, &store.CreateServiceAccountMessage{+ Workspace: workspace.ResourceID,Email: email,Name: sa.Title,ServiceKeyHash: string(passwordHash),diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.goindex 000059ff20..47d55d99cc 100644--- a/backend/api/v1/setting_service.go+++ b/backend/api/v1/setting_service.go@@ -571,9 +571,14 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *connect.Req}), nil}+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }setting, err := s.store.UpsertSetting(ctx, &store.SettingMessage{- Name: storeSettingName,- Value: storeSettingValue,+ Name: storeSettingName,+ Workspace: workspace.ResourceID,+ Value: storeSettingValue,})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to set setting: %v", err))diff --git a/backend/api/v1/workload_identity_service.go b/backend/api/v1/workload_identity_service.goindex 8cd2c6f049..658af7755e 100644--- a/backend/api/v1/workload_identity_service.go+++ b/backend/api/v1/workload_identity_service.go@@ -84,11 +84,16 @@ func (s *WorkloadIdentityService) CreateWorkloadIdentity(ctx context.Context, re}// Create the workload identity+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))+ }createdWI, err := s.store.CreateWorkloadIdentity(ctx, &store.CreateWorkloadIdentityMessage{- Email: email,- Name: wi.Title,- Project: projectID,- Config: storeConfig,+ Workspace: workspace.ResourceID,+ Email: email,+ Name: wi.Title,+ Project: projectID,+ Config: storeConfig,})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to create workload identity"))diff --git a/backend/component/sampleinstance/manager.go b/backend/component/sampleinstance/manager.goindex 21bd4caaec..81565f32a0 100644--- a/backend/component/sampleinstance/manager.go+++ b/backend/component/sampleinstance/manager.go@@ -160,7 +160,12 @@ func (m *Manager) GenerateOnboardingData(ctx context.Context, user *store.UserMereturn errors.Wrapf(err, "failed to find onboarding project %v", projectID)}if project == nil {+ workspace, err := m.store.GetWorkspace(ctx)+ if err != nil {+ return errors.Wrapf(err, "failed to get workspace")+ }sampleProject, err := m.store.CreateProject(ctx, &store.ProjectMessage{+ Workspace: workspace.ResourceID,ResourceID: "project-sample",Title: "Sample Project",Setting: &storepb.Project{},@@ -224,7 +229,12 @@ func (m *Manager) generateInstance(return errors.Wrapf(err, "failed to find onboarding instance %v", instanceMessage.ResourceID)}if instance == nil {+ workspace, err := m.store.GetWorkspace(ctx)+ if err != nil {+ return errors.Wrapf(err, "failed to get workspace")+ }sampleInstance, err := m.store.CreateInstance(ctx, &store.InstanceMessage{+ Workspace: workspace.ResourceID,ResourceID: instanceMessage.ResourceID,EnvironmentID: instanceMessage.EnvironmentID,Metadata: &storepb.Instance{diff --git a/backend/enterprise/license.go b/backend/enterprise/license.goindex 885a8a34ee..b61d9040d9 100644--- a/backend/enterprise/license.go+++ b/backend/enterprise/license.go@@ -213,7 +213,13 @@ func (s *LicenseService) loadSubscriptionFromDB(ctx context.Context) *v1pb.Subscreturn defaultFreeSubscription}- subscription, err := s.parseLicense(setting.License, setting.WorkspaceId)+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ slog.Debug("failed to get workspace", log.BBError(err))+ return defaultFreeSubscription+ }++ subscription, err := s.parseLicense(setting.License, workspace.ResourceID)if err != nil {slog.Debug("failed to parse enterprise license", log.BBError(err))return defaultFreeSubscription@@ -335,11 +341,11 @@ func (s *LicenseService) GetInstanceLimit(ctx context.Context) int {// StoreLicense will store license into file.func (s *LicenseService) StoreLicense(ctx context.Context, license string) error {if license != "" {- systemSetting, err := s.store.GetSystemSetting(ctx)+ workspace, err := s.store.GetWorkspace(ctx)if err != nil {- return errors.Wrapf(err, "failed to get system setting")+ return errors.Wrapf(err, "failed to get workspace")}- if _, err := s.parseLicense(license, systemSetting.WorkspaceId); err != nil {+ if _, err := s.parseLicense(license, workspace.ResourceID); err != nil {return err}}diff --git a/backend/generated-go/store/setting.pb.go b/backend/generated-go/store/setting.pb.goindex fb04ce59b7..6e7e2646fc 100644--- a/backend/generated-go/store/setting.pb.go+++ b/backend/generated-go/store/setting.pb.go@@ -362,8 +362,6 @@ type SystemSetting struct {state protoimpl.MessageState `protogen:"open.v1"`// Authentication secret for token signing (32-character random string).AuthSecret string `protobuf:"bytes,1,opt,name=auth_secret,json=authSecret,proto3" json:"auth_secret,omitempty"`- // Unique workspace identifier (UUID).- WorkspaceId string `protobuf:"bytes,2,opt,name=workspace_id,json=workspaceId,proto3" json:"workspace_id,omitempty"`// Enterprise license JWT token.License string `protobuf:"bytes,3,opt,name=license,proto3" json:"license,omitempty"`unknownFields protoimpl.UnknownFields@@ -407,13 +405,6 @@ func (x *SystemSetting) GetAuthSecret() string {return ""}-func (x *SystemSetting) GetWorkspaceId() string {- if x != nil {- return x.WorkspaceId- }- return ""-}-func (x *SystemSetting) GetLicense() string {if x != nil {return x.License@@ -2412,12 +2403,11 @@ var File_store_setting_proto protoreflect.FileDescriptorconst file_store_setting_proto_rawDesc = "" +"\n" +- "\x13store/setting.proto\x12\x0ebytebase.store\x1a\x1fgoogle/api/field_behavior.proto\x1a\x1egoogle/protobuf/duration.proto\x1a\x16google/type/expr.proto\x1a\x14store/approval.proto\x1a\x12store/common.proto\"m\n" ++ "\x13store/setting.proto\x12\x0ebytebase.store\x1a\x1fgoogle/api/field_behavior.proto\x1a\x1egoogle/protobuf/duration.proto\x1a\x16google/type/expr.proto\x1a\x14store/approval.proto\x1a\x12store/common.proto\"P\n" +"\rSystemSetting\x12\x1f\n" +"\vauth_secret\x18\x01 \x01(\tR\n" +- "authSecret\x12!\n" +- "\fworkspace_id\x18\x02 \x01(\tR\vworkspaceId\x12\x18\n" +- "\alicense\x18\x03 \x01(\tR\alicense\"\xcf\x0f\n" ++ "authSecret\x12\x18\n" ++ "\alicense\x18\x03 \x01(\tR\alicenseJ\x04\b\x02\x10\x03\"\xcf\x0f\n" +"\x17WorkspaceProfileSetting\x12!\n" +"\fexternal_url\x18\x01 \x01(\tR\vexternalUrl\x12'\n" +"\x0fdisallow_signup\x18\x02 \x01(\bR\x0edisallowSignup\x12\x1f\n" +diff --git a/backend/generated-go/store/setting_equal.pb.go b/backend/generated-go/store/setting_equal.pb.goindex 3c6fc8ec8a..dbf16712d7 100644--- a/backend/generated-go/store/setting_equal.pb.go+++ b/backend/generated-go/store/setting_equal.pb.go@@ -18,9 +18,6 @@ func (x *SystemSetting) Equal(y *SystemSetting) bool {if x.AuthSecret != y.AuthSecret {return false}- if x.WorkspaceId != y.WorkspaceId {- return false- }if x.License != y.License {return false}diff --git a/backend/migrator/migration/3.17/0009##add_workspace_table.sql b/backend/migrator/migration/3.17/0009##add_workspace_table.sqlnew file mode 100644index 0000000000..c06f40705e--- /dev/null+++ b/backend/migrator/migration/3.17/0009##add_workspace_table.sql@@ -0,0 +1,126 @@+-- Step 02: Create workspace table, add workspace to root tables, update indexes.++------------------------------+-- 1. Create workspace table+------------------------------+CREATE TABLE workspace (+ resource_id text PRIMARY KEY,+ name text NOT NULL,+ created_at timestamptz NOT NULL DEFAULT now(),+ deleted boolean NOT NULL DEFAULT FALSE+);++-- Seed the default workspace from the existing SYSTEM setting.+INSERT INTO workspace (resource_id, name)+SELECT+ COALESCE(NULLIF(value->>'workspaceId', ''), gen_random_uuid()::text),+ 'Default Workspace'+FROM setting+WHERE name = 'SYSTEM';++----------------------------------------------+-- 2. Add workspace to 12 root tables+----------------------------------------------++-- project+ALTER TABLE project ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE project SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE project ALTER COLUMN workspace SET NOT NULL;++-- instance+ALTER TABLE instance ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE instance SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE instance ALTER COLUMN workspace SET NOT NULL;++-- setting+ALTER TABLE setting ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE setting SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE setting ALTER COLUMN workspace SET NOT NULL;++-- policy+ALTER TABLE policy ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE policy SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE policy ALTER COLUMN workspace SET NOT NULL;++-- role+ALTER TABLE role ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE role SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE role ALTER COLUMN workspace SET NOT NULL;++-- idp+ALTER TABLE idp ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE idp SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE idp ALTER COLUMN workspace SET NOT NULL;++-- review_config+ALTER TABLE review_config ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE review_config SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE review_config ALTER COLUMN workspace SET NOT NULL;++-- user_group+ALTER TABLE user_group ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE user_group SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE user_group ALTER COLUMN workspace SET NOT NULL;++-- export_archive+ALTER TABLE export_archive ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE export_archive SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE export_archive ALTER COLUMN workspace SET NOT NULL;++-- audit_log+ALTER TABLE audit_log ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE audit_log SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE audit_log ALTER COLUMN workspace SET NOT NULL;++-- service_account+ALTER TABLE service_account ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE service_account SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE service_account ALTER COLUMN workspace SET NOT NULL;++-- workload_identity+ALTER TABLE workload_identity ADD COLUMN workspace text REFERENCES workspace(resource_id);+UPDATE workload_identity SET workspace = (SELECT resource_id FROM workspace LIMIT 1);+ALTER TABLE workload_identity ALTER COLUMN workspace SET NOT NULL;++----------------------------------------------+-- 3. Update unique indexes+----------------------------------------------+-- Current PKs (resource_id, name, email) already enforce global uniqueness.+-- These composite unique indexes prepare for multi-workspace where uniqueness+-- will be workspace-scoped (after a future PK migration).++-- project+CREATE UNIQUE INDEX idx_project_unique_workspace_resource_id ON project(workspace, resource_id);++-- instance+CREATE UNIQUE INDEX idx_instance_unique_workspace_resource_id ON instance(workspace, resource_id);++-- setting+CREATE UNIQUE INDEX idx_setting_unique_workspace_name ON setting(workspace, name);++-- role+CREATE UNIQUE INDEX idx_role_unique_workspace_resource_id ON role(workspace, resource_id);++-- idp+CREATE UNIQUE INDEX idx_idp_unique_workspace_resource_id ON idp(workspace, resource_id);++-- user_group: replace existing email unique index with workspace-scoped one+DROP INDEX idx_user_group_unique_email;+CREATE UNIQUE INDEX idx_user_group_unique_email ON user_group(workspace, email) WHERE email IS NOT NULL;++-- service_account+CREATE UNIQUE INDEX idx_service_account_unique_workspace_email ON service_account(workspace, email);++-- workload_identity+CREATE UNIQUE INDEX idx_workload_identity_unique_workspace_email ON workload_identity(workspace, email);++----------------------------------------------+-- 4. Workspace query performance indexes+----------------------------------------------+CREATE INDEX idx_project_workspace ON project(workspace);+CREATE INDEX idx_instance_workspace ON instance(workspace);+CREATE INDEX idx_setting_workspace ON setting(workspace);+CREATE INDEX idx_policy_workspace ON policy(workspace);+CREATE INDEX idx_audit_log_workspace ON audit_log(workspace);+CREATE INDEX idx_service_account_workspace ON service_account(workspace);+CREATE INDEX idx_workload_identity_workspace ON workload_identity(workspace);diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sqlindex 087698cb9f..4e3f55ff6c 100644--- a/backend/migrator/migration/LATEST.sql+++ b/backend/migrator/migration/LATEST.sql@@ -1,24 +1,15 @@--- idp stores generic identity provider.-CREATE TABLE idp (- resource_id text NOT NULL PRIMARY KEY,- name text NOT NULL,- domain text NOT NULL,- type text NOT NULL CONSTRAINT idp_type_check CHECK (type IN ('OAUTH2', 'OIDC', 'LDAP')),- -- config stores the corresponding configuration of the IdP, which may vary depending on the type of the IdP.- -- Stored as IdentityProviderConfig (proto/store/store/idp.proto)- config jsonb NOT NULL DEFAULT '{}'-);+-----------------------+-- Global identity: workspace and principal+-- We will use the IAM policy to list the principal's workspaces.+-------------------------- Project (created before principal for foreign key reference)-CREATE TABLE project (- resource_id text NOT NULL PRIMARY KEY,- deleted boolean NOT NULL DEFAULT FALSE,- name text NOT NULL,- -- Stored as Project (proto/store/store/project.proto)- setting jsonb NOT NULL DEFAULT '{}'+CREATE TABLE workspace (+ resource_id text PRIMARY KEY,+ name text NOT NULL,+ created_at timestamptz NOT NULL DEFAULT now(),+ deleted boolean NOT NULL DEFAULT FALSE);--- principalCREATE TABLE principal (id serial PRIMARY KEY,deleted boolean NOT NULL DEFAULT FALSE,@@ -35,30 +26,11 @@ CREATE TABLE principal (CREATE UNIQUE INDEX idx_principal_unique_email ON principal(email);--- service_account-CREATE TABLE service_account (- deleted boolean NOT NULL DEFAULT FALSE,- created_at timestamptz NOT NULL DEFAULT now(),- name text NOT NULL,- email text NOT NULL PRIMARY KEY,- service_key_hash text NOT NULL,- project text REFERENCES project(resource_id)-);--CREATE INDEX idx_service_account_project ON service_account(project) WHERE project IS NOT NULL;---- workload_identity-CREATE TABLE workload_identity (- deleted boolean NOT NULL DEFAULT FALSE,- created_at timestamptz NOT NULL DEFAULT now(),- name text NOT NULL,- email text NOT NULL PRIMARY KEY,- project text REFERENCES project(resource_id),- -- Stored as WorkloadIdentityConfig (proto/store/store/user.proto)- config jsonb NOT NULL DEFAULT '{}'-);+ALTER SEQUENCE principal_id_seq RESTART WITH 101;-CREATE INDEX idx_workload_identity_project ON workload_identity(project) WHERE project IS NOT NULL;+-----------------------+-- Workspace-scoped tables+------------------------- SettingCREATE TABLE setting (@@ -66,13 +38,18 @@ CREATE TABLE setting (-- APP_IM, AI, DATA_CLASSIFICATION, SEMANTIC_TYPES, ENVIRONMENT-- Enum: SettingName (proto/store/store/setting.proto)name text NOT NULL PRIMARY KEY,+ workspace text NOT NULL REFERENCES workspace(resource_id),-- Stored as JSON marshalled by protojson.Marshal (camelCase keys)value jsonb NOT NULL);+CREATE UNIQUE INDEX idx_setting_unique_workspace_name ON setting(workspace, name);+CREATE INDEX idx_setting_workspace ON setting(workspace);+-- RoleCREATE TABLE role (resource_id text NOT NULL PRIMARY KEY,+ workspace text NOT NULL REFERENCES workspace(resource_id),name text NOT NULL,description text NOT NULL,-- Stored as RolePermissions (proto/store/store/role.proto)@@ -81,11 +58,14 @@ CREATE TABLE role (payload jsonb NOT NULL DEFAULT '{}');+CREATE UNIQUE INDEX idx_role_unique_workspace_resource_id ON role(workspace, resource_id);+-- Policy-- policy stores the policies for each resources.CREATE TABLE policy (enforce boolean NOT NULL DEFAULT TRUE,updated_at timestamptz NOT NULL DEFAULT now(),+ workspace text NOT NULL REFERENCES workspace(resource_id),-- resource_type: WORKSPACE, ENVIRONMENT, PROJECT-- Enum: Policy.Resource (proto/store/store/policy.proto)resource_type text NOT NULL,@@ -106,58 +86,129 @@ CREATE TABLE policy (PRIMARY KEY (resource_type, resource, type));--- Project Hook-CREATE TABLE project_webhook (+CREATE INDEX idx_policy_workspace ON policy(workspace);++-- idp stores generic identity provider.+CREATE TABLE idp (+ resource_id text NOT NULL PRIMARY KEY,+ workspace text NOT NULL REFERENCES workspace(resource_id),+ name text NOT NULL,+ domain text NOT NULL,+ type text NOT NULL CONSTRAINT idp_type_check CHECK (type IN ('OAUTH2', 'OIDC', 'LDAP')),+ -- config stores the corresponding configuration of the IdP, which may vary depending on the type of the IdP.+ -- Stored as IdentityProviderConfig (proto/store/store/idp.proto)+ config jsonb NOT NULL DEFAULT '{}'+);++CREATE UNIQUE INDEX idx_idp_unique_workspace_resource_id ON idp(workspace, resource_id);++CREATE TABLE user_group (+ id text PRIMARY KEY DEFAULT gen_random_uuid()::text,+ workspace text NOT NULL REFERENCES workspace(resource_id),+ email text,+ name text NOT NULL,+ description text NOT NULL DEFAULT '',+ -- Stored as GroupPayload (proto/store/store/group.proto)+ payload jsonb NOT NULL DEFAULT '{}'+);++CREATE UNIQUE INDEX idx_user_group_unique_email ON user_group(workspace, email) WHERE email IS NOT NULL;++-- review config table.+CREATE TABLE review_config (+ id text NOT NULL PRIMARY KEY,+ workspace text NOT NULL REFERENCES workspace(resource_id),+ enabled boolean NOT NULL DEFAULT TRUE,+ name text NOT NULL,+ -- Stored as ReviewConfigPayload (proto/store/store/review_config.proto)+ payload jsonb NOT NULL DEFAULT '{}'+);++CREATE TABLE audit_log (resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,- project text NOT NULL REFERENCES project(resource_id),- -- Stored as ProjectWebhook (proto/store/store/project_webhook.proto)+ workspace text NOT NULL REFERENCES workspace(resource_id),+ created_at timestamptz NOT NULL DEFAULT now(),+ -- Stored as AuditLog (proto/store/store/audit_log.proto)payload jsonb NOT NULL DEFAULT '{}');-CREATE INDEX idx_project_webhook_project ON project_webhook(project);+CREATE INDEX idx_audit_log_workspace ON audit_log(workspace);+CREATE INDEX idx_audit_log_created_at ON audit_log(created_at);+CREATE INDEX idx_audit_log_payload_parent ON audit_log((payload->>'parent'));+CREATE INDEX idx_audit_log_payload_method ON audit_log((payload->>'method'));+CREATE INDEX idx_audit_log_payload_resource ON audit_log((payload->>'resource'));+CREATE INDEX idx_audit_log_payload_user ON audit_log((payload->>'user'));--- Instance-CREATE TABLE instance (+CREATE TABLE export_archive (+ resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,+ workspace text NOT NULL REFERENCES workspace(resource_id),+ created_at timestamptz NOT NULL DEFAULT now(),+ bytes bytea,+ -- Stored as ExportArchivePayload (proto/store/store/export_archive.proto)+ payload jsonb NOT NULL DEFAULT '{}'+);++-----------------------+-- Project and project-scoped tables+-----------------------++CREATE TABLE project (resource_id text NOT NULL PRIMARY KEY,+ workspace text NOT NULL REFERENCES workspace(resource_id),deleted boolean NOT NULL DEFAULT FALSE,- environment text,- -- Stored as Instance (proto/store/store/instance.proto)- metadata jsonb NOT NULL DEFAULT '{}'+ name text NOT NULL,+ -- Stored as Project (proto/store/store/project.proto)+ setting jsonb NOT NULL DEFAULT '{}');-CREATE INDEX idx_instance_metadata_engine ON instance((metadata->>'engine'));+CREATE UNIQUE INDEX idx_project_unique_workspace_resource_id ON project(workspace, resource_id);+CREATE INDEX idx_project_workspace ON project(workspace);--- db stores the databases for a particular instance--- data is synced periodically from the instance-CREATE TABLE db (- instance text NOT NULL REFERENCES instance(resource_id),+-- service_account+-- Service Account needs both workspace and project+CREATE TABLE service_account (+ deleted boolean NOT NULL DEFAULT FALSE,+ created_at timestamptz NOT NULL DEFAULT now(),name text NOT NULL,+ email text NOT NULL PRIMARY KEY,+ workspace text NOT NULL REFERENCES workspace(resource_id),+ service_key_hash text NOT NULL,+ project text REFERENCES project(resource_id)+);++CREATE INDEX idx_service_account_project ON service_account(project) WHERE project IS NOT NULL;+CREATE UNIQUE INDEX idx_service_account_unique_workspace_email ON service_account(workspace, email);+CREATE INDEX idx_service_account_workspace ON service_account(workspace);++-- workload_identity+CREATE TABLE workload_identity (deleted boolean NOT NULL DEFAULT FALSE,- project text NOT NULL REFERENCES project(resource_id),- environment text,- -- Stored as DatabaseMetadata (proto/store/store/database.proto)- metadata jsonb NOT NULL DEFAULT '{}',- PRIMARY KEY (instance, name)+ created_at timestamptz NOT NULL DEFAULT now(),+ name text NOT NULL,+ email text NOT NULL PRIMARY KEY,+ workspace text NOT NULL REFERENCES workspace(resource_id),+ project text REFERENCES project(resource_id),+ -- Stored as WorkloadIdentityConfig (proto/store/store/user.proto)+ config jsonb NOT NULL DEFAULT '{}');-CREATE INDEX idx_db_project ON db(project);+CREATE INDEX idx_workload_identity_project ON workload_identity(project) WHERE project IS NOT NULL;+CREATE UNIQUE INDEX idx_workload_identity_unique_workspace_email ON workload_identity(workspace, email);+CREATE INDEX idx_workload_identity_workspace ON workload_identity(workspace);--- db_schema stores the database schema metadata for a particular database.-CREATE TABLE db_schema (- instance text NOT NULL,- db_name text NOT NULL,- -- Stored as DatabaseSchemaMetadata (proto/store/store/database.proto)- metadata json NOT NULL DEFAULT '{}',- raw_dump text NOT NULL DEFAULT '',- -- Stored as DatabaseConfig (proto/store/store/database.proto)- config jsonb NOT NULL DEFAULT '{}',- PRIMARY KEY (instance, db_name),- CONSTRAINT db_schema_instance_db_name_fkey FOREIGN KEY(instance, db_name) REFERENCES db(instance, name)+-- Project Hook+CREATE TABLE project_webhook (+ resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,+ project text NOT NULL REFERENCES project(resource_id),+ -- Stored as ProjectWebhook (proto/store/store/project_webhook.proto)+ payload jsonb NOT NULL DEFAULT '{}');+CREATE INDEX idx_project_webhook_project ON project_webhook(project);+CREATE TABLE sheet_blob (- sha256 bytea NOT NULL PRIMARY KEY,- content text NOT NULL+ sha256 bytea NOT NULL PRIMARY KEY,+ content text NOT NULL);-- plan table stores the plan for a project@@ -195,7 +246,6 @@ CREATE TABLE plan_check_run ();CREATE UNIQUE INDEX idx_plan_check_run_unique_plan_id ON plan_check_run(project, plan_id);-CREATE INDEX idx_plan_check_run_active_status ON plan_check_run(status, id) WHERE status IN ('AVAILABLE', 'RUNNING');ALTER SEQUENCE plan_check_run_id_seq RESTART WITH 101;@@ -213,79 +263,6 @@ CREATE TABLE plan_webhook_delivery (FOREIGN KEY (project, plan_id) REFERENCES plan(project, id));--- task table stores the task for a plan-CREATE TABLE task (- id serial,- project text NOT NULL REFERENCES project(resource_id),- plan_id bigint NOT NULL,- instance text NOT NULL REFERENCES instance(resource_id),- environment text,- db_name text,- type text NOT NULL,- -- Stored as Task (proto/store/store/task.proto)- payload jsonb NOT NULL DEFAULT '{}',- PRIMARY KEY (project, id),- FOREIGN KEY (project, plan_id) REFERENCES plan(project, id)-);--CREATE INDEX idx_task_plan_id_environment ON task(project, plan_id, environment);--ALTER SEQUENCE task_id_seq RESTART WITH 101;---- task run table stores the task run-CREATE TABLE task_run (- id serial,- creator text,- created_at timestamptz NOT NULL DEFAULT now(),- updated_at timestamptz NOT NULL DEFAULT now(),- project text NOT NULL REFERENCES project(resource_id),- task_id integer NOT NULL,- attempt integer NOT NULL,- status text NOT NULL CHECK (status IN ('PENDING', 'AVAILABLE', 'RUNNING', 'DONE', 'FAILED', 'CANCELED')),- started_at timestamptz NULL,- run_at timestamptz,- -- result saves the task run result in json format- -- Stored as TaskRunResult (proto/store/store/task_run.proto)- result jsonb NOT NULL DEFAULT '{}',- replica_id TEXT,- -- Stored as TaskRunPayload (proto/store/store/task_run.proto)- payload jsonb NOT NULL DEFAULT '{}',- PRIMARY KEY (project, id),- FOREIGN KEY (project, task_id) REFERENCES task(project, id)-);--CREATE INDEX idx_task_run_task_id ON task_run(task_id);--CREATE UNIQUE INDEX uk_task_run_task_id_attempt ON task_run(project, task_id, attempt);---- Partial index for active task runs. Most task runs are in terminal states (DONE, FAILED, CANCELED)--- that never change. Queries frequently filter for active statuses (PENDING, RUNNING), so a partial--- index is more efficient than a full index on status - smaller size, faster maintenance, better cache efficiency.-CREATE INDEX idx_task_run_active_status_id ON task_run(status, id) WHERE status IN ('PENDING', 'AVAILABLE', 'RUNNING');--ALTER SEQUENCE task_run_id_seq RESTART WITH 101;--CREATE INDEX idx_task_run_running_replica ON task_run(replica_id) WHERE status = 'RUNNING' AND replica_id IS NOT NULL;---- replica_heartbeat tracks active replicas in HA deployments.--- Used to detect and clean up stale RUNNING task runs from crashed replicas.-CREATE TABLE replica_heartbeat (- replica_id TEXT PRIMARY KEY,- last_heartbeat TIMESTAMPTZ NOT NULL-);--CREATE TABLE task_run_log (- project text NOT NULL REFERENCES project(resource_id),- task_run_id integer NOT NULL,- created_at timestamptz NOT NULL DEFAULT now(),- -- Stored as TaskRunLog (proto/store/store/task_run_log.proto)- payload jsonb NOT NULL DEFAULT '{}',- PRIMARY KEY (project, task_run_id, created_at),- FOREIGN KEY (project, task_run_id) REFERENCES task_run(project, id)-);---- Pipeline related END-------------------------- issueCREATE TABLE issue (id serial,@@ -308,42 +285,12 @@ CREATE TABLE issue ();CREATE INDEX idx_issue_project ON issue(project);-CREATE UNIQUE INDEX idx_issue_unique_plan_id ON issue(project, plan_id);-CREATE INDEX idx_issue_creator ON issue(creator);-CREATE INDEX idx_issue_ts_vector ON issue USING GIN(ts_vector);ALTER SEQUENCE issue_id_seq RESTART WITH 101;--- instance change history records the changes an instance and its databases.-CREATE TABLE instance_change_history (- id bigserial PRIMARY KEY,- version text NOT NULL-);--CREATE UNIQUE INDEX idx_instance_change_history_unique_version ON instance_change_history (version);--ALTER SEQUENCE instance_change_history_id_seq RESTART WITH 101;--CREATE TABLE audit_log (- resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,- created_at timestamptz NOT NULL DEFAULT now(),- -- Stored as AuditLog (proto/store/store/audit_log.proto)- payload jsonb NOT NULL DEFAULT '{}'-);--CREATE INDEX idx_audit_log_created_at ON audit_log(created_at);--CREATE INDEX idx_audit_log_payload_parent ON audit_log((payload->>'parent'));--CREATE INDEX idx_audit_log_payload_method ON audit_log((payload->>'method'));--CREATE INDEX idx_audit_log_payload_resource ON audit_log((payload->>'resource'));--CREATE INDEX idx_audit_log_payload_user ON audit_log((payload->>'user'));-CREATE TABLE issue_comment (resource_id text NOT NULL DEFAULT gen_random_uuid()::text,creator text NOT NULL,@@ -360,22 +307,6 @@ CREATE TABLE issue_comment (CREATE INDEX idx_issue_comment_issue_id ON issue_comment(project, issue_id);CREATE UNIQUE INDEX idx_issue_comment_unique_resource_id ON issue_comment(resource_id);-CREATE TABLE query_history (- resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,- creator text NOT NULL,- created_at timestamptz NOT NULL DEFAULT now(),- project text NOT NULL REFERENCES project(resource_id),- database text NOT NULL, -- the database resource name, for example, instances/{instance}/databases/{database}- statement text NOT NULL,- -- type: QUERY, EXPORT- type text NOT NULL,- -- saved for details, like error, duration, etc.- -- Stored as QueryHistoryPayload (proto/store/store/query_history.proto)- payload jsonb NOT NULL DEFAULT '{}'-);--CREATE INDEX idx_query_history_creator_created_at_project ON query_history(creator, created_at, project DESC);--- worksheet table stores worksheets in SQL Editor.CREATE TABLE worksheet (id serial,@@ -409,7 +340,6 @@ CREATE TABLE worksheet_organizer ();CREATE INDEX idx_worksheet_organizer_principal ON worksheet_organizer(principal);-CREATE INDEX idx_worksheet_organizer_payload ON worksheet_organizer USING GIN(payload);CREATE TABLE db_group (@@ -423,34 +353,99 @@ CREATE TABLE db_group (PRIMARY KEY (project, resource_id));-CREATE TABLE export_archive (- resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,- created_at timestamptz NOT NULL DEFAULT now(),- bytes bytea,- -- Stored as ExportArchivePayload (proto/store/store/export_archive.proto)- payload jsonb NOT NULL DEFAULT '{}'+CREATE TABLE release (+ project text NOT NULL REFERENCES project(resource_id),+ train text NOT NULL DEFAULT '',+ iteration integer NOT NULL DEFAULT 0,+ deleted boolean NOT NULL DEFAULT FALSE,+ release_id text NOT NULL DEFAULT '',+ creator text NOT NULL,+ created_at timestamptz NOT NULL DEFAULT now(),+ category text NOT NULL DEFAULT '',+ -- Stored as ReleasePayload (proto/store/store/release.proto)+ payload jsonb NOT NULL DEFAULT '{}',+ PRIMARY KEY (project, train, iteration));-CREATE TABLE user_group (- id text PRIMARY KEY DEFAULT gen_random_uuid()::text,- email text,- name text NOT NULL,- description text NOT NULL DEFAULT '',- -- Stored as GroupPayload (proto/store/store/group.proto)- payload jsonb NOT NULL DEFAULT '{}'+CREATE INDEX idx_release_project ON release(project);+CREATE INDEX idx_release_project_release_id ON release(project, release_id);+CREATE INDEX idx_release_category ON release(project, category);++CREATE TABLE access_grant (+ id text PRIMARY KEY,+ project text NOT NULL REFERENCES project(resource_id),+ creator text NOT NULL,+ status text NOT NULL DEFAULT 'PENDING',+ expire_time timestamptz,+ -- Stored as AccessGrantPayload (proto/store/store/access_grant.proto)+ payload jsonb NOT NULL DEFAULT '{}',+ created_at timestamptz NOT NULL DEFAULT now(),+ updated_at timestamptz NOT NULL DEFAULT now());-CREATE UNIQUE INDEX idx_user_group_unique_email ON user_group(email) WHERE email IS NOT NULL;+CREATE INDEX idx_access_grant_project_creator_expire_time ON access_grant(project, creator, expire_time);--- review config table.-CREATE TABLE review_config (- id text NOT NULL PRIMARY KEY,- enabled boolean NOT NULL DEFAULT TRUE,- name text NOT NULL,- -- Stored as ReviewConfigPayload (proto/store/store/review_config.proto)+CREATE TABLE query_history (+ resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,+ creator text NOT NULL,+ created_at timestamptz NOT NULL DEFAULT now(),+ project text NOT NULL REFERENCES project(resource_id),+ database text NOT NULL, -- the database resource name, for example, instances/{instance}/databases/{database}+ statement text NOT NULL,+ -- type: QUERY, EXPORT+ type text NOT NULL,+ -- saved for details, like error, duration, etc.+ -- Stored as QueryHistoryPayload (proto/store/store/query_history.proto)payload jsonb NOT NULL DEFAULT '{}');+CREATE INDEX idx_query_history_creator_created_at_project ON query_history(creator, created_at, project DESC);++-----------------------+-- Instance and instance-scoped tables+-----------------------++CREATE TABLE instance (+ resource_id text NOT NULL PRIMARY KEY,+ workspace text NOT NULL REFERENCES workspace(resource_id),+ deleted boolean NOT NULL DEFAULT FALSE,+ environment text,+ -- Stored as Instance (proto/store/store/instance.proto)+ metadata jsonb NOT NULL DEFAULT '{}'+);++CREATE UNIQUE INDEX idx_instance_unique_workspace_resource_id ON instance(workspace, resource_id);+CREATE INDEX idx_instance_workspace ON instance(workspace);+CREATE INDEX idx_instance_metadata_engine ON instance((metadata->>'engine'));++-- db stores the databases for a particular instance+-- data is synced periodically from the instance+CREATE TABLE db (+ instance text NOT NULL REFERENCES instance(resource_id),+ name text NOT NULL,+ deleted boolean NOT NULL DEFAULT FALSE,+ project text NOT NULL REFERENCES project(resource_id),+ environment text,+ -- Stored as DatabaseMetadata (proto/store/store/database.proto)+ metadata jsonb NOT NULL DEFAULT '{}',+ PRIMARY KEY (instance, name)+);++CREATE INDEX idx_db_project ON db(project);++-- db_schema stores the database schema metadata for a particular database.+CREATE TABLE db_schema (+ instance text NOT NULL,+ db_name text NOT NULL,+ -- Stored as DatabaseSchemaMetadata (proto/store/store/database.proto)+ metadata json NOT NULL DEFAULT '{}',+ raw_dump text NOT NULL DEFAULT '',+ -- Stored as DatabaseConfig (proto/store/store/database.proto)+ config jsonb NOT NULL DEFAULT '{}',+ PRIMARY KEY (instance, db_name),+ CONSTRAINT db_schema_instance_db_name_fkey FOREIGN KEY(instance, db_name) REFERENCES db(instance, name)+);+CREATE TABLE revision (resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,instance text NOT NULL,@@ -465,7 +460,6 @@ CREATE TABLE revision ();CREATE UNIQUE INDEX idx_revision_unique_instance_db_name_type_version_deleted_at_null ON revision(instance, db_name, (payload->>'type'), version) WHERE deleted_at IS NULL;-CREATE INDEX idx_revision_instance_db_name_type_version ON revision(instance, db_name, (payload->>'type'), version);CREATE TABLE sync_history (@@ -495,25 +489,92 @@ CREATE TABLE changelog (CREATE INDEX idx_changelog_instance_db_name ON changelog (instance, db_name);-CREATE TABLE release (+-- instance change history records the changes an instance and its databases.+CREATE TABLE instance_change_history (+ id bigserial PRIMARY KEY,+ version text NOT NULL+);++CREATE UNIQUE INDEX idx_instance_change_history_unique_version ON instance_change_history (version);++ALTER SEQUENCE instance_change_history_id_seq RESTART WITH 101;++-----------------------+-- Pipeline (cross project + instance)+-----------------------++-- task table stores the task for a plan+CREATE TABLE task (+ id serial,project text NOT NULL REFERENCES project(resource_id),- train text NOT NULL DEFAULT '',- iteration integer NOT NULL DEFAULT 0,- deleted boolean NOT NULL DEFAULT FALSE,- release_id text NOT NULL DEFAULT '',- creator text NOT NULL,+ plan_id bigint NOT NULL,+ instance text NOT NULL REFERENCES instance(resource_id),+ environment text,+ db_name text,+ type text NOT NULL,+ -- Stored as Task (proto/store/store/task.proto)+ payload jsonb NOT NULL DEFAULT '{}',+ PRIMARY KEY (project, id),+ FOREIGN KEY (project, plan_id) REFERENCES plan(project, id)+);++CREATE INDEX idx_task_plan_id_environment ON task(project, plan_id, environment);++ALTER SEQUENCE task_id_seq RESTART WITH 101;++-- task run table stores the task run+CREATE TABLE task_run (+ id serial,+ creator text,created_at timestamptz NOT NULL DEFAULT now(),- category text NOT NULL DEFAULT '',- -- Stored as ReleasePayload (proto/store/store/release.proto)+ updated_at timestamptz NOT NULL DEFAULT now(),+ project text NOT NULL REFERENCES project(resource_id),+ task_id integer NOT NULL,+ attempt integer NOT NULL,+ status text NOT NULL CHECK (status IN ('PENDING', 'AVAILABLE', 'RUNNING', 'DONE', 'FAILED', 'CANCELED')),+ started_at timestamptz NULL,+ run_at timestamptz,+ -- result saves the task run result in json format+ -- Stored as TaskRunResult (proto/store/store/task_run.proto)+ result jsonb NOT NULL DEFAULT '{}',+ replica_id TEXT,+ -- Stored as TaskRunPayload (proto/store/store/task_run.proto)payload jsonb NOT NULL DEFAULT '{}',- PRIMARY KEY (project, train, iteration)+ PRIMARY KEY (project, id),+ FOREIGN KEY (project, task_id) REFERENCES task(project, id));-CREATE INDEX idx_release_project ON release(project);-CREATE INDEX idx_release_project_release_id ON release(project, release_id);-CREATE INDEX idx_release_category ON release(project, category);+CREATE INDEX idx_task_run_task_id ON task_run(task_id);+CREATE UNIQUE INDEX uk_task_run_task_id_attempt ON task_run(project, task_id, attempt);+-- Partial index for active task runs. Most task runs are in terminal states (DONE, FAILED, CANCELED)+-- that never change. Queries frequently filter for active statuses (PENDING, RUNNING), so a partial+-- index is more efficient than a full index on status - smaller size, faster maintenance, better cache efficiency.+CREATE INDEX idx_task_run_active_status_id ON task_run(status, id) WHERE status IN ('PENDING', 'AVAILABLE', 'RUNNING');+CREATE INDEX idx_task_run_running_replica ON task_run(replica_id) WHERE status = 'RUNNING' AND replica_id IS NOT NULL;++ALTER SEQUENCE task_run_id_seq RESTART WITH 101;++-- replica_heartbeat tracks active replicas in HA deployments.+-- Used to detect and clean up stale RUNNING task runs from crashed replicas.+CREATE TABLE replica_heartbeat (+ replica_id TEXT PRIMARY KEY,+ last_heartbeat TIMESTAMPTZ NOT NULL+);++CREATE TABLE task_run_log (+ project text NOT NULL REFERENCES project(resource_id),+ task_run_id integer NOT NULL,+ created_at timestamptz NOT NULL DEFAULT now(),+ -- Stored as TaskRunLog (proto/store/store/task_run_log.proto)+ payload jsonb NOT NULL DEFAULT '{}',+ PRIMARY KEY (project, task_run_id, created_at),+ FOREIGN KEY (project, task_run_id) REFERENCES task_run(project, id)+);++-----------------------+-- OAuth2 and auth+-------------------------- OAuth2 tablesCREATE TABLE oauth2_client (client_id text PRIMARY KEY,client_secret_hash text NOT NULL,@@ -550,49 +611,41 @@ CREATE TABLE web_refresh_token (CREATE INDEX idx_web_refresh_token_user_email ON web_refresh_token(user_email);CREATE INDEX idx_web_refresh_token_expires_at ON web_refresh_token(expires_at);-ALTER SEQUENCE principal_id_seq RESTART WITH 101;--CREATE TABLE access_grant (- id text PRIMARY KEY,- project text NOT NULL REFERENCES project(resource_id),- creator text NOT NULL,- status text NOT NULL DEFAULT 'PENDING',- expire_time timestamptz,- -- Stored as AccessGrantPayload (proto/store/store/access_grant.proto)- payload jsonb NOT NULL DEFAULT '{}',- created_at timestamptz NOT NULL DEFAULT now(),- updated_at timestamptz NOT NULL DEFAULT now()-);--CREATE INDEX idx_access_grant_project_creator_expire_time ON access_grant(project, creator, expire_time);+-----------------------+-- Seed data+-------------------------- Default project.-INSERT INTO project (name, resource_id) VALUES ('Default', 'default');---- Initialize settings with static values-INSERT INTO setting (name, value) VALUES ('APP_IM', '{}'::jsonb);-INSERT INTO setting (name, value) VALUES ('DATA_CLASSIFICATION', '{}'::jsonb);-INSERT INTO setting (name, value) VALUES ('WORKSPACE_APPROVAL', '{"rules":[{"template":{"flow":{"roles":["roles/projectOwner"]},"title":"Fallback Rule","description":"Requires project owner approval when no other rules match."},"condition":{"expression":"true"}}]}'::jsonb);-INSERT INTO setting (name, value) VALUES (- 'WORKSPACE_PROFILE',- ('{"enableMetricCollection":true,"directorySyncToken":"' || gen_random_uuid()::text || '","passwordRestriction":{"minLength":8}}')::jsonb-);-INSERT INTO setting (name, value) VALUES ('ENVIRONMENT', '{"environments":[{"title":"Test","id":"test"},{"title":"Prod","id":"prod"}]}'::jsonb);---- Initialize settings with dynamically generated values--- Generate random alphanumeric string (0-9, a-z, A-Z) compatible with Go's common.RandomString--- Initialize SYSTEM setting with auth_secret and workspace_id-INSERT INTO setting (name, value)-VALUES (- 'SYSTEM',- json_build_object(- 'authSecret', (SELECT string_agg(substr('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', floor(random() * 62 + 1)::int, 1), '')- FROM generate_series(1, 32)),- 'workspaceId', gen_random_uuid()::text- )-);---- Initialize workspace IAM policy--- Grant workspace member role to allUsers-INSERT INTO policy (resource_type, resource, type, payload, inherit_from_parent, enforce)-VALUES ('WORKSPACE', 'workspaces/' || (SELECT value->>'workspaceId' FROM setting WHERE name = 'SYSTEM'), 'IAM', '{"bindings":[{"role":"roles/workspaceMember","members":["allUsers"]}]}', FALSE, TRUE);+-- Generate a workspace once, then use it everywhere.+DO $$+DECLARE+ ws_id text := gen_random_uuid()::text;+ auth_secret text;+BEGIN+ -- Generate random alphanumeric auth_secret (0-9, a-z, A-Z) compatible with Go's common.RandomString+ SELECT string_agg(substr('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', floor(random() * 62 + 1)::int, 1), '')+ INTO auth_secret+ FROM generate_series(1, 32);++ -- Create the default workspace.+ INSERT INTO workspace (resource_id, name) VALUES (ws_id, 'Default Workspace');++ -- Default project.+ INSERT INTO project (name, resource_id, workspace) VALUES ('Default', 'default', ws_id);++ -- Initialize settings.+ INSERT INTO setting (name, workspace, value) VALUES ('SYSTEM', ws_id,+ json_build_object('authSecret', auth_secret));+ INSERT INTO setting (name, workspace, value) VALUES ('APP_IM', ws_id, '{}'::jsonb);+ INSERT INTO setting (name, workspace, value) VALUES ('DATA_CLASSIFICATION', ws_id, '{}'::jsonb);+ INSERT INTO setting (name, workspace, value) VALUES ('WORKSPACE_APPROVAL', ws_id,+ '{"rules":[{"template":{"flow":{"roles":["roles/projectOwner"]},"title":"Fallback Rule","description":"Requires project owner approval when no other rules match."},"condition":{"expression":"true"}}]}'::jsonb);+ INSERT INTO setting (name, workspace, value) VALUES ('WORKSPACE_PROFILE', ws_id,+ ('{"enableMetricCollection":true,"directorySyncToken":"' || gen_random_uuid()::text || '","passwordRestriction":{"minLength":8}}')::jsonb);+ INSERT INTO setting (name, workspace, value) VALUES ('ENVIRONMENT', ws_id,+ '{"environments":[{"title":"Test","id":"test"},{"title":"Prod","id":"prod"}]}'::jsonb);++ -- Initialize workspace IAM policy — grant workspace member role to allUsers.+ INSERT INTO policy (workspace, resource_type, resource, type, payload, inherit_from_parent, enforce)+ VALUES (ws_id, 'WORKSPACE', 'workspaces/' || ws_id, 'IAM',+ '{"bindings":[{"role":"roles/workspaceMember","members":["allUsers"]}]}', FALSE, TRUE);+END $$;diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.goindex 68c5f7c966..3e7d2329d5 100644--- a/backend/migrator/migrator_test.go+++ b/backend/migrator/migrator_test.go@@ -15,8 +15,8 @@ import (func TestLatestVersion(t *testing.T) {files, err := getSortedVersionedFiles()require.NoError(t, err)- require.Equal(t, semver.MustParse("3.17.8"), *files[len(files)-1].version)- require.Equal(t, "migration/3.17/0008##cross_workspace_uuid_pk.sql", files[len(files)-1].path)+ require.Equal(t, semver.MustParse("3.17.9"), *files[len(files)-1].version)+ require.Equal(t, "migration/3.17/0009##add_workspace_table.sql", files[len(files)-1].path)}func TestVersionUnique(t *testing.T) {diff --git a/backend/runner/taskrun/data_export_executor.go b/backend/runner/taskrun/data_export_executor.goindex 2c7dd853e8..182ae52103 100644--- a/backend/runner/taskrun/data_export_executor.go+++ b/backend/runner/taskrun/data_export_executor.go@@ -115,8 +115,13 @@ func (exec *DataExportExecutor) RunOnce(ctx context.Context, _ context.Context,return nil, exportErr}+ workspace, err := exec.store.GetWorkspace(ctx)+ if err != nil {+ return nil, errors.Wrap(err, "failed to get workspace")+ }exportArchive, err := exec.store.CreateExportArchive(ctx, &store.ExportArchiveMessage{- Bytes: bytes,+ Workspace: workspace.ResourceID,+ Bytes: bytes,Payload: &storepb.ExportArchivePayload{FileFormat: exportConfig.Format,},diff --git a/backend/server/server.go b/backend/server/server.goindex 488a7c2007..7ae9753f08 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -188,9 +188,14 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {}secret := systemSetting.AuthSecret+ workspace, err := s.store.GetWorkspace(ctx)+ if err != nil {+ return nil, errors.Wrap(err, "failed to get workspace")+ }+// Initialize telemetry reporter for hub.bytebase.com event reporting.telemetry.InitGlobalReporter(- systemSetting.GetWorkspaceId(),+ workspace.ResourceID,profile.Version,profile.GitCommit,workspaceProfile.GetEnableMetricCollection(),diff --git a/backend/store/audit_log.go b/backend/store/audit_log.goindex 46316855c6..b0d456b585 100644--- a/backend/store/audit_log.go+++ b/backend/store/audit_log.go@@ -19,6 +19,7 @@ import (type AuditLog struct {ResourceID string+ Workspace stringCreatedAt time.TimePayload *storepb.AuditLog}@@ -31,13 +32,13 @@ type AuditLogFind struct {OrderByKeys []*OrderByKey}-func (s *Store) CreateAuditLog(ctx context.Context, payload *storepb.AuditLog) error {+func (s *Store) CreateAuditLog(ctx context.Context, workspace string, payload *storepb.AuditLog) error {p, err := protojson.Marshal(payload)if err != nil {return errors.Wrapf(err, "failed to marshal payload")}- q := qb.Q().Space("INSERT INTO audit_log (payload) VALUES (?)", p)+ q := qb.Q().Space("INSERT INTO audit_log (workspace, payload) VALUES (?, ?)", workspace, p)sql, args, err := q.ToSQL()if err != nil {return errors.Wrapf(err, "failed to build sql")@@ -53,6 +54,7 @@ func (s *Store) SearchAuditLogs(ctx context.Context, find *AuditLogFind) ([]*Audq := qb.Q().Space(`SELECTresource_id,+ workspace,created_at,payloadFROM audit_log@@ -102,6 +104,7 @@ func (s *Store) SearchAuditLogs(ctx context.Context, find *AuditLogFind) ([]*Audif err := rows.Scan(&l.ResourceID,+ &l.Workspace,&l.CreatedAt,&payload,); err != nil {diff --git a/backend/store/export_archive.go b/backend/store/export_archive.goindex b5b1240465..5973602c25 100644--- a/backend/store/export_archive.go+++ b/backend/store/export_archive.go@@ -14,6 +14,7 @@ import (type ExportArchiveMessage struct {ResourceID string+ Workspace stringCreatedAt time.TimeBytes []bytePayload *storepb.ExportArchivePayload@@ -24,6 +25,7 @@ func (s *Store) GetExportArchive(ctx context.Context, resourceID string) (*Exporq := qb.Q().Space(`SELECTresource_id,+ workspace,created_at,bytes,payload@@ -40,6 +42,7 @@ func (s *Store) GetExportArchive(ctx context.Context, resourceID string) (*Exporvar bytesVal, payload []byteif err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&exportArchive.ResourceID,+ &exportArchive.Workspace,&exportArchive.CreatedAt,&bytesVal,&payload,@@ -68,12 +71,13 @@ func (s *Store) CreateExportArchive(ctx context.Context, create *ExportArchiveMeq := qb.Q().Space(`INSERT INTO export_archive (+ workspace,bytes,payload)- VALUES (?, ?)+ VALUES (?, ?, ?)RETURNING resource_id- `, create.Bytes, payload)+ `, create.Workspace, create.Bytes, payload)query, args, err := q.ToSQL()if err != nil {diff --git a/backend/store/group.go b/backend/store/group.goindex aefe02a081..dcde3f9a88 100644--- a/backend/store/group.go+++ b/backend/store/group.go@@ -43,6 +43,7 @@ type UpdateGroupMessage struct {// GroupMessage is the message for a group.type GroupMessage struct {ID string+ Workspace stringEmail stringTitle stringDescription string@@ -108,6 +109,7 @@ func (s *Store) ListGroups(ctx context.Context, find *FindGroupMessage) ([]*Grouq.Space(`SELECTuser_group.id,+ user_group.workspace,user_group.email,user_group.name,user_group.description,@@ -142,6 +144,7 @@ func (s *Store) ListGroups(ctx context.Context, find *FindGroupMessage) ([]*Grouvar payload []byteif err := rows.Scan(&group.ID,+ &group.Workspace,&email,&group.Title,&group.Description,@@ -188,13 +191,14 @@ func (s *Store) CreateGroup(ctx context.Context, create *GroupMessage) (*GroupMeq := qb.Q().Space(`INSERT INTO user_group (id,+ workspace,email,name,description,payload- ) VALUES (COALESCE(NULLIF(?, ''), gen_random_uuid()::text), ?, ?, ?, ?)+ ) VALUES (COALESCE(NULLIF(?, ''), gen_random_uuid()::text), ?, ?, ?, ?, ?)RETURNING id- `, create.ID, email, create.Title, create.Description, payloadBytes)+ `, create.ID, create.Workspace, email, create.Title, create.Description, payloadBytes)query, args, err := q.ToSQL()if err != nil {diff --git a/backend/store/idp.go b/backend/store/idp.goindex 21b33de510..cd77b96deb 100644--- a/backend/store/idp.go+++ b/backend/store/idp.go@@ -15,6 +15,7 @@ import (// IdentityProviderMessage is the message for identity provider.type IdentityProviderMessage struct {ResourceID string+ Workspace stringTitle stringDomain stringType storepb.IdentityProviderType@@ -53,6 +54,7 @@ type UpdateIdentityProviderMessage struct {func (s *Store) CreateIdentityProvider(ctx context.Context, create *IdentityProviderMessage) (*IdentityProviderMessage, error) {identityProvider := &IdentityProviderMessage{ResourceID: create.ResourceID,+ Workspace: create.Workspace,Title: create.Title,Domain: create.Domain,Type: create.Type,@@ -66,13 +68,14 @@ func (s *Store) CreateIdentityProvider(ctx context.Context, create *IdentityProvq := qb.Q().Space(`INSERT INTO idp (resource_id,+ workspace,name,domain,type,config)- VALUES (?, ?, ?, ?, ?)- `, create.ResourceID, create.Title, create.Domain, create.Type.String(), configBytes)+ VALUES (?, ?, ?, ?, ?, ?)+ `, create.ResourceID, create.Workspace, create.Title, create.Domain, create.Type.String(), configBytes)query, args, err := q.ToSQL()if err != nil {@@ -107,6 +110,7 @@ func (s *Store) ListIdentityProviders(ctx context.Context, find *FindIdentityProq := qb.Q().Space(`SELECTresource_id,+ workspace,name,domain,type,@@ -139,6 +143,7 @@ func (s *Store) ListIdentityProviders(ctx context.Context, find *FindIdentityProvar identityProviderConfig stringif err := rows.Scan(&identityProviderMessage.ResourceID,+ &identityProviderMessage.Workspace,&identityProviderMessage.Title,&identityProviderMessage.Domain,&identityProviderType,@@ -183,6 +188,7 @@ func (s *Store) UpdateIdentityProvider(ctx context.Context, patch *UpdateIdentitWHERE resource_id = ?RETURNINGresource_id,+ workspace,name,domain,type,@@ -199,6 +205,7 @@ func (s *Store) UpdateIdentityProvider(ctx context.Context, patch *UpdateIdentitvar identityProviderConfig stringif err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&identityProvider.ResourceID,+ &identityProvider.Workspace,&identityProvider.Title,&identityProvider.Domain,&identityProviderType,diff --git a/backend/store/instance.go b/backend/store/instance.goindex 051b58f1be..a8d6be56e9 100644--- a/backend/store/instance.go+++ b/backend/store/instance.go@@ -23,6 +23,7 @@ import (// InstanceMessage is the message for instance.type InstanceMessage struct {ResourceID string+ Workspace stringEnvironmentID *stringDeleted boolMetadata *storepb.Instance@@ -41,6 +42,7 @@ type UpdateInstanceMessage struct {// FindInstanceMessage is the message for finding instances.type FindInstanceMessage struct {+ Workspace *stringResourceID *stringResourceIDs *[]stringShowDeleted bool@@ -83,6 +85,9 @@ func (s *Store) ListInstances(ctx context.Context, find *FindInstanceMessage) ([if filterQ := find.FilterQ; filterQ != nil {where.And("?", filterQ)}+ if v := find.Workspace; v != nil {+ where.And("instance.workspace = ?", *v)+ }if v := find.ResourceID; v != nil {where.And("instance.resource_id = ?", *v)}@@ -96,6 +101,7 @@ func (s *Store) ListInstances(ctx context.Context, find *FindInstanceMessage) ([q := qb.Q().Space(`SELECTinstance.resource_id,+ instance.workspace,instance.environment,instance.deleted,instance.metadata@@ -137,6 +143,7 @@ func (s *Store) ListInstances(ctx context.Context, find *FindInstanceMessage) ([var metadata []byteif err := rows.Scan(&instanceMessage.ResourceID,+ &instanceMessage.Workspace,&environment,&instanceMessage.Deleted,&metadata,@@ -189,10 +196,11 @@ func (s *Store) CreateInstance(ctx context.Context, instanceCreate *InstanceMessq := qb.Q().Space(`INSERT INTO instance (resource_id,+ workspace,environment,metadata- ) VALUES (?, ?, ?)- `, instanceCreate.ResourceID, environment, metadataBytes)+ ) VALUES (?, ?, ?, ?)+ `, instanceCreate.ResourceID, instanceCreate.Workspace, environment, metadataBytes)query, args, err := q.ToSQL()if err != nil {return nil, errors.Wrapf(err, "failed to build sql")@@ -204,6 +212,7 @@ func (s *Store) CreateInstance(ctx context.Context, instanceCreate *InstanceMessinstance := &InstanceMessage{EnvironmentID: instanceCreate.EnvironmentID,ResourceID: instanceCreate.ResourceID,+ Workspace: instanceCreate.Workspace,Metadata: instanceCreate.Metadata,}s.instanceCache.Add(getInstanceCacheKey(instance.ResourceID), instance)diff --git a/backend/store/policy.go b/backend/store/policy.goindex 411130c119..e4dd03c215 100644--- a/backend/store/policy.go+++ b/backend/store/policy.go@@ -34,11 +34,11 @@ func generateEtag(t time.Time) string {// GetWorkspaceResourceName returns the workspace resource name "workspaces/{id}".func (s *Store) GetWorkspaceResourceName(ctx context.Context) (string, error) {- systemSetting, err := s.GetSystemSetting(ctx)+ workspace, err := s.GetWorkspace(ctx)if err != nil {- return "", errors.Wrap(err, "failed to get system setting")+ return "", err}- return common.FormatWorkspace(systemSetting.WorkspaceId), nil+ return common.FormatWorkspace(workspace.ResourceID), nil}func (s *Store) GetWorkspaceIamPolicy(ctx context.Context) (*IamPolicyMessage, error) {@@ -109,7 +109,12 @@ func (s *Store) PatchWorkspaceIamPolicy(ctx context.Context, patch *PatchIamPolireturn nil, err}+ workspace, err := s.GetWorkspace(ctx)+ if err != nil {+ return nil, err+ }if _, err := s.CreatePolicy(ctx, &PolicyMessage{+ Workspace: workspace.ResourceID,ResourceType: storepb.Policy_WORKSPACE,Resource: workspaceResource,Payload: string(policyPayload),@@ -447,6 +452,7 @@ func (s *Store) GetMaskingExemptionPolicyByProject(ctx context.Context, projectI// PolicyMessage is the mssage for policy.type PolicyMessage struct {+ Workspace stringResource stringResourceType storepb.Policy_ResourcePayload string@@ -511,6 +517,7 @@ func (s *Store) GetPolicy(ctx context.Context, find *FindPolicyMessage) (*Policyfunc (s *Store) ListPolicies(ctx context.Context, find *FindPolicyMessage) ([]*PolicyMessage, error) {q := qb.Q().Space(`SELECT+ workspace,updated_at,resource_type,resource,@@ -551,6 +558,7 @@ func (s *Store) ListPolicies(ctx context.Context, find *FindPolicyMessage) ([]*Pvar policyMessage PolicyMessagevar resourceTypeString, typeString stringif err := rows.Scan(+ &policyMessage.Workspace,&policyMessage.UpdatedAt,&resourceTypeString,&policyMessage.Resource,@@ -674,6 +682,7 @@ func upsertPolicyImpl(ctx context.Context, txn *sql.Tx, create *PolicyMessage) (q := qb.Q().Space(`INSERT INTO policy (+ workspace,resource_type,resource,inherit_from_parent,@@ -682,13 +691,14 @@ func upsertPolicyImpl(ctx context.Context, txn *sql.Tx, create *PolicyMessage) (enforce,updated_at)- VALUES (?, ?, ?, ?, ?, ?, ?)+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)ON CONFLICT(resource_type, resource, type) DO UPDATE SETinherit_from_parent = EXCLUDED.inherit_from_parent,payload = EXCLUDED.payload,enforce = EXCLUDED.enforce,updated_at = EXCLUDED.updated_at`,+ create.Workspace,create.ResourceType.String(),create.Resource,create.InheritFromParent,diff --git a/backend/store/project.go b/backend/store/project.goindex 49bf986903..e98e901c8b 100644--- a/backend/store/project.go+++ b/backend/store/project.go@@ -22,6 +22,7 @@ import (// ProjectMessage is the message for project.type ProjectMessage struct {ResourceID string+ Workspace stringTitle stringWebhooks []*ProjectWebhookMessageSetting *storepb.Project@@ -34,6 +35,7 @@ func (p *ProjectMessage) GetName() string {// FindProjectMessage is the message for finding projects.type FindProjectMessage struct {+ Workspace *stringResourceID *stringResourceIDs []stringShowDeleted bool@@ -81,10 +83,13 @@ func (s *Store) GetProject(ctx context.Context, find *FindProjectMessage) (*Proj// ListProjects lists all projects.func (s *Store) ListProjects(ctx context.Context, find *FindProjectMessage) ([]*ProjectMessage, error) {- q := qb.Q().Space("SELECT resource_id, name, setting, deleted FROM project WHERE TRUE")+ q := qb.Q().Space("SELECT resource_id, workspace, name, setting, deleted FROM project WHERE TRUE")if filterQ := find.FilterQ; filterQ != nil {q.And("?", filterQ)}+ if v := find.Workspace; v != nil {+ q.And("workspace = ?", *v)+ }if v := find.ResourceID; v != nil {q.And("resource_id = ?", *v)}@@ -128,6 +133,7 @@ func (s *Store) ListProjects(ctx context.Context, find *FindProjectMessage) ([]*var payload []byteif err := rows.Scan(&projectMessage.ResourceID,+ &projectMessage.Workspace,&projectMessage.Title,&payload,&projectMessage.Deleted,@@ -192,11 +198,12 @@ func (s *Store) CreateProject(ctx context.Context, create *ProjectMessage, creatproject := &ProjectMessage{ResourceID: create.ResourceID,+ Workspace: create.Workspace,Title: create.Title,Setting: create.Setting,}- q := qb.Q().Space("INSERT INTO project (resource_id, name, setting)")- q.Space("VALUES (?, ?, ?)", create.ResourceID, create.Title, payload)+ q := qb.Q().Space("INSERT INTO project (resource_id, workspace, name, setting)")+ q.Space("VALUES (?, ?, ?, ?)", create.ResourceID, create.Workspace, create.Title, payload)sql, args, err := q.ToSQL()if err != nil {return nil, err@@ -221,6 +228,7 @@ func (s *Store) CreateProject(ctx context.Context, create *ProjectMessage, creatreturn nil, err}policyMessage, err := upsertPolicyImpl(ctx, tx, &PolicyMessage{+ Workspace: create.Workspace,ResourceType: storepb.Policy_PROJECT,Resource: common.FormatProject(project.ResourceID),Payload: string(policyPayload),diff --git a/backend/store/review_config.go b/backend/store/review_config.goindex 7b6a360199..4d4b9d51ff 100644--- a/backend/store/review_config.go+++ b/backend/store/review_config.go@@ -14,10 +14,11 @@ import (// ReviewConfigMessage is the API message for sql review.type ReviewConfigMessage struct {- ID string- Enforce bool- Name string- Payload *storepb.ReviewConfigPayload+ ID string+ Workspace string+ Enforce bool+ Name string+ Payload *storepb.ReviewConfigPayload}// FindReviewConfigMessage is the API message for finding sql review.@@ -54,6 +55,7 @@ func (s *Store) ListReviewConfigs(ctx context.Context, find *FindReviewConfigMesq := qb.Q().Space(`SELECTid,+ workspace,enabled,name,payload@@ -83,6 +85,7 @@ func (s *Store) ListReviewConfigs(ctx context.Context, find *FindReviewConfigMesif err := rows.Scan(&sqlReview.ID,+ &sqlReview.Workspace,&sqlReview.Enforce,&sqlReview.Name,&payload,@@ -118,11 +121,12 @@ func (s *Store) CreateReviewConfig(ctx context.Context, create *ReviewConfigMessq := qb.Q().Space(`INSERT INTO review_config (id,+ workspace,name,payload)- VALUES (?, ?, ?)- `, create.ID, create.Name, payload)+ VALUES (?, ?, ?, ?)+ `, create.ID, create.Workspace, create.Name, payload)query, args, err := q.ToSQL()if err != nil {@@ -183,6 +187,7 @@ func (s *Store) UpdateReviewConfig(ctx context.Context, patch *PatchReviewConfigWHERE id = ?RETURNINGid,+ workspace,enabled,name,payload@@ -198,6 +203,7 @@ func (s *Store) UpdateReviewConfig(ctx context.Context, patch *PatchReviewConfigif err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&sqlReview.ID,+ &sqlReview.Workspace,&sqlReview.Enforce,&sqlReview.Name,&payload,diff --git a/backend/store/role.go b/backend/store/role.goindex 6fca94cbdd..faca088413 100644--- a/backend/store/role.go+++ b/backend/store/role.go@@ -15,6 +15,7 @@ import (// RoleMessage is the message for roles.type RoleMessage struct {ResourceID string+ Workspace stringName stringDescription stringPermissions map[permission.Permission]bool@@ -98,9 +99,9 @@ func (s *Store) CreateRole(ctx context.Context, create *RoleMessage) (*RoleMessaq := qb.Q().Space(`INSERT INTO- role (resource_id, name, description, permissions)- VALUES (?, ?, ?, ?)- `, create.ResourceID, create.Name, create.Description, permissionBytes)+ role (resource_id, workspace, name, description, permissions)+ VALUES (?, ?, ?, ?, ?)+ `, create.ResourceID, create.Workspace, create.Name, create.Description, permissionBytes)query, args, err := q.ToSQL()if err != nil {@@ -152,7 +153,7 @@ func (s *Store) ListRoles(ctx context.Context, find *FindRoleMessage) ([]*RoleMeq := qb.Q().Space(`SELECT- resource_id, name, description, permissions+ resource_id, workspace, name, description, permissionsFROM roleWHERE TRUE`)@@ -178,7 +179,7 @@ func (s *Store) ListRoles(ctx context.Context, find *FindRoleMessage) ([]*RoleMePermissions: map[permission.Permission]bool{},}var permissionBytes []byte- if err := rows.Scan(&role.ResourceID, &role.Name, &role.Description, &permissionBytes); err != nil {+ if err := rows.Scan(&role.ResourceID, &role.Workspace, &role.Name, &role.Description, &permissionBytes); err != nil {return nil, err}var rolePermissions storepb.RolePermissionsdiff --git a/backend/store/service_account.go b/backend/store/service_account.goindex c41525a5ab..def8275779 100644--- a/backend/store/service_account.go+++ b/backend/store/service_account.go@@ -15,6 +15,7 @@ type ServiceAccountMessage struct {// Email must be lower case, format: {name}@{project-id}.service.bytebase.com or {name}@service.bytebase.comEmail stringName string+ Workspace stringServiceKeyHash stringMemberDeleted bool// Project is the owning project. NULL for workspace-level service accounts.@@ -38,6 +39,7 @@ type CreateServiceAccountMessage struct {// Email must be lower case.Email stringName string+ Workspace stringServiceKeyHash string// Project is the owning project. NULL for workspace-level service accounts.Project *string@@ -88,6 +90,7 @@ func (s *Store) ListServiceAccounts(ctx context.Context, find *FindServiceAccoundeleted,email,name,+ workspace,service_key_hash,projectFROM service_account@@ -121,6 +124,7 @@ func (s *Store) ListServiceAccounts(ctx context.Context, find *FindServiceAccoun&sa.MemberDeleted,&sa.Email,&sa.Name,+ &sa.Workspace,&sa.ServiceKeyHash,&project,); err != nil {@@ -146,11 +150,12 @@ func (s *Store) CreateServiceAccount(ctx context.Context, create *CreateServiceAINSERT INTO service_account (email,name,+ workspace,service_key_hash,project)- VALUES (?, ?, ?, ?)- `, email, create.Name, create.ServiceKeyHash, create.Project)+ VALUES (?, ?, ?, ?, ?)+ `, email, create.Name, create.Workspace, create.ServiceKeyHash, create.Project)sqlStr, args, err := q.ToSQL()if err != nil {@@ -164,6 +169,7 @@ func (s *Store) CreateServiceAccount(ctx context.Context, create *CreateServiceAreturn &ServiceAccountMessage{Email: email,Name: create.Name,+ Workspace: create.Workspace,ServiceKeyHash: create.ServiceKeyHash,Project: create.Project,}, nil@@ -187,7 +193,7 @@ func (s *Store) UpdateServiceAccount(ctx context.Context, sa *ServiceAccountMess}sqlStr, args, err := qb.Q().Space(`UPDATE service_account SET ? WHERE email = ?- RETURNING deleted, email, name, service_key_hash, project`,+ RETURNING deleted, email, name, workspace, service_key_hash, project`,set, sa.Email).ToSQL()if err != nil {return nil, errors.Wrapf(err, "failed to build sql")@@ -199,6 +205,7 @@ func (s *Store) UpdateServiceAccount(ctx context.Context, sa *ServiceAccountMess&updated.MemberDeleted,&updated.Email,&updated.Name,+ &updated.Workspace,&updated.ServiceKeyHash,&project,); err != nil {diff --git a/backend/store/setting.go b/backend/store/setting.goindex 88acf374f6..1a7295c0a2 100644--- a/backend/store/setting.go+++ b/backend/store/setting.go@@ -16,8 +16,9 @@ import (// SettingMessage is the message of setting.type SettingMessage struct {- Name storepb.SettingName- Value proto.Message+ Name storepb.SettingName+ Workspace string+ Value proto.Message}func getSettingMessage(name storepb.SettingName) (proto.Message, error) {@@ -134,8 +135,9 @@ func (s *Store) UpdateLicense(ctx context.Context, license string) error {systemSetting.License = licenseif _, err := s.UpsertSetting(ctx, &SettingMessage{- Name: storepb.SettingName_SYSTEM,- Value: systemSetting,+ Name: storepb.SettingName_SYSTEM,+ Workspace: setting.Workspace,+ Value: systemSetting,}); err != nil {return errors.Wrap(err, "failed to upsert system setting")}@@ -255,6 +257,7 @@ func (s *Store) ListSettings(ctx context.Context, find *FindSettingMessage) ([]*q := qb.Q().Space(`SELECTname,+ workspace,valueFROM settingWHERE TRUE@@ -279,6 +282,7 @@ func (s *Store) ListSettings(ctx context.Context, find *FindSettingMessage) ([]*var valueString stringif err := rows.Scan(&nameString,+ &settingMessage.Workspace,&valueString,); err != nil {return nil, err@@ -318,11 +322,11 @@ func (s *Store) UpsertSetting(ctx context.Context, update *SettingMessage) (*Set}q := qb.Q().Space(`- INSERT INTO setting (name, value)- VALUES (?, ?)+ INSERT INTO setting (name, workspace, value)+ VALUES (?, ?, ?)ON CONFLICT (name) DO UPDATE SET value = EXCLUDED.value- RETURNING name, value- `, update.Name.String(), string(valueBytes))+ RETURNING name, workspace, value+ `, update.Name.String(), update.Workspace, string(valueBytes))query, args, err := q.ToSQL()if err != nil {@@ -334,6 +338,7 @@ func (s *Store) UpsertSetting(ctx context.Context, update *SettingMessage) (*Setvar valueString stringif err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&nameString,+ &setting.Workspace,&valueString,); err != nil {if err == sql.ErrNoRows {diff --git a/backend/store/workload_identity.go b/backend/store/workload_identity.goindex 7207a4d064..409772677a 100644--- a/backend/store/workload_identity.go+++ b/backend/store/workload_identity.go@@ -18,6 +18,7 @@ type WorkloadIdentityMessage struct {// Email must be lower case, format: {name}@{project-id}.workload.bytebase.com or {name}@workload.bytebase.comEmail stringName string+ Workspace stringMemberDeleted bool// Project is the owning project. NULL for workspace-level workload identities.Project *string@@ -40,8 +41,9 @@ type FindWorkloadIdentityMessage struct {// CreateWorkloadIdentityMessage is the message for creating a workload identity.type CreateWorkloadIdentityMessage struct {// Email must be lower case.- Email string- Name string+ Email string+ Name string+ Workspace string// Project is the owning project. NULL for workspace-level workload identities.Project *string// Config is the workload identity configuration.@@ -93,6 +95,7 @@ func (s *Store) ListWorkloadIdentities(ctx context.Context, find *FindWorkloadIddeleted,email,name,+ workspace,project,configFROM workload_identity@@ -127,6 +130,7 @@ func (s *Store) ListWorkloadIdentities(ctx context.Context, find *FindWorkloadId&wi.MemberDeleted,&wi.Email,&wi.Name,+ &wi.Workspace,&project,&configBytes,); err != nil {@@ -162,11 +166,12 @@ func (s *Store) CreateWorkloadIdentity(ctx context.Context, create *CreateWorkloINSERT INTO workload_identity (email,name,+ workspace,project,config)- VALUES (?, ?, ?, ?)- `, email, create.Name, create.Project, configBytes)+ VALUES (?, ?, ?, ?, ?)+ `, email, create.Name, create.Workspace, create.Project, configBytes)sqlStr, args, err := q.ToSQL()if err != nil {@@ -178,10 +183,11 @@ func (s *Store) CreateWorkloadIdentity(ctx context.Context, create *CreateWorklo}return &WorkloadIdentityMessage{- Email: email,- Name: create.Name,- Project: create.Project,- Config: create.Config,+ Email: email,+ Name: create.Name,+ Workspace: create.Workspace,+ Project: create.Project,+ Config: create.Config,}, nil}@@ -207,7 +213,7 @@ func (s *Store) UpdateWorkloadIdentity(ctx context.Context, wi *WorkloadIdentity}sqlStr, args, err := qb.Q().Space(`UPDATE workload_identity SET ? WHERE email = ?- RETURNING deleted, email, name, project, config`,+ RETURNING deleted, email, name, workspace, project, config`,set, wi.Email).ToSQL()if err != nil {return nil, errors.Wrapf(err, "failed to build sql")@@ -220,6 +226,7 @@ func (s *Store) UpdateWorkloadIdentity(ctx context.Context, wi *WorkloadIdentity&updated.MemberDeleted,&updated.Email,&updated.Name,+ &updated.Workspace,&project,&configBytes,); err != nil {diff --git a/backend/store/workspace.go b/backend/store/workspace.gonew file mode 100644index 0000000000..61b3f98f96--- /dev/null+++ b/backend/store/workspace.go@@ -0,0 +1,24 @@+package store++import (+ "context"++ "github.com/pkg/errors"+)++// WorkspaceMessage is the message for a workspace.+type WorkspaceMessage struct {+ ResourceID string+ Name string+}++// GetWorkspace returns the workspace.+func (s *Store) GetWorkspace(ctx context.Context) (*WorkspaceMessage, error) {+ var workspace WorkspaceMessage+ if err := s.GetDB().QueryRowContext(ctx,+ `SELECT resource_id, name FROM workspace WHERE deleted = FALSE LIMIT 1`,+ ).Scan(&workspace.ResourceID, &workspace.Name); err != nil {+ return nil, errors.Wrap(err, "failed to get workspace")+ }+ return &workspace, nil+}diff --git a/backend/utils/utils.go b/backend/utils/utils.goindex 62ee90170b..3ae83a47af 100644--- a/backend/utils/utils.go+++ b/backend/utils/utils.go@@ -135,7 +135,12 @@ func UpdateProjectPolicyFromRoleGrantIssue(ctx context.Context, stores *store.Stif err != nil {return err}+ workspace, err := stores.GetWorkspace(ctx)+ if err != nil {+ return errors.Wrap(err, "failed to get workspace")+ }if _, err := stores.CreatePolicy(ctx, &store.PolicyMessage{+ Workspace: workspace.ResourceID,Resource: common.FormatProject(issue.ProjectID),ResourceType: storepb.Policy_PROJECT,Payload: string(policyPayload),diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex 3f66c0af7d..209b54d996 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -4405,7 +4405,6 @@ The severity level for SQL review rules.| Field | Type | Label | Description || ----- | ---- | ----- | ----------- || auth_secret | [string](#string) | | Authentication secret for token signing (32-character random string). |-| workspace_id | [string](#string) | | Unique workspace identifier (UUID). || license | [string](#string) | | Enterprise license JWT token. |diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.htmlindex 213f788864..364823c616 100644--- a/proto/gen/grpc-doc/store/index.html+++ b/proto/gen/grpc-doc/store/index.html@@ -12106,13 +12106,6 @@ are /[a-z][0-9]-/. </p></td><td><p>Authentication secret for token signing (32-character random string). </p></td></tr>- <tr>- <td>workspace_id</td>- <td><a href="#string">string</a></td>- <td></td>- <td><p>Unique workspace identifier (UUID). </p></td>- </tr>-<tr><td>license</td><td><a href="#string">string</a></td>diff --git a/proto/store/store/setting.proto b/proto/store/store/setting.protoindex e609379a8e..d24bd49a56 100644--- a/proto/store/store/setting.proto+++ b/proto/store/store/setting.proto@@ -25,8 +25,8 @@ enum SettingName {message SystemSetting {// Authentication secret for token signing (32-character random string).string auth_secret = 1;- // Unique workspace identifier (UUID).- string workspace_id = 2;+ // Field 2 was workspace_id, now stored in the workspace table.+ reserved 2;// Enterprise license JWT token.string license = 3;}