Skip to content

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

chore: migrate GetPipelineByID to store v1 (#4540)

Danny Xuf3eee2ffae9c
diff --git a/backend/legacyapi/task.go b/backend/legacyapi/task.go
index a31f815c42..faea5a0aca 100644
--- a/backend/legacyapi/task.go
+++ b/backend/legacyapi/task.go
@@ -285,7 +285,6 @@ type TaskCreate struct {
Collation string `jsonapi:"attr,collation"`
Labels string `jsonapi:"attr,labels"`
BackupID *int `jsonapi:"attr,backupId"`
- VCSPushEvent *vcs.PushEvent
}
// TaskFind is the API message for finding tasks.
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index 945f7d0bca..0aa9008bfe 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -328,50 +328,20 @@ func postMigration(ctx context.Context, stores *store.Store, activityManager *ac
return true, nil, errors.Errorf("failed to update database %q for instance %q", database.DatabaseName, instance.ResourceID)
}
}
- // If write-back feature is allowed in the Plan, on the presence of schema path template, we
- // write back the latest schema after migration for VCS-based projects for
- // 1) Non-wildcard branch filter and baseline migration for SDL.
- // 2) all DDL/Ghost migrations.
- writeBack := false
- if license.IsFeatureEnabled(api.FeatureVCSSchemaWriteBack) {
- if repo != nil && repo.SchemaPathTemplate != "" {
- if repo.Project.SchemaChangeType == api.ProjectSchemaChangeTypeSDL {
- if task.Type == api.TaskDatabaseSchemaBaseline && !strings.Contains(repo.BranchFilter, "*") {
- writeBack = true
- // Transform the schema to standard style for SDL mode.
- if instance.Engine == db.MySQL {
- standardSchema, err := transform.SchemaTransform(parser.MySQL, schema)
- if err != nil {
- return true, nil, errors.Errorf("failed to transform to standard schema for database %q", database.DatabaseName)
- }
- schema = standardSchema
- }
- }
- } else {
- writeBack = (vcsPushEvent != nil) && (task.Type == api.TaskDatabaseSchemaUpdate || task.Type == api.TaskDatabaseSchemaUpdateGhostCutover)
- }
- }
+
+ writeBack, err := isWriteBack(ctx, stores, license, project, repo, task, vcsPushEvent)
+ if err != nil {
+ return true, nil, err
}
- if writeBack && issue != nil {
- if project.TenantMode == api.TenantModeTenant {
- composedPipeline, err := stores.GetPipelineByID(ctx, issue.PipelineUID)
+ // Transform the schema to standard style for SDL mode.
+ if writeBack {
+ if instance.Engine == db.MySQL {
+ standardSchema, err := transform.SchemaTransform(parser.MySQL, schema)
if err != nil {
- return true, nil, err
- }
- // For tenant mode project, we will only write back once and we happen to write back on lastTask done.
- var lastTask *api.Task
- for i := len(composedPipeline.StageList) - 1; i >= 0; i-- {
- stage := composedPipeline.StageList[i]
- if len(stage.TaskList) > 0 {
- lastTask = stage.TaskList[len(stage.TaskList)-1]
- break
- }
- }
- // Not the last task yet.
- if lastTask != nil && task.ID != lastTask.ID {
- writeBack = false
+ return true, nil, errors.Errorf("failed to transform to standard schema for database %q", database.DatabaseName)
}
+ schema = standardSchema
}
}
@@ -474,6 +444,54 @@ func postMigration(ctx context.Context, stores *store.Store, activityManager *ac
}, nil
}
+func isWriteBack(ctx context.Context, stores *store.Store, license enterpriseAPI.LicenseService, project *store.ProjectMessage, repo *api.Repository, task *store.TaskMessage, vcsPushEvent *vcsPlugin.PushEvent) (bool, error) {
+ if !license.IsFeatureEnabled(api.FeatureVCSSchemaWriteBack) {
+ return false, nil
+ }
+ if repo == nil || repo.SchemaPathTemplate == "" {
+ return false, nil
+ }
+ if task.Type == api.TaskDatabaseSchemaBaseline {
+ if strings.Contains(repo.BranchFilter, "*") {
+ return false, nil
+ }
+ return true, nil
+ }
+
+ if vcsPushEvent == nil {
+ return false, nil
+ }
+ if task.Type != api.TaskDatabaseSchemaUpdate && task.Type != api.TaskDatabaseSchemaUpdateGhostCutover {
+ return false, nil
+ }
+
+ // For tenant mode project, we will only write back once and we happen to write back on lastTask done.
+ if project.TenantMode == api.TenantModeTenant {
+ stages, err := stores.ListStageV2(ctx, task.PipelineID)
+ if err != nil {
+ return false, err
+ }
+ if len(stages) == 0 {
+ return false, nil
+ }
+ if stages[len(stages)-1].ID != task.StageID {
+ return false, nil
+ }
+ tasks, err := stores.ListTasks(ctx, &api.TaskFind{PipelineID: &task.PipelineID, StageID: &task.StageID})
+ if err != nil {
+ return false, err
+ }
+ if len(tasks) == 0 {
+ return false, nil
+ }
+ if tasks[len(tasks)-1].ID != task.ID {
+ return false, nil
+ }
+ }
+
+ return true, nil
+}
+
func runMigration(ctx context.Context, store *store.Store, dbFactory *dbfactory.DBFactory, activityManager *activity.Manager, license enterpriseAPI.LicenseService, stateCfg *state.State, profile config.Profile, task *store.TaskMessage, migrationType db.MigrationType, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent) (terminated bool, result *api.TaskRunResultPayload, err error) {
mi, err := preMigration(ctx, store, profile, task, migrationType, statement, schemaVersion, vcsPushEvent)
if err != nil {
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index 66c5422e1f..09a9880f9f 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -571,11 +571,7 @@ func (s *Scheduler) CanPrincipalChangeTaskStatus(ctx context.Context, principalI
if issue == nil {
return false, common.Errorf(common.NotFound, "issue not found by pipeline ID: %d", task.PipelineID)
}
- composedPipeline, err := s.store.GetPipelineByID(ctx, issue.PipelineUID)
- if err != nil {
- return false, err
- }
- groupValue, err := s.getGroupValueForTask(ctx, issue, composedPipeline, task)
+ groupValue, err := s.getGroupValueForTask(ctx, issue, task)
if err != nil {
return false, common.Wrapf(err, common.Internal, "failed to get assignee group value for taskID %d", task.ID)
}
@@ -602,9 +598,13 @@ func (s *Scheduler) CanPrincipalChangeTaskStatus(ctx context.Context, principalI
return false, nil
}
-func (s *Scheduler) getGroupValueForTask(ctx context.Context, issue *store.IssueMessage, composedPipeline *api.Pipeline, task *store.TaskMessage) (*api.AssigneeGroupValue, error) {
+func (s *Scheduler) getGroupValueForTask(ctx context.Context, issue *store.IssueMessage, task *store.TaskMessage) (*api.AssigneeGroupValue, error) {
environmentID := api.UnknownID
- for _, stage := range composedPipeline.StageList {
+ stages, err := s.store.ListStageV2(ctx, task.PipelineID)
+ if err != nil {
+ return nil, err
+ }
+ for _, stage := range stages {
if stage.ID == task.StageID {
environmentID = stage.EnvironmentID
break
diff --git a/backend/store/issue.go b/backend/store/issue.go
index a4052c4ff1..4bc0b4acfa 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -245,9 +245,16 @@ func (s *Store) composeIssue(ctx context.Context, issue *IssueMessage) (*api.Iss
}
composedIssue.Project = composedProject
- composedPipeline, err := s.GetPipelineByID(ctx, issue.PipelineUID)
+ pipeline, err := s.GetPipelineV2ByID(ctx, issue.PipelineUID)
if err != nil {
- return nil, err
+ return nil, errors.Wrapf(err, "failed to get pipeline with ID %d", issue.PipelineUID)
+ }
+ if pipeline == nil {
+ return nil, nil
+ }
+ composedPipeline, err := s.composePipeline(ctx, pipeline)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to compose pipeline")
}
composedIssue.Pipeline = composedPipeline
@@ -322,6 +329,162 @@ func (s *Store) composeIssueStripped(ctx context.Context, issue *IssueMessage) (
return composedIssue, nil
}
+func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {
+ composedPipeline := &api.Pipeline{
+ ID: pipeline.ID,
+ Name: pipeline.Name,
+ }
+
+ tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &pipeline.ID})
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to find Task list for pipeline %v", pipeline.ID)
+ }
+ taskRuns, err := s.listTaskRun(ctx, &TaskRunFind{PipelineID: &pipeline.ID})
+ if err != nil {
+ return nil, err
+ }
+ taskCheckRuns, err := s.ListTaskCheckRuns(ctx, &TaskCheckRunFind{PipelineID: &pipeline.ID})
+ if err != nil {
+ return nil, err
+ }
+ var composedTasks []*api.Task
+ for _, task := range tasks {
+ composedTask := task.toTask()
+ creator, err := s.GetPrincipalByID(ctx, task.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ composedTask.Creator = creator
+ updater, err := s.GetPrincipalByID(ctx, task.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ composedTask.Updater = updater
+
+ for _, taskRun := range taskRuns {
+ if taskRun.TaskID == task.ID {
+ composedTaskRun := taskRun.toTaskRun()
+ creator, err := s.GetPrincipalByID(ctx, composedTaskRun.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ composedTaskRun.Creator = creator
+ updater, err := s.GetPrincipalByID(ctx, composedTaskRun.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ composedTaskRun.Updater = updater
+ composedTask.TaskRunList = append(composedTask.TaskRunList, composedTaskRun)
+ }
+ }
+ for _, taskCheckRun := range taskCheckRuns {
+ if taskCheckRun.TaskID == task.ID {
+ taskCheckRun := taskCheckRun.toTaskCheckRun()
+ creator, err := s.GetPrincipalByID(ctx, taskCheckRun.CreatorID)
+ if err != nil {
+ return nil, err
+ }
+ taskCheckRun.Creator = creator
+ updater, err := s.GetPrincipalByID(ctx, taskCheckRun.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ taskCheckRun.Updater = updater
+ composedTask.TaskCheckRunList = append(composedTask.TaskCheckRunList, taskCheckRun)
+ }
+ }
+
+ instance, err := s.GetInstanceByID(ctx, task.InstanceID)
+ if err != nil {
+ return nil, err
+ }
+ if instance == nil {
+ return nil, errors.Errorf("instance not found with ID %v", task.InstanceID)
+ }
+ composedTask.Instance = instance
+ if task.DatabaseID != nil {
+ database, err := s.GetDatabase(ctx, &api.DatabaseFind{ID: task.DatabaseID})
+ if err != nil {
+ return nil, err
+ }
+ if database == nil {
+ return nil, errors.Errorf("database not found with ID %v", task.DatabaseID)
+ }
+ composedTask.Database = database
+ }
+
+ composedTasks = append(composedTasks, composedTask)
+ }
+
+ stages, err := s.ListStageV2(ctx, pipeline.ID)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to find stages for pipeline %d", pipeline.ID)
+ }
+ var composedStages []*api.Stage
+ for _, stage := range stages {
+ environment, err := s.GetEnvironmentByID(ctx, stage.EnvironmentID)
+ if err != nil {
+ return nil, err
+ }
+ composedStage := &api.Stage{
+ ID: stage.ID,
+ PipelineID: stage.PipelineID,
+ EnvironmentID: stage.EnvironmentID,
+ Environment: environment,
+ Name: stage.Name,
+ }
+ for _, composedTask := range composedTasks {
+ if composedTask.StageID == stage.ID {
+ composedStage.TaskList = append(composedStage.TaskList, composedTask)
+ }
+ }
+
+ composedStages = append(composedStages, composedStage)
+ }
+ composedPipeline.StageList = composedStages
+
+ return composedPipeline, nil
+}
+
+func (s *Store) composeSimplePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {
+ tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &pipeline.ID})
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to find tasks for pipeline %d", pipeline.ID)
+ }
+
+ stages, err := s.ListStageV2(ctx, pipeline.ID)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to find stage list")
+ }
+ var composedStages []*api.Stage
+ for _, stage := range stages {
+ environment, err := s.GetEnvironmentByID(ctx, stage.EnvironmentID)
+ if err != nil {
+ return nil, err
+ }
+ composedStage := &api.Stage{
+ ID: stage.ID,
+ Name: stage.Name,
+ EnvironmentID: stage.EnvironmentID,
+ Environment: environment,
+ PipelineID: stage.PipelineID,
+ }
+
+ for _, task := range tasks {
+ if task.StageID == stage.ID {
+ composedStage.TaskList = append(composedStage.TaskList, task.toTask())
+ }
+ }
+ composedStages = append(composedStages, composedStage)
+ }
+
+ return &api.Pipeline{
+ ID: pipeline.ID,
+ Name: pipeline.Name,
+ StageList: composedStages,
+ }, nil
+}
+
// IssueMessage is the mssage for issues.
type IssueMessage struct {
Project *ProjectMessage
diff --git a/backend/store/pipeline.go b/backend/store/pipeline.go
index c5930eb744..ebb4981bf5 100644
--- a/backend/store/pipeline.go
+++ b/backend/store/pipeline.go
@@ -9,197 +9,8 @@ import (
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
- api "github.com/bytebase/bytebase/backend/legacyapi"
)
-// GetPipelineByID gets an instance of Pipeline.
-func (s *Store) GetPipelineByID(ctx context.Context, id int) (*api.Pipeline, error) {
- pipeline, err := s.GetPipelineV2ByID(ctx, id)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get pipeline with ID %d", id)
- }
- if pipeline == nil {
- return nil, nil
- }
- composedPipeline, err := s.composePipeline(ctx, pipeline)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to compose pipeline")
- }
- return composedPipeline, nil
-}
-
-// GetSimplePipelineByID gets a pipeline in simple format.
-func (s *Store) GetSimplePipelineByID(ctx context.Context, id int) (*api.Pipeline, error) {
- pipeline, err := s.GetPipelineV2ByID(ctx, id)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get pipeline with ID %d", id)
- }
- if pipeline == nil {
- return nil, nil
- }
- composedPipeline, err := s.composeSimplePipeline(ctx, pipeline)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to compose pipeline")
- }
- return composedPipeline, nil
-}
-
-func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {
- composedPipeline := &api.Pipeline{
- ID: pipeline.ID,
- Name: pipeline.Name,
- }
-
- tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &pipeline.ID})
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find Task list for pipeline %v", pipeline.ID)
- }
- taskRuns, err := s.listTaskRun(ctx, &TaskRunFind{PipelineID: &pipeline.ID})
- if err != nil {
- return nil, err
- }
- taskCheckRuns, err := s.ListTaskCheckRuns(ctx, &TaskCheckRunFind{PipelineID: &pipeline.ID})
- if err != nil {
- return nil, err
- }
- var composedTasks []*api.Task
- for _, task := range tasks {
- composedTask := task.toTask()
- creator, err := s.GetPrincipalByID(ctx, task.CreatorID)
- if err != nil {
- return nil, err
- }
- composedTask.Creator = creator
- updater, err := s.GetPrincipalByID(ctx, task.UpdaterID)
- if err != nil {
- return nil, err
- }
- composedTask.Updater = updater
-
- for _, taskRun := range taskRuns {
- if taskRun.TaskID == task.ID {
- composedTaskRun := taskRun.toTaskRun()
- creator, err := s.GetPrincipalByID(ctx, composedTaskRun.CreatorID)
- if err != nil {
- return nil, err
- }
- composedTaskRun.Creator = creator
- updater, err := s.GetPrincipalByID(ctx, composedTaskRun.UpdaterID)
- if err != nil {
- return nil, err
- }
- composedTaskRun.Updater = updater
- composedTask.TaskRunList = append(composedTask.TaskRunList, composedTaskRun)
- }
- }
- for _, taskCheckRun := range taskCheckRuns {
- if taskCheckRun.TaskID == task.ID {
- taskCheckRun := taskCheckRun.toTaskCheckRun()
- creator, err := s.GetPrincipalByID(ctx, taskCheckRun.CreatorID)
- if err != nil {
- return nil, err
- }
- taskCheckRun.Creator = creator
- updater, err := s.GetPrincipalByID(ctx, taskCheckRun.UpdaterID)
- if err != nil {
- return nil, err
- }
- taskCheckRun.Updater = updater
- composedTask.TaskCheckRunList = append(composedTask.TaskCheckRunList, taskCheckRun)
- }
- }
-
- instance, err := s.GetInstanceByID(ctx, task.InstanceID)
- if err != nil {
- return nil, err
- }
- if instance == nil {
- return nil, errors.Errorf("instance not found with ID %v", task.InstanceID)
- }
- composedTask.Instance = instance
- if task.DatabaseID != nil {
- database, err := s.GetDatabase(ctx, &api.DatabaseFind{ID: task.DatabaseID})
- if err != nil {
- return nil, err
- }
- if database == nil {
- return nil, errors.Errorf("database not found with ID %v", task.DatabaseID)
- }
- composedTask.Database = database
- }
-
- composedTasks = append(composedTasks, composedTask)
- }
-
- stages, err := s.ListStageV2(ctx, pipeline.ID)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find stages for pipeline %d", pipeline.ID)
- }
- var composedStages []*api.Stage
- for _, stage := range stages {
- environment, err := s.GetEnvironmentByID(ctx, stage.EnvironmentID)
- if err != nil {
- return nil, err
- }
- composedStage := &api.Stage{
- ID: stage.ID,
- PipelineID: stage.PipelineID,
- EnvironmentID: stage.EnvironmentID,
- Environment: environment,
- Name: stage.Name,
- }
- for _, composedTask := range composedTasks {
- if composedTask.StageID == stage.ID {
- composedStage.TaskList = append(composedStage.TaskList, composedTask)
- }
- }
-
- composedStages = append(composedStages, composedStage)
- }
- composedPipeline.StageList = composedStages
-
- return composedPipeline, nil
-}
-
-func (s *Store) composeSimplePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {
- tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &pipeline.ID})
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find tasks for pipeline %d", pipeline.ID)
- }
-
- stages, err := s.ListStageV2(ctx, pipeline.ID)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find stage list")
- }
- var composedStages []*api.Stage
- for _, stage := range stages {
- environment, err := s.GetEnvironmentByID(ctx, stage.EnvironmentID)
- if err != nil {
- return nil, err
- }
- composedStage := &api.Stage{
- ID: stage.ID,
- Name: stage.Name,
- EnvironmentID: stage.EnvironmentID,
- Environment: environment,
- PipelineID: stage.PipelineID,
- }
-
- for _, task := range tasks {
- if task.StageID == stage.ID {
- composedStage.TaskList = append(composedStage.TaskList, task.toTask())
- }
- }
- composedStages = append(composedStages, composedStage)
- }
-
- return &api.Pipeline{
- ID: pipeline.ID,
- Name: pipeline.Name,
- StageList: composedStages,
- }, nil
-}
-
// PipelineMessage is the message for pipelines.
type PipelineMessage struct {
Name string