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 PipelineRaw to represent store model for Pipeline (#896)
Yilong Li57643491f1d5
diff --git a/api/pipeline.go b/api/pipeline.goindex 5c7b04b47e..661f0f18f6 100644--- a/api/pipeline.go+++ b/api/pipeline.go@@ -32,6 +32,40 @@ func (e PipelineStatus) String() string {return ""}+// PipelineRaw is the store model for an Pipeline.+// Fields have exactly the same meanings as Pipeline.+type PipelineRaw struct {+ ID int++ // Standard fields+ CreatorID int+ CreatedTs int64+ UpdaterID int+ UpdatedTs int64++ // Domain specific fields+ Name string+ Status PipelineStatus+}++// ToPipeline creates an instance of Pipeline based on the PipelineRaw.+// This is intended to be called when we need to compose an Pipeline relationship.+func (raw *PipelineRaw) ToPipeline() *Pipeline {+ return &Pipeline{+ ID: raw.ID,++ // Standard fields+ CreatorID: raw.CreatorID,+ CreatedTs: raw.CreatedTs,+ UpdaterID: raw.UpdaterID,+ UpdatedTs: raw.UpdatedTs,++ // Domain specific fields+ Name: raw.Name,+ Status: raw.Status,+ }+}+// Pipeline is the API message for pipelines.type Pipeline struct {ID int `jsonapi:"primary,pipeline"`@@ -95,8 +129,8 @@ type PipelinePatch struct {// PipelineService is the service for pipelines.type PipelineService interface {- CreatePipeline(ctx context.Context, create *PipelineCreate) (*Pipeline, error)- FindPipelineList(ctx context.Context, find *PipelineFind) ([]*Pipeline, error)- FindPipeline(ctx context.Context, find *PipelineFind) (*Pipeline, error)- PatchPipeline(ctx context.Context, patch *PipelinePatch) (*Pipeline, error)+ CreatePipeline(ctx context.Context, create *PipelineCreate) (*PipelineRaw, error)+ FindPipelineList(ctx context.Context, find *PipelineFind) ([]*PipelineRaw, error)+ FindPipeline(ctx context.Context, find *PipelineFind) (*PipelineRaw, error)+ PatchPipeline(ctx context.Context, patch *PipelinePatch) (*PipelineRaw, error)}diff --git a/server/issue.go b/server/issue.goindex 4c38bbabdb..6af0ca8ae9 100644--- a/server/issue.go+++ b/server/issue.go@@ -317,15 +317,9 @@ func (s *Server) composeIssueRelationship(ctx context.Context, issue *api.Issue)return err}- if issue.Pipeline == nil {- issue.Pipeline, err = s.composePipelineByID(ctx, issue.PipelineID)- if err != nil {- return err- }- } else {- if err := s.composePipelineRelationship(ctx, issue.Pipeline); err != nil {- return err- }+ issue.Pipeline, err = s.composePipelineByID(ctx, issue.PipelineID)+ if err != nil {+ return err}return nil@@ -908,14 +902,14 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I}pipelineCreate.CreatorID = creatorID- createdPipeline, err := s.PipelineService.CreatePipeline(ctx, pipelineCreate)+ pipelineRawCreated, err := s.PipelineService.CreatePipeline(ctx, pipelineCreate)if err != nil {return nil, fmt.Errorf("failed to create pipeline for issue, error %v", err)}for _, stageCreate := range pipelineCreate.StageList {stageCreate.CreatorID = creatorID- stageCreate.PipelineID = createdPipeline.ID+ stageCreate.PipelineID = pipelineRawCreated.IDcreatedStage, err := s.StageService.CreateStage(ctx, &stageCreate)if err != nil {return nil, fmt.Errorf("failed to create stage for issue, error %v", err)@@ -923,14 +917,19 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.Ifor _, taskCreate := range stageCreate.TaskList {taskCreate.CreatorID = creatorID- taskCreate.PipelineID = createdPipeline.ID+ taskCreate.PipelineID = pipelineRawCreated.IDtaskCreate.StageID = createdStage.IDif _, err = s.TaskService.CreateTask(ctx, &taskCreate); err != nil {return nil, fmt.Errorf("failed to create task for issue, error %v", err)}}}- return createdPipeline, nil++ pipelineCreated, err := s.composePipelineRelationship(ctx, pipelineRawCreated)+ if err != nil {+ return nil, fmt.Errorf("failed to compose pipeline relation, error %w", err)+ }+ return pipelineCreated, nil}func getUpdateTask(database *api.Database, migrationType db.MigrationType, vcsPushEvent *vcs.PushEvent, d *api.UpdateSchemaDetail, schemaVersion string, taskStatus api.TaskStatus) (*api.TaskCreate, error) {diff --git a/server/pipeline.go b/server/pipeline.goindex e402311887..cbaa496897 100644--- a/server/pipeline.go+++ b/server/pipeline.go@@ -12,47 +12,43 @@ func (s *Server) composePipelineByID(ctx context.Context, id int) (*api.PipelinepipelineFind := &api.PipelineFind{ID: &id,}- pipeline, err := s.PipelineService.FindPipeline(ctx, pipelineFind)+ pipelineRaw, err := s.PipelineService.FindPipeline(ctx, pipelineFind)if err != nil {return nil, err}- if pipeline == nil {+ if pipelineRaw == nil {return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("Pipeline not found with ID %v", id)}}- if err := s.composePipelineRelationship(ctx, pipeline); err != nil {+ pipeline, err := s.composePipelineRelationship(ctx, pipelineRaw)+ if err != nil {return nil, err}return pipeline, nil}-func (s *Server) composePipelineRelationship(ctx context.Context, pipeline *api.Pipeline) error {- var err error+func (s *Server) composePipelineRelationship(ctx context.Context, raw *api.PipelineRaw) (*api.Pipeline, error) {+ pipeline := raw.ToPipeline()- pipeline.Creator, err = s.composePrincipalByID(ctx, pipeline.CreatorID)+ creator, err := s.composePrincipalByID(ctx, pipeline.CreatorID)if err != nil {- return err+ return nil, err}+ pipeline.Creator = creator- pipeline.Updater, err = s.composePrincipalByID(ctx, pipeline.UpdaterID)+ updater, err := s.composePrincipalByID(ctx, pipeline.UpdaterID)if err != nil {- return err+ return nil, err}+ pipeline.Updater = updater- if pipeline.StageList == nil {- pipeline.StageList, err = s.composeStageListByPipelineID(ctx, pipeline.ID)- if err != nil {- return err- }- } else {- for _, stage := range pipeline.StageList {- if err := s.composeStageRelationship(ctx, stage); err != nil {- return err- }- }+ stageList, err := s.composeStageListByPipelineID(ctx, pipeline.ID)+ if err != nil {+ return nil, err}+ pipeline.StageList = stageList- return nil+ return pipeline, nil}func (s *Server) composePipelineRelationshipValidateOnly(ctx context.Context, pipeline *api.Pipeline) error {diff --git a/server/task_scheduler.go b/server/task_scheduler.goindex 2bad9bc1bd..600e88bfb9 100644--- a/server/task_scheduler.go+++ b/server/task_scheduler.go@@ -63,19 +63,20 @@ func (s *TaskScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {pipelineFind := &api.PipelineFind{Status: &pipelineStatus,}- pipelineList, err := s.server.PipelineService.FindPipelineList(ctx, pipelineFind)+ pipelineRawList, err := s.server.PipelineService.FindPipelineList(ctx, pipelineFind)if err != nil {s.l.Error("Failed to retrieve open pipelines", zap.Error(err))return}- for _, pipeline := range pipelineList {- if pipeline.ID == api.OnboardingPipelineID {+ for _, pipelineRaw := range pipelineRawList {+ if pipelineRaw.ID == api.OnboardingPipelineID {continue}- if err := s.server.composePipelineRelationship(ctx, pipeline); err != nil {+ pipeline, err := s.server.composePipelineRelationship(ctx, pipelineRaw)+ if err != nil {s.l.Error("Failed to fetch pipeline relationship",- zap.Int("id", pipeline.ID),- zap.String("name", pipeline.Name),+ zap.Int("id", pipelineRaw.ID),+ zap.String("name", pipelineRaw.Name),zap.Error(err),)continue@@ -83,7 +84,7 @@ func (s *TaskScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {if _, err := s.server.ScheduleNextTaskIfNeeded(ctx, pipeline); err != nil {s.l.Error("Failed to schedule next running task",- zap.Int("pipeline_id", pipeline.ID),+ zap.Int("pipeline_id", pipelineRaw.ID),zap.Error(err),)}diff --git a/store/pipeline.go b/store/pipeline.goindex aca53e0f6b..4124622d4a 100644--- a/store/pipeline.go+++ b/store/pipeline.go@@ -29,7 +29,7 @@ func NewPipelineService(logger *zap.Logger, db *DB, cache api.CacheService) *Pip}// CreatePipeline creates a new pipeline.-func (s *PipelineService) CreatePipeline(ctx context.Context, create *api.PipelineCreate) (*api.Pipeline, error) {+func (s *PipelineService) CreatePipeline(ctx context.Context, create *api.PipelineCreate) (*api.PipelineRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -53,7 +53,7 @@ func (s *PipelineService) CreatePipeline(ctx context.Context, create *api.Pipeli}// FindPipelineList retrieves a list of pipelines based on find.-func (s *PipelineService) FindPipelineList(ctx context.Context, find *api.PipelineFind) ([]*api.Pipeline, error) {+func (s *PipelineService) FindPipelineList(ctx context.Context, find *api.PipelineFind) ([]*api.PipelineRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)@@ -78,15 +78,15 @@ func (s *PipelineService) FindPipelineList(ctx context.Context, find *api.Pipeli// FindPipeline retrieves a single pipeline based on find.// Returns ECONFLICT if finding more than 1 matching records.-func (s *PipelineService) FindPipeline(ctx context.Context, find *api.PipelineFind) (*api.Pipeline, error) {+func (s *PipelineService) FindPipeline(ctx context.Context, find *api.PipelineFind) (*api.PipelineRaw, error) {if find.ID != nil {- pipeline := &api.Pipeline{}- has, err := s.cache.FindCache(api.PipelineCache, *find.ID, pipeline)+ pipelineRaw := &api.PipelineRaw{}+ has, err := s.cache.FindCache(api.PipelineCache, *find.ID, pipelineRaw)if err != nil {return nil, err}if has {- return pipeline, nil+ return pipelineRaw, nil}}@@ -96,32 +96,32 @@ func (s *PipelineService) FindPipeline(ctx context.Context, find *api.PipelineFi}defer tx.PTx.Rollback()- list, err := s.findPipelineList(ctx, tx.PTx, find)+ pipelineRawList, err := s.findPipelineList(ctx, tx.PTx, find)if err != nil {return nil, err}- if len(list) == 0 {+ if len(pipelineRawList) == 0 {return nil, nil- } else if len(list) > 1 {- return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d pipelines with filter %+v, expect 1", len(list), find)}+ } else if len(pipelineRawList) > 1 {+ return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d pipelines with filter %+v, expect 1", len(pipelineRawList), find)}}- if err := s.cache.UpsertCache(api.PipelineCache, list[0].ID, list[0]); err != nil {+ if err := s.cache.UpsertCache(api.PipelineCache, pipelineRawList[0].ID, pipelineRawList[0]); err != nil {return nil, err}- return list[0], nil+ return pipelineRawList[0], nil}// PatchPipeline updates an existing pipeline by ID.// Returns ENOTFOUND if pipeline does not exist.-func (s *PipelineService) PatchPipeline(ctx context.Context, patch *api.PipelinePatch) (*api.Pipeline, error) {+func (s *PipelineService) PatchPipeline(ctx context.Context, patch *api.PipelinePatch) (*api.PipelineRaw, error) {tx, err := s.db.BeginTx(ctx, nil)if err != nil {return nil, FormatError(err)}defer tx.PTx.Rollback()- pipeline, err := s.patchPipeline(ctx, tx.PTx, patch)+ pipelineRaw, err := s.patchPipeline(ctx, tx.PTx, patch)if err != nil {return nil, FormatError(err)}@@ -130,15 +130,15 @@ func (s *PipelineService) PatchPipeline(ctx context.Context, patch *api.Pipelinereturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.PipelineCache, pipeline.ID, pipeline); err != nil {+ if err := s.cache.UpsertCache(api.PipelineCache, pipelineRaw.ID, pipelineRaw); err != nil {return nil, err}- return pipeline, nil+ return pipelineRaw, nil}// createPipeline creates a new pipeline.-func (s *PipelineService) createPipeline(ctx context.Context, tx *sql.Tx, create *api.PipelineCreate) (*api.Pipeline, error) {+func (s *PipelineService) createPipeline(ctx context.Context, tx *sql.Tx, create *api.PipelineCreate) (*api.PipelineRaw, error) {row, err := tx.QueryContext(ctx, `INSERT INTO pipeline (creator_id,@@ -160,23 +160,23 @@ func (s *PipelineService) createPipeline(ctx context.Context, tx *sql.Tx, createdefer row.Close()row.Next()- var pipeline api.Pipeline+ var pipelineRaw api.PipelineRawif err := row.Scan(- &pipeline.ID,- &pipeline.CreatorID,- &pipeline.CreatedTs,- &pipeline.UpdaterID,- &pipeline.UpdatedTs,- &pipeline.Name,- &pipeline.Status,+ &pipelineRaw.ID,+ &pipelineRaw.CreatorID,+ &pipelineRaw.CreatedTs,+ &pipelineRaw.UpdaterID,+ &pipelineRaw.UpdatedTs,+ &pipelineRaw.Name,+ &pipelineRaw.Status,); err != nil {return nil, FormatError(err)}- return &pipeline, nil+ return &pipelineRaw, nil}-func (s *PipelineService) findPipelineList(ctx context.Context, tx *sql.Tx, find *api.PipelineFind) ([]*api.Pipeline, error) {+func (s *PipelineService) findPipelineList(ctx context.Context, tx *sql.Tx, find *api.PipelineFind) ([]*api.PipelineRaw, error) {// Build WHERE clause.where, args := []string{"1 = 1"}, []interface{}{}if v := find.ID; v != nil {@@ -204,33 +204,33 @@ func (s *PipelineService) findPipelineList(ctx context.Context, tx *sql.Tx, find}defer rows.Close()- // Iterate over result set and deserialize rows into pipelineList.- var pipelineList []*api.Pipeline+ // Iterate over result set and deserialize rows into pipelineRawList.+ var pipelineRawList []*api.PipelineRawfor rows.Next() {- var pipeline api.Pipeline+ var pipelineRaw api.PipelineRawif err := rows.Scan(- &pipeline.ID,- &pipeline.CreatorID,- &pipeline.CreatedTs,- &pipeline.UpdaterID,- &pipeline.UpdatedTs,- &pipeline.Name,- &pipeline.Status,+ &pipelineRaw.ID,+ &pipelineRaw.CreatorID,+ &pipelineRaw.CreatedTs,+ &pipelineRaw.UpdaterID,+ &pipelineRaw.UpdatedTs,+ &pipelineRaw.Name,+ &pipelineRaw.Status,); err != nil {return nil, FormatError(err)}- pipelineList = append(pipelineList, &pipeline)+ pipelineRawList = append(pipelineRawList, &pipelineRaw)}if err := rows.Err(); err != nil {return nil, FormatError(err)}- return pipelineList, nil+ return pipelineRawList, nil}// patchPipeline updates a pipeline by ID. Returns the new state of the pipeline after update.-func (s *PipelineService) patchPipeline(ctx context.Context, tx *sql.Tx, patch *api.PipelinePatch) (*api.Pipeline, error) {+func (s *PipelineService) patchPipeline(ctx context.Context, tx *sql.Tx, patch *api.PipelinePatch) (*api.PipelineRaw, error) {// Build UPDATE clause.set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}if v := patch.Status; v != nil {@@ -254,19 +254,19 @@ func (s *PipelineService) patchPipeline(ctx context.Context, tx *sql.Tx, patch *defer row.Close()if row.Next() {- var pipeline api.Pipeline+ var pipelineRaw api.PipelineRawif err := row.Scan(- &pipeline.ID,- &pipeline.CreatorID,- &pipeline.CreatedTs,- &pipeline.UpdaterID,- &pipeline.UpdatedTs,- &pipeline.Name,- &pipeline.Status,+ &pipelineRaw.ID,+ &pipelineRaw.CreatorID,+ &pipelineRaw.CreatedTs,+ &pipelineRaw.UpdaterID,+ &pipelineRaw.UpdatedTs,+ &pipelineRaw.Name,+ &pipelineRaw.Status,); err != nil {return nil, FormatError(err)}- return &pipeline, nil+ return &pipelineRaw, nil}return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("pipeline ID not found: %d", patch.ID)}