Skip to content

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

refactor: add TaskCheckRunRaw to represent store model for TaskCheckRun (#875)

Yilong Li8fa9c9fa5fd9
diff --git a/api/task_check_run.go b/api/task_check_run.go
index 2d95d8b192..a581240f82 100644
--- a/api/task_check_run.go
+++ b/api/task_check_run.go
@@ -108,6 +108,54 @@ type TaskCheckRunResultPayload struct {
ResultList []TaskCheckResult `json:"resultList,omitempty"`
}
+// TaskCheckRunRaw is the store model for a TaskCheckRun.
+// Fields have exactly the same meanings as TaskCheckRun.
+type TaskCheckRunRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ TaskID int
+
+ // Domain specific fields
+ Status TaskCheckRunStatus
+ Type TaskCheckType
+ Code common.Code
+ Comment string
+ Result string
+ 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() *TaskCheckRun {
+ return &TaskCheckRun{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ TaskID: raw.TaskID,
+
+ // Domain specific fields
+ Status: raw.Status,
+ Type: raw.Type,
+ Code: raw.Code,
+ Comment: raw.Comment,
+ Result: raw.Result,
+ Payload: raw.Payload,
+ }
+}
+
// TaskCheckRun is the API message for task check run.
type TaskCheckRun struct {
ID int `jsonapi:"primary,taskCheckRun"`
@@ -194,8 +242,8 @@ type TaskCheckRunService interface {
// 1. There is no existing RUNNING check run. If this is the case, then returns that RUNNING check run.
// 2. If SkipIfAlreadyTerminated is false, or if SkipIfAlreadyTerminated is true and there is no DONE/FAILED/CANCELED check run. If this is the case,
// then returns that terminated check run.
- CreateTaskCheckRunIfNeeded(ctx context.Context, create *TaskCheckRunCreate) (*TaskCheckRun, error)
- FindTaskCheckRunList(ctx context.Context, find *TaskCheckRunFind) ([]*TaskCheckRun, error)
- FindTaskCheckRunListTx(ctx context.Context, tx *sql.Tx, find *TaskCheckRunFind) ([]*TaskCheckRun, error)
- PatchTaskCheckRunStatus(ctx context.Context, patch *TaskCheckRunStatusPatch) (*TaskCheckRun, error)
+ CreateTaskCheckRunIfNeeded(ctx context.Context, create *TaskCheckRunCreate) (*TaskCheckRunRaw, error)
+ FindTaskCheckRunList(ctx context.Context, find *TaskCheckRunFind) ([]*TaskCheckRunRaw, error)
+ FindTaskCheckRunListTx(ctx context.Context, tx *sql.Tx, find *TaskCheckRunFind) ([]*TaskCheckRunRaw, error)
+ PatchTaskCheckRunStatus(ctx context.Context, patch *TaskCheckRunStatusPatch) (*TaskCheckRunRaw, error)
}
diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.go
index 792bff4c14..0941cb2c79 100644
--- a/server/task_check_scheduler.go
+++ b/server/task_check_scheduler.go
@@ -59,12 +59,16 @@ func (s *TaskCheckScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
taskCheckRunFind := &api.TaskCheckRunFind{
StatusList: &taskCheckRunStatusList,
}
- taskCheckRunList, err := s.server.TaskCheckRunService.FindTaskCheckRunList(ctx, taskCheckRunFind)
+ taskCheckRunRawList, err := s.server.TaskCheckRunService.FindTaskCheckRunList(ctx, taskCheckRunFind)
if err != nil {
s.l.Error("Failed to retrieve running tasks", zap.Error(err))
return
}
-
+ // TODO(dragonly): compose this
+ var taskCheckRunList []*api.TaskCheckRun
+ for _, raw := range taskCheckRunRawList {
+ taskCheckRunList = append(taskCheckRunList, raw.ToTaskCheckRun())
+ }
for _, taskCheckRun := range taskCheckRunList {
executor, ok := s.executors[string(taskCheckRun.Type)]
if !ok {
@@ -337,10 +341,15 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
taskCheckRunFind := &api.TaskCheckRunFind{
TaskID: &task.ID,
}
- taskCheckRunList, err := s.server.TaskCheckRunService.FindTaskCheckRunList(ctx, taskCheckRunFind)
+ taskCheckRunRawList, err := s.server.TaskCheckRunService.FindTaskCheckRunList(ctx, taskCheckRunFind)
if err != nil {
return nil, err
}
+ // TODO(dragonly): compose this
+ var taskCheckRunList []*api.TaskCheckRun
+ for _, raw := range taskCheckRunRawList {
+ taskCheckRunList = append(taskCheckRunList, raw.ToTaskCheckRun())
+ }
task.TaskCheckRunList = taskCheckRunList
return task, err
diff --git a/store/task.go b/store/task.go
index 2693bdca82..d98d29192a 100644
--- a/store/task.go
+++ b/store/task.go
@@ -331,10 +331,15 @@ func (s *TaskService) findTaskList(ctx context.Context, tx *sql.Tx, find *api.Ta
taskCheckRunFind := &api.TaskCheckRunFind{
TaskID: &task.ID,
}
- taskCheckRunList, err := s.TaskCheckRunService.FindTaskCheckRunListTx(ctx, tx, taskCheckRunFind)
+ taskCheckRunRawList, err := s.TaskCheckRunService.FindTaskCheckRunListTx(ctx, tx, taskCheckRunFind)
if err != nil {
return nil, err
}
+ // TODO(dragonly): compose this
+ var taskCheckRunList []*api.TaskCheckRun
+ for _, raw := range taskCheckRunRawList {
+ taskCheckRunList = append(taskCheckRunList, raw.ToTaskCheckRun())
+ }
task.TaskCheckRunList = taskCheckRunList
}
if err := rows.Err(); err != nil {
@@ -539,10 +544,15 @@ func (s *TaskService) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *ap
taskCheckRunFind := &api.TaskCheckRunFind{
TaskID: &task.ID,
}
- taskCheckRunList, err := s.TaskCheckRunService.FindTaskCheckRunListTx(ctx, tx, taskCheckRunFind)
+ taskCheckRunRawList, err := s.TaskCheckRunService.FindTaskCheckRunListTx(ctx, tx, taskCheckRunFind)
if err != nil {
return nil, err
}
+ // TODO(dragonly): compose this
+ var taskCheckRunList []*api.TaskCheckRun
+ for _, raw := range taskCheckRunRawList {
+ taskCheckRunList = append(taskCheckRunList, raw.ToTaskCheckRun())
+ }
task.TaskCheckRunList = taskCheckRunList
return t, nil
diff --git a/store/task_check_run.go b/store/task_check_run.go
index 0583bd16d3..3a6bfd2a77 100644
--- a/store/task_check_run.go
+++ b/store/task_check_run.go
@@ -27,7 +27,7 @@ func NewTaskCheckRunService(logger *zap.Logger, db *DB) *TaskCheckRunService {
}
// CreateTaskCheckRunIfNeeded creates a new taskCheckRun. See interface for the expected behavior
-func (s *TaskCheckRunService) CreateTaskCheckRunIfNeeded(ctx context.Context, create *api.TaskCheckRunCreate) (*api.TaskCheckRun, error) {
+func (s *TaskCheckRunService) CreateTaskCheckRunIfNeeded(ctx context.Context, create *api.TaskCheckRunCreate) (*api.TaskCheckRunRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -90,7 +90,7 @@ func (s *TaskCheckRunService) CreateTaskCheckRunIfNeeded(ctx context.Context, cr
}
// createTaskCheckRunTx creates a new taskCheckRun.
-func (s *TaskCheckRunService) createTaskCheckRunTx(ctx context.Context, tx *sql.Tx, create *api.TaskCheckRunCreate) (*api.TaskCheckRun, error) {
+func (s *TaskCheckRunService) createTaskCheckRunTx(ctx context.Context, tx *sql.Tx, create *api.TaskCheckRunCreate) (*api.TaskCheckRunRaw, error) {
if create.Payload == "" {
create.Payload = "{}"
}
@@ -119,35 +119,35 @@ func (s *TaskCheckRunService) createTaskCheckRunTx(ctx context.Context, tx *sql.
}
defer rows.Close()
- var t *api.TaskCheckRun
+ var tRaw *api.TaskCheckRunRaw
for rows.Next() {
- var taskCheckRun api.TaskCheckRun
+ var taskCheckRunRaw api.TaskCheckRunRaw
if err := rows.Scan(
- &taskCheckRun.ID,
- &taskCheckRun.CreatorID,
- &taskCheckRun.CreatedTs,
- &taskCheckRun.UpdaterID,
- &taskCheckRun.UpdatedTs,
- &taskCheckRun.TaskID,
- &taskCheckRun.Status,
- &taskCheckRun.Type,
- &taskCheckRun.Code,
- &taskCheckRun.Comment,
- &taskCheckRun.Result,
- &taskCheckRun.Payload,
+ &taskCheckRunRaw.ID,
+ &taskCheckRunRaw.CreatorID,
+ &taskCheckRunRaw.CreatedTs,
+ &taskCheckRunRaw.UpdaterID,
+ &taskCheckRunRaw.UpdatedTs,
+ &taskCheckRunRaw.TaskID,
+ &taskCheckRunRaw.Status,
+ &taskCheckRunRaw.Type,
+ &taskCheckRunRaw.Code,
+ &taskCheckRunRaw.Comment,
+ &taskCheckRunRaw.Result,
+ &taskCheckRunRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
- t = &taskCheckRun
+ tRaw = &taskCheckRunRaw
}
- if t != nil {
- return t, nil
+ if tRaw != nil {
+ return tRaw, nil
}
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("task check run cannot be created for task ID %v", create.TaskID)}
}
// FindTaskCheckRunList retrieves a list of taskCheckRuns based on find.
-func (s *TaskCheckRunService) FindTaskCheckRunList(ctx context.Context, find *api.TaskCheckRunFind) ([]*api.TaskCheckRun, error) {
+func (s *TaskCheckRunService) FindTaskCheckRunList(ctx context.Context, find *api.TaskCheckRunFind) ([]*api.TaskCheckRunRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -163,7 +163,7 @@ func (s *TaskCheckRunService) FindTaskCheckRunList(ctx context.Context, find *ap
}
// FindTaskCheckRunListTx retrieves a list of taskCheckRuns based on find.
-func (s *TaskCheckRunService) FindTaskCheckRunListTx(ctx context.Context, tx *sql.Tx, find *api.TaskCheckRunFind) ([]*api.TaskCheckRun, error) {
+func (s *TaskCheckRunService) FindTaskCheckRunListTx(ctx context.Context, tx *sql.Tx, find *api.TaskCheckRunFind) ([]*api.TaskCheckRunRaw, error) {
list, err := s.findTaskCheckRunList(ctx, tx, find)
if err != nil {
return nil, err
@@ -173,7 +173,7 @@ func (s *TaskCheckRunService) FindTaskCheckRunListTx(ctx context.Context, tx *sq
}
// PatchTaskCheckRunStatus updates a taskCheckRun status. Returns the new state of the taskCheckRun after update.
-func (s *TaskCheckRunService) PatchTaskCheckRunStatus(ctx context.Context, patch *api.TaskCheckRunStatusPatch) (*api.TaskCheckRun, error) {
+func (s *TaskCheckRunService) PatchTaskCheckRunStatus(ctx context.Context, patch *api.TaskCheckRunStatusPatch) (*api.TaskCheckRunRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -193,7 +193,7 @@ func (s *TaskCheckRunService) PatchTaskCheckRunStatus(ctx context.Context, patch
}
// patchTaskCheckRunStatusTx updates a taskCheckRun status. Returns the new state of the taskCheckRun after update.
-func (s *TaskCheckRunService) patchTaskCheckRunStatusTx(ctx context.Context, tx *sql.Tx, patch *api.TaskCheckRunStatusPatch) (*api.TaskCheckRun, error) {
+func (s *TaskCheckRunService) patchTaskCheckRunStatusTx(ctx context.Context, tx *sql.Tx, patch *api.TaskCheckRunStatusPatch) (*api.TaskCheckRunRaw, error) {
// Build UPDATE clause.
if patch.Result == "" {
patch.Result = "{}"
@@ -222,34 +222,34 @@ func (s *TaskCheckRunService) patchTaskCheckRunStatusTx(ctx context.Context, tx
}
defer rows.Close()
- var t *api.TaskCheckRun
+ var tRaw *api.TaskCheckRunRaw
for rows.Next() {
- var taskCheckRun api.TaskCheckRun
+ var taskCheckRunRaw api.TaskCheckRunRaw
if err := rows.Scan(
- &taskCheckRun.ID,
- &taskCheckRun.CreatorID,
- &taskCheckRun.CreatedTs,
- &taskCheckRun.UpdaterID,
- &taskCheckRun.UpdatedTs,
- &taskCheckRun.TaskID,
- &taskCheckRun.Status,
- &taskCheckRun.Type,
- &taskCheckRun.Code,
- &taskCheckRun.Comment,
- &taskCheckRun.Result,
- &taskCheckRun.Payload,
+ &taskCheckRunRaw.ID,
+ &taskCheckRunRaw.CreatorID,
+ &taskCheckRunRaw.CreatedTs,
+ &taskCheckRunRaw.UpdaterID,
+ &taskCheckRunRaw.UpdatedTs,
+ &taskCheckRunRaw.TaskID,
+ &taskCheckRunRaw.Status,
+ &taskCheckRunRaw.Type,
+ &taskCheckRunRaw.Code,
+ &taskCheckRunRaw.Comment,
+ &taskCheckRunRaw.Result,
+ &taskCheckRunRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
- t = &taskCheckRun
+ tRaw = &taskCheckRunRaw
}
- if t != nil {
- return t, nil
+ if tRaw != nil {
+ return tRaw, nil
}
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("task check run ID not found: %d", *patch.ID)}
}
-func (s *TaskCheckRunService) findTaskCheckRunList(ctx context.Context, tx *sql.Tx, find *api.TaskCheckRunFind) ([]*api.TaskCheckRun, error) {
+func (s *TaskCheckRunService) findTaskCheckRunList(ctx context.Context, tx *sql.Tx, find *api.TaskCheckRunFind) ([]*api.TaskCheckRunRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -298,10 +298,10 @@ func (s *TaskCheckRunService) findTaskCheckRunList(ctx context.Context, tx *sql.
}
defer rows.Close()
- // Iterate over result set and deserialize rows into taskCheckRunList.
- var taskCheckRunList []*api.TaskCheckRun
+ // Iterate over result set and deserialize rows into taskCheckRunRawList.
+ var taskCheckRunRawList []*api.TaskCheckRunRaw
for rows.Next() {
- var taskCheckRun api.TaskCheckRun
+ var taskCheckRun api.TaskCheckRunRaw
if err := rows.Scan(
&taskCheckRun.ID,
&taskCheckRun.CreatorID,
@@ -319,11 +319,11 @@ func (s *TaskCheckRunService) findTaskCheckRunList(ctx context.Context, tx *sql.
return nil, FormatError(err)
}
- taskCheckRunList = append(taskCheckRunList, &taskCheckRun)
+ taskCheckRunRawList = append(taskCheckRunRawList, &taskCheckRun)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return taskCheckRunList, nil
+ return taskCheckRunRawList, nil
}