Skip to content

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

refactor: decouple api/service/store packages for TaskRun & TaskCheckRun (#1166)

Yilong Lif5140832dd0e
diff --git a/api/task.go b/api/task.go
index 611bd44b83..34b60bd3ca 100644
--- a/api/task.go
+++ b/api/task.go
@@ -1,7 +1,6 @@
package api
import (
- "context"
"encoding/json"
"github.com/bytebase/bytebase/common"
@@ -143,71 +142,6 @@ type TaskDatabaseRestorePayload struct {
BackupID int `json:"backupId,omitempty"`
}
-// TaskRaw is the store model for an Task.
-// Fields have exactly the same meanings as Task.
-type TaskRaw struct {
- ID int
-
- // Standard fields
- CreatorID int
- CreatedTs int64
- UpdaterID int
- UpdatedTs int64
-
- // Related fields
- PipelineID int
- StageID int
- InstanceID int
- // Could be empty for creating database task when the task isn't yet completed successfully.
- DatabaseID *int
- TaskRunRawList []*TaskRunRaw
- TaskCheckRunRawList []*TaskCheckRunRaw
-
- // Domain specific fields
- Name string
- Status TaskStatus
- Type TaskType
- Payload string
- EarliestAllowedTs int64
- BlockedBy []string
-}
-
-// ToTask creates an instance of Task based on the TaskRaw.
-// This is intended to be called when we need to compose an Task relationship.
-func (raw *TaskRaw) ToTask() *Task {
- task := &Task{
- ID: raw.ID,
-
- // Standard fields
- CreatorID: raw.CreatorID,
- CreatedTs: raw.CreatedTs,
- UpdaterID: raw.UpdaterID,
- UpdatedTs: raw.UpdatedTs,
-
- // Related fields
- PipelineID: raw.PipelineID,
- StageID: raw.StageID,
- InstanceID: raw.InstanceID,
- // Could be empty for creating database task when the task isn't yet completed successfully.
- DatabaseID: raw.DatabaseID,
-
- // Domain specific fields
- Name: raw.Name,
- Status: raw.Status,
- Type: raw.Type,
- Payload: raw.Payload,
- EarliestAllowedTs: raw.EarliestAllowedTs,
- BlockedBy: raw.BlockedBy,
- }
- for _, taskRunRaw := range raw.TaskRunRawList {
- task.TaskRunList = append(task.TaskRunList, taskRunRaw.ToTaskRun())
- }
- for _, taskCheckRunRaw := range raw.TaskCheckRunRawList {
- task.TaskCheckRunList = append(task.TaskCheckRunList, taskCheckRunRaw.ToTaskCheckRun())
- }
- return task
-}
-
// Task is the API message for a task.
type Task struct {
ID int `jsonapi:"primary,task"`
@@ -243,34 +177,6 @@ type Task struct {
BlockedBy []string `jsonapi:"attr,blockedBy"`
}
-// ToRaw converts a Task to TaskRaw.
-// TODO(dragonly): This is a hack for function `createIssue`. We MUST review the code and remove this hack.
-func (task *Task) ToRaw() *TaskRaw {
- return &TaskRaw{
- ID: task.ID,
-
- // Standard fields
- CreatorID: task.CreatorID,
- CreatedTs: task.CreatedTs,
- UpdaterID: task.UpdaterID,
- UpdatedTs: task.UpdatedTs,
-
- // Related fields
- PipelineID: task.PipelineID,
- StageID: task.StageID,
- InstanceID: task.InstanceID,
- DatabaseID: task.DatabaseID,
-
- // Domain specific fields
- Name: task.Name,
- Status: task.Status,
- Type: task.Type,
- Payload: task.Payload,
- EarliestAllowedTs: task.EarliestAllowedTs,
- BlockedBy: task.BlockedBy,
- }
-}
-
// TaskCreate is the API message for creating a task.
type TaskCreate struct {
// Standard fields
@@ -350,12 +256,3 @@ type TaskStatusPatch struct {
Comment *string `jsonapi:"attr,comment"`
Result *string
}
-
-// TaskService is the service for tasks.
-type TaskService interface {
- CreateTask(ctx context.Context, create *TaskCreate) (*TaskRaw, error)
- FindTaskList(ctx context.Context, find *TaskFind) ([]*TaskRaw, error)
- FindTask(ctx context.Context, find *TaskFind) (*TaskRaw, error)
- PatchTask(ctx context.Context, patch *TaskPatch) (*TaskRaw, error)
- PatchTaskStatus(ctx context.Context, patch *TaskStatusPatch) (*TaskRaw, error)
-}
diff --git a/api/task_check_run.go b/api/task_check_run.go
index 1d9dbd0884..c49da626bd 100644
--- a/api/task_check_run.go
+++ b/api/task_check_run.go
@@ -1,8 +1,6 @@
package api
import (
- "context"
- "database/sql"
"encoding/json"
"github.com/bytebase/bytebase/common"
@@ -110,54 +108,6 @@ 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"`
@@ -237,15 +187,3 @@ type TaskCheckRunStatusPatch struct {
Code common.Code
Result string
}
-
-// TaskCheckRunService is the service for task check runs.
-type TaskCheckRunService interface {
- // For a particular task and a particular check type, we only create a new TaskCheckRun if matches all conditions below:
- // 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) (*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/api/task_run.go b/api/task_run.go
index da19299085..d2ac392fa3 100644
--- a/api/task_run.go
+++ b/api/task_run.go
@@ -1,8 +1,6 @@
package api
import (
- "context"
- "database/sql"
"encoding/json"
"github.com/bytebase/bytebase/common"
@@ -45,56 +43,6 @@ 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"`
@@ -171,11 +119,3 @@ type TaskRunStatusPatch struct {
Comment *string
Result *string
}
-
-// TaskRunService is the service for task runs.
-type TaskRunService interface {
- 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/bin/server/cmd/root.go b/bin/server/cmd/root.go
index 52b01e0ad6..7a4e6dc279 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -465,8 +465,7 @@ func (m *Main) Run(ctx context.Context) error {
s.IssueService = store.NewIssueService(m.l, db, cacheService)
s.PipelineService = store.NewPipelineService(m.l, db, cacheService)
s.StageService = store.NewStageService(m.l, db)
- s.TaskCheckRunService = store.NewTaskCheckRunService(m.l, db)
- s.TaskService = store.NewTaskService(m.l, db, store.NewTaskRunService(m.l, db), s.TaskCheckRunService)
+ s.TaskService = store.NewTaskService(m.l, db, storeInstance)
s.RepositoryService = store.NewRepositoryService(m.l, db, s.ProjectService)
s.LabelService = store.NewLabelService(m.l, db)
s.DeploymentConfigService = store.NewDeploymentConfigService(m.l, db)
diff --git a/server/issue.go b/server/issue.go
index 8e88172acc..c70711039f 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -14,6 +14,7 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/vcs"
+ "github.com/bytebase/bytebase/store"
ghostsql "github.com/github/gh-ost/go/sql"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
@@ -529,7 +530,7 @@ func (s *Server) createPipelineValidateOnly(ctx context.Context, pc *api.Pipelin
// Convert array index to ID.
blockedBy = append(blockedBy, strconv.Itoa(blockedByIndex+idOffset))
}
- taskRaw := &api.TaskRaw{
+ taskRaw := &store.TaskRaw{
ID: id,
Name: tc.Name,
Status: tc.Status,
diff --git a/server/server.go b/server/server.go
index e25ef43166..b22053bf46 100644
--- a/server/server.go
+++ b/server/server.go
@@ -50,8 +50,7 @@ type Server struct {
IssueService api.IssueService
PipelineService api.PipelineService
StageService api.StageService
- TaskService api.TaskService
- TaskCheckRunService api.TaskCheckRunService
+ TaskService store.TaskService
RepositoryService api.RepositoryService
LabelService api.LabelService
DeploymentConfigService api.DeploymentConfigService
diff --git a/server/task.go b/server/task.go
index b379b49729..be44d79afb 100644
--- a/server/task.go
+++ b/server/task.go
@@ -10,6 +10,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/store"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
@@ -191,7 +192,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to marshal statement advise payload: %v, err: %w", task.Name, err))
}
- _, err = s.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ _, err = s.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
CreatorID: api.SystemBotID,
TaskID: task.ID,
Type: api.TaskCheckDatabaseStatementSyntax,
@@ -208,7 +209,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
}
if s.feature(api.FeatureBackwardCompatibility) {
- _, err = s.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ _, err = s.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
CreatorID: api.SystemBotID,
TaskID: task.ID,
Type: api.TaskCheckDatabaseStatementCompatibility,
@@ -267,7 +268,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to marshal statement advise payload: %v, err: %w", task.Name, err))
}
- _, err = s.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ _, err = s.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
CreatorID: api.SystemBotID,
TaskID: task.ID,
Type: api.TaskCheckGeneralEarliestAllowedTime,
@@ -418,7 +419,7 @@ func (s *Server) composeTaskListByPipelineAndStageID(ctx context.Context, pipeli
return taskList, nil
}
-func (s *Server) composeTaskRelationship(ctx context.Context, raw *api.TaskRaw) (*api.Task, error) {
+func (s *Server) composeTaskRelationship(ctx context.Context, raw *store.TaskRaw) (*api.Task, error) {
task := raw.ToTask()
creator, err := s.store.GetPrincipalByID(ctx, task.CreatorID)
diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.go
index 0941cb2c79..8be57a8094 100644
--- a/server/task_check_scheduler.go
+++ b/server/task_check_scheduler.go
@@ -59,16 +59,11 @@ func (s *TaskCheckScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
taskCheckRunFind := &api.TaskCheckRunFind{
StatusList: &taskCheckRunStatusList,
}
- taskCheckRunRawList, err := s.server.TaskCheckRunService.FindTaskCheckRunList(ctx, taskCheckRunFind)
+ taskCheckRunList, err := s.server.store.FindTaskCheckRun(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 {
@@ -117,7 +112,7 @@ func (s *TaskCheckScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
Code: common.Ok,
Result: string(bytes),
}
- _, err = s.server.TaskCheckRunService.PatchTaskCheckRunStatus(ctx, taskCheckRunStatusPatch)
+ _, err = s.server.store.PatchTaskCheckRunStatus(ctx, taskCheckRunStatusPatch)
if err != nil {
s.l.Error("Failed to mark task check run as DONE",
zap.Int("id", taskCheckRun.ID),
@@ -153,7 +148,7 @@ func (s *TaskCheckScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
Code: common.ErrorCode(err),
Result: string(bytes),
}
- _, err = s.server.TaskCheckRunService.PatchTaskCheckRunStatus(ctx, taskCheckRunStatusPatch)
+ _, err = s.server.store.PatchTaskCheckRunStatus(ctx, taskCheckRunStatusPatch)
if err != nil {
s.l.Error("Failed to mark task check run as FAILED",
zap.Int("id", taskCheckRun.ID),
@@ -196,7 +191,7 @@ func (s *TaskCheckScheduler) shouldScheduleTimingTaskCheck(ctx context.Context,
StatusList: &statusList,
Latest: true,
}
- taskCheckRunList, err := s.server.TaskCheckRunService.FindTaskCheckRunList(ctx, taskCheckRunFind)
+ taskCheckRunList, err := s.server.store.FindTaskCheckRun(ctx, taskCheckRunFind)
if err != nil {
return false, err
}
@@ -241,7 +236,7 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
if err != nil {
return nil, err
}
- _, err = s.server.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ _, err = s.server.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
CreatorID: creatorID,
TaskID: task.ID,
Type: api.TaskCheckGeneralEarliestAllowedTime,
@@ -282,7 +277,7 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
return nil, fmt.Errorf("database ID not found %v", task.DatabaseID)
}
- _, err = s.server.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ _, err = s.server.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
CreatorID: creatorID,
TaskID: task.ID,
Type: api.TaskCheckDatabaseConnect,
@@ -292,7 +287,7 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
return nil, err
}
- _, err = s.server.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ _, err = s.server.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
CreatorID: creatorID,
TaskID: task.ID,
Type: api.TaskCheckInstanceMigrationSchema,
@@ -313,7 +308,7 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
if err != nil {
return nil, fmt.Errorf("failed to marshal statement advise payload: %v, err: %w", task.Name, err)
}
- _, err = s.server.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ _, err = s.server.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
CreatorID: creatorID,
TaskID: task.ID,
Type: api.TaskCheckDatabaseStatementSyntax,
@@ -325,7 +320,7 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
}
if s.server.feature(api.FeatureBackwardCompatibility) {
- _, err = s.server.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ _, err = s.server.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
CreatorID: creatorID,
TaskID: task.ID,
Type: api.TaskCheckDatabaseStatementCompatibility,
@@ -341,15 +336,10 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
taskCheckRunFind := &api.TaskCheckRunFind{
TaskID: &task.ID,
}
- taskCheckRunRawList, err := s.server.TaskCheckRunService.FindTaskCheckRunList(ctx, taskCheckRunFind)
+ taskCheckRunList, err := s.server.store.FindTaskCheckRun(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
@@ -368,7 +358,7 @@ func (s *Server) passCheck(ctx context.Context, server *Server, task *api.Task,
Latest: true,
}
- taskCheckRunList, err := server.TaskCheckRunService.FindTaskCheckRunList(ctx, taskCheckRunFind)
+ taskCheckRunList, err := server.store.FindTaskCheckRun(ctx, taskCheckRunFind)
if err != nil {
return false, err
}
diff --git a/store/task.go b/store/task.go
index 827d98f008..2b24acb3f2 100644
--- a/store/task.go
+++ b/store/task.go
@@ -12,26 +12,99 @@ import (
"go.uber.org/zap"
)
+// TaskService is the service for tasks.
+type TaskService interface {
+ CreateTask(ctx context.Context, create *api.TaskCreate) (*TaskRaw, error)
+ FindTaskList(ctx context.Context, find *api.TaskFind) ([]*TaskRaw, error)
+ FindTask(ctx context.Context, find *api.TaskFind) (*TaskRaw, error)
+ PatchTask(ctx context.Context, patch *api.TaskPatch) (*TaskRaw, error)
+ PatchTaskStatus(ctx context.Context, patch *api.TaskStatusPatch) (*TaskRaw, error)
+}
+
+// TaskRaw is the store model for an Task.
+// Fields have exactly the same meanings as Task.
+type TaskRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ PipelineID int
+ StageID int
+ InstanceID int
+ // Could be empty for creating database task when the task isn't yet completed successfully.
+ DatabaseID *int
+ TaskRunRawList []*taskRunRaw
+ TaskCheckRunRawList []*taskCheckRunRaw
+
+ // Domain specific fields
+ Name string
+ Status api.TaskStatus
+ Type api.TaskType
+ Payload string
+ EarliestAllowedTs int64
+ BlockedBy []string
+}
+
+// ToTask creates an instance of Task based on the TaskRaw.
+// This is intended to be called when we need to compose an Task relationship.
+func (raw *TaskRaw) ToTask() *api.Task {
+ task := &api.Task{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ PipelineID: raw.PipelineID,
+ StageID: raw.StageID,
+ InstanceID: raw.InstanceID,
+ // Could be empty for creating database task when the task isn't yet completed successfully.
+ DatabaseID: raw.DatabaseID,
+
+ // Domain specific fields
+ Name: raw.Name,
+ Status: raw.Status,
+ Type: raw.Type,
+ Payload: raw.Payload,
+ EarliestAllowedTs: raw.EarliestAllowedTs,
+ BlockedBy: raw.BlockedBy,
+ }
+ for _, taskRunRaw := range raw.TaskRunRawList {
+ task.TaskRunList = append(task.TaskRunList, taskRunRaw.toTaskRun())
+ }
+ for _, taskCheckRunRaw := range raw.TaskCheckRunRawList {
+ task.TaskCheckRunList = append(task.TaskCheckRunList, taskCheckRunRaw.toTaskCheckRun())
+ }
+ return task
+}
+
var (
- _ api.TaskService = (*TaskService)(nil)
+ _ TaskService = (*TaskServiceImpl)(nil)
)
-// TaskService represents a service for managing task.
-type TaskService struct {
+// TaskServiceImpl represents a service for managing task.
+type TaskServiceImpl struct {
l *zap.Logger
db *DB
- TaskRunService api.TaskRunService
- TaskCheckRunService api.TaskCheckRunService
+ store *Store
}
// NewTaskService returns a new instance of TaskService.
-func NewTaskService(logger *zap.Logger, db *DB, taskRunService api.TaskRunService, taskCheckRunService api.TaskCheckRunService) *TaskService {
- return &TaskService{l: logger, db: db, TaskRunService: taskRunService, TaskCheckRunService: taskCheckRunService}
+func NewTaskService(logger *zap.Logger, db *DB, store *Store) *TaskServiceImpl {
+ return &TaskServiceImpl{l: logger, db: db, store: store}
}
// CreateTask creates a new task.
-func (s *TaskService) CreateTask(ctx context.Context, create *api.TaskCreate) (*api.TaskRaw, error) {
+func (s *TaskServiceImpl) CreateTask(ctx context.Context, create *api.TaskCreate) (*TaskRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -51,7 +124,7 @@ func (s *TaskService) CreateTask(ctx context.Context, create *api.TaskCreate) (*
}
// FindTaskList retrieves a list of tasks based on find.
-func (s *TaskService) FindTaskList(ctx context.Context, find *api.TaskFind) ([]*api.TaskRaw, error) {
+func (s *TaskServiceImpl) FindTaskList(ctx context.Context, find *api.TaskFind) ([]*TaskRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -68,7 +141,7 @@ func (s *TaskService) FindTaskList(ctx context.Context, find *api.TaskFind) ([]*
// FindTask retrieves a single task based on find.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *TaskService) FindTask(ctx context.Context, find *api.TaskFind) (*api.TaskRaw, error) {
+func (s *TaskServiceImpl) FindTask(ctx context.Context, find *api.TaskFind) (*TaskRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -80,7 +153,7 @@ func (s *TaskService) FindTask(ctx context.Context, find *api.TaskFind) (*api.Ta
// PatchTask updates an existing task.
// Returns ENOTFOUND if task does not exist.
-func (s *TaskService) PatchTask(ctx context.Context, patch *api.TaskPatch) (*api.TaskRaw, error) {
+func (s *TaskServiceImpl) PatchTask(ctx context.Context, patch *api.TaskPatch) (*TaskRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -101,7 +174,7 @@ func (s *TaskService) PatchTask(ctx context.Context, patch *api.TaskPatch) (*api
// PatchTaskStatus updates an existing task status and the corresponding task run status atomically.
// Returns ENOTFOUND if task does not exist.
-func (s *TaskService) PatchTaskStatus(ctx context.Context, patch *api.TaskStatusPatch) (*api.TaskRaw, error) {
+func (s *TaskServiceImpl) PatchTaskStatus(ctx context.Context, patch *api.TaskStatusPatch) (*TaskRaw, error) {
// Without using serializable isolation transaction, we will get race condition and have multiple task runs inserted because
// we do a read and write on task, without guanrantee consistency on task runs.
// Once we have multiple task runs, the task will get to unrecoverable state because find task run will fail with two active runs.
@@ -124,7 +197,7 @@ func (s *TaskService) PatchTaskStatus(ctx context.Context, patch *api.TaskStatus
}
// createTask creates a new task.
-func (s *TaskService) createTask(ctx context.Context, tx *sql.Tx, create *api.TaskCreate) (*api.TaskRaw, error) {
+func (s *TaskServiceImpl) createTask(ctx context.Context, tx *sql.Tx, create *api.TaskCreate) (*TaskRaw, error) {
var row *sql.Rows
var err error
@@ -197,7 +270,7 @@ func (s *TaskService) createTask(ctx context.Context, tx *sql.Tx, create *api.Ta
defer row.Close()
row.Next()
- var taskRaw api.TaskRaw
+ var taskRaw TaskRaw
var databaseID sql.NullInt32
if err := row.Scan(
&taskRaw.ID,
@@ -226,7 +299,7 @@ func (s *TaskService) createTask(ctx context.Context, tx *sql.Tx, create *api.Ta
return &taskRaw, nil
}
-func (s *TaskService) findTask(ctx context.Context, tx *sql.Tx, find *api.TaskFind) (*api.TaskRaw, error) {
+func (s *TaskServiceImpl) findTask(ctx context.Context, tx *sql.Tx, find *api.TaskFind) (*TaskRaw, error) {
list, err := s.findTaskList(ctx, tx, find)
if err != nil {
return nil, err
@@ -240,7 +313,7 @@ func (s *TaskService) findTask(ctx context.Context, tx *sql.Tx, find *api.TaskFi
return list[0], nil
}
-func (s *TaskService) findTaskList(ctx context.Context, tx *sql.Tx, find *api.TaskFind) ([]*api.TaskRaw, error) {
+func (s *TaskServiceImpl) findTaskList(ctx context.Context, tx *sql.Tx, find *api.TaskFind) ([]*TaskRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -287,9 +360,9 @@ func (s *TaskService) findTaskList(ctx context.Context, tx *sql.Tx, find *api.Ta
defer rows.Close()
// Iterate over result set and deserialize rows into taskRawList.
- var taskRawList []*api.TaskRaw
+ var taskRawList []*TaskRaw
for rows.Next() {
- var taskRaw api.TaskRaw
+ var taskRaw TaskRaw
if err := rows.Scan(
&taskRaw.ID,
&taskRaw.CreatorID,
@@ -315,7 +388,7 @@ func (s *TaskService) findTaskList(ctx context.Context, tx *sql.Tx, find *api.Ta
taskRunFind := &api.TaskRunFind{
TaskID: &taskRaw.ID,
}
- taskRunRawList, err := s.TaskRunService.FindTaskRunListTx(ctx, tx, taskRunFind)
+ taskRunRawList, err := s.store.findTaskRunImpl(ctx, tx, taskRunFind)
if err != nil {
return nil, err
}
@@ -324,7 +397,7 @@ func (s *TaskService) findTaskList(ctx context.Context, tx *sql.Tx, find *api.Ta
taskCheckRunFind := &api.TaskCheckRunFind{
TaskID: &taskRaw.ID,
}
- taskCheckRunRawList, err := s.TaskCheckRunService.FindTaskCheckRunListTx(ctx, tx, taskCheckRunFind)
+ taskCheckRunRawList, err := s.store.findTaskCheckRunImpl(ctx, tx, taskCheckRunFind)
if err != nil {
return nil, err
}
@@ -337,7 +410,7 @@ func (s *TaskService) findTaskList(ctx context.Context, tx *sql.Tx, find *api.Ta
}
// patchTask updates a task by ID. Returns the new state of the task after update.
-func (s *TaskService) patchTask(ctx context.Context, tx *sql.Tx, patch *api.TaskPatch) (*api.TaskRaw, error) {
+func (s *TaskServiceImpl) patchTask(ctx context.Context, tx *sql.Tx, patch *api.TaskPatch) (*TaskRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.DatabaseID; v != nil {
@@ -370,7 +443,7 @@ func (s *TaskService) patchTask(ctx context.Context, tx *sql.Tx, patch *api.Task
defer row.Close()
if row.Next() {
- var taskRaw api.TaskRaw
+ var taskRaw TaskRaw
if err := row.Scan(
&taskRaw.ID,
&taskRaw.CreatorID,
@@ -397,7 +470,7 @@ func (s *TaskService) patchTask(ctx context.Context, tx *sql.Tx, patch *api.Task
}
// patchTaskStatus updates a task status by ID. Returns the new state of the task after update.
-func (s *TaskService) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *api.TaskStatusPatch) (*api.TaskRaw, error) {
+func (s *TaskServiceImpl) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *api.TaskStatusPatch) (*TaskRaw, error) {
// Updates the corresponding task run if applicable.
// We update the task run first because updating task below returns row and it's a bit complicated to
// arrange code to prevent that opening row interfering with the task run update.
@@ -419,7 +492,7 @@ func (s *TaskService) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *ap
api.TaskRunRunning,
},
}
- taskRunRaw, err := s.TaskRunService.FindTaskRunTx(ctx, tx, taskRunFind)
+ taskRunRaw, err := s.store.getTaskRunRawTx(ctx, tx, taskRunFind)
if err != nil {
return nil, err
}
@@ -434,7 +507,7 @@ func (s *TaskService) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *ap
Type: taskRaw.Type,
Payload: taskRaw.Payload,
}
- if _, err := s.TaskRunService.CreateTaskRunTx(ctx, tx, taskRunCreate); err != nil {
+ if _, err := s.store.createTaskRunImpl(ctx, tx, taskRunCreate); err != nil {
return nil, err
}
} else {
@@ -459,7 +532,7 @@ func (s *TaskService) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *ap
case api.TaskCanceled:
taskRunStatusPatch.Status = api.TaskRunCanceled
}
- if _, err := s.TaskRunService.PatchTaskRunStatusTx(ctx, tx, taskRunStatusPatch); err != nil {
+ if _, err := s.store.patchTaskRunStatusImpl(ctx, tx, taskRunStatusPatch); err != nil {
return nil, err
}
}
@@ -484,9 +557,9 @@ func (s *TaskService) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *ap
return nil, FormatError(err)
}
- var taskPatchedRaw *api.TaskRaw
+ var taskPatchedRaw *TaskRaw
if row.Next() {
- var taskRaw api.TaskRaw
+ var taskRaw TaskRaw
if err := row.Scan(
&taskRaw.ID,
&taskRaw.CreatorID,
@@ -518,7 +591,7 @@ func (s *TaskService) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *ap
taskRunFind := &api.TaskRunFind{
TaskID: &taskRaw.ID,
}
- taskRunRawList, err := s.TaskRunService.FindTaskRunListTx(ctx, tx, taskRunFind)
+ taskRunRawList, err := s.store.findTaskRunImpl(ctx, tx, taskRunFind)
if err != nil {
return nil, err
}
@@ -527,7 +600,7 @@ func (s *TaskService) patchTaskStatus(ctx context.Context, tx *sql.Tx, patch *ap
taskCheckRunFind := &api.TaskCheckRunFind{
TaskID: &taskRaw.ID,
}
- taskCheckRunRawList, err := s.TaskCheckRunService.FindTaskCheckRunListTx(ctx, tx, taskCheckRunFind)
+ taskCheckRunRawList, err := s.store.findTaskCheckRunImpl(ctx, tx, taskCheckRunFind)
if err != nil {
return nil, err
}
diff --git a/store/task_check_run.go b/store/task_check_run.go
index 5b045c61c7..8014b6e35f 100644
--- a/store/task_check_run.go
+++ b/store/task_check_run.go
@@ -11,23 +11,122 @@ import (
"go.uber.org/zap"
)
-var (
- _ api.TaskCheckRunService = (*TaskCheckRunService)(nil)
-)
+// 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 api.TaskCheckRunStatus
+ Type api.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() *api.TaskCheckRun {
+ return &api.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,
+ }
+}
+
+// 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, fmt.Errorf("failed to create TaskCheckRun with TaskCheckRunCreate[%+v], error[%w]", create, err)
+ }
+ taskCheckRun, err := s.composeTaskCheckRun(ctx, taskCheckRunRaw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose TaskCheckRun with taskCheckRunRaw[%+v], error[%w]", taskCheckRunRaw, err)
+ }
+ return taskCheckRun, 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)
+ if err != nil {
+ return nil, fmt.Errorf("failed to find TaskCheckRun list, error[%w]", err)
+ }
+ var taskCheckRunList []*api.TaskCheckRun
+ for _, raw := range taskCheckRunRawList {
+ taskCheckRun, err := s.composeTaskCheckRun(ctx, raw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose TaskCheckRun with taskCheckRunRaw[%+v], error[%w]", raw, err)
+ }
+ taskCheckRunList = append(taskCheckRunList, taskCheckRun)
+ }
+ return taskCheckRunList, nil
+}
-// TaskCheckRunService represents a service for managing taskCheckRun.
-type TaskCheckRunService struct {
- l *zap.Logger
- db *DB
+// 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)
+ if err != nil {
+ return nil, fmt.Errorf("failed to patch TaskCheckRunStatus with TaskCheckRunStatusPatch[%+v], error[%w]", patch, err)
+ }
+ taskCheckRun, err := s.composeTaskCheckRun(ctx, taskCheckRunRaw)
+ if err != nil {
+ return nil, fmt.Errorf("failed to compose TaskCheckRunStatus with taskCheckRunRaw[%+v], error[%w]", taskCheckRunRaw, err)
+ }
+ return taskCheckRun, nil
}
-// NewTaskCheckRunService returns a new TaskCheckRunService.
-func NewTaskCheckRunService(logger *zap.Logger, db *DB) *TaskCheckRunService {
- return &TaskCheckRunService{l: logger, db: db}
+//
+// private functions
+//
+
+// 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)
+ if err != nil {
+ return nil, err
+ }
+ taskCheckRun.Creator = creator
+
+ updater, err := s.GetPrincipalByID(ctx, taskCheckRun.UpdaterID)
+ if err != nil {
+ return nil, err
+ }
+ taskCheckRun.Updater = updater
+
+ return taskCheckRun, nil
}
-// CreateTaskCheckRunIfNeeded creates a new taskCheckRun. See interface for the expected behavior
-func (s *TaskCheckRunService) CreateTaskCheckRunIfNeeded(ctx context.Context, create *api.TaskCheckRunCreate) (*api.TaskCheckRunRaw, error) {
+// createTaskCheckRunRawIfNeeded creates a new taskCheckRun. See interface for the expected behavior
+func (s *Store) createTaskCheckRunRawIfNeeded(ctx context.Context, create *api.TaskCheckRunCreate) (*taskCheckRunRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -46,7 +145,7 @@ func (s *TaskCheckRunService) CreateTaskCheckRunIfNeeded(ctx context.Context, cr
StatusList: &statusList,
}
- taskCheckRunList, err := s.FindTaskCheckRunListTx(ctx, tx.PTx, taskCheckRunFind)
+ taskCheckRunList, err := s.findTaskCheckRunRawTx(ctx, tx.PTx, taskCheckRunFind)
if err != nil {
return nil, err
}
@@ -76,7 +175,7 @@ func (s *TaskCheckRunService) CreateTaskCheckRunIfNeeded(ctx context.Context, cr
return taskCheckRunList[0], nil
}
- taskCheckRun, err := s.createTaskCheckRunTx(ctx, tx.PTx, create)
+ taskCheckRun, err := s.createTaskCheckRunImpl(ctx, tx.PTx, create)
if err != nil {
return nil, err
}
@@ -88,8 +187,8 @@ func (s *TaskCheckRunService) CreateTaskCheckRunIfNeeded(ctx context.Context, cr
return taskCheckRun, nil
}
-// createTaskCheckRunTx creates a new taskCheckRun.
-func (s *TaskCheckRunService) createTaskCheckRunTx(ctx context.Context, tx *sql.Tx, create *api.TaskCheckRunCreate) (*api.TaskCheckRunRaw, error) {
+// createTaskCheckRunImpl creates a new taskCheckRun.
+func (s *Store) createTaskCheckRunImpl(ctx context.Context, tx *sql.Tx, create *api.TaskCheckRunCreate) (*taskCheckRunRaw, error) {
if create.Payload == "" {
create.Payload = "{}"
}
@@ -118,9 +217,9 @@ func (s *TaskCheckRunService) createTaskCheckRunTx(ctx context.Context, tx *sql.
}
defer rows.Close()
- var tRaw *api.TaskCheckRunRaw
+ var tRaw *taskCheckRunRaw
for rows.Next() {
- var taskCheckRunRaw api.TaskCheckRunRaw
+ var taskCheckRunRaw taskCheckRunRaw
if err := rows.Scan(
&taskCheckRunRaw.ID,
&taskCheckRunRaw.CreatorID,
@@ -145,15 +244,15 @@ func (s *TaskCheckRunService) createTaskCheckRunTx(ctx context.Context, tx *sql.
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.TaskCheckRunRaw, error) {
+// 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, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- list, err := s.findTaskCheckRunList(ctx, tx.PTx, find)
+ list, err := s.findTaskCheckRunImpl(ctx, tx.PTx, find)
if err != nil {
return nil, err
}
@@ -161,9 +260,9 @@ func (s *TaskCheckRunService) FindTaskCheckRunList(ctx context.Context, find *ap
return list, nil
}
-// FindTaskCheckRunListTx retrieves a list of taskCheckRuns based on find.
-func (s *TaskCheckRunService) FindTaskCheckRunListTx(ctx context.Context, tx *sql.Tx, find *api.TaskCheckRunFind) ([]*api.TaskCheckRunRaw, error) {
- list, err := s.findTaskCheckRunList(ctx, tx, find)
+// findTaskCheckRunRawTx retrieves a list of taskCheckRuns based on find.
+func (s *Store) findTaskCheckRunRawTx(ctx context.Context, tx *sql.Tx, find *api.TaskCheckRunFind) ([]*taskCheckRunRaw, error) {
+ list, err := s.findTaskCheckRunImpl(ctx, tx, find)
if err != nil {
return nil, err
}
@@ -171,15 +270,15 @@ func (s *TaskCheckRunService) FindTaskCheckRunListTx(ctx context.Context, tx *sq
return list, nil
}
-// 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.TaskCheckRunRaw, error) {
+// 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) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.PTx.Rollback()
- taskCheckRun, err := s.patchTaskCheckRunStatusTx(ctx, tx.PTx, patch)
+ taskCheckRun, err := s.patchTaskCheckRunStatusImpl(ctx, tx.PTx, patch)
if err != nil {
return nil, FormatError(err)
}
@@ -191,8 +290,8 @@ func (s *TaskCheckRunService) PatchTaskCheckRunStatus(ctx context.Context, patch
return taskCheckRun, nil
}
-// 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.TaskCheckRunRaw, error) {
+// patchTaskCheckRunStatusImpl updates a taskCheckRun status. Returns the new state of the taskCheckRun after update.
+func (s *Store) patchTaskCheckRunStatusImpl(ctx context.Context, tx *sql.Tx, patch *api.TaskCheckRunStatusPatch) (*taskCheckRunRaw, error) {
// Build UPDATE clause.
if patch.Result == "" {
patch.Result = "{}"
@@ -221,9 +320,9 @@ func (s *TaskCheckRunService) patchTaskCheckRunStatusTx(ctx context.Context, tx
}
defer rows.Close()
- var tRaw *api.TaskCheckRunRaw
+ var tRaw *taskCheckRunRaw
for rows.Next() {
- var taskCheckRunRaw api.TaskCheckRunRaw
+ var taskCheckRunRaw taskCheckRunRaw
if err := rows.Scan(
&taskCheckRunRaw.ID,
&taskCheckRunRaw.CreatorID,
@@ -248,7 +347,7 @@ func (s *TaskCheckRunService) patchTaskCheckRunStatusTx(ctx context.Context, tx
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.TaskCheckRunRaw, error) {
+func (s *Store) findTaskCheckRunImpl(ctx context.Context, tx *sql.Tx, find *api.TaskCheckRunFind) ([]*taskCheckRunRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -298,9 +397,9 @@ func (s *TaskCheckRunService) findTaskCheckRunList(ctx context.Context, tx *sql.
defer rows.Close()
// Iterate over result set and deserialize rows into taskCheckRunRawList.
- var taskCheckRunRawList []*api.TaskCheckRunRaw
+ var taskCheckRunRawList []*taskCheckRunRaw
for rows.Next() {
- var taskCheckRun api.TaskCheckRunRaw
+ var taskCheckRun taskCheckRunRaw
if err := rows.Scan(
&taskCheckRun.ID,
&taskCheckRun.CreatorID,
diff --git a/store/task_run.go b/store/task_run.go
index 7e8e4ab9f9..356aff6d8b 100644
--- a/store/task_run.go
+++ b/store/task_run.go
@@ -8,26 +8,60 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
- "go.uber.org/zap"
)
-var (
- _ api.TaskRunService = (*TaskRunService)(nil)
-)
+// 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
-// TaskRunService represents a service for managing taskRun.
-type TaskRunService struct {
- l *zap.Logger
- db *DB
+ // Related fields
+ TaskID int
+
+ // Domain specific fields
+ Name string
+ Status api.TaskRunStatus
+ Type api.TaskType
+ Code common.Code
+ Comment string
+ Result string
+ Payload string
}
-// NewTaskRunService returns a new TaskRunService.
-func NewTaskRunService(logger *zap.Logger, db *DB) *TaskRunService {
- return &TaskRunService{l: logger, db: db}
+// 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() *api.TaskRun {
+ return &api.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,
+ }
}
-// CreateTaskRunTx creates a new taskRun.
-func (s *TaskRunService) CreateTaskRunTx(ctx context.Context, tx *sql.Tx, create *api.TaskRunCreate) (*api.TaskRunRaw, error) {
+// createTaskRunImpl creates a new taskRun.
+func (s *Store) createTaskRunImpl(ctx context.Context, tx *sql.Tx, create *api.TaskRunCreate) (*taskRunRaw, error) {
if create.Payload == "" {
create.Payload = "{}"
}
@@ -58,7 +92,7 @@ func (s *TaskRunService) CreateTaskRunTx(ctx context.Context, tx *sql.Tx, create
defer row.Close()
row.Next()
- var taskRunRaw api.TaskRunRaw
+ var taskRunRaw taskRunRaw
if err := row.Scan(
&taskRunRaw.ID,
&taskRunRaw.CreatorID,
@@ -80,20 +114,10 @@ func (s *TaskRunService) CreateTaskRunTx(ctx context.Context, tx *sql.Tx, create
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.TaskRunRaw, error) {
- taskRunRawList, err := s.findTaskRunList(ctx, tx, find)
- if err != nil {
- return nil, err
- }
-
- return taskRunRawList, nil
-}
-
-// FindTaskRunTx retrieves a single taskRun based on find.
+// getTaskRunRawTx 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.TaskRunRaw, error) {
- taskRunRawList, err := s.findTaskRunList(ctx, tx, find)
+func (s *Store) getTaskRunRawTx(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) (*taskRunRaw, error) {
+ taskRunRawList, err := s.findTaskRunImpl(ctx, tx, find)
if err != nil {
return nil, err
}
@@ -105,8 +129,8 @@ func (s *TaskRunService) FindTaskRunTx(ctx context.Context, tx *sql.Tx, find *ap
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.TaskRunRaw, error) {
+// patchTaskRunStatusImpl updates a taskRun status. Returns the new state of the taskRun after update.
+func (s *Store) patchTaskRunStatusImpl(ctx context.Context, tx *sql.Tx, patch *api.TaskRunStatusPatch) (*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,7 +172,7 @@ func (s *TaskRunService) PatchTaskRunStatusTx(ctx context.Context, tx *sql.Tx, p
defer row.Close()
row.Next()
- var taskRunRaw api.TaskRunRaw
+ var taskRunRaw taskRunRaw
if err := row.Scan(
&taskRunRaw.ID,
&taskRunRaw.CreatorID,
@@ -170,7 +194,7 @@ func (s *TaskRunService) PatchTaskRunStatusTx(ctx context.Context, tx *sql.Tx, p
return &taskRunRaw, nil
}
-func (s *TaskRunService) findTaskRunList(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) ([]*api.TaskRunRaw, error) {
+func (s *Store) findTaskRunImpl(ctx context.Context, tx *sql.Tx, find *api.TaskRunFind) ([]*taskRunRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -214,9 +238,9 @@ func (s *TaskRunService) findTaskRunList(ctx context.Context, tx *sql.Tx, find *
defer rows.Close()
// Iterate over result set and deserialize rows into taskRunRawList.
- var taskRunRawList []*api.TaskRunRaw
+ var taskRunRawList []*taskRunRaw
for rows.Next() {
- var taskRunRaw api.TaskRunRaw
+ var taskRunRaw taskRunRaw
if err := rows.Scan(
&taskRunRaw.ID,
&taskRunRaw.CreatorID,