Skip to content

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

chore: migrate to task check run v1 store (#4434)

Danny Xu979c8c85125c
diff --git a/backend/legacyapi/task_check_run.go b/backend/legacyapi/task_check_run.go
index b818577f69..b883acdab6 100644
--- a/backend/legacyapi/task_check_run.go
+++ b/backend/legacyapi/task_check_run.go
@@ -1,8 +1,6 @@
package api
import (
- "encoding/json"
-
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/advisor"
advisorDB "github.com/bytebase/bytebase/backend/plugin/advisor/db"
@@ -154,54 +152,6 @@ type TaskCheckRun struct {
Payload string `jsonapi:"attr,payload"`
}
-// TaskCheckRunCreate is the API message for creating a task check run.
-type TaskCheckRunCreate struct {
- // Standard fields
- // Value is assigned from the jwt subject field passed by the client.
- CreatorID int
-
- // Related fields
- TaskID int
-
- // Domain specific fields
- Type TaskCheckType `jsonapi:"attr,type"`
- Comment string `jsonapi:"attr,comment"`
- Payload string `jsonapi:"attr,payload"`
-}
-
-// TaskCheckRunFind is the API message for finding task check runs.
-type TaskCheckRunFind struct {
- ID *int
-
- // Related fields
- TaskID *int
- Type *TaskCheckType
-
- // Domain specific fields
- StatusList *[]TaskCheckRunStatus
-}
-
-func (find *TaskCheckRunFind) String() string {
- str, err := json.Marshal(*find)
- if err != nil {
- return err.Error()
- }
- return string(str)
-}
-
-// TaskCheckRunStatusPatch is the API message for patching a task check run.
-type TaskCheckRunStatusPatch struct {
- ID *int
-
- // Standard fields
- UpdaterID int
-
- // Domain specific fields
- Status TaskCheckRunStatus
- Code common.Code
- Result string
-}
-
// IsSyntaxCheckSupported checks the engine type if syntax check supports it.
func IsSyntaxCheckSupported(dbType db.Type) bool {
if dbType == db.Postgres || dbType == db.MySQL || dbType == db.TiDB {
diff --git a/backend/runner/taskcheck/scheduler.go b/backend/runner/taskcheck/scheduler.go
index 90ecdc17ec..ec27acb9df 100644
--- a/backend/runner/taskcheck/scheduler.go
+++ b/backend/runner/taskcheck/scheduler.go
@@ -66,7 +66,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
// Inspect all running task checks
taskCheckRunStatusList := []api.TaskCheckRunStatus{api.TaskCheckRunRunning}
- taskCheckRunFind := &api.TaskCheckRunFind{
+ taskCheckRunFind := &store.TaskCheckRunFind{
StatusList: &taskCheckRunStatusList,
}
taskCheckRunList, err := s.store.FindTaskCheckRun(ctx, taskCheckRunFind)
@@ -97,7 +97,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
zap.String("type", string(taskCheckRun.Type)),
zap.Error(err),
)
- taskCheckRunStatusPatch := &api.TaskCheckRunStatusPatch{
+ taskCheckRunStatusPatch := &store.TaskCheckRunStatusPatch{
ID: &taskCheckRun.ID,
UpdaterID: api.SystemBotID,
Status: api.TaskCheckRunFailed,
@@ -146,7 +146,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
return
}
- taskCheckRunStatusPatch := &api.TaskCheckRunStatusPatch{
+ taskCheckRunStatusPatch := &store.TaskCheckRunStatusPatch{
ID: &taskCheckRun.ID,
UpdaterID: api.SystemBotID,
Status: api.TaskCheckRunDone,
@@ -181,7 +181,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
return
}
- taskCheckRunStatusPatch := &api.TaskCheckRunStatusPatch{
+ taskCheckRunStatusPatch := &store.TaskCheckRunStatusPatch{
ID: &taskCheckRun.ID,
UpdaterID: api.SystemBotID,
Status: api.TaskCheckRunFailed,
@@ -217,8 +217,8 @@ func (s *Scheduler) Register(taskType api.TaskCheckType, executor Executor) {
s.executors[taskType] = executor
}
-func (s *Scheduler) getTaskCheck(ctx context.Context, project *store.ProjectMessage, task *api.Task, creatorID int) ([]*api.TaskCheckRunCreate, error) {
- var createList []*api.TaskCheckRunCreate
+func (s *Scheduler) getTaskCheck(ctx context.Context, project *store.ProjectMessage, task *api.Task, creatorID int) ([]*store.TaskCheckRunCreate, error) {
+ var createList []*store.TaskCheckRunCreate
create, err := s.getLGTMTaskCheck(ctx, project, task, creatorID)
if err != nil {
@@ -323,7 +323,7 @@ func (s *Scheduler) ScheduleCheck(ctx context.Context, project *store.ProjectMes
return task, err
}
-func getStmtTypeTaskCheck(task *api.Task, instance *store.InstanceMessage, dbSchema *store.DBSchema, statement string) ([]*api.TaskCheckRunCreate, error) {
+func getStmtTypeTaskCheck(task *api.Task, instance *store.InstanceMessage, dbSchema *store.DBSchema, statement string) ([]*store.TaskCheckRunCreate, error) {
if !api.IsStatementTypeCheckSupported(instance.Engine) {
return nil, nil
}
@@ -336,7 +336,7 @@ func getStmtTypeTaskCheck(task *api.Task, instance *store.InstanceMessage, dbSch
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal statement type payload: %v", task.Name)
}
- return []*api.TaskCheckRunCreate{
+ return []*store.TaskCheckRunCreate{
{
CreatorID: api.SystemBotID,
TaskID: task.ID,
@@ -346,7 +346,7 @@ func getStmtTypeTaskCheck(task *api.Task, instance *store.InstanceMessage, dbSch
}, nil
}
-func (*Scheduler) getSQLReviewTaskCheck(task *api.Task, instance *store.InstanceMessage, dbSchema *store.DBSchema, statement string) ([]*api.TaskCheckRunCreate, error) {
+func (*Scheduler) getSQLReviewTaskCheck(task *api.Task, instance *store.InstanceMessage, dbSchema *store.DBSchema, statement string) ([]*store.TaskCheckRunCreate, error) {
if !api.IsSQLReviewSupported(instance.Engine) {
return nil, nil
}
@@ -359,7 +359,7 @@ func (*Scheduler) getSQLReviewTaskCheck(task *api.Task, instance *store.Instance
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal statement advise payload: %v", task.Name)
}
- return []*api.TaskCheckRunCreate{
+ return []*store.TaskCheckRunCreate{
{
CreatorID: api.SystemBotID,
TaskID: task.ID,
@@ -369,7 +369,7 @@ func (*Scheduler) getSQLReviewTaskCheck(task *api.Task, instance *store.Instance
}, nil
}
-func getSyntaxCheckTaskCheck(task *api.Task, instance *store.InstanceMessage, dbSchema *store.DBSchema, statement string) ([]*api.TaskCheckRunCreate, error) {
+func getSyntaxCheckTaskCheck(task *api.Task, instance *store.InstanceMessage, dbSchema *store.DBSchema, statement string) ([]*store.TaskCheckRunCreate, error) {
if !api.IsSyntaxCheckSupported(instance.Engine) {
return nil, nil
}
@@ -382,7 +382,7 @@ func getSyntaxCheckTaskCheck(task *api.Task, instance *store.InstanceMessage, db
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal statement advise payload: %v", task.Name)
}
- return []*api.TaskCheckRunCreate{
+ return []*store.TaskCheckRunCreate{
{
CreatorID: api.SystemBotID,
TaskID: task.ID,
@@ -392,8 +392,8 @@ func getSyntaxCheckTaskCheck(task *api.Task, instance *store.InstanceMessage, db
}, nil
}
-func (*Scheduler) getGeneralTaskCheck(_ context.Context, task *api.Task, creatorID int) ([]*api.TaskCheckRunCreate, error) {
- return []*api.TaskCheckRunCreate{
+func (*Scheduler) getGeneralTaskCheck(_ context.Context, task *api.Task, creatorID int) ([]*store.TaskCheckRunCreate, error) {
+ return []*store.TaskCheckRunCreate{
{
CreatorID: creatorID,
TaskID: task.ID,
@@ -407,11 +407,11 @@ func (*Scheduler) getGeneralTaskCheck(_ context.Context, task *api.Task, creator
}, nil
}
-func (*Scheduler) getGhostTaskCheck(_ context.Context, task *api.Task, creatorID int) ([]*api.TaskCheckRunCreate, error) {
+func (*Scheduler) getGhostTaskCheck(_ context.Context, task *api.Task, creatorID int) ([]*store.TaskCheckRunCreate, error) {
if task.Type != api.TaskDatabaseSchemaUpdateGhostSync {
return nil, nil
}
- return []*api.TaskCheckRunCreate{
+ return []*store.TaskCheckRunCreate{
{
CreatorID: creatorID,
TaskID: task.ID,
@@ -420,11 +420,11 @@ func (*Scheduler) getGhostTaskCheck(_ context.Context, task *api.Task, creatorID
}, nil
}
-func (*Scheduler) getPITRTaskCheck(_ context.Context, task *api.Task, creatorID int) ([]*api.TaskCheckRunCreate, error) {
+func (*Scheduler) getPITRTaskCheck(_ context.Context, task *api.Task, creatorID int) ([]*store.TaskCheckRunCreate, error) {
if task.Type != api.TaskDatabaseRestorePITRRestore {
return nil, nil
}
- return []*api.TaskCheckRunCreate{
+ return []*store.TaskCheckRunCreate{
{
CreatorID: creatorID,
TaskID: task.ID,
@@ -433,7 +433,7 @@ func (*Scheduler) getPITRTaskCheck(_ context.Context, task *api.Task, creatorID
}, nil
}
-func (s *Scheduler) getLGTMTaskCheck(ctx context.Context, project *store.ProjectMessage, task *api.Task, creatorID int) ([]*api.TaskCheckRunCreate, error) {
+func (s *Scheduler) getLGTMTaskCheck(ctx context.Context, project *store.ProjectMessage, task *api.Task, creatorID int) ([]*store.TaskCheckRunCreate, error) {
if !s.licenseService.IsFeatureEnabled(api.FeatureLGTM) {
return nil, nil
}
@@ -449,7 +449,7 @@ func (s *Scheduler) getLGTMTaskCheck(ctx context.Context, project *store.Project
// don't schedule LGTM check if the approval policy is auto-approval.
return nil, nil
}
- return []*api.TaskCheckRunCreate{
+ return []*store.TaskCheckRunCreate{
{
CreatorID: creatorID,
TaskID: task.ID,
@@ -460,7 +460,7 @@ func (s *Scheduler) getLGTMTaskCheck(ctx context.Context, project *store.Project
// SchedulePipelineTaskCheck schedules the task checks for a pipeline.
func (s *Scheduler) SchedulePipelineTaskCheck(ctx context.Context, project *store.ProjectMessage, pipeline *api.Pipeline) error {
- var createList []*api.TaskCheckRunCreate
+ var createList []*store.TaskCheckRunCreate
for _, stage := range pipeline.StageList {
for _, task := range stage.TaskList {
create, err := s.getTaskCheck(ctx, project, task, api.SystemBotID)
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index 2bc1701c1c..d1c981495f 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -387,7 +387,7 @@ func (s *Scheduler) PatchTaskStatement(ctx context.Context, task *api.Task, task
log.Error("failed to cancel external approval on SQL modified", zap.Int("issue_id", issue.ID), zap.Error(err))
}
if taskPatched.Type == api.TaskDatabaseSchemaUpdateGhostSync {
- if _, err := s.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ if err := s.store.CreateTaskCheckRunIfNeeded(ctx, &store.TaskCheckRunCreate{
CreatorID: taskPatched.CreatorID,
TaskID: task.ID,
Type: api.TaskCheckGhostSync,
@@ -411,7 +411,7 @@ func (s *Scheduler) PatchTaskStatement(ctx context.Context, task *api.Task, task
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, errors.Wrapf(err, "failed to marshal statement advise payload: %v", task.Name))
}
- if _, err := s.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ if err := s.store.CreateTaskCheckRunIfNeeded(ctx, &store.TaskCheckRunCreate{
CreatorID: api.SystemBotID,
TaskID: task.ID,
Type: api.TaskCheckDatabaseStatementSyntax,
@@ -442,7 +442,7 @@ func (s *Scheduler) PatchTaskStatement(ctx context.Context, task *api.Task, task
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, errors.Wrapf(err, "failed to marshal check statement type payload: %v", task.Name))
}
- if _, err := s.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ if err := s.store.CreateTaskCheckRunIfNeeded(ctx, &store.TaskCheckRunCreate{
CreatorID: api.SystemBotID,
TaskID: task.ID,
Type: api.TaskCheckDatabaseStatementType,
@@ -537,7 +537,7 @@ func (s *Scheduler) triggerDatabaseStatementAdviseTask(ctx context.Context, stat
return errors.Wrapf(err, "failed to marshal statement advise payload: %v", task.Name)
}
- if _, err := s.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ if err := s.store.CreateTaskCheckRunIfNeeded(ctx, &store.TaskCheckRunCreate{
CreatorID: api.SystemBotID,
TaskID: task.ID,
Type: api.TaskCheckDatabaseStatementAdvise,
diff --git a/backend/store/task.go b/backend/store/task.go
index 181c8c3e59..ee0a90a51f 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -31,7 +31,7 @@ type TaskMessage struct {
// Could be empty for creating database task when the task isn't yet completed successfully.
DatabaseID *int
TaskRunRawList []*TaskRunMessage
- TaskCheckRunRawList []*taskCheckRunRaw
+ TaskCheckRunRawList []*TaskCheckRunMessage
// Domain specific fields
Name string
@@ -168,10 +168,10 @@ func (s *Store) composeTask(ctx context.Context, raw *TaskMessage) (*api.Task, e
if err != nil {
return nil, err
}
- taskCheckRunFind := &api.TaskCheckRunFind{
+ taskCheckRunFind := &TaskCheckRunFind{
TaskID: &task.ID,
}
- taskCheckRunRawList, err := s.listTaskCheckRun(ctx, taskCheckRunFind)
+ taskCheckRunRawList, err := s.ListTaskCheckRuns(ctx, taskCheckRunFind)
if err != nil {
return nil, err
}
diff --git a/backend/store/task_check_run.go b/backend/store/task_check_run.go
index 7c970e4dcb..b6b946bff1 100644
--- a/backend/store/task_check_run.go
+++ b/backend/store/task_check_run.go
@@ -14,9 +14,9 @@ import (
api "github.com/bytebase/bytebase/backend/legacyapi"
)
-// taskCheckRunRaw is the store model for a TaskCheckRun.
+// TaskCheckRunMessage is the store model for a TaskCheckRun.
// Fields have exactly the same meanings as TaskCheckRun.
-type taskCheckRunRaw struct {
+type TaskCheckRunMessage struct {
ID int
// Standard fields
@@ -37,135 +37,152 @@ type taskCheckRunRaw struct {
Payload string
}
-// toTaskCheckRun creates an instance of TaskCheckRun based on the taskCheckRunRaw.
-// This is intended to be called when we need to compose a TaskCheckRun relationship.
-func (raw *taskCheckRunRaw) toTaskCheckRun() *api.TaskCheckRun {
+func (run *TaskCheckRunMessage) toTaskCheckRun() *api.TaskCheckRun {
return &api.TaskCheckRun{
- ID: raw.ID,
+ ID: run.ID,
// Standard fields
- CreatorID: raw.CreatorID,
- CreatedTs: raw.CreatedTs,
- UpdaterID: raw.UpdaterID,
- UpdatedTs: raw.UpdatedTs,
+ CreatorID: run.CreatorID,
+ CreatedTs: run.CreatedTs,
+ UpdaterID: run.UpdaterID,
+ UpdatedTs: run.UpdatedTs,
// Related fields
- TaskID: raw.TaskID,
+ TaskID: run.TaskID,
// Domain specific fields
- Status: raw.Status,
- Type: raw.Type,
- Code: raw.Code,
- Comment: raw.Comment,
- Result: raw.Result,
- Payload: raw.Payload,
+ Status: run.Status,
+ Type: run.Type,
+ Code: run.Code,
+ Comment: run.Comment,
+ Result: run.Result,
+ Payload: run.Payload,
}
}
-// CreateTaskCheckRunIfNeeded creates an instance of TaskCheckRun if needed.
-func (s *Store) CreateTaskCheckRunIfNeeded(ctx context.Context, create *api.TaskCheckRunCreate) (*api.TaskCheckRun, error) {
- taskCheckRunRaw, err := s.createTaskCheckRunRawIfNeeded(ctx, create)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to create TaskCheckRun with TaskCheckRunCreate[%+v]", create)
- }
- taskCheckRun, err := s.composeTaskCheckRun(ctx, taskCheckRunRaw)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to compose TaskCheckRun with taskCheckRunRaw[%+v]", taskCheckRunRaw)
- }
- return taskCheckRun, nil
-}
-
// BatchCreateTaskCheckRun inserts many TaskCheckRun instances, which is too slow otherwise to insert one by one.
-func (s *Store) BatchCreateTaskCheckRun(ctx context.Context, creates []*api.TaskCheckRunCreate) ([]*api.TaskCheckRun, error) {
+func (s *Store) BatchCreateTaskCheckRun(ctx context.Context, creates []*TaskCheckRunCreate) ([]*api.TaskCheckRun, error) {
if len(creates) == 0 {
return nil, nil
}
- taskCheckRunRawList, err := s.batchCreateTaskCheckRunRaw(ctx, creates)
+ taskCheckRuns, err := s.batchCreateTaskCheckRunV1(ctx, creates)
if err != nil {
- return nil, errors.Wrapf(err, "failed to create TaskCheckRun with TaskCheckRunCreates[%+v]", creates)
+ return nil, errors.Wrapf(err, "failed to create task check run %#v", creates)
}
- var taskCheckRunList []*api.TaskCheckRun
- for _, taskCheckRunRaw := range taskCheckRunRawList {
- taskCheckRun, err := s.composeTaskCheckRun(ctx, taskCheckRunRaw)
+ var composedTaskCheckRuns []*api.TaskCheckRun
+ for _, run := range taskCheckRuns {
+ composedTaskCheckRun, err := s.composeTaskCheckRun(ctx, run)
if err != nil {
- return nil, errors.Wrapf(err, "failed to compose TaskCheckRun with taskCheckRunRaw[%+v]", taskCheckRunRaw)
+ return nil, errors.Wrapf(err, "failed to compose task check run %#v", run)
}
- taskCheckRunList = append(taskCheckRunList, taskCheckRun)
+ composedTaskCheckRuns = append(composedTaskCheckRuns, composedTaskCheckRun)
}
- return taskCheckRunList, nil
+ return composedTaskCheckRuns, nil
}
// FindTaskCheckRun finds a list of TaskCheckRun instances.
-func (s *Store) FindTaskCheckRun(ctx context.Context, find *api.TaskCheckRunFind) ([]*api.TaskCheckRun, error) {
- taskCheckRunRawList, err := s.findTaskCheckRunRaw(ctx, find)
+func (s *Store) FindTaskCheckRun(ctx context.Context, find *TaskCheckRunFind) ([]*api.TaskCheckRun, error) {
+ taskCheckRuns, err := s.ListTaskCheckRuns(ctx, find)
if err != nil {
return nil, errors.Wrapf(err, "failed to find TaskCheckRun list with TaskCheckRunFind[%+v]", find)
}
- var taskCheckRunList []*api.TaskCheckRun
- for _, raw := range taskCheckRunRawList {
- taskCheckRun, err := s.composeTaskCheckRun(ctx, raw)
+ var composedTaskCheckRuns []*api.TaskCheckRun
+ for _, run := range taskCheckRuns {
+ composedTaskCheckRun, err := s.composeTaskCheckRun(ctx, run)
if err != nil {
- return nil, errors.Wrapf(err, "failed to compose TaskCheckRun with taskCheckRunRaw[%+v]", raw)
+ return nil, errors.Wrapf(err, "failed to compose task check run %#v", run)
}
- taskCheckRunList = append(taskCheckRunList, taskCheckRun)
+ composedTaskCheckRuns = append(composedTaskCheckRuns, composedTaskCheckRun)
}
- return taskCheckRunList, nil
+ return composedTaskCheckRuns, nil
}
// PatchTaskCheckRunStatus patches an instance of TaskCheckRunStatus.
-func (s *Store) PatchTaskCheckRunStatus(ctx context.Context, patch *api.TaskCheckRunStatusPatch) (*api.TaskCheckRun, error) {
- taskCheckRunRaw, err := s.patchTaskCheckRunRawStatus(ctx, patch)
+func (s *Store) PatchTaskCheckRunStatus(ctx context.Context, patch *TaskCheckRunStatusPatch) (*api.TaskCheckRun, error) {
+ taskCheckRun, err := s.patchTaskCheckRunStatusV1(ctx, patch)
if err != nil {
- return nil, errors.Wrapf(err, "failed to patch TaskCheckRunStatus with TaskCheckRunStatusPatch[%+v]", patch)
+ return nil, errors.Wrapf(err, "failed to patch task check run %#v", patch)
}
- taskCheckRun, err := s.composeTaskCheckRun(ctx, taskCheckRunRaw)
+ composedTaskCheckRun, err := s.composeTaskCheckRun(ctx, taskCheckRun)
if err != nil {
- return nil, errors.Wrapf(err, "failed to compose TaskCheckRunStatus with taskCheckRunRaw[%+v]", taskCheckRunRaw)
+ return nil, errors.Wrapf(err, "failed to compose task check run %#v", taskCheckRun)
}
- return taskCheckRun, nil
+ return composedTaskCheckRun, nil
}
-//
-// private functions
-//
+func (s *Store) composeTaskCheckRun(ctx context.Context, run *TaskCheckRunMessage) (*api.TaskCheckRun, error) {
+ composedTaskCheckRun := run.toTaskCheckRun()
-// composeTaskCheckRun composes an instance of TaskCheckRun by taskCheckRunRaw.
-func (s *Store) composeTaskCheckRun(ctx context.Context, raw *taskCheckRunRaw) (*api.TaskCheckRun, error) {
- taskCheckRun := raw.toTaskCheckRun()
-
- creator, err := s.GetPrincipalByID(ctx, taskCheckRun.CreatorID)
+ creator, err := s.GetPrincipalByID(ctx, composedTaskCheckRun.CreatorID)
if err != nil {
return nil, err
}
- taskCheckRun.Creator = creator
+ composedTaskCheckRun.Creator = creator
- updater, err := s.GetPrincipalByID(ctx, taskCheckRun.UpdaterID)
+ updater, err := s.GetPrincipalByID(ctx, composedTaskCheckRun.UpdaterID)
if err != nil {
return nil, err
}
- taskCheckRun.Updater = updater
+ composedTaskCheckRun.Updater = updater
- return taskCheckRun, nil
+ return composedTaskCheckRun, nil
}
-// createTaskCheckRunRawIfNeeded creates a new taskCheckRun. See interface for the expected behavior.
-func (s *Store) createTaskCheckRunRawIfNeeded(ctx context.Context, create *api.TaskCheckRunCreate) (*taskCheckRunRaw, error) {
+// TaskCheckRunCreate is the API message for creating a task check run.
+type TaskCheckRunCreate struct {
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ CreatorID int
+
+ // Related fields
+ TaskID int
+
+ // Domain specific fields
+ Type api.TaskCheckType
+ Comment string
+ Payload string
+}
+
+// TaskCheckRunFind is the API message for finding task check runs.
+type TaskCheckRunFind struct {
+ // Related fields
+ TaskID *int
+ StageID *int
+ PipelineID *int
+ Type *api.TaskCheckType
+
+ // Domain specific fields
+ StatusList *[]api.TaskCheckRunStatus
+}
+
+// TaskCheckRunStatusPatch is the API message for patching a task check run.
+type TaskCheckRunStatusPatch struct {
+ ID *int
+ UpdaterID int
+
+ Status api.TaskCheckRunStatus
+ Code common.Code
+ Result string
+}
+
+// CreateTaskCheckRunIfNeeded creates a new taskCheckRun.
+func (s *Store) CreateTaskCheckRunIfNeeded(ctx context.Context, create *TaskCheckRunCreate) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
- return nil, FormatError(err)
+ return err
}
defer tx.Rollback()
statusList := []api.TaskCheckRunStatus{api.TaskCheckRunRunning}
- taskCheckRunFind := &api.TaskCheckRunFind{
+ taskCheckRunFind := &TaskCheckRunFind{
TaskID: &create.TaskID,
Type: &create.Type,
StatusList: &statusList,
}
- taskCheckRunList, err := s.findTaskCheckRunRawTx(ctx, tx, taskCheckRunFind)
+ taskCheckRunList, err := s.findTaskCheckRunImpl(ctx, tx, taskCheckRunFind)
if err != nil {
- return nil, err
+ return err
}
if runningCount := len(taskCheckRunList); runningCount > 0 {
@@ -176,26 +193,22 @@ func (s *Store) createTaskCheckRunRawIfNeeded(ctx context.Context, create *api.T
zap.String("task_check_type", string(create.Type)),
)
}
- return taskCheckRunList[0], nil
+ return nil
}
list, err := s.createTaskCheckRunImpl(ctx, tx, create)
if err != nil {
- return nil, err
+ return err
}
if len(list) != 1 {
- return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d task check runs, expect 1", len(list))}
+ return &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d task check runs, expect 1", len(list))}
}
- if err := tx.Commit(); err != nil {
- return nil, FormatError(err)
- }
-
- return list[0], nil
+ return tx.Commit()
}
-func (s *Store) batchCreateTaskCheckRunRaw(ctx context.Context, creates []*api.TaskCheckRunCreate) ([]*taskCheckRunRaw, error) {
+func (s *Store) batchCreateTaskCheckRunV1(ctx context.Context, creates []*TaskCheckRunCreate) ([]*TaskCheckRunMessage, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -214,7 +227,7 @@ func (s *Store) batchCreateTaskCheckRunRaw(ctx context.Context, creates []*api.T
return taskCheckRunList, nil
}
-func (*Store) createTaskCheckRunImpl(ctx context.Context, tx *Tx, creates ...*api.TaskCheckRunCreate) ([]*taskCheckRunRaw, error) {
+func (*Store) createTaskCheckRunImpl(ctx context.Context, tx *Tx, creates ...*TaskCheckRunCreate) ([]*TaskCheckRunMessage, error) {
var query strings.Builder
var values []interface{}
var queryValues []string
@@ -252,66 +265,40 @@ func (*Store) createTaskCheckRunImpl(ctx context.Context, tx *Tx, creates ...*ap
return nil, err
}
- var taskCheckRunRawList []*taskCheckRunRaw
+ var taskCheckRuns []*TaskCheckRunMessage
rows, err := tx.QueryContext(ctx, query.String(), values...)
if err != nil {
return nil, FormatError(err)
}
defer rows.Close()
for rows.Next() {
- var taskCheckRunRaw taskCheckRunRaw
+ var run TaskCheckRunMessage
if err := rows.Scan(
- &taskCheckRunRaw.ID,
- &taskCheckRunRaw.CreatorID,
- &taskCheckRunRaw.CreatedTs,
- &taskCheckRunRaw.UpdaterID,
- &taskCheckRunRaw.UpdatedTs,
- &taskCheckRunRaw.TaskID,
- &taskCheckRunRaw.Status,
- &taskCheckRunRaw.Type,
- &taskCheckRunRaw.Code,
- &taskCheckRunRaw.Comment,
- &taskCheckRunRaw.Result,
- &taskCheckRunRaw.Payload,
+ &run.ID,
+ &run.CreatorID,
+ &run.CreatedTs,
+ &run.UpdaterID,
+ &run.UpdatedTs,
+ &run.TaskID,
+ &run.Status,
+ &run.Type,
+ &run.Code,
+ &run.Comment,
+ &run.Result,
+ &run.Payload,
); err != nil {
return nil, FormatError(err)
}
- taskCheckRunRawList = append(taskCheckRunRawList, &taskCheckRunRaw)
+ taskCheckRuns = append(taskCheckRuns, &run)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return taskCheckRunRawList, nil
-}
-
-// findTaskCheckRunRaw retrieves a list of taskCheckRuns based on find.
-func (s *Store) findTaskCheckRunRaw(ctx context.Context, find *api.TaskCheckRunFind) ([]*taskCheckRunRaw, error) {
- tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.Rollback()
-
- list, err := s.findTaskCheckRunImpl(ctx, tx, find)
- if err != nil {
- return nil, err
- }
-
- return list, nil
-}
-
-// findTaskCheckRunRawTx retrieves a list of taskCheckRuns based on find.
-func (s *Store) findTaskCheckRunRawTx(ctx context.Context, tx *Tx, find *api.TaskCheckRunFind) ([]*taskCheckRunRaw, error) {
- list, err := s.findTaskCheckRunImpl(ctx, tx, find)
- if err != nil {
- return nil, err
- }
-
- return list, nil
+ return taskCheckRuns, nil
}
-// patchTaskCheckRunRawStatus updates a taskCheckRun status. Returns the new state of the taskCheckRun after update.
-func (s *Store) patchTaskCheckRunRawStatus(ctx context.Context, patch *api.TaskCheckRunStatusPatch) (*taskCheckRunRaw, error) {
+// patchTaskCheckRunStatusV1 updates a taskCheckRun status. Returns the new state of the taskCheckRun after update.
+func (s *Store) patchTaskCheckRunStatusV1(ctx context.Context, patch *TaskCheckRunStatusPatch) (*TaskCheckRunMessage, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -331,7 +318,7 @@ func (s *Store) patchTaskCheckRunRawStatus(ctx context.Context, patch *api.TaskC
}
// patchTaskCheckRunStatusImpl updates a taskCheckRun status. Returns the new state of the taskCheckRun after update.
-func (*Store) patchTaskCheckRunStatusImpl(ctx context.Context, tx *Tx, patch *api.TaskCheckRunStatusPatch) (*taskCheckRunRaw, error) {
+func (*Store) patchTaskCheckRunStatusImpl(ctx context.Context, tx *Tx, patch *TaskCheckRunStatusPatch) (*TaskCheckRunMessage, error) {
// Build UPDATE clause.
if patch.Result == "" {
patch.Result = "{}"
@@ -347,7 +334,7 @@ func (*Store) patchTaskCheckRunStatusImpl(ctx context.Context, tx *Tx, patch *ap
where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)
}
- var taskCheckRunRaw taskCheckRunRaw
+ var taskCheckRun TaskCheckRunMessage
if err := tx.QueryRowContext(ctx, `
UPDATE task_check_run
SET `+strings.Join(set, ", ")+`
@@ -356,28 +343,29 @@ func (*Store) patchTaskCheckRunStatusImpl(ctx context.Context, tx *Tx, patch *ap
`,
args...,
).Scan(
- &taskCheckRunRaw.ID,
- &taskCheckRunRaw.CreatorID,
- &taskCheckRunRaw.CreatedTs,
- &taskCheckRunRaw.UpdaterID,
- &taskCheckRunRaw.UpdatedTs,
- &taskCheckRunRaw.TaskID,
- &taskCheckRunRaw.Status,
- &taskCheckRunRaw.Type,
- &taskCheckRunRaw.Code,
- &taskCheckRunRaw.Comment,
- &taskCheckRunRaw.Result,
- &taskCheckRunRaw.Payload,
+ &taskCheckRun.ID,
+ &taskCheckRun.CreatorID,
+ &taskCheckRun.CreatedTs,
+ &taskCheckRun.UpdaterID,
+ &taskCheckRun.UpdatedTs,
+ &taskCheckRun.TaskID,
+ &taskCheckRun.Status,
+ &taskCheckRun.Type,
+ &taskCheckRun.Code,
+ &taskCheckRun.Comment,
+ &taskCheckRun.Result,
+ &taskCheckRun.Payload,
); err != nil {
if err == sql.ErrNoRows {
return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("task check run ID not found: %d", *patch.ID)}
}
return nil, FormatError(err)
}
- return &taskCheckRunRaw, nil
+ return &taskCheckRun, nil
}
-func (s *Store) listTaskCheckRun(ctx context.Context, find *api.TaskCheckRunFind) ([]*taskCheckRunRaw, error) {
+// ListTaskCheckRuns list task check runs.
+func (s *Store) ListTaskCheckRuns(ctx context.Context, find *TaskCheckRunFind) ([]*TaskCheckRunMessage, error) {
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, FormatError(err)
@@ -396,17 +384,24 @@ func (s *Store) listTaskCheckRun(ctx context.Context, find *api.TaskCheckRunFind
return list, nil
}
-func (*Store) findTaskCheckRunImpl(ctx context.Context, tx *Tx, find *api.TaskCheckRunFind) ([]*taskCheckRunRaw, error) {
+func (*Store) findTaskCheckRunImpl(ctx context.Context, tx *Tx, find *TaskCheckRunFind) ([]*TaskCheckRunMessage, 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)
- }
if v := find.TaskID; v != nil {
- where, args = append(where, fmt.Sprintf("task_id = $%d", len(args)+1)), append(args, *v)
+ where, args = append(where, fmt.Sprintf("task_check_run.task_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.StageID; v != nil {
+ where, args = append(where, fmt.Sprintf("task.stage_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.PipelineID; v != nil {
+ where, args = append(where, fmt.Sprintf("task.pipeline_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if find.StageID != nil || find.PipelineID != nil {
+ joinClause = "JOIN task ON task.id = task_check_run.task_id"
}
if v := find.Type; v != nil {
- where, args = append(where, fmt.Sprintf("type = $%d", len(args)+1)), append(args, *v)
+ where, args = append(where, fmt.Sprintf("task_check_run.type = $%d", len(args)+1)), append(args, *v)
}
if v := find.StatusList; v != nil {
list := []string{}
@@ -417,22 +412,23 @@ func (*Store) findTaskCheckRunImpl(ctx context.Context, tx *Tx, find *api.TaskCh
where = append(where, fmt.Sprintf("status in (%s)", strings.Join(list, ",")))
}
- rows, err := tx.QueryContext(ctx, `
+ rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT
- id,
- creator_id,
- created_ts,
- updater_id,
- updated_ts,
- task_id,
- status,
- type,
- code,
- comment,
- result,
- payload
+ task_check_run.id,
+ task_check_run.creator_id,
+ task_check_run.created_ts,
+ task_check_run.updater_id,
+ task_check_run.updated_ts,
+ task_check_run.task_id,
+ task_check_run.status,
+ task_check_run.type,
+ task_check_run.code,
+ task_check_run.comment,
+ task_check_run.result,
+ task_check_run.payload
FROM task_check_run
- WHERE `+strings.Join(where, " AND "),
+ %s
+ WHERE %s`, joinClause, strings.Join(where, " AND ")),
args...,
)
if err != nil {
@@ -440,10 +436,9 @@ func (*Store) findTaskCheckRunImpl(ctx context.Context, tx *Tx, find *api.TaskCh
}
defer rows.Close()
- // Iterate over result set and deserialize rows into taskCheckRunRawList.
- var taskCheckRunRawList []*taskCheckRunRaw
+ var taskCheckRuns []*TaskCheckRunMessage
for rows.Next() {
- var taskCheckRun taskCheckRunRaw
+ var taskCheckRun TaskCheckRunMessage
if err := rows.Scan(
&taskCheckRun.ID,
&taskCheckRun.CreatorID,
@@ -461,11 +456,11 @@ func (*Store) findTaskCheckRunImpl(ctx context.Context, tx *Tx, find *api.TaskCh
return nil, FormatError(err)
}
- taskCheckRunRawList = append(taskCheckRunRawList, &taskCheckRun)
+ taskCheckRuns = append(taskCheckRuns, &taskCheckRun)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return taskCheckRunRawList, nil
+ return taskCheckRuns, nil
}