Skip to content

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

chore: migrate to issue v1 store (#4401)

Danny Xubb9678b5fdf2
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 838da34479..42c20d2144 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -170,12 +170,12 @@ func (s *ProjectService) DeleteProject(ctx context.Context, request *v1pb.Delete
if len(databases) > 0 {
return nil, status.Errorf(codes.FailedPrecondition, "transfer all databases under the project before deleting the project")
}
- issueList, err := s.store.FindIssueStripped(ctx, &api.IssueFind{ProjectID: &project.UID, StatusList: []api.IssueStatus{api.IssueOpen}})
+ openIssues, err := s.store.ListIssueV2(ctx, &store.FindIssueMessage{ProjectUID: &project.UID, StatusList: []api.IssueStatus{api.IssueOpen}})
if err != nil {
return nil, err
}
- if len(issueList) > 0 {
- return nil, status.Errorf(codes.FailedPrecondition, "resolve all issues before deleting the project")
+ if len(openIssues) > 0 {
+ return nil, status.Errorf(codes.FailedPrecondition, "resolve all open issues before deleting the project")
}
if _, err := s.store.UpdateProjectV2(ctx, &store.UpdateProjectMessage{
diff --git a/backend/component/activity/manager.go b/backend/component/activity/manager.go
index c56bcc3262..2993470bee 100644
--- a/backend/component/activity/manager.go
+++ b/backend/component/activity/manager.go
@@ -9,6 +9,8 @@ import (
"strconv"
"time"
+ "github.com/gosimple/slug"
+
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/config"
@@ -41,14 +43,14 @@ func NewManager(store *store.Store, profile config.Profile) *Manager {
}
// BatchCreateTaskStatusUpdateApprovalActivity creates a batch task status update activities for task approvals.
-func (m *Manager) BatchCreateTaskStatusUpdateApprovalActivity(ctx context.Context, taskList []*api.Task, updaterID int, issue *api.Issue, stage *api.Stage) error {
+func (m *Manager) BatchCreateTaskStatusUpdateApprovalActivity(ctx context.Context, taskList []*api.Task, updaterID int, issue *store.IssueMessage, stage *api.Stage) error {
var createList []*api.ActivityCreate
for _, task := range taskList {
payload, err := json.Marshal(api.ActivityPipelineTaskStatusUpdatePayload{
TaskID: task.ID,
OldStatus: api.TaskPendingApproval,
NewStatus: api.TaskPending,
- IssueName: issue.Name,
+ IssueName: issue.Title,
TaskName: task.Name,
})
if err != nil {
@@ -76,11 +78,11 @@ func (m *Manager) BatchCreateTaskStatusUpdateApprovalActivity(ctx context.Contex
activityType := api.ActivityPipelineTaskStatusUpdate
webhookList, err := m.store.FindProjectWebhook(ctx, &api.ProjectWebhookFind{
- ProjectID: &issue.ProjectID,
+ ProjectID: &issue.Project.UID,
ActivityType: &activityType,
})
if err != nil {
- return errors.Wrapf(err, "failed to find project webhook after changing the issue status: %v", issue.Name)
+ return errors.Wrapf(err, "failed to find project webhook after changing the issue status: %v", issue.Title)
}
if len(webhookList) == 0 {
return nil
@@ -91,18 +93,18 @@ func (m *Manager) BatchCreateTaskStatusUpdateApprovalActivity(ctx context.Contex
ActivityType: string(activityType),
Title: fmt.Sprintf("Stage tasks approved - %s", stage.Name),
Issue: &webhook.Issue{
- ID: issue.ID,
- Name: issue.Name,
+ ID: issue.UID,
+ Name: issue.Title,
Status: string(issue.Status),
Type: string(issue.Type),
Description: issue.Description,
},
Project: &webhook.Project{
- ID: issue.ProjectID,
- Name: issue.Project.Name,
+ ID: issue.Project.UID,
+ Name: issue.Project.Title,
},
Description: anyActivity.Comment,
- Link: fmt.Sprintf("%s/issue/%s", m.profile.ExternalURL, api.IssueSlug(issue)),
+ Link: fmt.Sprintf("%s/issue/%s-%d", m.profile.ExternalURL, slug.Make(issue.Title), issue.UID),
CreatorID: anyActivity.CreatorID,
CreatorName: anyActivity.Creator.Name,
CreatorEmail: anyActivity.Creator.Email,
diff --git a/backend/runner/apprun/runner.go b/backend/runner/apprun/runner.go
index a5e3d03db2..d57632740f 100644
--- a/backend/runner/apprun/runner.go
+++ b/backend/runner/apprun/runner.go
@@ -8,6 +8,7 @@ import (
"sync"
"time"
+ "github.com/gosimple/slug"
"github.com/pkg/errors"
"go.uber.org/zap"
@@ -76,21 +77,27 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {
return
}
- assigneeNeedAtterntion := true
- find := &api.IssueFind{
- StatusList: []api.IssueStatus{api.IssueOpen},
- AssigneeNeedAttention: &assigneeNeedAtterntion,
- }
+ issueByID := make(map[int]*store.IssueMessage)
+ pipelineByID := make(map[int]*api.Pipeline)
- issueByID := make(map[int]*api.Issue)
- issues, err := r.store.FindIssue(ctx, find)
+ needAttention := true
+ issues, err := r.store.ListIssueV2(ctx, &store.FindIssueMessage{
+ StatusList: []api.IssueStatus{api.IssueOpen},
+ NeedAttention: &needAttention,
+ })
if err != nil {
- log.Error("failed to find issue stripped", zap.Any("issueFind", find), zap.Error(err))
+ log.Error("failed to find issues", zap.Error(err))
return
}
for _, issue := range issues {
- issueByID[issue.ID] = issue
- r.scheduleApproval(ctx, issue, &value)
+ issueByID[issue.UID] = issue
+ pipeline, err := r.store.GetPipelineByID(ctx, issue.PipelineUID)
+ if err != nil {
+ log.Error("failed to get pipeline", zap.Int("id", issue.PipelineUID), zap.Error(err))
+ return
+ }
+ pipelineByID[issue.PipelineUID] = pipeline
+ r.scheduleApproval(ctx, issue, pipeline, &value)
}
externalApprovalList, err := r.store.FindExternalApproval(ctx, &api.ExternalApprovalFind{})
@@ -117,12 +124,17 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {
log.Error("expect to have found issue in application runner", zap.Int("issue_id", externalApproval.IssueID))
continue
}
- stage := utils.GetActiveStage(issue.Pipeline)
+ composedPipeline, ok := pipelineByID[issue.PipelineUID]
+ if !ok {
+ log.Error("expect to have found pipeline in application runner", zap.Int("pipeline_id", issue.PipelineUID))
+ continue
+ }
+ stage := utils.GetActiveStage(composedPipeline)
if stage == nil {
- stage = issue.Pipeline.StageList[len(issue.Pipeline.StageList)-1]
+ stage = composedPipeline.StageList[len(composedPipeline.StageList)-1]
}
if issue.Status != api.IssueOpen {
- if err := r.CancelExternalApproval(ctx, issue.ID, api.ExternalApprovalCancelReasonIssueNotOpen); err != nil {
+ if err := r.CancelExternalApproval(ctx, issue.UID, api.ExternalApprovalCancelReasonIssueNotOpen); err != nil {
log.Error("failed to cancel external approval", zap.Error(err))
}
continue
@@ -143,7 +155,7 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {
switch status {
case feishu.ApprovalStatusApproved:
// double check
- if stage.ID == payload.StageID && payload.AssigneeID == issue.AssigneeID {
+ if stage.ID == payload.StageID && payload.AssigneeID == issue.Assignee.ID {
// approve stage
if err := func() error {
var taskIDList []int
@@ -194,7 +206,7 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {
}
stageName := "UNKNOWN"
- for _, stage := range issue.Pipeline.StageList {
+ for _, stage := range composedPipeline.StageList {
if stage.ID == payload.StageID {
stageName = stage.Name
break
@@ -213,7 +225,7 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {
activityCreate := &api.ActivityCreate{
CreatorID: payload.AssigneeID,
- ContainerID: issue.ID,
+ ContainerID: issue.UID,
Type: api.ActivityIssueCommentCreate,
Level: api.ActivityInfo,
Comment: "",
@@ -239,8 +251,8 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {
}
}
-func (r *Runner) cancelOldExternalApprovalIfNeeded(ctx context.Context, issue *api.Issue, stage *api.Stage, settingValue *api.SettingAppIMValue) (*api.ExternalApproval, error) {
- approval, err := r.store.GetExternalApprovalByIssueID(ctx, issue.ID)
+func (r *Runner) cancelOldExternalApprovalIfNeeded(ctx context.Context, issue *store.IssueMessage, stage *api.Stage, settingValue *api.SettingAppIMValue) (*api.ExternalApproval, error) {
+ approval, err := r.store.GetExternalApprovalByIssueID(ctx, issue.UID)
if err != nil {
return nil, err
}
@@ -258,7 +270,7 @@ func (r *Runner) cancelOldExternalApprovalIfNeeded(ctx context.Context, issue *a
reason = api.ExternalApprovalCancelReasonNoTaskPendingApproval
return true
}
- if payload.AssigneeID != issue.AssigneeID {
+ if payload.AssigneeID != issue.Assignee.ID {
reason = api.ExternalApprovalCancelReasonReassigned
return true
}
@@ -377,7 +389,7 @@ func (r *Runner) CancelExternalApproval(ctx context.Context, issueID int, reason
)
}
-func (r *Runner) shouldCreateExternalApproval(ctx context.Context, issue *api.Issue, stage *api.Stage, oldApproval *api.ExternalApproval) (bool, error) {
+func (r *Runner) shouldCreateExternalApproval(ctx context.Context, issue *store.IssueMessage, stage *api.Stage, oldApproval *api.ExternalApproval) (bool, error) {
policy, err := r.store.GetPipelineApprovalPolicy(ctx, stage.EnvironmentID)
if err != nil {
return false, err
@@ -392,7 +404,7 @@ func (r *Runner) shouldCreateExternalApproval(ctx context.Context, issue *api.Is
return false, err
}
// nothing changes
- if oldPayload.StageID == stage.ID && oldPayload.AssigneeID == issue.AssigneeID {
+ if oldPayload.StageID == stage.ID && oldPayload.AssigneeID == issue.Assignee.ID {
return false, nil
}
}
@@ -434,7 +446,7 @@ func (r *Runner) shouldCreateExternalApproval(ctx context.Context, issue *api.Is
return true, nil
}
-func (r *Runner) createExternalApproval(ctx context.Context, issue *api.Issue, stage *api.Stage, settingValue *api.SettingAppIMValue) error {
+func (r *Runner) createExternalApproval(ctx context.Context, issue *store.IssueMessage, stage *api.Stage, settingValue *api.SettingAppIMValue) error {
users, err := r.p.GetIDByEmail(ctx,
feishu.TokenCtx{
AppID: settingValue.AppID,
@@ -485,9 +497,9 @@ func (r *Runner) createExternalApproval(ctx context.Context, issue *api.Issue, s
AppSecret: settingValue.AppSecret,
},
feishu.Content{
- Issue: fmt.Sprintf("#%d %s", issue.ID, issue.Name),
+ Issue: fmt.Sprintf("#%d %s", issue.UID, issue.Title),
Stage: stage.Name,
- Link: fmt.Sprintf("%s/issue/%s", r.profile.ExternalURL, api.IssueSlug(issue)),
+ Link: fmt.Sprintf("%s/issue/%s-%d", r.profile.ExternalURL, slug.Make(issue.Title), issue.UID),
TaskList: taskList,
},
settingValue.ExternalApproval.ApprovalDefinitionID,
@@ -498,7 +510,7 @@ func (r *Runner) createExternalApproval(ctx context.Context, issue *api.Issue, s
}
payload := api.ExternalApprovalPayloadFeishu{
StageID: stage.ID,
- AssigneeID: issue.AssigneeID,
+ AssigneeID: issue.Assignee.ID,
InstanceCode: instanceCode,
RequesterID: users[issue.Creator.Email],
Rejected: false,
@@ -508,9 +520,9 @@ func (r *Runner) createExternalApproval(ctx context.Context, issue *api.Issue, s
return err
}
if _, err := r.store.CreateExternalApproval(ctx, &api.ExternalApprovalCreate{
- IssueID: issue.ID,
- ApproverID: issue.AssigneeID,
- RequesterID: issue.CreatorID,
+ IssueID: issue.UID,
+ ApproverID: issue.Assignee.ID,
+ RequesterID: issue.Creator.ID,
Type: api.ExternalApprovalTypeFeishu,
Payload: string(b),
}); err != nil {
@@ -520,7 +532,7 @@ func (r *Runner) createExternalApproval(ctx context.Context, issue *api.Issue, s
}
// scheduleApproval tries to cancel old external apporvals and create new external approvals if needed.
-func (r *Runner) scheduleApproval(ctx context.Context, issue *api.Issue, settingValue *api.SettingAppIMValue) {
+func (r *Runner) scheduleApproval(ctx context.Context, issue *store.IssueMessage, composedPipeline *api.Pipeline, settingValue *api.SettingAppIMValue) {
if !settingValue.ExternalApproval.Enabled {
return
}
@@ -530,9 +542,9 @@ func (r *Runner) scheduleApproval(ctx context.Context, issue *api.Issue, setting
return
}
- stage := utils.GetActiveStage(issue.Pipeline)
+ stage := utils.GetActiveStage(composedPipeline)
if stage == nil {
- stage = issue.Pipeline.StageList[len(issue.Pipeline.StageList)-1]
+ stage = composedPipeline.StageList[len(composedPipeline.StageList)-1]
}
oldApproval, err := r.cancelOldExternalApprovalIfNeeded(ctx, issue, stage, settingValue)
diff --git a/backend/server/stage.go b/backend/server/stage.go
index ce27b47b2c..b00d3c261e 100644
--- a/backend/server/stage.go
+++ b/backend/server/stage.go
@@ -78,11 +78,7 @@ func (s *Server) registerStageRoutes(g *echo.Group) {
if issue == nil {
return echo.NewHTTPError(http.StatusNotFound, "issue not found")
}
- composedIssue, err := s.store.GetIssueByID(ctx, issue.UID)
- if err != nil {
- return err
- }
- if err := s.ActivityManager.BatchCreateTaskStatusUpdateApprovalActivity(ctx, tasks, currentPrincipalID, composedIssue, stage); err != nil {
+ if err := s.ActivityManager.BatchCreateTaskStatusUpdateApprovalActivity(ctx, tasks, currentPrincipalID, issue, stage); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to create task status update activity").SetInternal(err)
}
diff --git a/backend/store/database.go b/backend/store/database.go
index 982f0c912d..90686bff80 100644
--- a/backend/store/database.go
+++ b/backend/store/database.go
@@ -81,16 +81,13 @@ func (s *Store) GetDatabase(ctx context.Context, find *api.DatabaseFind) (*api.D
}
v2Find.IncludeAllDatabase = find.IncludeAllDatabase
- databases, err := s.ListDatabases(ctx, v2Find)
+ database, err := s.GetDatabaseV2(ctx, v2Find)
if err != nil {
return nil, err
}
- if len(databases) == 0 {
+ if database == nil {
return nil, nil
- } else if len(databases) > 1 {
- return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d databases with filter %+v, expect 1. ", len(databases), v2Find)}
}
- database := databases[0]
composedDatabase, err := s.composeDatabase(ctx, database)
if err != nil {
return nil, err
diff --git a/backend/store/issue.go b/backend/store/issue.go
index 642662d6ae..0c6740e764 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -84,24 +84,6 @@ func (s *Store) GetIssueByID(ctx context.Context, id int) (*api.Issue, error) {
return issue, nil
}
-// FindIssue finds a list of issues.
-func (s *Store) FindIssue(ctx context.Context, find *api.IssueFind) ([]*api.Issue, error) {
- issueRawList, err := s.findIssueRaw(ctx, find)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find Issue list with IssueFind[%+v]", find)
- }
- var issueList []*api.Issue
- for _, raw := range issueRawList {
- issue, err := s.composeIssue(ctx, raw)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to compose Issue with issueRaw[%+v]", raw)
- }
- issueList = append(issueList, issue)
- }
-
- return issueList, nil
-}
-
// FindIssueStripped finds a list of issues in stripped format.
// We do not load the pipeline in order to reduce the size of the response payload and the complexity of composing the issue list.
func (s *Store) FindIssueStripped(ctx context.Context, find *api.IssueFind) ([]*api.Issue, error) {
@@ -636,10 +618,10 @@ type FindIssueMessage struct {
PrincipalID *int
// To support pagination, we add into creator, assignee and subscriber.
// Only principleID or one of the following three fields can be set.
- CreatorID *int
- AssigneeID *int
- SubscriberID *int
- AssigneeNeedAttention *bool
+ CreatorID *int
+ AssigneeID *int
+ SubscriberID *int
+ NeedAttention *bool
StatusList []api.IssueStatus
// If specified, only find issues whose ID is smaller that SinceID.
@@ -805,7 +787,7 @@ func (s *Store) ListIssueV2(ctx context.Context, find *FindIssueMessage) ([]*Iss
if v := find.AssigneeID; v != nil {
where, args = append(where, fmt.Sprintf("issue.assignee_id = $%d", len(args)+1)), append(args, *v)
}
- if v := find.AssigneeNeedAttention; v != nil {
+ if v := find.NeedAttention; v != nil {
where, args = append(where, fmt.Sprintf("issue.assignee_need_attention = $%d", len(args)+1)), append(args, *v)
}
if v := find.SubscriberID; v != nil {