Skip to content

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

chore: migrate pipeline to v1 store (#4408)

Danny Xu9f9775ea16ad
diff --git a/backend/legacyapi/pipeline.go b/backend/legacyapi/pipeline.go
index 1341b00bf6..bb652120f6 100644
--- a/backend/legacyapi/pipeline.go
+++ b/backend/legacyapi/pipeline.go
@@ -46,7 +46,7 @@ type PipelineFind struct {
ID *int
// Domain specific fields
- Status *PipelineStatus
+ Active *bool
}
func (find *PipelineFind) String() string {
@@ -56,15 +56,3 @@ func (find *PipelineFind) String() string {
}
return string(str)
}
-
-// PipelinePatch is the API message for patching a pipeline.
-type PipelinePatch struct {
- ID int `jsonapi:"primary,pipelinePatch"`
-
- // Standard fields
- // Value is assigned from the jwt subject field passed by the client.
- UpdaterID int
-
- // Domain specific fields
- Status *PipelineStatus `jsonapi:"attr,status"`
-}
diff --git a/backend/runner/backuprun/runner.go b/backend/runner/backuprun/runner.go
index 2386778625..53bbb04a48 100644
--- a/backend/runner/backuprun/runner.go
+++ b/backend/runner/backuprun/runner.go
@@ -454,10 +454,9 @@ func (r *Runner) ScheduleBackupTask(ctx context.Context, database *store.Databas
return nil, errors.Wrapf(err, "failed to create task payload for backup %q", backupName)
}
- createdPipeline, err := r.store.CreatePipeline(ctx, &api.PipelineCreate{
- Name: fmt.Sprintf("backup-%s", backupName),
- CreatorID: creatorID,
- })
+ pipeline, err := r.store.CreatePipelineV2(ctx, &store.PipelineMessage{
+ Name: fmt.Sprintf("backup-%s", backupName),
+ }, creatorID)
if err != nil {
return nil, errors.Wrapf(err, "failed to create pipeline for backup %q", backupName)
}
@@ -466,7 +465,7 @@ func (r *Runner) ScheduleBackupTask(ctx context.Context, database *store.Databas
{
Name: fmt.Sprintf("backup-%s", backupName),
EnvironmentID: environment.UID,
- PipelineID: createdPipeline.ID,
+ PipelineID: pipeline.ID,
CreatorID: creatorID,
},
})
@@ -480,7 +479,7 @@ func (r *Runner) ScheduleBackupTask(ctx context.Context, database *store.Databas
createdStage := createdStages[0]
if _, err := r.store.CreateTask(ctx, &api.TaskCreate{
Name: fmt.Sprintf("backup-%s", backupName),
- PipelineID: createdPipeline.ID,
+ PipelineID: pipeline.ID,
StageID: createdStage.ID,
InstanceID: instance.UID,
DatabaseID: &database.UID,
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index 5f47ad29a4..284eaf9fc4 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -923,11 +923,11 @@ func (s *Scheduler) scheduleAutoApprovedTasks(ctx context.Context) error {
// scheduleActiveStageToRunning tries to schedule the tasks in the active stage.
func (s *Scheduler) scheduleActiveStageToRunning(ctx context.Context) error {
- pipelineStatus := api.PipelineOpen
+ active := true
pipelineFind := &api.PipelineFind{
- Status: &pipelineStatus,
+ Active: &active,
}
- pipelineList, err := s.store.FindPipeline(ctx, pipelineFind, false)
+ pipelineList, err := s.store.FindPipeline(ctx, pipelineFind)
if err != nil {
return errors.Wrap(err, "failed to retrieve open pipelines")
}
@@ -1033,8 +1033,10 @@ func (s *Scheduler) PatchTaskStatus(ctx context.Context, task *api.Task, taskSta
}
}
- if err := s.onTaskPatched(ctx, composedIssue, taskPatched); err != nil {
- return nil, err
+ if composedIssue != nil {
+ if err := s.onTaskPatched(ctx, composedIssue, taskPatched); err != nil {
+ return nil, err
+ }
}
return taskPatched, nil
@@ -1251,10 +1253,8 @@ func (s *Scheduler) ChangeIssueStatus(ctx context.Context, issue *store.IssueMes
if err != nil {
return nil, err
}
- var pipelineStatus api.PipelineStatus
switch newStatus {
case api.IssueOpen:
- pipelineStatus = api.PipelineOpen
case api.IssueDone:
// Returns error if any of the tasks is not DONE.
for _, stage := range composedPipeline.StageList {
@@ -1264,7 +1264,6 @@ func (s *Scheduler) ChangeIssueStatus(ctx context.Context, issue *store.IssueMes
}
}
}
- pipelineStatus = api.PipelineDone
case api.IssueCanceled:
// If we want to cancel the issue, we find the current running tasks, mark each of them CANCELED.
// We keep PENDING and FAILED tasks as is since the issue maybe reopened later, and it's better to
@@ -1282,16 +1281,6 @@ func (s *Scheduler) ChangeIssueStatus(ctx context.Context, issue *store.IssueMes
}
}
}
- pipelineStatus = api.PipelineCanceled
- }
-
- pipelinePatch := &api.PipelinePatch{
- ID: issue.PipelineUID,
- UpdaterID: updaterID,
- Status: &pipelineStatus,
- }
- if _, err := s.store.PatchPipeline(ctx, pipelinePatch); err != nil {
- return nil, errors.Wrapf(err, "failed to update issue %q's status, failed to update pipeline status with patch %+v", issue.Title, pipelinePatch)
}
updateIssueMessage := &store.UpdateIssueMessage{Status: &newStatus}
@@ -1344,10 +1333,6 @@ func (s *Scheduler) ChangeIssueStatus(ctx context.Context, issue *store.IssueMes
}
func (s *Scheduler) onTaskPatched(ctx context.Context, issue *api.Issue, taskPatched *api.Task) error {
- if issue == nil {
- return s.onTaskPatchedWithoutIssue(ctx, taskPatched)
- }
-
foundStage := false
stageTaskHasPendingApproval := false
stageTaskAllTerminated := true
@@ -1450,28 +1435,3 @@ func (s *Scheduler) onTaskPatched(ctx context.Context, issue *api.Issue, taskPat
return nil
}
-
-func (s *Scheduler) onTaskPatchedWithoutIssue(ctx context.Context, taskPatched *api.Task) error {
- pipeline, err := s.store.GetPipelineByID(ctx, taskPatched.PipelineID)
- if err != nil {
- return errors.Errorf("failed to fetch pipeline/issue as DONE after completing task %v", taskPatched.Name)
- }
- if pipeline == nil {
- return errors.Errorf("pipeline not found for ID %v", taskPatched.PipelineID)
- }
- // every task in the pipeline completes, and the assignee is system bot
- // the task is NOT associated with an issue, then we mark the pipeline as DONE.
- if areAllTasksDone(pipeline) {
- // System-generated tasks such as backup tasks don't have corresponding issues.
- status := api.PipelineDone
- pipelinePatch := &api.PipelinePatch{
- ID: pipeline.ID,
- UpdaterID: api.SystemBotID,
- Status: &status,
- }
- if _, err := s.store.PatchPipeline(ctx, pipelinePatch); err != nil {
- return errors.Wrapf(err, "failed to mark pipeline %v as DONE after completing task %v", pipeline.Name, taskPatched.Name)
- }
- }
- return nil
-}
diff --git a/backend/server/issue.go b/backend/server/issue.go
index b4092f127e..fac600ee2b 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -480,7 +480,7 @@ func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate,
}
func (s *Server) createPipeline(ctx context.Context, creatorID int, pipelineCreate *api.PipelineCreate) (*api.Pipeline, error) {
- pipelineCreated, err := s.store.CreatePipeline(ctx, pipelineCreate)
+ pipelineCreated, err := s.store.CreatePipeline(ctx, pipelineCreate, creatorID)
if err != nil {
return nil, errors.Wrap(err, "failed to create pipeline for issue")
}
diff --git a/backend/store/cache.go b/backend/store/cache.go
deleted file mode 100644
index 96ff5f7534..0000000000
--- a/backend/store/cache.go
+++ /dev/null
@@ -1,78 +0,0 @@
-package store
-
-import (
- "bytes"
- "encoding/gob"
- "fmt"
- "sync"
-
- "github.com/pkg/errors"
-)
-
-// cacheNamespace is the type of a cache.
-type cacheNamespace string
-
-const (
- // pipelineCacheNamespace is the cache type of pipelines.
- pipelineCacheNamespace cacheNamespace = "pl"
-)
-
-// CacheService implements a cache.
-type CacheService struct {
- sync.Mutex
- cache map[string][]byte
-}
-
-// newCacheService creates a cache service.
-func newCacheService() *CacheService {
- return &CacheService{
- cache: make(map[string][]byte),
- }
-}
-
-// FindCache finds the value in cache.
-func (s *CacheService) FindCache(namespace cacheNamespace, id int, entry interface{}) (bool, error) {
- key := generateKey(namespace, id)
-
- s.Lock()
- defer s.Unlock()
- value, exists := s.cache[key]
- if exists {
- dec := gob.NewDecoder(bytes.NewReader(value))
- if err := dec.Decode(entry); err != nil {
- return false, errors.Wrapf(err, "failed to decode entry for cache namespace: %s", namespace)
- }
- return true, nil
- }
- return false, nil
-}
-
-// UpsertCache upserts the value to cache.
-func (s *CacheService) UpsertCache(namespace cacheNamespace, id int, entry interface{}) error {
- key := generateKey(namespace, id)
-
- var buf bytes.Buffer
- enc := gob.NewEncoder(&buf)
- if err := enc.Encode(entry); err != nil {
- return errors.Wrapf(err, "failed to encode entry for cache namespace: %s", namespace)
- }
-
- s.Lock()
- defer s.Unlock()
- s.cache[key] = buf.Bytes()
-
- return nil
-}
-
-// DeleteCache deletes the key from cache.
-func (s *CacheService) DeleteCache(namespace cacheNamespace, id int) {
- key := generateKey(namespace, id)
-
- s.Lock()
- defer s.Unlock()
- delete(s.cache, key)
-}
-
-func generateKey(namespace cacheNamespace, id int) string {
- return fmt.Sprintf("%s%d", namespace, id)
-}
diff --git a/backend/store/issue.go b/backend/store/issue.go
index 25da5706c8..e5f8174143 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -306,19 +306,21 @@ func (s *Store) composeIssueStripped(ctx context.Context, issue *IssueMessage) (
composedIssue.Project = composedProject
// Creating a stripped pipeline.
- find := &api.PipelineFind{ID: &issue.PipelineUID}
- pipelineRaw, err := s.getPipelineRaw(ctx, find)
+ pipeline, err := s.GetPipelineV2ByID(ctx, issue.PipelineUID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get Pipeline with ID %d", issue.PipelineUID)
}
- if pipelineRaw == nil {
+ if pipeline == nil {
return nil, nil
}
- pipeline := pipelineRaw.toPipeline()
+ composedPipeline := &api.Pipeline{
+ ID: pipeline.ID,
+ Name: pipeline.Name,
+ }
stageRawList, err := s.findStageRaw(ctx, &api.StageFind{PipelineID: &issue.PipelineUID})
if err != nil {
- return nil, errors.Wrapf(err, "failed to find Stage list with StageFind[%+v]", find)
+ return nil, errors.Wrapf(err, "failed to find stage list")
}
var stageList []*api.Stage
for _, raw := range stageRawList {
@@ -341,8 +343,8 @@ func (s *Store) composeIssueStripped(ctx context.Context, issue *IssueMessage) (
}
stageList = append(stageList, stage)
}
- pipeline.StageList = stageList
- composedIssue.Pipeline = pipeline
+ composedPipeline.StageList = stageList
+ composedIssue.Pipeline = composedPipeline
return composedIssue, nil
}
diff --git a/backend/store/pipeline.go b/backend/store/pipeline.go
index 358d94dd3f..b496d1a3a6 100644
--- a/backend/store/pipeline.go
+++ b/backend/store/pipeline.go
@@ -7,230 +7,88 @@ import (
"strings"
"github.com/pkg/errors"
- "go.uber.org/zap"
"github.com/bytebase/bytebase/backend/common"
- "github.com/bytebase/bytebase/backend/common/log"
api "github.com/bytebase/bytebase/backend/legacyapi"
)
-// pipelineRaw is the store model for an Pipeline.
-// Fields have exactly the same meanings as Pipeline.
-type pipelineRaw struct {
- ID int
-
- // Domain specific fields
- Name string
- Status api.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() *api.Pipeline {
- return &api.Pipeline{
- ID: raw.ID,
-
- // Domain specific fields
- Name: raw.Name,
- Status: raw.Status,
- }
-}
-
// CreatePipeline creates an instance of Pipeline.
-func (s *Store) CreatePipeline(ctx context.Context, create *api.PipelineCreate) (*api.Pipeline, error) {
- pipelineRaw, err := s.createPipelineRaw(ctx, create)
+func (s *Store) CreatePipeline(ctx context.Context, create *api.PipelineCreate, creatorID int) (*api.Pipeline, error) {
+ pipeline, err := s.CreatePipelineV2(ctx, &PipelineMessage{Name: create.Name}, creatorID)
if err != nil {
return nil, errors.Wrapf(err, "failed to create Pipeline with PipelineCreate[%+v]", create)
}
- pipeline, err := s.composePipeline(ctx, pipelineRaw)
+ composedPipeline, err := s.composePipeline(ctx, pipeline)
if err != nil {
- return nil, errors.Wrapf(err, "failed to compose Pipeline with pipelineRaw[%+v]", pipelineRaw)
+ return nil, errors.Wrapf(err, "failed to compose pipeline")
}
- return pipeline, nil
+ return composedPipeline, nil
}
// GetPipelineByID gets an instance of Pipeline.
func (s *Store) GetPipelineByID(ctx context.Context, id int) (*api.Pipeline, error) {
- find := &api.PipelineFind{ID: &id}
- pipelineRaw, err := s.getPipelineRaw(ctx, find)
+ pipeline, err := s.GetPipelineV2ByID(ctx, id)
if err != nil {
- return nil, errors.Wrapf(err, "failed to get Pipeline with ID %d", id)
+ return nil, errors.Wrapf(err, "failed to get pipeline with ID %d", id)
}
- if pipelineRaw == nil {
+ if pipeline == nil {
return nil, nil
}
- pipeline, err := s.composePipeline(ctx, pipelineRaw)
+ composedPipeline, err := s.composePipeline(ctx, pipeline)
if err != nil {
- return nil, errors.Wrapf(err, "failed to compose Pipeline with pipelineRaw[%+v]", pipelineRaw)
+ return nil, errors.Wrapf(err, "failed to compose pipeline")
}
- return pipeline, nil
+ return composedPipeline, nil
}
// FindPipeline finds a list of Pipeline instances.
-func (s *Store) FindPipeline(ctx context.Context, find *api.PipelineFind, returnOnErr bool) ([]*api.Pipeline, error) {
- pipelineRawList, err := s.findPipelineRaw(ctx, find)
+func (s *Store) FindPipeline(ctx context.Context, find *api.PipelineFind) ([]*api.Pipeline, error) {
+ pipelines, err := s.ListPipelineV2(ctx, find)
if err != nil {
return nil, errors.Wrapf(err, "failed to find Pipeline list with PipelineFind[%+v]", find)
}
- var pipelineList []*api.Pipeline
- for _, raw := range pipelineRawList {
- pipeline, err := s.composePipeline(ctx, raw)
+ var composedPipelines []*api.Pipeline
+ for _, pipeline := range pipelines {
+ composedPipeline, err := s.composePipeline(ctx, pipeline)
if err != nil {
- if returnOnErr {
- return nil, errors.Wrapf(err, "failed to compose Pipeline with pipelineRaw[%+v]", raw)
- }
- log.Error("failed to compose pipeline",
- zap.Any("pipelineRaw", raw),
- zap.Error(err),
- )
- continue
+ return nil, err
}
- pipelineList = append(pipelineList, pipeline)
- }
- return pipelineList, nil
-}
-
-// PatchPipeline patches an instance of Pipeline.
-func (s *Store) PatchPipeline(ctx context.Context, patch *api.PipelinePatch) (*api.Pipeline, error) {
- pipelineRaw, err := s.patchPipelineRaw(ctx, patch)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to patch Pipeline with PipelinePatch[%+v]", patch)
- }
- pipeline, err := s.composePipeline(ctx, pipelineRaw)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to compose Pipeline with pipelineRaw[%+v]", pipelineRaw)
+ composedPipelines = append(composedPipelines, composedPipeline)
}
- return pipeline, nil
+ return composedPipelines, nil
}
-//
-// private function
-//
-
// Note: MUST keep in sync with composePipelineValidateOnly.
-func (s *Store) composePipeline(ctx context.Context, raw *pipelineRaw) (*api.Pipeline, error) {
- pipeline := raw.toPipeline()
-
- stageList, err := s.FindStage(ctx, &api.StageFind{PipelineID: &pipeline.ID})
- if err != nil {
- return nil, err
- }
- pipeline.StageList = stageList
-
- return pipeline, nil
-}
-
-// createPipelineRaw creates a new pipeline.
-func (s *Store) createPipelineRaw(ctx context.Context, create *api.PipelineCreate) (*pipelineRaw, error) {
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.Rollback()
-
- pipeline, err := s.createPipelineImpl(ctx, tx, create)
- if err != nil {
- return nil, err
- }
-
- if err := tx.Commit(); err != nil {
- return nil, FormatError(err)
- }
-
- if err := s.cache.UpsertCache(pipelineCacheNamespace, pipeline.ID, pipeline); err != nil {
- return nil, err
- }
-
- return pipeline, nil
-}
-
-// findPipelineRaw retrieves a list of pipelines based on find.
-func (s *Store) findPipelineRaw(ctx context.Context, find *api.PipelineFind) ([]*pipelineRaw, error) {
- tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
- if err != nil {
- return nil, FormatError(err)
+func (s *Store) composePipeline(ctx context.Context, pipeline *PipelineMessage) (*api.Pipeline, error) {
+ composedPipeline := &api.Pipeline{
+ ID: pipeline.ID,
+ Name: pipeline.Name,
}
- defer tx.Rollback()
- list, err := s.findPipelineImpl(ctx, tx, find)
+ stageList, err := s.FindStage(ctx, &api.StageFind{PipelineID: &pipeline.ID})
if err != nil {
return nil, err
}
+ composedPipeline.StageList = stageList
- if err == nil {
- for _, pipeline := range list {
- if err := s.cache.UpsertCache(pipelineCacheNamespace, pipeline.ID, pipeline); err != nil {
- return nil, err
- }
- }
- }
-
- return list, nil
+ return composedPipeline, nil
}
-// getPipelineRaw retrieves a single pipeline based on find.
-// Returns ECONFLICT if finding more than 1 matching records.
-func (s *Store) getPipelineRaw(ctx context.Context, find *api.PipelineFind) (*pipelineRaw, error) {
- if find.ID != nil {
- pipelineRaw := &pipelineRaw{}
- has, err := s.cache.FindCache(pipelineCacheNamespace, *find.ID, pipelineRaw)
- if err != nil {
- return nil, err
- }
- if has {
- return pipelineRaw, nil
- }
- }
-
- tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.Rollback()
-
- pipelineRawList, err := s.findPipelineImpl(ctx, tx, find)
- if err != nil {
- return nil, err
- }
-
- if len(pipelineRawList) == 0 {
- return nil, nil
- } else if len(pipelineRawList) > 1 {
- return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d pipelines with filter %+v, expect 1", len(pipelineRawList), find)}
- }
- if err := s.cache.UpsertCache(pipelineCacheNamespace, pipelineRawList[0].ID, pipelineRawList[0]); err != nil {
- return nil, err
- }
- return pipelineRawList[0], nil
+// PipelineMessage is the message for pipelines.
+type PipelineMessage struct {
+ Name string
+ // Output only.
+ ID int
}
-// patchPipelineRaw updates an existing pipeline by ID.
-// Returns ENOTFOUND if pipeline does not exist.
-func (s *Store) patchPipelineRaw(ctx context.Context, patch *api.PipelinePatch) (*pipelineRaw, error) {
+// CreatePipelineV2 creates a pipeline.
+func (s *Store) CreatePipelineV2(ctx context.Context, create *PipelineMessage, creatorID int) (*PipelineMessage, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.Rollback()
- pipelineRaw, err := s.patchPipelineImpl(ctx, tx, patch)
- if err != nil {
- return nil, FormatError(err)
- }
-
- if err := tx.Commit(); err != nil {
- return nil, FormatError(err)
- }
-
- if err := s.cache.UpsertCache(pipelineCacheNamespace, pipelineRaw.ID, pipelineRaw); err != nil {
- return nil, err
- }
-
- return pipelineRaw, nil
-}
-
-// createPipelineImpl creates a new pipeline.
-func (*Store) createPipelineImpl(ctx context.Context, tx *Tx, create *api.PipelineCreate) (*pipelineRaw, error) {
query := `
INSERT INTO pipeline (
creator_id,
@@ -238,100 +96,99 @@ func (*Store) createPipelineImpl(ctx context.Context, tx *Tx, create *api.Pipeli
name,
status
)
- VALUES ($1, $2, $3, 'OPEN')
- RETURNING id, name, status
+ VALUES ($1, $2, $3, $4)
+ RETURNING id, name
`
- var pipelineRaw pipelineRaw
+ pipeline := &PipelineMessage{}
if err := tx.QueryRowContext(ctx, query,
- create.CreatorID,
- create.CreatorID,
+ creatorID,
+ creatorID,
create.Name,
+ api.PipelineOpen,
).Scan(
- &pipelineRaw.ID,
- &pipelineRaw.Name,
- &pipelineRaw.Status,
+ &pipeline.ID,
+ &pipeline.Name,
); err != nil {
if err == sql.ErrNoRows {
return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
return nil, FormatError(err)
}
- return &pipelineRaw, nil
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return pipeline, nil
}
-func (*Store) findPipelineImpl(ctx context.Context, tx *Tx, find *api.PipelineFind) ([]*pipelineRaw, error) {
+// GetPipelineV2ByID gets the pipeline by ID.
+func (s *Store) GetPipelineV2ByID(ctx context.Context, id int) (*PipelineMessage, error) {
+ pipelines, err := s.ListPipelineV2(ctx, &api.PipelineFind{ID: &id})
+ if err != nil {
+ return nil, err
+ }
+
+ if len(pipelines) == 0 {
+ return nil, nil
+ } else if len(pipelines) > 1 {
+ return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d pipelines, expect 1", len(pipelines))}
+ }
+ pipeline := pipelines[0]
+ return pipeline, nil
+}
+
+// ListPipelineV2 lists pipelines.
+func (s *Store) ListPipelineV2(ctx context.Context, find *api.PipelineFind) ([]*PipelineMessage, error) {
// Build WHERE clause.
+ joinClause := ""
where, args := []string{"TRUE"}, []interface{}{}
if v := find.ID; v != nil {
- where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
+ where, args = append(where, fmt.Sprintf("pipeline.id = $%d", len(args)+1)), append(args, *v)
}
- if v := find.Status; v != nil {
- where, args = append(where, fmt.Sprintf("status = $%d", len(args)+1)), append(args, *v)
+ if v := find.Active; v != nil {
+ joinClause = "JOIN task ON pipeline.id = task.pipeline_id"
+ where, args = append(where, fmt.Sprintf(`(SELECT COUNT(1) FROM task WHERE task.status = $%d AND pipeline.id = task.pipeline_id) > 0`, len(args)+1)), append(args, api.TaskPending)
}
-
- rows, err := tx.QueryContext(ctx, `
+ query := fmt.Sprintf(`
SELECT
- id,
- name,
- status
+ pipeline.id,
+ pipeline.name
FROM pipeline
- WHERE `+strings.Join(where, " AND "),
- args...,
- )
+ %s
+ WHERE %s
+ GROUP BY pipeline.id`, joinClause, strings.Join(where, " AND "))
+
+ tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
return nil, FormatError(err)
}
defer rows.Close()
- // Iterate over result set and deserialize rows into pipelineRawList.
- var pipelineRawList []*pipelineRaw
+ var pipelines []*PipelineMessage
for rows.Next() {
- var pipelineRaw pipelineRaw
+ var pipeline PipelineMessage
if err := rows.Scan(
- &pipelineRaw.ID,
- &pipelineRaw.Name,
- &pipelineRaw.Status,
+ &pipeline.ID,
+ &pipeline.Name,
); err != nil {
return nil, FormatError(err)
}
-
- pipelineRawList = append(pipelineRawList, &pipelineRaw)
+ pipelines = append(pipelines, &pipeline)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return pipelineRawList, nil
-}
-
-// patchPipelineImpl updates a pipeline by ID. Returns the new state of the pipeline after update.
-func (*Store) patchPipelineImpl(ctx context.Context, tx *Tx, patch *api.PipelinePatch) (*pipelineRaw, error) {
- // Build UPDATE clause.
- set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
- if v := patch.Status; v != nil {
- set, args = append(set, fmt.Sprintf("status = $%d", len(args)+1)), append(args, api.PipelineStatus(*v))
- }
-
- args = append(args, patch.ID)
-
- var pipelineRaw pipelineRaw
- // Execute update query with RETURNING.
- if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
- UPDATE pipeline
- SET `+strings.Join(set, ", ")+`
- WHERE id = $%d
- RETURNING id, name, status
- `, len(args)),
- args...,
- ).Scan(
- &pipelineRaw.ID,
- &pipelineRaw.Name,
- &pipelineRaw.Status,
- ); err != nil {
- if err == sql.ErrNoRows {
- return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("pipeline ID not found: %d", patch.ID)}
- }
+ if err := tx.Commit(); err != nil {
return nil, FormatError(err)
}
- return &pipelineRaw, nil
+
+ return pipelines, nil
}
diff --git a/backend/store/store.go b/backend/store/store.go
index b8a59b1975..53dcfe96cb 100644
--- a/backend/store/store.go
+++ b/backend/store/store.go
@@ -10,8 +10,7 @@ import (
// Store provides database access to all raw objects.
type Store struct {
- db *DB
- cache *CacheService
+ db *DB
userIDCache sync.Map // map[int]*UserMessage
userEmailCache sync.Map // map[string]*UserMessage
@@ -34,8 +33,7 @@ type Store struct {
// New creates a new instance of Store.
func New(db *DB) *Store {
return &Store{
- db: db,
- cache: newCacheService(),
+ db: db,
}
}