MCPcopy Create free account
hub / github.com/bytebase/bytebase / FindBlockingTaskByVersion

Method FindBlockingTaskByVersion

backend/store/task.go:97–153  ·  view source on GitHub ↗

Get a blocking task in the pipeline. A task is blocked by a task with a smaller schema version within the same pipeline.

(ctx context.Context, projectID string, planUID int64, instanceID, databaseName string, version string)

Source from the content-addressed store, hash-verified

95// Get a blocking task in the pipeline.
96// A task is blocked by a task with a smaller schema version within the same pipeline.
97func (s *Store) FindBlockingTaskByVersion(ctx context.Context, projectID string, planUID int64, instanceID, databaseName string, version string) (*int64, error) {
98 myVersion, err := model.NewVersion(version)
99 if err != nil {
100 return nil, err
101 }
102 q := qb.Q().Space(`
103 SELECT
104 task.id,
105 task.payload->>'schemaVersion'
106 FROM task
107 LEFT JOIN plan ON plan.project = task.project AND plan.id = task.plan_id
108 LEFT JOIN issue ON issue.project = plan.project AND issue.plan_id = plan.id
109 LEFT JOIN LATERAL (
110 SELECT COALESCE(
111 (SELECT
112 task_run.status
113 FROM task_run
114 WHERE task_run.project = task.project AND task_run.task_id = task.id
115 ORDER BY task_run.id DESC
116 LIMIT 1
117 ), 'NOT_STARTED'
118 ) AS status
119 ) AS latest_task_run ON TRUE
120 WHERE task.project = ? AND task.plan_id = ? AND task.instance = ? AND task.db_name = ?
121 AND task.payload->>'schemaVersion' IS NOT NULL
122 AND (task.payload->>'skipped')::BOOLEAN IS NOT TRUE
123 AND latest_task_run.status != 'DONE'
124 AND COALESCE(issue.status, 'OPEN') = 'OPEN'
125 ORDER BY task.id ASC`, projectID, planUID, instanceID, databaseName)
126 query, args, err := q.ToSQL()
127 if err != nil {
128 return nil, errors.Wrapf(err, "failed to build sql")
129 }
130 rows, err := s.GetDB().QueryContext(ctx, query, args...)
131 if err != nil {
132 return nil, err
133 }
134 defer rows.Close()
135 for rows.Next() {
136 var id int64
137 var v string
138 if err := rows.Scan(&id, &v); err != nil {
139 return nil, errors.Wrapf(err, "failed to scan rows")
140 }
141 otherVersion, err := model.NewVersion(v)
142 if err != nil {
143 return nil, err
144 }
145 if otherVersion.LessThan(myVersion) {
146 return &id, nil
147 }
148 }
149 if rows.Err() != nil {
150 return nil, rows.Err()
151 }
152 return nil, nil
153}
154

Callers

nothing calls this directly

Calls 10

GetDBMethod · 0.95
LessThanMethod · 0.95
NewVersionFunction · 0.92
QFunction · 0.92
SpaceMethod · 0.80
ToSQLMethod · 0.80
QueryContextMethod · 0.80
ScanMethod · 0.80
CloseMethod · 0.65
NextMethod · 0.45

Tested by

no test coverage detected