Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: avoid calling real compose task in validation only issue creation (#3786)
Danny Xua4edfab8a2c5
diff --git a/server/issue.go b/server/issue.goindex 57738df9d1..17bbbc26ef 100644--- a/server/issue.go+++ b/server/issue.go@@ -364,19 +364,18 @@ func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate)return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Cannot set assignee with user id %d", issueCreate.AssigneeID))}- pipeline, err := s.createPipeline(ctx, issueCreate, pipelineCreate)- if err != nil {- return nil, err- }-if issueCreate.ValidateOnly {- issue, err := s.store.CreateIssueValidateOnly(ctx, pipeline, issueCreate)+ issue, err := s.store.CreateIssueValidateOnly(ctx, pipelineCreate, issueCreate)if err != nil {return nil, err}return issue, nil}+ pipeline, err := s.createPipeline(ctx, issueCreate, pipelineCreate)+ if err != nil {+ return nil, err+ }issueCreate.PipelineID = pipeline.IDissue, err := s.store.CreateIssue(ctx, issueCreate)if err != nil {@@ -427,26 +426,6 @@ func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate)}func (s *Server) createPipeline(ctx context.Context, issueCreate *api.IssueCreate, pipelineCreate *api.PipelineCreate) (*api.Pipeline, error) {- // Return an error if the issue has no task to be executed- hasTask := false- for _, stage := range pipelineCreate.StageList {- if len(stage.TaskList) > 0 {- hasTask = true- break- }- }- if !hasTask {- err := errors.Errorf("issue has no task to be executed")- return nil, echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)- }-- pipelineCreate.CreatorID = issueCreate.CreatorID-- // Create the pipeline, stages, and tasks.- if issueCreate.ValidateOnly {- return s.store.CreatePipelineValidateOnly(ctx, pipelineCreate)- }-pipelineCreated, err := s.store.CreatePipeline(ctx, pipelineCreate)if err != nil {return nil, errors.Wrap(err, "failed to create pipeline for issue")@@ -661,7 +640,8 @@ func (s *Server) getPipelineCreateForDatabaseCreate(ctx context.Context, issueCr})return &api.PipelineCreate{- Name: fmt.Sprintf("Pipeline - Create database %v from backup %v", c.DatabaseName, backup.Name),+ Name: fmt.Sprintf("Pipeline - Create database %v from backup %v", c.DatabaseName, backup.Name),+ CreatorID: issueCreate.CreatorID,StageList: []api.StageCreate{{Name: instance.Environment.Name,@@ -680,7 +660,8 @@ func (s *Server) getPipelineCreateForDatabaseCreate(ctx context.Context, issueCr}return &api.PipelineCreate{- Name: fmt.Sprintf("Pipeline - Create database %s", c.DatabaseName),+ Name: fmt.Sprintf("Pipeline - Create database %s", c.DatabaseName),+ CreatorID: issueCreate.CreatorID,StageList: []api.StageCreate{{Name: instance.Environment.Name,@@ -717,7 +698,8 @@ func (s *Server) getPipelineCreateForDatabasePITR(ctx context.Context, issueCrea}return &api.PipelineCreate{- Name: "Database Point-in-time Recovery pipeline",+ Name: "Database Point-in-time Recovery pipeline",+ CreatorID: issueCreate.CreatorID,StageList: []api.StageCreate{{Name: database.Instance.Environment.Name,@@ -742,7 +724,8 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.Con}}create := &api.PipelineCreate{- Name: "Change database pipeline",+ Name: "Change database pipeline",+ CreatorID: issueCreate.CreatorID,}project, err := s.store.GetProjectByID(ctx, issueCreate.ProjectID)@@ -902,8 +885,10 @@ func (s *Server) getPipelineCreateForDatabaseSchemaUpdateGhost(ctx context.Contereturn nil, echo.NewHTTPError(http.StatusBadRequest, "not implemented yet")}- create := &api.PipelineCreate{}- create.Name = "Update database schema (gh-ost) pipeline"+ create := &api.PipelineCreate{+ Name: "Update database schema (gh-ost) pipeline",+ CreatorID: issueCreate.CreatorID,+ }schemaVersion := common.DefaultMigrationVersion()for _, detail := range c.DetailList {if detail.Statement == "" {diff --git a/store/issue.go b/store/issue.goindex 503c68abdd..a8e5461244 100644--- a/store/issue.go+++ b/store/issue.go@@ -207,7 +207,11 @@ func (s *Store) CountIssueGroupByTypeAndStatus(ctx context.Context) ([]*metric.I// CreateIssueValidateOnly creates an issue for validation purpose// Do NOT write to the database.-func (s *Store) CreateIssueValidateOnly(ctx context.Context, pipeline *api.Pipeline, create *api.IssueCreate) (*api.Issue, error) {+func (s *Store) CreateIssueValidateOnly(ctx context.Context, pipelineCreate *api.PipelineCreate, create *api.IssueCreate) (*api.Issue, error) {+ pipeline, err := s.createPipelineValidateOnly(ctx, pipelineCreate)+ if err != nil {+ return nil, err+ }issue := &api.Issue{CreatorID: create.CreatorID,CreatedTs: time.Now().Unix(),@@ -230,9 +234,13 @@ func (s *Store) CreateIssueValidateOnly(ctx context.Context, pipeline *api.Pipelreturn issue, nil}-// CreatePipelineValidateOnly creates a pipeline for validation purpose+// createPipelineValidateOnly creates a pipeline for validation purpose// Do NOT write to the database.-func (s *Store) CreatePipelineValidateOnly(ctx context.Context, create *api.PipelineCreate) (*api.Pipeline, error) {+func (s *Store) createPipelineValidateOnly(ctx context.Context, create *api.PipelineCreate) (*api.Pipeline, error) {+ creator, err := s.GetPrincipalByID(ctx, create.CreatorID)+ if err != nil {+ return nil, err+ }// We cannot emit ID or use default zero by following https://google.aip.dev/163, otherwise// jsonapi resource relationships will collide different resources into the same bucket.id := 0@@ -242,21 +250,30 @@ func (s *Store) CreatePipelineValidateOnly(ctx context.Context, create *api.PipeName: create.Name,Status: api.PipelineOpen,CreatorID: create.CreatorID,+ Creator: creator,CreatedTs: ts,UpdaterID: create.CreatorID,+ Updater: creator,UpdatedTs: ts,}for _, sc := range create.StageList {id+++ env, err := s.GetEnvironmentByID(ctx, sc.EnvironmentID)+ if err != nil {+ return nil, err+ }stage := &api.Stage{ID: id,Name: sc.Name,CreatorID: create.CreatorID,+ Creator: creator,CreatedTs: ts,UpdaterID: create.CreatorID,+ Updater: creator,UpdatedTs: ts,PipelineID: sc.PipelineID,EnvironmentID: sc.EnvironmentID,+ Environment: env,}// We don't know IDs before inserting, so we use array index instead.// indexBlockedByIndex[indexA] holds indices of the tasks that block taskList[indexA]@@ -273,13 +290,15 @@ func (s *Store) CreatePipelineValidateOnly(ctx context.Context, create *api.Pipe// Convert array index to ID.blockedBy = append(blockedBy, strconv.Itoa(blockedByIndex+idOffset))}- taskRaw := &taskRaw{+ task := &api.Task{ID: id,Name: tc.Name,Status: tc.Status,CreatorID: create.CreatorID,+ Creator: creator,CreatedTs: ts,UpdaterID: create.CreatorID,+ Updater: creator,UpdatedTs: ts,Type: tc.Type,Payload: tc.Payload,@@ -288,17 +307,32 @@ func (s *Store) CreatePipelineValidateOnly(ctx context.Context, create *api.PipeStageID: stage.ID,InstanceID: tc.InstanceID,DatabaseID: tc.DatabaseID,+ BlockedBy: blockedBy,}- task, err := s.composeTask(ctx, taskRaw)- // We need to compose task.BlockedBy here because task and taskDAG are not inserted yet.- task.BlockedBy = blockedBy+ 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)+ }+ task.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)+ }+ task.Database = database+ }+stage.TaskList = append(stage.TaskList, task)}pipeline.StageList = append(pipeline.StageList, stage)}+return pipeline, nil}@@ -341,9 +375,7 @@ func (s *Store) composeIssueValidateOnly(ctx context.Context, issue *api.Issue)return err}issue.Project = project-- // Note: issue.Pipeline must be generated by CreatePipelineValidateOnly().- return s.composePipelineValidateOnly(ctx, issue.Pipeline)+ return nil}// Note: MUST keep in sync with composeIssueValidateOnly.diff --git a/store/pipeline.go b/store/pipeline.goindex eee2a2eefc..0325b52be0 100644--- a/store/pipeline.go+++ b/store/pipeline.go@@ -119,28 +119,6 @@ func (s *Store) PatchPipeline(ctx context.Context, patch *api.PipelinePatch) (*a// private function//-func (s *Store) composePipelineValidateOnly(ctx context.Context, pipeline *api.Pipeline) error {- creator, err := s.GetPrincipalByID(ctx, pipeline.CreatorID)- if err != nil {- return err- }- pipeline.Creator = creator-- updater, err := s.GetPrincipalByID(ctx, pipeline.UpdaterID)- if err != nil {- return err- }- pipeline.Updater = updater-- for _, stage := range pipeline.StageList {- if err := s.composeStageValidateOnly(ctx, stage); err != nil {- return err- }- }-- return nil-}-// Note: MUST keep in sync with composePipelineValidateOnly.func (s *Store) composePipeline(ctx context.Context, raw *pipelineRaw) (*api.Pipeline, error) {pipeline := raw.toPipeline()diff --git a/store/stage.go b/store/stage.goindex 5beaa34a4d..23ce9eae78 100644--- a/store/stage.go+++ b/store/stage.go@@ -86,30 +86,6 @@ func (s *Store) FindStage(ctx context.Context, find *api.StageFind) ([]*api.Stag// private functions//-func (s *Store) composeStageValidateOnly(ctx context.Context, stage *api.Stage) error {- creator, err := s.GetPrincipalByID(ctx, stage.CreatorID)- if err != nil {- return err- }- stage.Creator = creator-- updater, err := s.GetPrincipalByID(ctx, stage.UpdaterID)- if err != nil {- return err- }- stage.Updater = updater-- env, err := s.GetEnvironmentByID(ctx, stage.EnvironmentID)- if err != nil {- return err- }- stage.Environment = env-- // Note: stage.TaskList is already composed in CreatePipelineValidateOnly(). Do not need to compose here.-- return nil-}-// Note: MUST keep in sync with composeStageValidateOnly.func (s *Store) composeStage(ctx context.Context, raw *stageRaw) (*api.Stage, error) {stage := raw.toStage()