Skip to content

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

refactor: task find and patch (#15183)

Danny Xucd3cfb6150ac
diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.go
index 7280022b67..41e3e5927f 100644
--- a/backend/api/v1/plan_service.go
+++ b/backend/api/v1/plan_service.go
@@ -393,7 +393,7 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePlanRe
}
tasksMap := map[int]*store.TaskMessage{}
- var taskPatchList []*api.TaskPatch
+ var taskPatchList []*store.TaskPatch
var issueCommentCreates []*store.IssueCommentMessage
var taskDAGRebuildList []struct {
fromTaskIDs []int
@@ -401,7 +401,7 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePlanRe
}
if oldPlan.PipelineUID != nil {
- tasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: oldPlan.PipelineUID})
+ tasks, err := s.store.ListTasks(ctx, &store.TaskFind{PipelineID: oldPlan.PipelineUID})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list tasks: %v", err)
}
@@ -417,7 +417,7 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePlanRe
}
for _, task := range tasks {
doUpdate := false
- taskPatch := &api.TaskPatch{
+ taskPatch := &store.TaskPatch{
ID: task.ID,
UpdaterID: user.ID,
}
diff --git a/backend/api/v1/plan_service_plan_check.go b/backend/api/v1/plan_service_plan_check.go
index 99acaa22e4..4d8ba62673 100644
--- a/backend/api/v1/plan_service_plan_check.go
+++ b/backend/api/v1/plan_service_plan_check.go
@@ -16,7 +16,7 @@ import (
func getPlanCheckRunsFromPlan(ctx context.Context, s *store.Store, plan *store.PlanMessage) ([]*store.PlanCheckRunMessage, error) {
var skippedSpecIDs map[string]struct{}
if plan.PipelineUID != nil {
- tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: plan.PipelineUID})
+ tasks, err := s.ListTasks(ctx, &store.TaskFind{PipelineID: plan.PipelineUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get tasks for pipeline %d", *plan.PipelineUID)
}
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index a75df51dcc..d6432c11b0 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -538,7 +538,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, request *v1pb.BatchR
return nil, status.Errorf(codes.Internal, "failed to find the stage to run")
}
- stageToRunTasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: &rolloutID, StageID: &stageToRun.ID})
+ stageToRunTasks, err := s.store.ListTasks(ctx, &store.TaskFind{PipelineID: &rolloutID, StageID: &stageToRun.ID})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list tasks, error: %v", err)
}
@@ -655,7 +655,7 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, request *v1pb.Batch
return nil, status.Errorf(codes.Internal, "failed to find issue, error: %v", err)
}
- tasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: &rolloutID})
+ tasks, err := s.store.ListTasks(ctx, &store.TaskFind{PipelineID: &rolloutID})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list tasks, error: %v", err)
}
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 28a1b2312a..4756b4171b 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -642,7 +642,7 @@ func (s *SQLService) doExportFromIssue(ctx context.Context, issueName string) (*
if rollout == nil {
return nil, status.Errorf(codes.NotFound, "rollout %d not found", *issue.PipelineUID)
}
- tasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: &rollout.ID})
+ tasks, err := s.store.ListTasks(ctx, &store.TaskFind{PipelineID: &rollout.ID})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get tasks: %v", err)
}
diff --git a/backend/legacyapi/task.go b/backend/legacyapi/task.go
index 40ad12efb9..b0a79f7330 100644
--- a/backend/legacyapi/task.go
+++ b/backend/legacyapi/task.go
@@ -2,11 +2,8 @@ package api
import (
"encoding/json"
- "time"
"github.com/pkg/errors"
-
- storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
// TaskStatus is the status of a task.
@@ -100,62 +97,6 @@ type Progress struct {
Payload string `json:"payload"`
}
-// TaskFind is the API message for finding tasks.
-type TaskFind struct {
- ID *int
- IDs *[]int
-
- // Related fields
- PipelineID *int
- StageID *int
- InstanceID *string
- DatabaseName *string
-
- // Domain specific fields
- TypeList *[]TaskType
- // Payload contains JSONB expressions
- // Ref: https://www.postgresql.org/docs/current/functions-json.html
- Payload string
- NoBlockingStage bool
- NonRollbackTask bool
-
- LatestTaskRunStatusList *[]TaskRunStatus
-}
-
-func (find *TaskFind) String() string {
- str, err := json.Marshal(*find)
- if err != nil {
- return err.Error()
- }
- return string(str)
-}
-
-// TaskPatch is the API message for patching a task.
-type TaskPatch struct {
- ID int
-
- // Standard fields
- // Value is assigned from the jwt subject field passed by the client.
- UpdaterID int
-
- // Domain specific fields
- DatabaseName *string
- EarliestAllowedTs *time.Time
- UpdateEarliestAllowedTs bool
-
- // Payload and others cannot be set at the same time.
- Payload *string
-
- SheetID *int
- SchemaVersion *string
- ExportFormat *storepb.ExportFormat
- ExportPassword *string
- PreUpdateBackupDetail *storepb.PreUpdateBackupDetail
-
- // Flags for gh-ost.
- Flags *map[string]string
-}
-
func GetSheetUIDFromTaskPayload(payload string) (*int, error) {
var taskPayload struct {
SheetID int `json:"sheetId"`
diff --git a/backend/migrator/migration/3.4/0007##less_id.sql b/backend/migrator/migration/3.4/0007##less_id.sql
index 5da6f8a8f7..d02a186164 100644
--- a/backend/migrator/migration/3.4/0007##less_id.sql
+++ b/backend/migrator/migration/3.4/0007##less_id.sql
@@ -98,7 +98,7 @@ ALTER TABLE vcs_connector ALTER COLUMN project SET NOT NULL;
ALTER TABLE vcs_connector ADD constraint vcs_connector_project_fkey FOREIGN KEY (project) references project(resource_id);
CREATE UNIQUE INDEX idx_vcs_connector_unique_project_resource_id ON vcs_connector(project, resource_id);
-DELETE FROM anomaly WHERE database_id IS NULL;
+DELETE FROM anomaly;
DROP INDEX IF EXISTS idx_anomaly_unique_project_database_id_type;
ALTER TABLE anomaly ADD COLUMN instance TEXT;
ALTER TABLE anomaly ADD COLUMN db_name TEXT;
diff --git a/backend/runner/taskrun/database_create_executor.go b/backend/runner/taskrun/database_create_executor.go
index 34c50d7474..103da48b10 100644
--- a/backend/runner/taskrun/database_create_executor.go
+++ b/backend/runner/taskrun/database_create_executor.go
@@ -178,7 +178,7 @@ func (exec *DatabaseCreateExecutor) RunOnce(ctx context.Context, driverCtx conte
// The task database_id represents its related database entry both for creating and patching,
// so we should sync its value right here when the related database entry created.
// The new statement should include the schema from peer tenant database.
- taskDatabaseIDPatch := &api.TaskPatch{
+ taskDatabaseIDPatch := &store.TaskPatch{
ID: task.ID,
UpdaterID: api.SystemBotID,
DatabaseName: &database.DatabaseName,
@@ -284,7 +284,7 @@ func (exec *DatabaseCreateExecutor) reconcilePlan(ctx context.Context, project *
}
// We somehow reconciled the plan before, so we just return.
- tasks, err := exec.store.ListTasks(ctx, &api.TaskFind{
+ tasks, err := exec.store.ListTasks(ctx, &store.TaskFind{
PipelineID: issue.PipelineUID,
InstanceID: &createdDatabase.InstanceID,
DatabaseName: &createdDatabase.DatabaseName,
@@ -296,7 +296,7 @@ func (exec *DatabaseCreateExecutor) reconcilePlan(ctx context.Context, project *
return nil
}
- tasks, err = exec.store.ListTasks(ctx, &api.TaskFind{
+ tasks, err = exec.store.ListTasks(ctx, &store.TaskFind{
PipelineID: issue.PipelineUID,
InstanceID: &peerDatabase.InstanceID,
DatabaseName: &peerDatabase.DatabaseName,
diff --git a/backend/runner/taskrun/schedulerv2.go b/backend/runner/taskrun/schedulerv2.go
index 7a851a2a3b..063c446244 100644
--- a/backend/runner/taskrun/schedulerv2.go
+++ b/backend/runner/taskrun/schedulerv2.go
@@ -727,7 +727,7 @@ func (s *SchedulerV2) ListenTaskSkippedOrDone(ctx context.Context) {
return nil
}
- stageTasks, err := s.store.ListTasks(ctx, &api.TaskFind{StageID: &task.StageID})
+ stageTasks, err := s.store.ListTasks(ctx, &store.TaskFind{StageID: &task.StageID})
if err != nil {
return errors.Wrapf(err, "failed to list tasks")
}
diff --git a/backend/store/rollout.go b/backend/store/rollout.go
index a2654bcd49..7db1f0fb9d 100644
--- a/backend/store/rollout.go
+++ b/backend/store/rollout.go
@@ -4,13 +4,11 @@ import (
"context"
"github.com/pkg/errors"
-
- api "github.com/bytebase/bytebase/backend/legacyapi"
)
// GetRollout gets the rollout by rollout ID.
func (s *Store) GetRollout(ctx context.Context, rolloutID int) (*PipelineMessage, error) {
- tasks, err := s.ListTasks(ctx, &api.TaskFind{PipelineID: &rolloutID})
+ tasks, err := s.ListTasks(ctx, &TaskFind{PipelineID: &rolloutID})
if err != nil {
return nil, errors.Wrapf(err, "failed to find tasks for pipeline %d", rolloutID)
}
diff --git a/backend/store/task.go b/backend/store/task.go
index 5530fbabb3..d826db5d32 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -15,6 +15,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
api "github.com/bytebase/bytebase/backend/legacyapi"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
// TaskMessage is the message for tasks.
@@ -38,9 +39,57 @@ type TaskMessage struct {
LatestTaskRunStatus api.TaskRunStatus
}
+// TaskFind is the API message for finding tasks.
+type TaskFind struct {
+ ID *int
+ IDs *[]int
+
+ // Related fields
+ PipelineID *int
+ StageID *int
+ InstanceID *string
+ DatabaseName *string
+
+ // Domain specific fields
+ TypeList *[]api.TaskType
+ // Payload contains JSONB expressions
+ // Ref: https://www.postgresql.org/docs/current/functions-json.html
+ Payload string
+ NoBlockingStage bool
+ NonRollbackTask bool
+
+ LatestTaskRunStatusList *[]api.TaskRunStatus
+}
+
+// TaskPatch is the API message for patching a task.
+type TaskPatch struct {
+ ID int
+
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ UpdaterID int
+
+ // Domain specific fields
+ DatabaseName *string
+ EarliestAllowedTs *time.Time
+ UpdateEarliestAllowedTs bool
+
+ // Payload and others cannot be set at the same time.
+ Payload *string
+
+ SheetID *int
+ SchemaVersion *string
+ ExportFormat *storepb.ExportFormat
+ ExportPassword *string
+ PreUpdateBackupDetail *storepb.PreUpdateBackupDetail
+
+ // Flags for gh-ost.
+ Flags *map[string]string
+}
+
// GetTaskV2ByID gets a task by ID.
func (s *Store) GetTaskV2ByID(ctx context.Context, id int) (*TaskMessage, error) {
- tasks, err := s.ListTasks(ctx, &api.TaskFind{ID: &id})
+ tasks, err := s.ListTasks(ctx, &TaskFind{ID: &id})
if err != nil {
return nil, errors.Wrapf(err, "failed to get Task with ID %d", id)
}
@@ -194,7 +243,7 @@ func (s *Store) CreateTasksV2(ctx context.Context, creates ...*TaskMessage) ([]*
}
// ListTasks retrieves a list of tasks based on find.
-func (s *Store) ListTasks(ctx context.Context, find *api.TaskFind) ([]*TaskMessage, error) {
+func (s *Store) ListTasks(ctx context.Context, find *TaskFind) ([]*TaskMessage, error) {
where, args := []string{"TRUE"}, []any{}
if v := find.ID; v != nil {
where, args = append(where, fmt.Sprintf("task.id = $%d", len(args)+1)), append(args, *v)
@@ -316,7 +365,7 @@ func (s *Store) ListTasks(ctx context.Context, find *api.TaskFind) ([]*TaskMessa
// UpdateTaskV2 updates an existing task.
// Returns ENOTFOUND if task does not exist.
-func (s *Store) UpdateTaskV2(ctx context.Context, patch *api.TaskPatch) (*TaskMessage, error) {
+func (s *Store) UpdateTaskV2(ctx context.Context, patch *TaskPatch) (*TaskMessage, error) {
set, args := []string{}, []any{}
if v := patch.DatabaseName; v != nil {
set, args = append(set, fmt.Sprintf("db_name = $%d", len(args)+1)), append(args, *v)