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: list auto rollout tasks (#8685)
p0ny5a47e2f3414c
diff --git a/backend/runner/taskrun/schedulerv2.go b/backend/runner/taskrun/schedulerv2.goindex 86b06ff22a..ffb20754a5 100644--- a/backend/runner/taskrun/schedulerv2.go+++ b/backend/runner/taskrun/schedulerv2.go@@ -99,7 +99,24 @@ func (s *SchedulerV2) runOnce(ctx context.Context) {}func (s *SchedulerV2) scheduleAutoRolloutTasks(ctx context.Context) error {- taskIDs, err := s.store.ListNotSkippedTasksWithNoTaskRun(ctx)+ environments, err := s.store.ListEnvironmentV2(ctx, &store.FindEnvironmentMessage{ShowDeleted: true})+ if err != nil {+ return errors.Wrapf(err, "failed to list environments")+ }++ var autoRolloutEnvironmentIDs []int+ for _, environment := range environments {+ policy, err := s.store.GetPipelineApprovalPolicy(ctx, environment.UID)+ if err != nil {+ return errors.Wrapf(err, "failed to get approval policy for environment ID %d", environment.UID)+ }+ if policy.Value != api.PipelineApprovalValueManualNever {+ continue+ }+ autoRolloutEnvironmentIDs = append(autoRolloutEnvironmentIDs, environment.UID)+ }++ taskIDs, err := s.store.ListTasksToAutoRollout(ctx, autoRolloutEnvironmentIDs)if err != nil {return errors.Wrapf(err, "failed to list tasks with zero task run")}@@ -124,28 +141,6 @@ func (s *SchedulerV2) scheduleAutoRolloutTask(ctx context.Context, taskUID int)if instance.Deleted {return nil}- // TODO(p0ny): support create database with environment override.- environmentID := instance.EnvironmentID- if task.DatabaseID != nil {- database, err := s.store.GetDatabaseV2(ctx, &store.FindDatabaseMessage{UID: task.DatabaseID})- if err != nil {- return err- }- if database != nil {- environmentID = database.EffectiveEnvironmentID- }- }- environment, err := s.store.GetEnvironmentV2(ctx, &store.FindEnvironmentMessage{ResourceID: &environmentID})- if err != nil {- return err- }- policy, err := s.store.GetPipelineApprovalPolicy(ctx, environment.UID)- if err != nil {- return errors.Wrapf(err, "failed to get approval policy for environment ID %d", environment.UID)- }- if policy.Value != api.PipelineApprovalValueManualNever {- return nil- }issue, err := s.store.GetIssueV2(ctx, &store.FindIssueMessage{PipelineID: &task.PipelineID})if err != nil {diff --git a/backend/store/task.go b/backend/store/task.goindex f5ec53f1d8..ead1e124fd 100644--- a/backend/store/task.go+++ b/backend/store/task.go@@ -4,6 +4,8 @@ import ("context""database/sql""fmt"+ "math"+ "sort""strings""github.com/jackc/pgtype"@@ -395,15 +397,20 @@ func (s *Store) BatchSkipTasks(ctx context.Context, taskUIDs []int, comment strireturn nil}-// ListNotSkippedTasksWithNoTaskRun returns tasks that+// ListTasksToAutoRollout returns tasks that// 1. have no task runs// 2. are not skipped-// 3. are associated with an open issue or no issue.-func (s *Store) ListNotSkippedTasksWithNoTaskRun(ctx context.Context) ([]int, error) {+// 3. are associated with an open issue or no issue+// 4. are in an environment that has auto rollout enabled+// 5. are in the stage that is the first among the selected stages in the pipeline.+func (s *Store) ListTasksToAutoRollout(ctx context.Context, environmentIDs []int) ([]int, error) {rows, err := s.db.db.QueryContext(ctx, `SELECT+ task.pipeline_id,+ task.stage_id,task.idFROM task+ LEFT JOIN stage ON stage.id = task.stage_idLEFT JOIN pipeline ON pipeline.id = task.pipeline_idLEFT JOIN issue ON issue.pipeline_id = pipeline.idLEFT JOIN LATERAL@@ -412,25 +419,47 @@ func (s *Store) ListNotSkippedTasksWithNoTaskRun(ctx context.Context) ([]int, erWHERE task_run.e IS NULLAND COALESCE((task.payload->>'skipped')::BOOLEAN, FALSE) IS FALSEAND COALESCE(issue.status, 'OPEN') = 'OPEN'- ORDER BY task.id`)+ AND stage.environment_id = ANY($1)+ `, environmentIDs)if err != nil {return nil, err}defer rows.Close()- var ids []int-+ pipelineStageTasks := map[int]map[int][]int{}for rows.Next() {- var id int- if err := rows.Scan(&id); err != nil {+ var pipeline, stage, task int+ if err := rows.Scan(&pipeline, &stage, &task); err != nil {return nil, err}- ids = append(ids, id)++ if _, ok := pipelineStageTasks[pipeline]; !ok {+ pipelineStageTasks[pipeline] = map[int][]int{}+ }+ pipelineStageTasks[pipeline][stage] = append(pipelineStageTasks[pipeline][stage], task)}if err := rows.Err(); err != nil {return nil, err}+ var ids []int+ for pipeline := range pipelineStageTasks {+ minStage := math.MaxInt32+ for stage := range pipelineStageTasks[pipeline] {+ if stage < minStage {+ minStage = stage+ }+ }+ if minStage == math.MaxInt32 {+ continue+ }+ ids = append(ids, pipelineStageTasks[pipeline][minStage]...)+ }++ sort.Slice(ids, func(i, j int) bool {+ return ids[i] < ids[j]+ })+return ids, nil}