Skip to content

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

refactor: add TaskRunRaw to represent store model for TaskRun (#873)

Yilong Licc940a8b1e84
diff --git a/api/task_run.go b/api/task_run.go
index 045488bd6e..da19299085 100644
--- a/api/task_run.go
+++ b/api/task_run.go
@@ -45,6 +45,56 @@ type TaskRunResultPayload struct {
Version string `json:"version,omitempty"`
}
+// TaskRunRaw is the store model for an TaskRun.
+// Fields have exactly the same meanings as TaskRun.
+type TaskRunRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ TaskID int
+
+ // Domain specific fields
+ Name string
+ Status TaskRunStatus
+ Type TaskType
+ Code common.Code
+ Comment string
+ Result string
+ Payload string
+}
+
+// ToTaskRun creates an instance of TaskRun based on the TaskRunRaw.
+// This is intended to be called when we need to compose an TaskRun relationship.
+func (raw *TaskRunRaw) ToTaskRun() *TaskRun {
+ return &TaskRun{
+ 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
+ Name: raw.Name,
+ Status: raw.Status,
+ Type: raw.Type,
+ Code: raw.Code,
+ Comment: raw.Comment,
+ Result: raw.Result,
+ Payload: raw.Payload,
+ }
+}
+
// TaskRun is the API message for a task run.
type TaskRun struct {
ID int `jsonapi:"primary,taskRun"`
@@ -124,8 +174,8 @@ type TaskRunStatusPatch struct {
// TaskRunService is the service for task runs.
type TaskRunService interface {
- CreateTaskRunTx(ctx context.Context, tx *sql.Tx, create *TaskRunCreate) (*TaskRun, error)
- FindTaskRunListTx(ctx context.Context, tx *sql.Tx, find *TaskRunFind) ([]*TaskRun, error)
- FindTaskRunTx(ctx context.Context, tx *sql.Tx, find *TaskRunFind) (*TaskRun, error)
- PatchTaskRunStatusTx(ctx context.Context, tx *sql.Tx, patch *TaskRunStatusPatch) (*TaskRun, error)
+ CreateTaskRunTx(ctx context.Context, tx *sql.Tx, create *TaskRunCreate) (*TaskRunRaw, error)
+ FindTaskRunListTx(ctx context.Context, tx *sql.Tx, find *TaskRunFind) ([]*TaskRunRaw, error)
+ FindTaskRunTx(ctx context.Context, tx *sql.Tx, find *TaskRunFind) (*TaskRunRaw, error)
+ PatchTaskRunStatusTx(ctx context.Context, tx *sql.Tx, patch *TaskRunStatusPatch) (*TaskRunRaw, error)
}
diff --git a/store/task.go b/store/task.go
index 03686c9e23..2693bdca82 100644
--- a/store/task.go
+++ b/store/task.go
@@ -317,10 +317,15 @@ func (s *TaskService) findTaskList(ctx context.Context, tx *sql.Tx, find *api.Ta
taskRunFind := &api.TaskRunFind{
TaskID: &task.ID,
}
- taskRunList, err := s.TaskRunService.FindTaskRunListTx(ctx, tx, taskRunFind)
+ taskRunRawList, err := s.TaskRunService.FindTaskRunListTx(ctx, tx, taskRunFind)
if err != nil {
return nil, err
}
+ // TODO(dragonly): compose TaskRunList
+ var taskRunList []*api.TaskRun
+ for _, taskRunRaw := range taskRunRawList {
+ taskRunList = append(taskRunList, taskRunRaw.ToTaskRun())
+ }
task.TaskRunList = taskRunList
taskCheckRunFind := &api.TaskCheckRunFind{
@@ -520,10 +525,15 @@ func (s *TaskService) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *ap
taskRunFind := &api.TaskRunFind{
TaskID: &task.ID,
}
- taskRunList, err := s.TaskRunService.FindTaskRunListTx(ctx, tx, taskRunFind)
+ taskRunRawList, err := s.TaskRunService.FindTaskRunListTx(ctx, tx, taskRunFind)
if err != nil {
return nil, err
}
+ // TODO(dragonly): compose TaskRunList
+ var taskRunList []*api.TaskRun
+ for _, taskRunRaw := range taskRunRawList {
+ taskRunList = append(taskRunList, taskRunRaw.ToTaskRun())
+ }
task.TaskRunList = taskRunList
taskCheckRunFind := &api.TaskCheckRunFind{
diff --git a/store/task_run.go b/store/task_run.go
index 49f151e9fc..7e8e4ab9f9 100644
--- a/store/task_run.go
+++ b/store/task_run.go
@@ -27,7 +27,7 @@ func NewTaskRunService(logger *zap.Logger, db *DB) *TaskRunService {
}
// CreateTaskRunTx creates a new taskRun.
-func (s *TaskRunService) CreateTaskRunTx(ctx context.Context, tx *sql.Tx, create *api.TaskRunCreate) (*api.TaskRun, error) {
+func (s *TaskRunService) CreateTaskRunTx(ctx context.Context, tx *sql.Tx, create *api.TaskRunCreate) (*api.TaskRunRaw, error) {
if create.Payload == "" {
create.Payload = "{}"
}
@@ -58,55 +58,55 @@ func (s *TaskRunService) CreateTaskRunTx(ctx context.Context, tx *sql.Tx, create
defer row.Close()
row.Next()
- var taskRun api.TaskRun
+ var taskRunRaw api.TaskRunRaw
if err := row.Scan(
- &taskRun.ID,
- &taskRun.CreatorID,
- &taskRun.CreatedTs,
- &taskRun.UpdaterID,
- &taskRun.UpdatedTs,
- &taskRun.TaskID,
- &taskRun.Name,
- &taskRun.Status,
- &taskRun.Type,
- &taskRun.Code,
- &taskRun.Comment,
- &taskRun.Result,
- &taskRun.Payload,
+ &taskRunRaw.ID,
+ &taskRunRaw.CreatorID,
+ &taskRunRaw.CreatedTs,
+ &taskRunRaw.UpdaterID,
+ &taskRunRaw.UpdatedTs,
+ &taskRunRaw.TaskID,
+ &taskRunRaw.Name,
+ &taskRunRaw.Status,
+ &taskRunRaw.Type,
+ &taskRunRaw.Code,
+ &taskRunRaw.Comment,
+ &taskRunRaw.Result,
+ &taskRunRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
- return &taskRun, nil
+ return &taskRunRaw, nil
}
// FindTaskRunListTx retrieves a list of taskRuns based on find.
-func (s *TaskRunService) FindTaskRunListTx(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) ([]*api.TaskRun, error) {
- list, err := s.findTaskRunList(ctx, tx, find)
+func (s *TaskRunService) FindTaskRunListTx(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) ([]*api.TaskRunRaw, error) {
+ taskRunRawList, err := s.findTaskRunList(ctx, tx, find)
if err != nil {
return nil, err
}
- return list, nil
+ return taskRunRawList, nil
}
// FindTaskRunTx retrieves a single taskRun based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *TaskRunService) FindTaskRunTx(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) (*api.TaskRun, error) {
- list, err := s.findTaskRunList(ctx, tx, find)
+func (s *TaskRunService) FindTaskRunTx(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) (*api.TaskRunRaw, error) {
+ taskRunRawList, err := s.findTaskRunList(ctx, tx, find)
if err != nil {
return nil, err
}
- if len(list) == 0 {
+ if len(taskRunRawList) == 0 {
return nil, nil
- } else if len(list) > 1 {
- return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d task runs with filter %+v, expect 1", len(list), find)}
+ } else if len(taskRunRawList) > 1 {
+ return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d task runs with filter %+v, expect 1", len(taskRunRawList), find)}
}
- return list[0], nil
+ return taskRunRawList[0], nil
}
// PatchTaskRunStatusTx updates a taskRun status. Returns the new state of the taskRun after update.
-func (s *TaskRunService) PatchTaskRunStatusTx(ctx context.Context, tx *sql.Tx, patch *api.TaskRunStatusPatch) (*api.TaskRun, error) {
+func (s *TaskRunService) PatchTaskRunStatusTx(ctx context.Context, tx *sql.Tx, patch *api.TaskRunStatusPatch) (*api.TaskRunRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
set, args = append(set, "status = $2"), append(args, patch.Status)
@@ -148,29 +148,29 @@ func (s *TaskRunService) PatchTaskRunStatusTx(ctx context.Context, tx *sql.Tx, p
defer row.Close()
row.Next()
- var taskRun api.TaskRun
+ var taskRunRaw api.TaskRunRaw
if err := row.Scan(
- &taskRun.ID,
- &taskRun.CreatorID,
- &taskRun.CreatedTs,
- &taskRun.UpdaterID,
- &taskRun.UpdatedTs,
- &taskRun.TaskID,
- &taskRun.Name,
- &taskRun.Status,
- &taskRun.Type,
- &taskRun.Code,
- &taskRun.Comment,
- &taskRun.Result,
- &taskRun.Payload,
+ &taskRunRaw.ID,
+ &taskRunRaw.CreatorID,
+ &taskRunRaw.CreatedTs,
+ &taskRunRaw.UpdaterID,
+ &taskRunRaw.UpdatedTs,
+ &taskRunRaw.TaskID,
+ &taskRunRaw.Name,
+ &taskRunRaw.Status,
+ &taskRunRaw.Type,
+ &taskRunRaw.Code,
+ &taskRunRaw.Comment,
+ &taskRunRaw.Result,
+ &taskRunRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
- return &taskRun, nil
+ return &taskRunRaw, nil
}
-func (s *TaskRunService) findTaskRunList(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) ([]*api.TaskRun, error) {
+func (s *TaskRunService) findTaskRunList(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) ([]*api.TaskRunRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -213,33 +213,33 @@ func (s *TaskRunService) findTaskRunList(ctx context.Context, tx *sql.Tx, find *
}
defer rows.Close()
- // Iterate over result set and deserialize rows into taskRunList.
- var taskRunList []*api.TaskRun
+ // Iterate over result set and deserialize rows into taskRunRawList.
+ var taskRunRawList []*api.TaskRunRaw
for rows.Next() {
- var taskRun api.TaskRun
+ var taskRunRaw api.TaskRunRaw
if err := rows.Scan(
- &taskRun.ID,
- &taskRun.CreatorID,
- &taskRun.CreatedTs,
- &taskRun.UpdaterID,
- &taskRun.UpdatedTs,
- &taskRun.TaskID,
- &taskRun.Name,
- &taskRun.Status,
- &taskRun.Type,
- &taskRun.Code,
- &taskRun.Comment,
- &taskRun.Result,
- &taskRun.Payload,
+ &taskRunRaw.ID,
+ &taskRunRaw.CreatorID,
+ &taskRunRaw.CreatedTs,
+ &taskRunRaw.UpdaterID,
+ &taskRunRaw.UpdatedTs,
+ &taskRunRaw.TaskID,
+ &taskRunRaw.Name,
+ &taskRunRaw.Status,
+ &taskRunRaw.Type,
+ &taskRunRaw.Code,
+ &taskRunRaw.Comment,
+ &taskRunRaw.Result,
+ &taskRunRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
- taskRunList = append(taskRunList, &taskRun)
+ taskRunRawList = append(taskRunRawList, &taskRunRaw)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return taskRunList, nil
+ return taskRunRawList, nil
}