Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: task run v1 store (#4428)
Danny Xua76007dbc671
diff --git a/backend/legacyapi/task_run.go b/backend/legacyapi/task_run.goindex 7d15806054..87f7b334fd 100644--- a/backend/legacyapi/task_run.go+++ b/backend/legacyapi/task_run.go@@ -1,8 +1,6 @@package apiimport (- "encoding/json"-"github.com/bytebase/bytebase/backend/common")@@ -53,55 +51,3 @@ type TaskRun struct {Result string `jsonapi:"attr,result"`Payload string `jsonapi:"attr,payload"`}--// TaskRunCreate is the API message for creating a task run.-type TaskRunCreate struct {- // Standard fields- // Value is assigned from the jwt subject field passed by the client.- CreatorID int-- // Related fields- TaskID int-- // Domain specific fields- Name string `jsonapi:"attr,name"`- Type TaskType `jsonapi:"attr,type"`- Payload string `jsonapi:"attr,payload"`-}--// TaskRunFind is the API message for finding task runs.-type TaskRunFind struct {- ID *int-- // Related fields- TaskID *int-- // Domain specific fields- StatusList *[]TaskRunStatus-}--func (find *TaskRunFind) String() string {- str, err := json.Marshal(*find)- if err != nil {- return err.Error()- }- return string(str)-}--// TaskRunStatusPatch is the API message for patching a task run.-type TaskRunStatusPatch struct {- ID *int-- // Standard fields- UpdaterID int-- // Related fields- TaskID *int-- // Domain specific fields- Status TaskRunStatus- Code *common.Code- // Records the status detail (e.g. error message on failure)- Comment *string- Result *string-}diff --git a/backend/store/task.go b/backend/store/task.goindex 77e5d66d1c..181c8c3e59 100644--- a/backend/store/task.go+++ b/backend/store/task.go@@ -30,7 +30,7 @@ type TaskMessage struct {InstanceID int// Could be empty for creating database task when the task isn't yet completed successfully.DatabaseID *int- TaskRunRawList []*taskRunRaw+ TaskRunRawList []*TaskRunMessageTaskCheckRunRawList []*taskCheckRunRaw// Domain specific fields@@ -162,7 +162,7 @@ func (s *Store) composeTask(ctx context.Context, raw *TaskMessage) (*api.Task, e}task.Updater = updater- taskRunRawList, err := s.listTaskRun(ctx, &api.TaskRunFind{+ taskRunRawList, err := s.listTaskRun(ctx, &TaskRunFind{TaskID: &task.ID,})if err != nil {@@ -543,26 +543,23 @@ func (s *Store) UpdateTaskStatusV2(ctx context.Context, patch *api.TaskStatusPat}defer tx.Rollback()- taskRunFind := &api.TaskRunFind{+ taskRunFind := &TaskRunFind{TaskID: &task.ID,StatusList: &[]api.TaskRunStatus{api.TaskRunRunning,},}- taskRunRaw, err := s.getTaskRunRawTx(ctx, tx, taskRunFind)+ taskRun, err := s.getTaskRunTx(ctx, tx, taskRunFind)if err != nil {return nil, err}- if taskRunRaw == nil {+ if taskRun == nil {if patch.Status == api.TaskRunning {- taskRunCreate := &api.TaskRunCreate{- CreatorID: patch.UpdaterID,- TaskID: task.ID,- Name: fmt.Sprintf("%s %d", task.Name, time.Now().Unix()),- Type: task.Type,- }- // insert a running taskRun- if _, err := s.createTaskRunImpl(ctx, tx, taskRunCreate); err != nil {+ if err := s.createTaskRunImpl(ctx, tx, &TaskRunMessage{+ TaskID: task.ID,+ Name: fmt.Sprintf("%s %d", task.Name, time.Now().Unix()),+ Type: task.Type,+ }, patch.UpdaterID); err != nil {return nil, err}}@@ -570,8 +567,8 @@ func (s *Store) UpdateTaskStatusV2(ctx context.Context, patch *api.TaskStatusPatif patch.Status == api.TaskRunning {return nil, errors.Errorf("task is already running: %v", task.Name)}- taskRunStatusPatch := &api.TaskRunStatusPatch{- ID: &taskRunRaw.ID,+ taskRunStatusPatch := &TaskRunStatusPatch{+ ID: &taskRun.ID,UpdaterID: patch.UpdaterID,Code: patch.Code,Result: patch.Result,diff --git a/backend/store/task_run.go b/backend/store/task_run.goindex ba67e5b5e7..1b907c0eab 100644--- a/backend/store/task_run.go+++ b/backend/store/task_run.go@@ -12,58 +12,81 @@ import (api "github.com/bytebase/bytebase/backend/legacyapi")-// taskRunRaw is the store model for an TaskRun.-// Fields have exactly the same meanings as TaskRun.-type taskRunRaw struct {- ID int+// TaskRunMessage is message for task run.+type TaskRunMessage struct {+ TaskID int+ Name string+ Status api.TaskRunStatus+ Type api.TaskType+ Code common.Code+ Comment string+ Result string+ Payload string- // Standard fields+ // Output only.+ ID intCreatorID intCreatedTs int64UpdaterID intUpdatedTs int64+}+// TaskRunCreate is the API message for creating a task run.+type TaskRunCreate struct {+ CreatorID int+ TaskID int+ Name string+ Type api.TaskType+ Payload string+}++// TaskRunFind is the API message for finding task runs.+type TaskRunFind struct {// Related fields- TaskID int+ TaskID *int// Domain specific fields- Name string- Status api.TaskRunStatus- Type api.TaskType- Code common.Code- Comment string- Result string- Payload string+ StatusList *[]api.TaskRunStatus}-// 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,+// TaskRunStatusPatch is the API message for patching a task run.+type TaskRunStatusPatch struct {+ ID *int- // Standard fields- CreatorID: raw.CreatorID,- CreatedTs: raw.CreatedTs,- UpdaterID: raw.UpdaterID,- UpdatedTs: raw.UpdatedTs,+ // Standard fields+ UpdaterID int- // Related fields- TaskID: raw.TaskID,+ // Related fields+ TaskID *int- // Domain specific fields- Name: raw.Name,- Status: raw.Status,- Type: raw.Type,- Code: raw.Code,- Comment: raw.Comment,- Result: raw.Result,- Payload: raw.Payload,+ // Domain specific fields+ Status api.TaskRunStatus+ Code *common.Code+ // Records the status detail (e.g. error message on failure)+ Comment *string+ Result *string+}++func (taskRun *TaskRunMessage) toTaskRun() *api.TaskRun {+ return &api.TaskRun{+ ID: taskRun.ID,+ CreatorID: taskRun.CreatorID,+ CreatedTs: taskRun.CreatedTs,+ UpdaterID: taskRun.UpdaterID,+ UpdatedTs: taskRun.UpdatedTs,+ TaskID: taskRun.TaskID,+ Name: taskRun.Name,+ Status: taskRun.Status,+ Type: taskRun.Type,+ Code: taskRun.Code,+ Comment: taskRun.Comment,+ Result: taskRun.Result,+ Payload: taskRun.Payload,}}// createTaskRunImpl creates a new taskRun.-func (*Store) createTaskRunImpl(ctx context.Context, tx *Tx, create *api.TaskRunCreate) (*taskRunRaw, error) {+func (*Store) createTaskRunImpl(ctx context.Context, tx *Tx, create *TaskRunMessage, creatorID int) error {if create.Payload == "" {create.Payload = "{}"}@@ -76,59 +99,37 @@ func (*Store) createTaskRunImpl(ctx context.Context, tx *Tx, create *api.TaskRunstatus,type,payload- )- VALUES ($1, $2, $3, $4, 'RUNNING', $5, $6)- RETURNING id, creator_id, created_ts, updater_id, updated_ts, task_id, name, status, type, code, comment, result, payload+ ) VALUES ($1, $2, $3, $4, $5, $6, $7)`- var taskRunRaw taskRunRaw- if err := tx.QueryRowContext(ctx, query,- create.CreatorID,- create.CreatorID,+ if _, err := tx.ExecContext(ctx, query,+ creatorID,+ creatorID,create.TaskID,create.Name,+ api.TaskRunRunning,create.Type,- create.Payload,- ).Scan(- &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,+ "{}", /* payload */); err != nil {- if err == sql.ErrNoRows {- return nil, common.FormatDBErrorEmptyRowWithQuery(query)- }- return nil, FormatError(err)+ return err}- return &taskRunRaw, nil+ return nil}-// getTaskRunRawTx retrieves a single taskRun based on find.-// Returns ECONFLICT if finding more than 1 matching records.-func (s *Store) getTaskRunRawTx(ctx context.Context, tx *Tx, find *api.TaskRunFind) (*taskRunRaw, error) {- taskRunRawList, err := s.findTaskRunImpl(ctx, tx, find)+func (s *Store) getTaskRunTx(ctx context.Context, tx *Tx, find *TaskRunFind) (*TaskRunMessage, error) {+ taskRuns, err := s.findTaskRunImpl(ctx, tx, find)if err != nil {return nil, err}- if len(taskRunRawList) == 0 {+ if len(taskRuns) == 0 {return nil, nil- } else if len(taskRunRawList) > 1 {- return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d task runs with filter %+v, expect 1", len(taskRunRawList), find)}+ } else if len(taskRuns) > 1 {+ return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d task runs with filter %+v, expect 1", len(taskRuns), find)}}- return taskRunRawList[0], nil+ return taskRuns[0], nil}// patchTaskRunStatusImpl updates a taskRun status. Returns the new state of the taskRun after update.-func (*Store) patchTaskRunStatusImpl(ctx context.Context, tx *Tx, patch *api.TaskRunStatusPatch) (*taskRunRaw, error) {- // Build UPDATE clause.+func (*Store) patchTaskRunStatusImpl(ctx context.Context, tx *Tx, patch *TaskRunStatusPatch) (*TaskRunMessage, error) {set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}set, args = append(set, "status = $2"), append(args, patch.Status)if v := patch.Code; v != nil {@@ -154,7 +155,7 @@ func (*Store) patchTaskRunStatusImpl(ctx context.Context, tx *Tx, patch *api.Taswhere, args = append(where, fmt.Sprintf("task_id = $%d", len(args)+1)), append(args, *v)}- var taskRunRaw taskRunRaw+ var taskRun TaskRunMessageif err := tx.QueryRowContext(ctx, `UPDATE task_runSET `+strings.Join(set, ", ")+`@@ -163,29 +164,29 @@ func (*Store) patchTaskRunStatusImpl(ctx context.Context, tx *Tx, patch *api.Tas`,args...,).Scan(- &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,+ &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,); err != nil {if err == sql.ErrNoRows {return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("project ID not found: %d", patch.ID)}}return nil, FormatError(err)}- return &taskRunRaw, nil+ return &taskRun, nil}-func (s *Store) listTaskRun(ctx context.Context, find *api.TaskRunFind) ([]*taskRunRaw, error) {+func (s *Store) listTaskRun(ctx context.Context, find *TaskRunFind) ([]*TaskRunMessage, error) {tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})if err != nil {return nil, FormatError(err)@@ -204,12 +205,8 @@ func (s *Store) listTaskRun(ctx context.Context, find *api.TaskRunFind) ([]*taskreturn list, nil}-func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *api.TaskRunFind) ([]*taskRunRaw, error) {- // Build WHERE clause.+func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *TaskRunFind) ([]*TaskRunMessage, error) {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)}@@ -247,33 +244,32 @@ func (*Store) findTaskRunImpl(ctx context.Context, tx *Tx, find *api.TaskRunFind}defer rows.Close()- // Iterate over result set and deserialize rows into taskRunRawList.- var taskRunRawList []*taskRunRaw+ var taskRuns []*TaskRunMessagefor rows.Next() {- var taskRunRaw taskRunRaw+ var taskRun TaskRunMessageif err := rows.Scan(- &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,+ &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,); err != nil {return nil, FormatError(err)}- taskRunRawList = append(taskRunRawList, &taskRunRaw)+ taskRuns = append(taskRuns, &taskRun)}if err := rows.Err(); err != nil {return nil, FormatError(err)}- return taskRunRawList, nil+ return taskRuns, nil}diff --git a/backend/tests/start_test.go b/backend/tests/start_test.goindex cf378601ca..589731e164 100644--- a/backend/tests/start_test.go+++ b/backend/tests/start_test.go@@ -93,7 +93,7 @@ func TestMain(m *testing.M) {if err := postgres.InitDB(externalPgBinDir, externalPgDataDir, externalPgUser); err != nil {log.Fatal(err)}- if err = postgres.Start(externalPgPort, externalPgBinDir, externalPgDataDir, false /* serverLog */); err != nil {+ if err = postgres.Start(externalPgPort, externalPgBinDir, externalPgDataDir, true /* serverLog */); err != nil {log.Fatal(err)}