Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: pass user objects instead of IDs to avoid redundant DB lookups (#18454)

Danny Xu3ba7408a9d15
diff --git a/backend/api/v1/actuator_service.go b/backend/api/v1/actuator_service.go
index 6dbe24231a..6f6d28c7ca 100644
--- a/backend/api/v1/actuator_service.go
+++ b/backend/api/v1/actuator_service.go
@@ -122,7 +122,7 @@ func (s *ActuatorService) SetupSample(
if !ok || user == nil {
return nil, connect.NewError(connect.CodeInternal, errors.New("user not found"))
}
- if err := s.sampleInstanceManager.GenerateOnboardingData(ctx, user.ID, s.schemaSyncer); err != nil {
+ if err := s.sampleInstanceManager.GenerateOnboardingData(ctx, user, s.schemaSyncer); err != nil {
// When running inside docker on mac, we sometimes get database does not exist error.
// This is due to the docker overlay storage incompatibility with mac OS file system.
// Onboarding error is not critical, so we just emit an error log.
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.go
index 805e6dbb49..a8007987f5 100644
--- a/backend/api/v1/issue_service.go
+++ b/backend/api/v1/issue_service.go
@@ -45,8 +45,8 @@ type IssueService struct {
type filterIssueMessage struct {
ApprovalStatus *v1pb.Issue_ApprovalStatus
- // ApproverID is the principal uid.
- ApproverID *int
+ // Approver is the user who can approve the issue.
+ Approver *store.UserMessage
}
// NewIssueService creates a new IssueService.
@@ -192,7 +192,7 @@ func (s *IssueService) getIssueFind(
return "", connect.NewError(connect.CodeInternal, errors.Errorf("failed to get user %v with error %v", value, err.Error()))
}
if variable == "current_approver" {
- filterIssue.ApproverID = &user.ID
+ filterIssue.Approver = user
} else {
issueFind.CreatorID = &user.Email
}
@@ -802,7 +802,7 @@ func (s *IssueService) ApproveIssue(ctx context.Context, req *connect.Request[v1
}
s.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: s.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_ISSUE_APPROVAL_NOTIFY,
Comment: "",
Issue: webhook.NewIssue(issue),
@@ -820,7 +820,7 @@ func (s *IssueService) ApproveIssue(ctx context.Context, req *connect.Request[v1
// notify issue approved
s.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: s.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_NOTIFY_ISSUE_APPROVED,
Comment: "",
Issue: webhook.NewIssue(issue),
@@ -872,7 +872,7 @@ func (s *IssueService) ApproveIssue(ctx context.Context, req *connect.Request[v1
return errors.Wrap(err, "failed to check if the approval is approved")
}
if approved {
- if err := webhook.ChangeIssueStatus(ctx, s.store, s.webhookManager, issue, storepb.Issue_DONE, s.store.GetSystemBotUser(ctx), ""); err != nil {
+ if err := webhook.ChangeIssueStatus(ctx, s.store, s.webhookManager, issue, storepb.Issue_DONE, store.SystemBotUser, ""); err != nil {
return errors.Wrap(err, "failed to update issue status")
}
}
@@ -1030,7 +1030,7 @@ func (s *IssueService) RequestIssue(ctx context.Context, req *connect.Request[v1
}
s.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: s.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_ISSUE_APPROVAL_NOTIFY,
Comment: "",
Issue: webhook.NewIssue(issue),
@@ -1525,7 +1525,7 @@ func (s *IssueService) getIssueMessage(ctx context.Context, name string) (*store
}
func (s *IssueService) isUserReviewer(ctx context.Context, issue *store.IssueMessage, role string, user *store.UserMessage) bool {
- roles := s.getUserRoleMap(ctx, issue.Project.ResourceID, user.ID)
+ roles := s.getUserRoleMap(ctx, issue.Project.ResourceID, user)
return roles[role]
}
diff --git a/backend/api/v1/issue_service_converter.go b/backend/api/v1/issue_service_converter.go
index 86cbf7c6b6..fa87b60f59 100644
--- a/backend/api/v1/issue_service_converter.go
+++ b/backend/api/v1/issue_service_converter.go
@@ -35,7 +35,7 @@ func (s *IssueService) convertToIssues(ctx context.Context, issues []*store.Issu
slog.Error("failed to parse the issue name", log.BBError(err), slog.String("issue", v1Issue.Name))
continue
}
- if v := issueFilter.ApproverID; v != nil && !s.isIssueNextApprover(ctx, v1Issue, projectID, *v) {
+ if v := issueFilter.Approver; v != nil && !s.isIssueNextApprover(ctx, v1Issue, projectID, v) {
continue
}
}
@@ -44,10 +44,14 @@ func (s *IssueService) convertToIssues(ctx context.Context, issues []*store.Issu
return converted, nil
}
-func (s *IssueService) getUserRoleMap(ctx context.Context, projectResourceID string, principalUID int) map[string]bool {
+func (s *IssueService) getUserRoleMap(ctx context.Context, projectResourceID string, user *store.UserMessage) map[string]bool {
+ if user == nil {
+ return map[string]bool{}
+ }
+
policy, err := s.store.GetProjectIamPolicy(ctx, projectResourceID)
- slog.Error("failed to get project iam policy", log.BBError(err), slog.String("project", projectResourceID))
if err != nil {
+ slog.Error("failed to get project iam policy", log.BBError(err), slog.String("project", projectResourceID))
return map[string]bool{}
}
workspacePolicy, err := s.store.GetWorkspaceIamPolicy(ctx)
@@ -56,20 +60,15 @@ func (s *IssueService) getUserRoleMap(ctx context.Context, projectResourceID str
return map[string]bool{}
}
- user, err := s.store.GetUserByID(ctx, principalUID)
- if err != nil {
- slog.Error("failed to get user", log.BBError(err), slog.Int("user_id", principalUID))
- return map[string]bool{}
- }
- if user == nil {
- return map[string]bool{}
- }
-
return utils.GetUserFormattedRolesMap(ctx, s.store, user, policy.Policy, workspacePolicy.Policy)
}
-func (s *IssueService) isIssueNextApprover(ctx context.Context, issue *v1pb.Issue, projectResourceID string, principalUID int) bool {
- roles := s.getUserRoleMap(ctx, projectResourceID, principalUID)
+func (s *IssueService) isIssueNextApprover(ctx context.Context, issue *v1pb.Issue, projectResourceID string, user *store.UserMessage) bool {
+ if user == nil {
+ return false
+ }
+
+ roles := s.getUserRoleMap(ctx, projectResourceID, user)
approvalRoles := issue.GetApprovalTemplate().GetFlow().GetRoles()
index := len(issue.Approvers)
if index >= len(approvalRoles) {
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index ec1e39eb06..7ae437d2ae 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -204,7 +204,7 @@ func (s *ProjectService) CreateProject(ctx context.Context, req *connect.Request
}
project, err := s.store.CreateProject(ctx,
projectMessage,
- user.ID,
+ user,
)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
diff --git a/backend/component/sampleinstance/manager.go b/backend/component/sampleinstance/manager.go
index 06745f9344..c4368c3f9d 100644
--- a/backend/component/sampleinstance/manager.go
+++ b/backend/component/sampleinstance/manager.go
@@ -152,7 +152,7 @@ func (m *Manager) Port() int {
}
// GenerateOnboardingData generates onboarding data including project and instance.
-func (m *Manager) GenerateOnboardingData(ctx context.Context, userID int, schemaSyncer *schemasync.Syncer) error {
+func (m *Manager) GenerateOnboardingData(ctx context.Context, user *store.UserMessage, schemaSyncer *schemasync.Syncer) error {
setting := &storepb.Project{
AllowModifyStatement: true,
AutoResolveIssue: true,
@@ -170,7 +170,7 @@ func (m *Manager) GenerateOnboardingData(ctx context.Context, userID int, schema
ResourceID: "project-sample",
Title: "Sample Project",
Setting: setting,
- }, userID)
+ }, user)
if err != nil {
return errors.Wrapf(err, "failed to create onboarding project")
}
diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.go
index fcc464265a..a81bdf91d0 100644
--- a/backend/runner/approval/runner.go
+++ b/backend/runner/approval/runner.go
@@ -189,7 +189,7 @@ func (r *Runner) findApprovalTemplateForIssue(ctx context.Context, issue *store.
if err := utils.UpdateProjectPolicyFromGrantIssue(ctx, r.store, issue, payload.GrantRequest); err != nil {
return false, err
}
- if err := webhook.ChangeIssueStatus(ctx, r.store, r.webhookManager, issue, storepb.Issue_DONE, r.store.GetSystemBotUser(ctx), ""); err != nil {
+ if err := webhook.ChangeIssueStatus(ctx, r.store, r.webhookManager, issue, storepb.Issue_DONE, store.SystemBotUser, ""); err != nil {
return false, errors.Wrap(err, "failed to update issue status")
}
}
@@ -234,7 +234,7 @@ func (r *Runner) findApprovalTemplateForIssue(ctx context.Context, issue *store.
return err
}
r.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: r.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_NOTIFY_PIPELINE_ROLLOUT,
Comment: "",
Issue: webhook.NewIssue(issue),
@@ -258,7 +258,7 @@ func (r *Runner) findApprovalTemplateForIssue(ctx context.Context, issue *store.
return
}
r.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: r.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_ISSUE_APPROVAL_NOTIFY,
Comment: "",
Issue: webhook.NewIssue(issue),
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index f6d7b7832f..362345c2ce 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -252,7 +252,7 @@ func (s *Scheduler) scheduleAutoRolloutTask(ctx context.Context, taskUID int) er
}
}
s.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: s.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_ISSUE_PIPELINE_TASK_RUN_STATUS_UPDATE,
Comment: "",
Issue: webhook.NewIssue(issue),
@@ -839,7 +839,7 @@ func (s *Scheduler) ListenTaskSkippedOrDone(ctx context.Context) {
// every task in the stage terminated
// create "stage ends" activity.
s.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: s.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_ISSUE_PIPELINE_STAGE_STATUS_UPDATE,
Comment: "",
Issue: webhook.NewIssue(issueN),
@@ -881,7 +881,7 @@ func (s *Scheduler) ListenTaskSkippedOrDone(ctx context.Context) {
return errors.Wrapf(err, "failed to get rollout policy")
}
s.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: s.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_NOTIFY_PIPELINE_ROLLOUT,
Comment: "",
Issue: webhook.NewIssue(issueN),
@@ -927,7 +927,7 @@ func (s *Scheduler) ListenTaskSkippedOrDone(ctx context.Context) {
}
s.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: s.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_ISSUE_STATUS_UPDATE,
Comment: "",
Issue: webhook.NewIssue(updatedIssue),
@@ -972,7 +972,7 @@ func (s *Scheduler) createActivityForTaskRunStatusUpdate(ctx context.Context, ta
return errors.Wrap(err, "failed to get issue")
}
s.webhookManager.CreateEvent(ctx, &webhook.Event{
- Actor: s.store.GetSystemBotUser(ctx),
+ Actor: store.SystemBotUser,
Type: storepb.Activity_ISSUE_PIPELINE_TASK_RUN_STATUS_UPDATE,
Comment: "",
Issue: webhook.NewIssue(issue),
diff --git a/backend/store/principal.go b/backend/store/principal.go
index 401470fa19..274d467134 100644
--- a/backend/store/principal.go
+++ b/backend/store/principal.go
@@ -3,7 +3,6 @@ package store
import (
"context"
"database/sql"
- "log/slog"
"strings"
"time"
@@ -13,12 +12,12 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/bytebase/bytebase/backend/common"
- "github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/common/qb"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)
-var systemBotUser = &UserMessage{
+// SystemBotUser is the static system bot user.
+var SystemBotUser = &UserMessage{
ID: common.SystemBotID,
Name: "Bytebase",
@@ -73,19 +72,6 @@ type UserStat struct {
Count int
}
-// GetSystemBotUser gets the system bot.
-func (s *Store) GetSystemBotUser(ctx context.Context) *UserMessage {
- user, err := s.GetUserByID(ctx, common.SystemBotID)
- if err != nil {
- slog.Error("failed to find system bot", slog.Int("id", common.SystemBotID), log.BBError(err))
- return systemBotUser
- }
- if user == nil {
- return systemBotUser
- }
- return user
-}
-
// GetUserByID gets the user by ID.
func (s *Store) GetUserByID(ctx context.Context, id int) (*UserMessage, error) {
if v, ok := s.userIDCache.Get(id); ok && s.enableCache {
diff --git a/backend/store/project.go b/backend/store/project.go
index 71e4e695d3..1727d00afd 100644
--- a/backend/store/project.go
+++ b/backend/store/project.go
@@ -170,10 +170,9 @@ func (s *Store) ListProjects(ctx context.Context, find *FindProjectMessage) ([]*
}
// CreateProject creates a project.
-func (s *Store) CreateProject(ctx context.Context, create *ProjectMessage, creatorID int) (*ProjectMessage, error) {
- user, err := s.GetUserByID(ctx, creatorID)
- if err != nil {
- return nil, err
+func (s *Store) CreateProject(ctx context.Context, create *ProjectMessage, creator *UserMessage) (*ProjectMessage, error) {
+ if creator == nil {
+ return nil, errors.Errorf("creator cannot be nil")
}
if create.Setting == nil {
create.Setting = &storepb.Project{}
@@ -210,7 +209,7 @@ func (s *Store) CreateProject(ctx context.Context, create *ProjectMessage, creat
{
Role: common.FormatRole(common.ProjectOwner),
Members: []string{
- common.FormatUserEmail(user.Email),
+ common.FormatUserEmail(creator.Email),
},
Condition: &expr.Expr{},
},