Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: add composite PKs for plan and task chains (#19607)
ecmadaoad55f17403b8
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.goindex 7ff9c3ff29..60ad9dfb3f 100644--- a/backend/api/v1/issue_service.go+++ b/backend/api/v1/issue_service.go@@ -486,7 +486,7 @@ func (s *IssueService) buildIssueMessage(ctx context.Context, project *store.Proreturn nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("%v", err.Error()))}- plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{UID: &planID, ProjectID: &project.ResourceID})+ plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{UID: &planID, ProjectID: project.ResourceID})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get plan"))}@@ -636,7 +636,7 @@ func (s *IssueService) ApproveIssue(ctx context.Context, req *connect.Request[v1// Auto-create rollout if this approval completes the approval flowif issueV1.ApprovalStatus == v1pb.Issue_APPROVED {if issue.PlanUID != nil {- s.bus.RolloutCreationChan <- *issue.PlanUID+ s.bus.RolloutCreationChan <- bus.PlanRef{ProjectID: issue.ProjectID, PlanID: *issue.PlanUID}}}diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.goindex a998af3972..b3ec75b0aa 100644--- a/backend/api/v1/plan_service.go+++ b/backend/api/v1/plan_service.go@@ -56,7 +56,7 @@ func (s *PlanService) GetPlan(ctx context.Context, request *connect.Request[v1pb}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{UID: &planID,- ProjectID: &projectID,+ ProjectID: projectID,})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get plan"))@@ -92,7 +92,7 @@ func (s *PlanService) ListPlans(ctx context.Context, request *connect.Request[v1find := &store.FindPlanMessage{Limit: &limitPlusOne,Offset: &offset.offset,- ProjectID: &projectID,+ ProjectID: projectID,}if req.Filter != "" {@@ -236,7 +236,7 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[vif project == nil {return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project %q not found", projectID))}- oldPlan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: &projectID, UID: &planID})+ oldPlan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: projectID, UID: &planID})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to get plan %q: %v", req.Plan.Name, err))}@@ -280,7 +280,8 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[v}planUpdate := &store.UpdatePlanMessage{- UID: oldPlan.UID,+ UID: oldPlan.UID,+ ProjectID: oldPlan.ProjectID,}var planCheckRunsTrigger bool@@ -390,7 +391,7 @@ func (s *PlanService) GetPlanCheckRun(ctx context.Context, request *connect.Requreturn nil, connect.NewError(connect.CodeInvalidArgument, err)}- planCheckRun, err := s.store.GetPlanCheckRun(ctx, planUID)+ planCheckRun, err := s.store.GetPlanCheckRun(ctx, projectID, planUID)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get plan check run"))}@@ -420,7 +421,7 @@ func (s *PlanService) RunPlanChecks(ctx context.Context, request *connect.Reques}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{UID: &planID,- ProjectID: &projectID,+ ProjectID: projectID,})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get plan"))@@ -483,7 +484,7 @@ func (s *PlanService) CancelPlanCheckRun(ctx context.Context, request *connect.Rreturn nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project %v not found", projectID))}- planCheckRun, err := s.store.GetPlanCheckRun(ctx, planUID)+ planCheckRun, err := s.store.GetPlanCheckRun(ctx, projectID, planUID)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get plan check run"))}@@ -496,17 +497,17 @@ func (s *PlanService) CancelPlanCheckRun(ctx context.Context, request *connect.R}// Cancel in-flight plan check run if running.- if cancelFunc, ok := s.bus.RunningPlanCheckRunsCancelFunc.Load(planCheckRun.UID); ok {+ if cancelFunc, ok := s.bus.RunningPlanCheckRunsCancelFunc.Load(bus.PlanCheckRunRef{ProjectID: projectID, UID: planCheckRun.UID}); ok {cancelFunc.(context.CancelFunc)()}// Broadcast cancel signal to all replicas for HA.- if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_PLAN_CHECK_RUN, int32(planCheckRun.UID)); err != nil {+ if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_PLAN_CHECK_RUN, projectID, int32(planCheckRun.UID)); err != nil {slog.Warn("failed to send cancel signal", log.BBError(err))}// Update the status to canceled.- if err := s.store.BatchCancelPlanCheckRuns(ctx, []int{planCheckRun.UID}); err != nil {+ if err := s.store.BatchCancelPlanCheckRuns(ctx, projectID, []int{planCheckRun.UID}); err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to cancel plan check run"))}@@ -747,8 +748,9 @@ func getPlanCheckRunFromPlan(ctx context.Context, s *store.Store, project *store}return &store.PlanCheckRunMessage{- PlanUID: plan.UID,- Status: store.PlanCheckRunStatusRunning,+ ProjectID: plan.ProjectID,+ PlanUID: plan.UID,+ Status: store.PlanCheckRunStatusRunning,}, nil}@@ -774,7 +776,7 @@ func convertToPlans(ctx context.Context, s *store.Store, plans []*store.PlanMess}}- planCheckRuns, err := s.ListPlanCheckRuns(ctx, &store.FindPlanCheckRunMessage{PlanUIDs: &planUIDs})+ planCheckRuns, err := s.ListPlanCheckRuns(ctx, &store.FindPlanCheckRunMessage{ProjectID: plans[0].ProjectID, PlanUIDs: &planUIDs})if err != nil {return nil, errors.Wrap(err, "failed to batch list plan check runs")}@@ -796,7 +798,7 @@ func convertToPlan(ctx context.Context, s *store.Store, plan *store.PlanMessage)if err != nil {return nil, errors.Wrapf(err, "failed to get issue by plan uid %d", plan.UID)}- planCheckRun, err := s.GetPlanCheckRun(ctx, int64(plan.UID))+ planCheckRun, err := s.GetPlanCheckRun(ctx, plan.ProjectID, int64(plan.UID))if err != nil {return nil, errors.Wrapf(err, "failed to get plan check run for plan uid %d", plan.UID)}diff --git a/backend/api/v1/revision_service.go b/backend/api/v1/revision_service.goindex d950563d87..df4561e9c1 100644--- a/backend/api/v1/revision_service.go+++ b/backend/api/v1/revision_service.go@@ -199,7 +199,7 @@ func (s *RevisionService) createRevisions(if err != nil {return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("failed to get taskRun from %q", revision.TaskRun))}- taskRun, err := s.store.GetTaskRunByUID(ctx, taskRunID)+ taskRun, err := s.store.GetTaskRunByUID(ctx, projectID, taskRunID)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get taskRun"))}diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex ea40250a33..5e1522b965 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -71,7 +71,7 @@ func (s *RolloutService) GetRollout(ctx context.Context, req *connect.Request[v1// getRolloutWithTasks inlinedplan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -85,7 +85,7 @@ func (s *RolloutService) GetRollout(ctx context.Context, req *connect.Request[v1return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("rollout %d not found in project %s", planID, projectID))}- tasks, err := s.store.ListTasks(ctx, &store.TaskFind{PlanID: &planID})+ tasks, err := s.store.ListTasks(ctx, &store.TaskFind{ProjectID: projectID, PlanID: &planID})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get tasks"))}@@ -137,7 +137,7 @@ func (s *RolloutService) ListRollouts(ctx context.Context, req *connect.Request[// Filter plans to only those with rollouts (tasks).hasRollout := truefindPlan := &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,Limit: &limitPlusOne,Offset: &offset.offset,HasRollout: &hasRollout,@@ -166,7 +166,7 @@ func (s *RolloutService) ListRollouts(ctx context.Context, req *connect.Request[var allTasks []*store.TaskMessageif len(planIDs) > 0 {var err error- allTasks, err = s.store.ListTasks(ctx, &store.TaskFind{PlanIDs: &planIDs})+ allTasks, err = s.store.ListTasks(ctx, &store.TaskFind{ProjectID: projectID, PlanIDs: &planIDs})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list tasks"))}@@ -237,7 +237,7 @@ func (s *RolloutService) CreateRollout(ctx context.Context, req *connect.Requestreturn nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project not found for id: %v", projectID))}- plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{UID: &planID, ProjectID: &projectID})+ plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: projectID, UID: &planID})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get plan"))}@@ -287,7 +287,7 @@ func (s *RolloutService) CreateRollout(ctx context.Context, req *connect.Requestreturn nil, connect.NewError(connect.CodeInternal, err)}- tasks, err = s.store.ListTasks(ctx, &store.TaskFind{PlanID: &planID})+ tasks, err = s.store.ListTasks(ctx, &store.TaskFind{ProjectID: projectID, PlanID: &planID})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get tasks"))}@@ -332,7 +332,7 @@ func (s *RolloutService) ListTaskRuns(ctx context.Context, req *connect.Request[}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -342,6 +342,7 @@ func (s *RolloutService) ListTaskRuns(ctx context.Context, req *connect.Request[return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("rollout %d not found in project %s", planID, projectID))}taskRuns, err := s.store.ListTaskRuns(ctx, &store.FindTaskRunMessage{+ ProjectID: projectID,PlanUID: &planID,Environment: maybeStageID,TaskUID: maybeTaskID,@@ -378,7 +379,7 @@ func CreateRolloutAndPendingTasks(}// Create rollout tasks- tasks, err = s.CreateTasks(ctx, plan.UID, tasks)+ tasks, err = s.CreateTasks(ctx, project.ResourceID, plan.UID, tasks)if err != nil {return errors.Wrap(err, "failed to create rollout tasks")}@@ -387,8 +388,9 @@ func CreateRolloutAndPendingTasks(config := proto.CloneOf(plan.Config)config.HasRollout = true_, err = s.UpdatePlan(ctx, &store.UpdatePlanMessage{- UID: plan.UID,- Config: config,+ UID: plan.UID,+ ProjectID: project.ResourceID,+ Config: config,})if err != nil {return errors.Wrap(err, "failed to update plan hasRollout")@@ -453,7 +455,8 @@ func CreateRolloutAndPendingTasks(if policy.Automatic {create := &store.TaskRunMessage{- TaskUID: task.ID,+ TaskUID: task.ID,+ ProjectID: task.ProjectID,}// System-generated task run for auto-rollout@@ -474,7 +477,7 @@ func (s *RolloutService) GetTaskRun(ctx context.Context, req *connect.Request[v1}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -485,8 +488,9 @@ func (s *RolloutService) GetTaskRun(ctx context.Context, req *connect.Request[v1}taskRun, err := s.store.GetTaskRunV1(ctx, &store.FindTaskRunMessage{- UID: &taskRunUID,- PlanUID: &planID,+ ProjectID: projectID,+ UID: &taskRunUID,+ PlanUID: &planID,})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get task run"))@@ -510,7 +514,7 @@ func (s *RolloutService) GetTaskRunLog(ctx context.Context, req *connect.Request}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -521,8 +525,9 @@ func (s *RolloutService) GetTaskRunLog(ctx context.Context, req *connect.Request}taskRun, err := s.store.GetTaskRunV1(ctx, &store.FindTaskRunMessage{- UID: &taskRunUID,- PlanUID: &planID,+ ProjectID: projectID,+ UID: &taskRunUID,+ PlanUID: &planID,})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get task run"))@@ -531,7 +536,7 @@ func (s *RolloutService) GetTaskRunLog(ctx context.Context, req *connect.Requestreturn nil, connect.NewError(connect.CodeNotFound, errors.Errorf("task run %d not found in plan %d", taskRunUID, planID))}- logs, err := s.store.ListTaskRunLogs(ctx, taskRunUID)+ logs, err := s.store.ListTaskRunLogs(ctx, projectID, taskRunUID)if err != nil {return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "failed to list task run logs"))}@@ -546,7 +551,7 @@ func (s *RolloutService) GetTaskRunSession(ctx context.Context, req *connect.Req}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -556,7 +561,7 @@ func (s *RolloutService) GetTaskRunSession(ctx context.Context, req *connect.Reqreturn nil, connect.NewError(connect.CodeNotFound, errors.Errorf("plan %d not found in project %s", planID, projectID))}- tasks, err := s.store.ListTasks(ctx, &store.TaskFind{ID: &taskUID, PlanID: &planID})+ tasks, err := s.store.ListTasks(ctx, &store.TaskFind{ProjectID: projectID, ID: &taskUID, PlanID: &planID})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get task"))}@@ -709,7 +714,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Request}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -720,7 +725,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Request}// Reset notification state so user gets fresh feedback on retry- if err := s.store.ResetPlanWebhookDelivery(ctx, planID); err != nil {+ if err := s.store.ResetPlanWebhookDelivery(ctx, projectID, planID); err != nil {slog.Error("failed to reset plan webhook delivery", log.BBError(err))// Don't fail the request - notification is non-critical}@@ -757,7 +762,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Request}// Get all tasks in the same environment- stageToRunTasks, err := s.store.ListTasks(ctx, &store.TaskFind{PlanID: &planID, Environment: &environmentToRun})+ stageToRunTasks, err := s.store.ListTasks(ctx, &store.TaskFind{ProjectID: projectID, PlanID: &planID, Environment: &environmentToRun})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list tasks"))}@@ -796,7 +801,8 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Request}create := &store.TaskRunMessage{- TaskUID: task.ID,+ TaskUID: task.ID,+ ProjectID: projectID,}if request.GetRunTime() != nil {t := request.GetRunTime().AsTime()@@ -841,7 +847,7 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, req *connect.Reques}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -859,7 +865,7 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, req *connect.Requesreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find issue"))}- tasks, err := s.store.ListTasks(ctx, &store.TaskFind{PlanID: &planID})+ tasks, err := s.store.ListTasks(ctx, &store.TaskFind{ProjectID: projectID, PlanID: &planID})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list tasks"))}@@ -898,12 +904,12 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, req *connect.Reques}}- if err := s.store.BatchSkipTasks(ctx, taskUIDs, request.Reason); err != nil {+ if err := s.store.BatchSkipTasks(ctx, projectID, taskUIDs, request.Reason); err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to skip tasks"))}// Signal to check if plan is complete and successful (may send PIPELINE_COMPLETED)- s.bus.PlanCompletionCheckChan <- planID+ s.bus.PlanCompletionCheckChan <- bus.PlanRef{ProjectID: projectID, PlanID: planID}return connect.NewResponse(&v1pb.BatchSkipTasksResponse{}), nil}@@ -931,7 +937,7 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, req *connect.R}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -983,7 +989,8 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, req *connect.R}taskRuns, err := s.store.ListTaskRuns(ctx, &store.FindTaskRunMessage{- UIDs: &taskRunIDs,+ ProjectID: projectID,+ UIDs: &taskRunIDs,})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list task runs"))@@ -1001,17 +1008,17 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, req *connect.Rfor _, taskRun := range taskRuns {if taskRun.Status == storepb.TaskRun_RUNNING {- if cancelFunc, ok := s.bus.RunningTaskRunsCancelFunc.Load(taskRun.ID); ok {+ if cancelFunc, ok := s.bus.RunningTaskRunsCancelFunc.Load(bus.TaskRunRef{ProjectID: projectID, ID: taskRun.ID}); ok {cancelFunc.(context.CancelFunc)()}// Broadcast cancel signal to all replicas for HA.- if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_TASK_RUN, int32(taskRun.ID)); err != nil {+ if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_TASK_RUN, projectID, int32(taskRun.ID)); err != nil {slog.Warn("failed to send cancel signal", log.BBError(err))}}}- if err := s.store.BatchCancelTaskRuns(ctx, taskRunIDs); err != nil {+ if err := s.store.BatchCancelTaskRuns(ctx, projectID, taskRunIDs); err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to batch patch task run status to canceled"))}@@ -1028,7 +1035,7 @@ func (s *RolloutService) PreviewTaskRunRollback(ctx context.Context, req *connec}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -1039,8 +1046,9 @@ func (s *RolloutService) PreviewTaskRunRollback(ctx context.Context, req *connec}taskRuns, err := s.store.ListTaskRuns(ctx, &store.FindTaskRunMessage{- UID: &taskRunUID,- PlanUID: &planID,+ ProjectID: projectID,+ UID: &taskRunUID,+ PlanUID: &planID,})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list task runs"))@@ -1067,7 +1075,7 @@ func (s *RolloutService) PreviewTaskRunRollback(ctx context.Context, req *connec}// Get backup detail from task run logs.- logs, err := s.store.ListTaskRunLogs(ctx, taskRunUID)+ logs, err := s.store.ListTaskRunLogs(ctx, projectID, taskRunUID)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list task run logs"))}@@ -1082,7 +1090,7 @@ func (s *RolloutService) PreviewTaskRunRollback(ctx context.Context, req *connecreturn nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("task run %v has no backup detail in logs", taskRun.ID))}- tasks, err := s.store.ListTasks(ctx, &store.TaskFind{ID: &taskUID, PlanID: &planID})+ tasks, err := s.store.ListTasks(ctx, &store.TaskFind{ProjectID: projectID, ID: &taskUID, PlanID: &planID})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get task"))}diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex 0b09718ef6..a491b1292a 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -891,7 +891,7 @@ func (s *SQLService) doExportFromIssue(ctx context.Context, requestName string)}plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: &projectID,+ ProjectID: projectID,UID: &planID,})if err != nil {@@ -901,7 +901,7 @@ func (s *SQLService) doExportFromIssue(ctx context.Context, requestName string)return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("rollout %d not found in project %s", planID, projectID))}- tasks, err := s.store.ListTasks(ctx, &store.TaskFind{PlanID: &plan.UID})+ tasks, err := s.store.ListTasks(ctx, &store.TaskFind{ProjectID: projectID, PlanID: &plan.UID})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to get tasks: %v", err))}@@ -928,8 +928,9 @@ func (s *SQLService) doExportFromIssue(ctx context.Context, requestName string)}taskRuns, err := s.store.ListTaskRuns(ctx, &store.FindTaskRunMessage{- TaskUID: &task.ID,- Status: &targetTaskRunStatus,+ ProjectID: projectID,+ TaskUID: &task.ID,+ Status: &targetTaskRunStatus,})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to get task run: %v", err))diff --git a/backend/component/bus/bus.go b/backend/component/bus/bus.goindex 881c4d85a2..22409515e9 100644--- a/backend/component/bus/bus.go+++ b/backend/component/bus/bus.go@@ -5,6 +5,24 @@ import ("sync")+// PlanRef identifies a plan by project and UID.+type PlanRef struct {+ ProjectID string+ PlanID int64+}++// TaskRunRef identifies a task run by project and ID.+type TaskRunRef struct {+ ProjectID string+ ID int+}++// PlanCheckRunRef identifies a plan check run by project and UID.+type PlanCheckRunRef struct {+ ProjectID string+ UID int+}+// Bus is the message bus for all in-memory communication within the server.type Bus struct {// ApprovalCheckChan signals when an issue needs approval template finding.@@ -12,10 +30,10 @@ type Bus struct {ApprovalCheckChan chan int64 // issue UID// RunningTaskRunsCancelFunc is the cancelFunc of running taskruns.- RunningTaskRunsCancelFunc sync.Map // map[taskRunID]context.CancelFunc+ RunningTaskRunsCancelFunc sync.Map // map[TaskRunRef]context.CancelFunc// RunningPlanCheckRunsCancelFunc is the cancelFunc of running plan checks.- RunningPlanCheckRunsCancelFunc sync.Map // map[planCheckRunUID]context.CancelFunc+ RunningPlanCheckRunsCancelFunc sync.Map // map[PlanCheckRunRef]context.CancelFunc// PlanCheckTickleChan is the tickler for plan check scheduler.PlanCheckTickleChan chan int@@ -23,10 +41,10 @@ type Bus struct {TaskRunTickleChan chan int// RolloutCreationChan is the channel for automatic rollout creation.- RolloutCreationChan chan int64+ RolloutCreationChan chan PlanRef// PlanCompletionCheckChan signals when a plan might be complete (for PIPELINE_COMPLETED webhook).- PlanCompletionCheckChan chan int64+ PlanCompletionCheckChan chan PlanRef}func New() (*Bus, error) {@@ -34,7 +52,7 @@ func New() (*Bus, error) {ApprovalCheckChan: make(chan int64, 1000),PlanCheckTickleChan: make(chan int, 1000),TaskRunTickleChan: make(chan int, 1000),- RolloutCreationChan: make(chan int64, 100),- PlanCompletionCheckChan: make(chan int64, 1000),+ RolloutCreationChan: make(chan PlanRef, 100),+ PlanCompletionCheckChan: make(chan PlanRef, 1000),}, nil}diff --git a/backend/generated-go/store/signal.pb.go b/backend/generated-go/store/signal.pb.goindex bb9ef77209..99c2afe198 100644--- a/backend/generated-go/store/signal.pb.go+++ b/backend/generated-go/store/signal.pb.go@@ -76,6 +76,7 @@ type Signal struct {state protoimpl.MessageState `protogen:"open.v1"`Type Signal_Type `protobuf:"varint,1,opt,name=type,proto3,enum=bytebase.store.Signal_Type" json:"type,omitempty"`Uid int32 `protobuf:"varint,2,opt,name=uid,proto3" json:"uid,omitempty"`+ Project string `protobuf:"bytes,3,opt,name=project,proto3" json:"project,omitempty"`unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCache}@@ -124,14 +125,22 @@ func (x *Signal) GetUid() int32 {return 0}+func (x *Signal) GetProject() string {+ if x != nil {+ return x.Project+ }+ return ""+}+var File_store_signal_proto protoreflect.FileDescriptorconst file_store_signal_proto_rawDesc = "" +"\n" +- "\x12store/signal.proto\x12\x0ebytebase.store\"\x99\x01\n" ++ "\x12store/signal.proto\x12\x0ebytebase.store\"\xb3\x01\n" +"\x06Signal\x12/\n" +"\x04type\x18\x01 \x01(\x0e2\x1b.bytebase.store.Signal.TypeR\x04type\x12\x10\n" +- "\x03uid\x18\x02 \x01(\x05R\x03uid\"L\n" ++ "\x03uid\x18\x02 \x01(\x05R\x03uid\x12\x18\n" ++ "\aproject\x18\x03 \x01(\tR\aproject\"L\n" +"\x04Type\x12\x14\n" +"\x10TYPE_UNSPECIFIED\x10\x00\x12\x19\n" +"\x15CANCEL_PLAN_CHECK_RUN\x10\x01\x12\x13\n" +diff --git a/backend/generated-go/store/signal_equal.pb.go b/backend/generated-go/store/signal_equal.pb.goindex f7f45497d0..6a1de7e124 100644--- a/backend/generated-go/store/signal_equal.pb.go+++ b/backend/generated-go/store/signal_equal.pb.go@@ -16,5 +16,8 @@ func (x *Signal) Equal(y *Signal) bool {if x.Uid != y.Uid {return false}+ if x.Project != y.Project {+ return false+ }return true}diff --git a/backend/migrator/migration/3.16/0005##add_project_to_plan_chain.sql b/backend/migrator/migration/3.16/0005##add_project_to_plan_chain.sqlnew file mode 100644index 0000000000..94de316f7c--- /dev/null+++ b/backend/migrator/migration/3.16/0005##add_project_to_plan_chain.sql@@ -0,0 +1,94 @@+-- Phase A: Drop all FKs referencing plan chain tables++-- FKs referencing plan(id)+ALTER TABLE plan_check_run DROP CONSTRAINT plan_check_run_plan_id_fkey;+ALTER TABLE plan_webhook_delivery DROP CONSTRAINT plan_webhook_delivery_plan_id_fkey;+ALTER TABLE task DROP CONSTRAINT task_plan_id_fkey;+ALTER TABLE issue DROP CONSTRAINT issue_plan_id_fkey;+-- FKs referencing task(id)+ALTER TABLE task_run DROP CONSTRAINT task_run_task_id_fkey;+-- FKs referencing task_run(id)+ALTER TABLE task_run_log DROP CONSTRAINT task_run_log_task_run_id_fkey;++-- Phase B: Add project columns and change PKs (top-down)++-- plan (already has project, just change PK)+ALTER TABLE plan DROP CONSTRAINT plan_pkey;+ALTER TABLE plan ADD PRIMARY KEY (project, id);++-- task+ALTER TABLE task ADD COLUMN project TEXT;+UPDATE task SET project = plan.project FROM plan WHERE task.plan_id = plan.id;+ALTER TABLE task ALTER COLUMN project SET NOT NULL;+ALTER TABLE task DROP CONSTRAINT task_pkey;+ALTER TABLE task ADD PRIMARY KEY (project, id);++-- task_run+ALTER TABLE task_run ADD COLUMN project TEXT;+UPDATE task_run SET project = task.project FROM task WHERE task_run.task_id = task.id;+ALTER TABLE task_run ALTER COLUMN project SET NOT NULL;+ALTER TABLE task_run DROP CONSTRAINT task_run_pkey;+ALTER TABLE task_run ADD PRIMARY KEY (project, id);++-- task_run_log+ALTER TABLE task_run_log ADD COLUMN project TEXT;+UPDATE task_run_log SET project = task_run.project FROM task_run WHERE task_run_log.task_run_id = task_run.id;+ALTER TABLE task_run_log ALTER COLUMN project SET NOT NULL;+ALTER TABLE task_run_log DROP CONSTRAINT task_run_log_pkey;+ALTER TABLE task_run_log ADD PRIMARY KEY (project, task_run_id, created_at);++-- plan_check_run+ALTER TABLE plan_check_run ADD COLUMN project TEXT;+UPDATE plan_check_run SET project = plan.project FROM plan WHERE plan_check_run.plan_id = plan.id;+ALTER TABLE plan_check_run ALTER COLUMN project SET NOT NULL;+ALTER TABLE plan_check_run DROP CONSTRAINT plan_check_run_pkey;+ALTER TABLE plan_check_run ADD PRIMARY KEY (project, id);++-- plan_webhook_delivery+ALTER TABLE plan_webhook_delivery ADD COLUMN project TEXT;+UPDATE plan_webhook_delivery SET project = plan.project FROM plan WHERE plan_webhook_delivery.plan_id = plan.id;+-- Handle orphan rows (plan_id with no matching plan)+DELETE FROM plan_webhook_delivery WHERE project IS NULL;+ALTER TABLE plan_webhook_delivery ALTER COLUMN project SET NOT NULL;+ALTER TABLE plan_webhook_delivery DROP CONSTRAINT plan_webhook_delivery_pkey;+ALTER TABLE plan_webhook_delivery ADD PRIMARY KEY (project, plan_id);++-- Phase C: Re-add composite FKs and update indexes++-- task -> plan+ALTER TABLE task ADD CONSTRAINT task_plan_id_fkey+ FOREIGN KEY (project, plan_id) REFERENCES plan(project, id);++-- task_run -> task+ALTER TABLE task_run ADD CONSTRAINT task_run_task_id_fkey+ FOREIGN KEY (project, task_id) REFERENCES task(project, id);++-- task_run_log -> task_run+ALTER TABLE task_run_log ADD CONSTRAINT task_run_log_task_run_id_fkey+ FOREIGN KEY (project, task_run_id) REFERENCES task_run(project, id);++-- plan_check_run -> plan+ALTER TABLE plan_check_run ADD CONSTRAINT plan_check_run_plan_id_fkey+ FOREIGN KEY (project, plan_id) REFERENCES plan(project, id);++-- plan_webhook_delivery -> plan+ALTER TABLE plan_webhook_delivery ADD CONSTRAINT plan_webhook_delivery_plan_id_fkey+ FOREIGN KEY (project, plan_id) REFERENCES plan(project, id);++-- issue -> plan (cross-chain, issue already has project column)+ALTER TABLE issue ADD CONSTRAINT issue_plan_id_fkey+ FOREIGN KEY (project, plan_id) REFERENCES plan(project, id);++-- Update indexes to include project+DROP INDEX idx_task_plan_id_environment;+CREATE INDEX idx_task_plan_id_environment ON task(project, plan_id, environment);++DROP INDEX uk_task_run_task_id_attempt;+CREATE UNIQUE INDEX uk_task_run_task_id_attempt ON task_run(project, task_id, attempt);++DROP INDEX idx_plan_check_run_unique_plan_id;+CREATE UNIQUE INDEX idx_plan_check_run_unique_plan_id ON plan_check_run(project, plan_id);++-- issue unique plan_id index+DROP INDEX idx_issue_unique_plan_id;+CREATE UNIQUE INDEX idx_issue_unique_plan_id ON issue(project, plan_id);diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sqlindex 0a6873f771..a1369be971 100644--- a/backend/migrator/migration/LATEST.sql+++ b/backend/migrator/migration/LATEST.sql@@ -166,7 +166,7 @@ CREATE TABLE sheet_blob (-- plan table stores the plan for a projectCREATE TABLE plan (- id bigserial PRIMARY KEY,+ id bigserial,resource_id text NOT NULL DEFAULT gen_random_uuid()::text,deleted boolean NOT NULL DEFAULT FALSE,creator text NOT NULL,@@ -176,7 +176,8 @@ CREATE TABLE plan (name text NOT NULL,description text NOT NULL,-- Stored as PlanConfig (proto/store/store/plan.proto)- config jsonb NOT NULL DEFAULT '{}'+ config jsonb NOT NULL DEFAULT '{}',+ PRIMARY KEY (project, id));CREATE INDEX idx_plan_project ON plan(project);@@ -187,16 +188,19 @@ CREATE UNIQUE INDEX idx_plan_unique_resource_id ON plan(resource_id);ALTER SEQUENCE plan_id_seq RESTART WITH 101;CREATE TABLE plan_check_run (- id serial PRIMARY KEY,+ id serial,created_at timestamptz NOT NULL DEFAULT now(),updated_at timestamptz NOT NULL DEFAULT now(),- plan_id bigint NOT NULL REFERENCES plan(id),+ project text NOT NULL,+ plan_id bigint NOT NULL,status text NOT NULL CHECK (status IN ('AVAILABLE', 'RUNNING', 'DONE', 'FAILED', 'CANCELED')),-- Stored as PlanCheckRunResult (proto/store/store/plan_check_run.proto)- result jsonb NOT NULL DEFAULT '{}'+ result jsonb NOT NULL DEFAULT '{}',+ PRIMARY KEY (project, id),+ FOREIGN KEY (project, plan_id) REFERENCES plan(project, id));-CREATE UNIQUE INDEX idx_plan_check_run_unique_plan_id ON plan_check_run(plan_id);+CREATE UNIQUE INDEX idx_plan_check_run_unique_plan_id ON plan_check_run(project, plan_id);CREATE INDEX idx_plan_check_run_active_status ON plan_check_run(status, id) WHERE status IN ('AVAILABLE', 'RUNNING');@@ -206,38 +210,45 @@ ALTER SEQUENCE plan_check_run_id_seq RESTART WITH 101;-- One row per plan at any time - mutually exclusive events.-- Row is deleted when user clicks BatchRunTasks to reset notification state.CREATE TABLE plan_webhook_delivery (- plan_id BIGINT PRIMARY KEY REFERENCES plan(id),+ project TEXT NOT NULL,+ plan_id BIGINT NOT NULL,-- Event type: 'PIPELINE_FAILED' or 'PIPELINE_COMPLETED'event_type TEXT NOT NULL,- delivered_at TIMESTAMPTZ NOT NULL DEFAULT NOW()+ delivered_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),+ PRIMARY KEY (project, plan_id),+ FOREIGN KEY (project, plan_id) REFERENCES plan(project, id));-- task table stores the task for a planCREATE TABLE task (- id serial PRIMARY KEY,+ id serial,resource_id text NOT NULL DEFAULT gen_random_uuid()::text,- plan_id bigint NOT NULL REFERENCES plan(id),+ project text NOT NULL REFERENCES project(resource_id),+ plan_id bigint NOT NULL,instance text NOT NULL REFERENCES instance(resource_id),environment text,db_name text,type text NOT NULL,-- Stored as Task (proto/store/store/task.proto)- payload jsonb NOT NULL DEFAULT '{}'+ payload jsonb NOT NULL DEFAULT '{}',+ PRIMARY KEY (project, id),+ FOREIGN KEY (project, plan_id) REFERENCES plan(project, id));-CREATE INDEX idx_task_plan_id_environment ON task(plan_id, environment);+CREATE INDEX idx_task_plan_id_environment ON task(project, plan_id, environment);CREATE UNIQUE INDEX idx_task_unique_resource_id ON task(resource_id);ALTER SEQUENCE task_id_seq RESTART WITH 101;-- task run table stores the task runCREATE TABLE task_run (- id serial PRIMARY KEY,+ id serial,resource_id text NOT NULL DEFAULT gen_random_uuid()::text,creator text,created_at timestamptz NOT NULL DEFAULT now(),updated_at timestamptz NOT NULL DEFAULT now(),- task_id integer NOT NULL REFERENCES task(id),+ project text NOT NULL,+ task_id integer NOT NULL,attempt integer NOT NULL,status text NOT NULL CHECK (status IN ('PENDING', 'AVAILABLE', 'RUNNING', 'DONE', 'FAILED', 'CANCELED')),started_at timestamptz NULL,@@ -247,13 +258,15 @@ CREATE TABLE task_run (result jsonb NOT NULL DEFAULT '{}',replica_id TEXT,-- Stored as TaskRunPayload (proto/store/store/task_run.proto)- payload jsonb NOT NULL DEFAULT '{}'+ payload jsonb NOT NULL DEFAULT '{}',+ PRIMARY KEY (project, id),+ FOREIGN KEY (project, task_id) REFERENCES task(project, id));CREATE INDEX idx_task_run_task_id ON task_run(task_id);CREATE UNIQUE INDEX idx_task_run_unique_resource_id ON task_run(resource_id);-CREATE UNIQUE INDEX uk_task_run_task_id_attempt ON task_run (task_id, attempt);+CREATE UNIQUE INDEX uk_task_run_task_id_attempt ON task_run(project, task_id, attempt);-- Partial index for active task runs. Most task runs are in terminal states (DONE, FAILED, CANCELED)-- that never change. Queries frequently filter for active statuses (PENDING, RUNNING), so a partial@@ -272,11 +285,13 @@ CREATE TABLE replica_heartbeat ();CREATE TABLE task_run_log (- task_run_id integer NOT NULL REFERENCES task_run(id),+ project text NOT NULL,+ task_run_id integer NOT NULL,created_at timestamptz NOT NULL DEFAULT now(),-- Stored as TaskRunLog (proto/store/store/task_run_log.proto)payload jsonb NOT NULL DEFAULT '{}',- PRIMARY KEY (task_run_id, created_at)+ PRIMARY KEY (project, task_run_id, created_at),+ FOREIGN KEY (project, task_run_id) REFERENCES task_run(project, id));-- Pipeline related END@@ -289,7 +304,7 @@ CREATE TABLE issue (created_at timestamptz NOT NULL DEFAULT now(),updated_at timestamptz NOT NULL DEFAULT now(),project text NOT NULL REFERENCES project(resource_id),- plan_id bigint REFERENCES plan(id),+ plan_id bigint,name text NOT NULL,status text NOT NULL CHECK (status IN ('OPEN', 'DONE', 'CANCELED')),-- type: DATABASE_CHANGE, GRANT_REQUEST, DATABASE_EXPORT@@ -298,12 +313,13 @@ CREATE TABLE issue (description text NOT NULL DEFAULT '',-- Stored as Issue (proto/store/store/issue.proto)payload jsonb NOT NULL DEFAULT '{}',- ts_vector tsvector+ ts_vector tsvector,+ FOREIGN KEY (project, plan_id) REFERENCES plan(project, id));CREATE INDEX idx_issue_project ON issue(project);-CREATE UNIQUE INDEX idx_issue_unique_plan_id ON issue(plan_id);+CREATE UNIQUE INDEX idx_issue_unique_plan_id ON issue(project, plan_id);CREATE INDEX idx_issue_creator ON issue(creator);diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.goindex 43db12c665..053e240fd9 100644--- a/backend/migrator/migrator_test.go+++ b/backend/migrator/migrator_test.go@@ -15,8 +15,8 @@ import (func TestLatestVersion(t *testing.T) {files, err := getSortedVersionedFiles()require.NoError(t, err)- require.Equal(t, semver.MustParse("3.16.4"), *files[len(files)-1].version)- require.Equal(t, "migration/3.16/0004##drop_worksheet_organizer_id.sql", files[len(files)-1].path)+ require.Equal(t, semver.MustParse("3.16.5"), *files[len(files)-1].version)+ require.Equal(t, "migration/3.16/0005##add_project_to_plan_chain.sql", files[len(files)-1].path)}func TestVersionUnique(t *testing.T) {diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.goindex 420de192f9..705d6f1976 100644--- a/backend/runner/approval/runner.go+++ b/backend/runner/approval/runner.go@@ -127,7 +127,7 @@ func (r *Runner) processIssue(ctx context.Context, issueUID int64) {return}if approved {- r.bus.RolloutCreationChan <- *issue.PlanUID+ r.bus.RolloutCreationChan <- bus.PlanRef{ProjectID: issue.ProjectID, PlanID: *issue.PlanUID}}}}@@ -506,7 +506,7 @@ func buildCELVariablesForDatabaseChange(ctx context.Context, stores *store.Storeif issue.PlanUID == nil {return nil, false, errors.Errorf("expected plan UID in issue %v", issue.UID)}- plan, err := stores.GetPlan(ctx, &store.FindPlanMessage{UID: issue.PlanUID})+ plan, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: issue.ProjectID, UID: issue.PlanUID})if err != nil {return nil, false, errors.Wrapf(err, "failed to get plan %v", *issue.PlanUID)}@@ -514,7 +514,7 @@ func buildCELVariablesForDatabaseChange(ctx context.Context, stores *store.Storereturn nil, false, errors.Errorf("plan %v not found", *issue.PlanUID)}- planCheckRun, err := stores.GetPlanCheckRun(ctx, plan.UID)+ planCheckRun, err := stores.GetPlanCheckRun(ctx, issue.ProjectID, plan.UID)if err != nil {return nil, false, errors.Wrapf(err, "failed to get plan check run for plan %v", plan.UID)}@@ -630,7 +630,7 @@ func buildCELVariablesForDataExport(ctx context.Context, stores *store.Store, isif issue.PlanUID == nil {return nil, false, errors.Errorf("expected plan UID in issue %v", issue.UID)}- plan, err := stores.GetPlan(ctx, &store.FindPlanMessage{UID: issue.PlanUID})+ plan, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: issue.ProjectID, UID: issue.PlanUID})if err != nil {return nil, false, errors.Wrapf(err, "failed to get plan %v", *issue.PlanUID)}@@ -856,7 +856,7 @@ func getApprovalSourceFromIssue(ctx context.Context, stores *store.Store, issueif issue.PlanUID == nil {return storepb.WorkspaceApprovalSetting_Rule_SOURCE_UNSPECIFIED, errors.Errorf("expected plan UID in issue %v", issue.UID)}- plan, err := stores.GetPlan(ctx, &store.FindPlanMessage{UID: issue.PlanUID})+ plan, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: issue.ProjectID, UID: issue.PlanUID})if err != nil {return storepb.WorkspaceApprovalSetting_Rule_SOURCE_UNSPECIFIED, errors.Wrapf(err, "failed to get plan %v", *issue.PlanUID)}diff --git a/backend/runner/notifylistener/listener.go b/backend/runner/notifylistener/listener.goindex 9c3c1619d7..a72769941c 100644--- a/backend/runner/notifylistener/listener.go+++ b/backend/runner/notifylistener/listener.go@@ -90,11 +90,13 @@ func (l *Listener) handleNotification(payload string) {switch signal.Type {case storepb.Signal_CANCEL_PLAN_CHECK_RUN:- if cancel, ok := l.bus.RunningPlanCheckRunsCancelFunc.Load(int(signal.Uid)); ok {+ ref := bus.PlanCheckRunRef{ProjectID: signal.Project, UID: int(signal.Uid)}+ if cancel, ok := l.bus.RunningPlanCheckRunsCancelFunc.Load(ref); ok {cancel.(context.CancelFunc)()}case storepb.Signal_CANCEL_TASK_RUN:- if cancel, ok := l.bus.RunningTaskRunsCancelFunc.Load(int(signal.Uid)); ok {+ ref := bus.TaskRunRef{ProjectID: signal.Project, ID: int(signal.Uid)}+ if cancel, ok := l.bus.RunningTaskRunsCancelFunc.Load(ref); ok {cancel.(context.CancelFunc)()}default:diff --git a/backend/runner/plancheck/scheduler.go b/backend/runner/plancheck/scheduler.goindex 4a926fe4d8..ac58279504 100644--- a/backend/runner/plancheck/scheduler.go+++ b/backend/runner/plancheck/scheduler.go@@ -86,48 +86,49 @@ func (s *Scheduler) runOnce(ctx context.Context) {}for _, c := range claimed {- go s.runPlanCheckRun(ctx, c.UID, c.PlanUID)+ go s.runPlanCheckRun(ctx, c.ProjectID, c.UID, c.PlanUID)}}-func (s *Scheduler) runPlanCheckRun(ctx context.Context, uid int, planUID int64) {+func (s *Scheduler) runPlanCheckRun(ctx context.Context, projectID string, uid int, planUID int64) {ctxWithCancel, cancel := context.WithCancel(ctx)defer cancel()- s.bus.RunningPlanCheckRunsCancelFunc.Store(uid, cancel)- defer s.bus.RunningPlanCheckRunsCancelFunc.Delete(uid)+ planCheckRef := bus.PlanCheckRunRef{ProjectID: projectID, UID: uid}+ s.bus.RunningPlanCheckRunsCancelFunc.Store(planCheckRef, cancel)+ defer s.bus.RunningPlanCheckRunsCancelFunc.Delete(planCheckRef)// Fetch plan to derive check targets at runtime- plan, err := s.store.GetPlan(ctxWithCancel, &store.FindPlanMessage{UID: &planUID})+ plan, err := s.store.GetPlan(ctxWithCancel, &store.FindPlanMessage{ProjectID: projectID, UID: &planUID})if err != nil {- s.markPlanCheckRunFailed(ctxWithCancel, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())return}if plan == nil {- s.markPlanCheckRunFailed(ctxWithCancel, uid, "plan not found")+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, "plan not found")return}project, err := s.store.GetProject(ctxWithCancel, &store.FindProjectMessage{ResourceID: &plan.ProjectID})if err != nil {- s.markPlanCheckRunFailed(ctxWithCancel, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())return}if project == nil {- s.markPlanCheckRunFailed(ctxWithCancel, uid, "project not found")+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, "project not found")return}// Get database group if needed (for spec expansion)databaseGroup, err := s.getDatabaseGroupForPlan(ctxWithCancel, plan)if err != nil {- s.markPlanCheckRunFailed(ctxWithCancel, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())return}// Derive check targets from plantargets, err := DeriveCheckTargets(ctxWithCancel, s.store, project, plan, databaseGroup)if err != nil {- s.markPlanCheckRunFailed(ctxWithCancel, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())return}@@ -142,20 +143,21 @@ func (s *Scheduler) runPlanCheckRun(ctx context.Context, uid int, planUID int64)}if err != nil {if errors.Is(err, context.Canceled) {- s.markPlanCheckRunCanceled(ctxWithCancel, uid, err.Error())+ s.markPlanCheckRunCanceled(ctxWithCancel, projectID, uid, err.Error())} else {- s.markPlanCheckRunFailed(ctxWithCancel, uid, err.Error())+ s.markPlanCheckRunFailed(ctxWithCancel, projectID, uid, err.Error())}} else {- s.markPlanCheckRunDone(ctxWithCancel, uid, planUID, results)+ s.markPlanCheckRunDone(ctxWithCancel, projectID, uid, planUID, results)}}-func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, uid int, planUID int64, results []*storepb.PlanCheckRunResult_Result) {+func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, projectID string, uid int, planUID int64, results []*storepb.PlanCheckRunResult_Result) {result := &storepb.PlanCheckRunResult{Results: results,}if err := s.store.UpdatePlanCheckRun(ctx,+ projectID,store.PlanCheckRunStatusDone,result,uid,@@ -179,11 +181,12 @@ func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, uid int, planUID i}}-func (s *Scheduler) markPlanCheckRunFailed(ctx context.Context, uid int, reason string) {+func (s *Scheduler) markPlanCheckRunFailed(ctx context.Context, projectID string, uid int, reason string) {result := &storepb.PlanCheckRunResult{Error: reason,}if err := s.store.UpdatePlanCheckRun(ctx,+ projectID,store.PlanCheckRunStatusFailed,result,uid,@@ -192,11 +195,12 @@ func (s *Scheduler) markPlanCheckRunFailed(ctx context.Context, uid int, reason}}-func (s *Scheduler) markPlanCheckRunCanceled(ctx context.Context, uid int, reason string) {+func (s *Scheduler) markPlanCheckRunCanceled(ctx context.Context, projectID string, uid int, reason string) {result := &storepb.PlanCheckRunResult{Error: reason,}if err := s.store.UpdatePlanCheckRun(ctx,+ projectID,store.PlanCheckRunStatusCanceled,result,uid,diff --git a/backend/runner/taskrun/data_export_executor.go b/backend/runner/taskrun/data_export_executor.goindex 2ecc9b5d66..1ba724e2b8 100644--- a/backend/runner/taskrun/data_export_executor.go+++ b/backend/runner/taskrun/data_export_executor.go@@ -52,7 +52,7 @@ func (exec *DataExportExecutor) RunOnce(ctx context.Context, _ context.Context,}// Get plan to retrieve export format from spec- plan, err := exec.store.GetPlan(ctx, &store.FindPlanMessage{UID: &task.PlanID})+ plan, err := exec.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: task.ProjectID, UID: &task.PlanID})if err != nil {return nil, errors.Wrapf(err, "failed to get plan")}diff --git a/backend/runner/taskrun/database_create_executor.go b/backend/runner/taskrun/database_create_executor.goindex ab3baf84f9..5337f42aa4 100644--- a/backend/runner/taskrun/database_create_executor.go+++ b/backend/runner/taskrun/database_create_executor.go@@ -57,7 +57,7 @@ func (exec *DatabaseCreateExecutor) RunOnce(ctx context.Context, driverCtx contereturn nil, errors.Errorf("creating database is not supported for engine %v", instance.Metadata.GetEngine().String())}- plan, err := exec.store.GetPlan(ctx, &store.FindPlanMessage{UID: &task.PlanID})+ plan, err := exec.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: task.ProjectID, UID: &task.PlanID})if err != nil {return nil, errors.Wrapf(err, "failed to get plan %v", task.PlanID)}diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.goindex ecce2fbe65..cd65eedabf 100644--- a/backend/runner/taskrun/database_migrate_executor.go+++ b/backend/runner/taskrun/database_migrate_executor.go@@ -170,14 +170,14 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, dvar priorBackupDetail *storepb.PriorBackupDetailif task.Payload.GetEnablePriorBackup() {// Check if this specific task run wants to skip backup.- taskRun, err := exec.store.GetTaskRunByUID(ctx, taskRunUID)+ taskRun, err := exec.store.GetTaskRunByUID(ctx, database.ProjectID, taskRunUID)if err != nil {return nil, errors.Wrapf(err, "failed to get task run")}skipBackup := taskRun.PayloadProto.GetSkipPriorBackup()if !skipBackup {- exec.store.CreateTaskRunLogS(ctx, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{+ exec.store.CreateTaskRunLogS(ctx, database.ProjectID, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{Type: storepb.TaskRunLog_PRIOR_BACKUP_START,PriorBackupStart: &storepb.TaskRunLog_PriorBackupStart{},})@@ -187,7 +187,7 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, dvar backupErr errorpriorBackupDetail, backupErr = exec.backupData(ctx, driverCtx, sheet.Statement, task.Payload, task, instance, database)if backupErr != nil {- exec.store.CreateTaskRunLogS(ctx, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{+ exec.store.CreateTaskRunLogS(ctx, database.ProjectID, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{Type: storepb.TaskRunLog_PRIOR_BACKUP_END,PriorBackupEnd: &storepb.TaskRunLog_PriorBackupEnd{Error: backupErr.Error(),@@ -197,7 +197,7 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, dreturn nil, backupErr}- exec.store.CreateTaskRunLogS(ctx, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{+ exec.store.CreateTaskRunLogS(ctx, database.ProjectID, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{Type: storepb.TaskRunLog_PRIOR_BACKUP_END,PriorBackupEnd: &storepb.TaskRunLog_PriorBackupEnd{PriorBackupDetail: priorBackupDetail,@@ -232,7 +232,7 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, dopts.MaximumRetries = int(project.Setting.GetExecutionRetryPolicy().GetMaximumRetries())}opts.CreateTaskRunLog = func(t time.Time, e *storepb.TaskRunLog) error {- return exec.store.CreateTaskRunLog(ctx, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)+ return exec.store.CreateTaskRunLog(ctx, database.ProjectID, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)}// Begin migration - create pending changelog@@ -321,7 +321,7 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, drivopts.MaximumRetries = int(project.Setting.GetExecutionRetryPolicy().GetMaximumRetries())}opts.CreateTaskRunLog = func(t time.Time, e *storepb.TaskRunLog) error {- return exec.store.CreateTaskRunLog(ctx, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)+ return exec.store.CreateTaskRunLog(ctx, database.ProjectID, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)}// Prepare gh-ost migration context before beginning migration@@ -480,7 +480,7 @@ func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, dropts.MaximumRetries = int(project.Setting.GetExecutionRetryPolicy().GetMaximumRetries())}opts.CreateTaskRunLog = func(t time.Time, e *storepb.TaskRunLog) error {- return exec.store.CreateTaskRunLog(ctx, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)+ return exec.store.CreateTaskRunLog(ctx, database.ProjectID, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)}// Get database driver once for all files@@ -519,7 +519,7 @@ func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, drslog.String("file", file.Path))// Log release file execution- exec.store.CreateTaskRunLogS(ctx, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{+ exec.store.CreateTaskRunLogS(ctx, database.ProjectID, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{Type: storepb.TaskRunLog_RELEASE_FILE_EXECUTE,ReleaseFileExecute: &storepb.TaskRunLog_ReleaseFileExecute{Version: file.Version,@@ -629,7 +629,7 @@ func (exec *DatabaseMigrateExecutor) runDeclarativeRelease(ctx context.Context,slog.String("file", file.Path))// Log release file execution- exec.store.CreateTaskRunLogS(ctx, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{+ exec.store.CreateTaskRunLogS(ctx, database.ProjectID, taskRunUID, time.Now(), exec.profile.ReplicaID, &storepb.TaskRunLog{Type: storepb.TaskRunLog_RELEASE_FILE_EXECUTE,ReleaseFileExecute: &storepb.TaskRunLog_ReleaseFileExecute{Version: file.Version,@@ -660,7 +660,7 @@ func (exec *DatabaseMigrateExecutor) runDeclarativeRelease(ctx context.Context,opts.MaximumRetries = int(project.Setting.GetExecutionRetryPolicy().GetMaximumRetries())}opts.CreateTaskRunLog = func(t time.Time, e *storepb.TaskRunLog) error {- return exec.store.CreateTaskRunLog(ctx, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)+ return exec.store.CreateTaskRunLog(ctx, database.ProjectID, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)}// Compute SDL diff before beginning migrationdiff --git a/backend/runner/taskrun/pending_scheduler.go b/backend/runner/taskrun/pending_scheduler.goindex daae1a6ac9..07bca546e4 100644--- a/backend/runner/taskrun/pending_scheduler.go+++ b/backend/runner/taskrun/pending_scheduler.go@@ -61,9 +61,7 @@ func (s *Scheduler) schedulePendingTaskRuns(ctx context.Context) error {}}()- taskRuns, err := s.store.ListTaskRuns(ctx, &store.FindTaskRunMessage{- Status: &[]storepb.TaskRun_Status{storepb.TaskRun_PENDING},- })+ taskRuns, err := s.store.ListTaskRunsByStatus(ctx, []storepb.TaskRun_Status{storepb.TaskRun_PENDING})if err != nil {return errors.Wrapf(err, "failed to list pending task runs")}@@ -84,7 +82,7 @@ func (s *Scheduler) schedulePendingTaskRuns(ctx context.Context) error {}func (s *Scheduler) schedulePendingTaskRun(ctx context.Context, taskRun *store.TaskRunMessage, sc *schedulingContext) error {- task, err := s.store.GetTaskByID(ctx, taskRun.TaskUID)+ task, err := s.store.GetTaskByID(ctx, taskRun.ProjectID, taskRun.TaskUID)if err != nil {return errors.Wrapf(err, "failed to get task")}@@ -105,8 +103,8 @@ func (s *Scheduler) schedulePendingTaskRun(ctx context.Context, taskRun *store.Tif err != nil {return errors.Wrapf(err, "failed to get max parallel limit")}- if !sc.checkParallelLimit(task.PlanID, maxParallel) {- s.storeParallelLimitCause(ctx, taskRun.ID)+ if !sc.checkParallelLimit(task, maxParallel) {+ s.storeParallelLimitCause(ctx, taskRun.ProjectID, taskRun.ID)return nil}@@ -119,7 +117,7 @@ func (s *Scheduler) schedulePendingTaskRun(ctx context.Context, taskRun *store.Treturn nil}-func (s *Scheduler) storeParallelLimitCause(ctx context.Context, taskRunID int) {+func (s *Scheduler) storeParallelLimitCause(ctx context.Context, projectID string, taskRunID int) {payload := &storepb.TaskRunPayload{SchedulerInfo: &storepb.SchedulerInfo{ReportTime: timestamppb.Now(),@@ -130,13 +128,13 @@ func (s *Scheduler) storeParallelLimitCause(ctx context.Context, taskRunID int)},},}- if err := s.store.UpdateTaskRunPayload(ctx, taskRunID, payload); err != nil {+ if err := s.store.UpdateTaskRunPayload(ctx, projectID, taskRunID, payload); err != nil {slog.Error("failed to store parallel limit cause", log.BBError(err))}}func (s *Scheduler) getMaxParallelForTask(ctx context.Context, task *store.TaskMessage) (int, error) {- plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{UID: &task.PlanID})+ plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: task.ProjectID, UID: &task.PlanID})if err != nil {return 0, errors.Wrapf(err, "failed to get plan")}@@ -157,14 +155,15 @@ func (s *Scheduler) getMaxParallelForTask(ctx context.Context, task *store.TaskMfunc (s *Scheduler) promoteTaskRun(ctx context.Context, taskRun *store.TaskRunMessage) error {// Clear scheduler info by writing empty payload- if err := s.store.UpdateTaskRunPayload(ctx, taskRun.ID, &storepb.TaskRunPayload{}); err != nil {+ if err := s.store.UpdateTaskRunPayload(ctx, taskRun.ProjectID, taskRun.ID, &storepb.TaskRunPayload{}); err != nil {slog.Error("failed to clear scheduler info", log.BBError(err))}if _, err := s.store.UpdateTaskRunStatus(ctx, &store.TaskRunStatusPatch{- ID: taskRun.ID,- Updater: "",- Status: storepb.TaskRun_AVAILABLE,+ ID: taskRun.ID,+ ProjectID: taskRun.ProjectID,+ Updater: "",+ Status: storepb.TaskRun_AVAILABLE,}); err != nil {return errors.Wrapf(err, "failed to update task run status to available")}@@ -177,59 +176,69 @@ func (s *Scheduler) promoteTaskRun(ctx context.Context, taskRun *store.TaskRunMereturn nil}+// planKey identifies a plan by project and ID.+type planKey struct {+ projectID string+ planID int64+}+// schedulingContext holds pre-fetched and indexed active task run data for a scheduling cycle.type schedulingContext struct {// Pre-indexed active task runs (AVAILABLE + RUNNING)- activeByDatabase map[string]int // dbKey -> blocking taskID (first found)- activeCountByPlan map[int64]int // planID -> active count+ activeByDatabase map[string]int // dbKey -> blocking taskID (first found)+ activeCountByPlan map[planKey]int // (project, planID) -> active count// Tracks promotions this roundpromotedDBs map[string]bool- promotedCounts map[int64]int+ promotedCounts map[planKey]int}func newSchedulingContext(ctx context.Context, s *store.Store) (*schedulingContext, error) {- activeTaskRuns, err := s.ListTaskRuns(ctx, &store.FindTaskRunMessage{- Status: &[]storepb.TaskRun_Status{storepb.TaskRun_AVAILABLE, storepb.TaskRun_RUNNING},- })+ activeTaskRuns, err := s.ListTaskRunsByStatus(ctx, []storepb.TaskRun_Status{storepb.TaskRun_AVAILABLE, storepb.TaskRun_RUNNING})if err != nil {return nil, errors.Wrapf(err, "failed to list active task runs")}sc := &schedulingContext{activeByDatabase: make(map[string]int),- activeCountByPlan: make(map[int64]int),+ activeCountByPlan: make(map[planKey]int),promotedDBs: make(map[string]bool),- promotedCounts: make(map[int64]int),+ promotedCounts: make(map[planKey]int),}if len(activeTaskRuns) == 0 {return sc, nil}- // Batch fetch all tasks- taskIDs := make([]int, len(activeTaskRuns))- for i, tr := range activeTaskRuns {- taskIDs[i] = tr.TaskUID+ // Group task run task UIDs by project for correct project-scoped lookups.+ taskIDsByProject := make(map[string][]int)+ for _, tr := range activeTaskRuns {+ taskIDsByProject[tr.ProjectID] = append(taskIDsByProject[tr.ProjectID], tr.TaskUID)}- tasks, err := s.ListTasks(ctx, &store.TaskFind{IDs: &taskIDs})- if err != nil {- return nil, errors.Wrapf(err, "failed to list tasks")+ // Batch fetch tasks per project.+ type taskKey struct {+ projectID string+ id int}-- taskByID := make(map[int]*store.TaskMessage, len(tasks))- for _, t := range tasks {- taskByID[t.ID] = t+ taskByKey := make(map[taskKey]*store.TaskMessage)+ for projectID, ids := range taskIDsByProject {+ tasks, err := s.ListTasks(ctx, &store.TaskFind{ProjectID: projectID, IDs: &ids})+ if err != nil {+ return nil, errors.Wrapf(err, "failed to list tasks for project %s", projectID)+ }+ for _, t := range tasks {+ taskByKey[taskKey{projectID: t.ProjectID, id: t.ID}] = t+ }}// Build indexesfor _, tr := range activeTaskRuns {- task := taskByID[tr.TaskUID]+ task := taskByKey[taskKey{projectID: tr.ProjectID, id: tr.TaskUID}]if task == nil {continue}- sc.activeCountByPlan[task.PlanID]+++ sc.activeCountByPlan[planKey{projectID: task.ProjectID, planID: task.PlanID}]++if task.DatabaseName != nil && isSequentialTask(task) {dbKey := getDatabaseKey(task.InstanceID, *task.DatabaseName)@@ -262,11 +271,12 @@ func (sc *schedulingContext) checkDatabaseMutualExclusion(task *store.TaskMessagreturn true, nil}-func (sc *schedulingContext) checkParallelLimit(planID int64, maxParallel int) bool {+func (sc *schedulingContext) checkParallelLimit(task *store.TaskMessage, maxParallel int) bool {if maxParallel <= 0 {return true // no limit}- currentCount := sc.activeCountByPlan[planID] + sc.promotedCounts[planID]+ pk := planKey{projectID: task.ProjectID, planID: task.PlanID}+ currentCount := sc.activeCountByPlan[pk] + sc.promotedCounts[pk]return currentCount < maxParallel}@@ -274,5 +284,5 @@ func (sc *schedulingContext) markPromoted(task *store.TaskMessage) {if task.DatabaseName != nil && isSequentialTask(task) {sc.promotedDBs[getDatabaseKey(task.InstanceID, *task.DatabaseName)] = true}- sc.promotedCounts[task.PlanID]+++ sc.promotedCounts[planKey{projectID: task.ProjectID, planID: task.PlanID}]++}diff --git a/backend/runner/taskrun/rollout_creator.go b/backend/runner/taskrun/rollout_creator.goindex c919795001..1543b279b6 100644--- a/backend/runner/taskrun/rollout_creator.go+++ b/backend/runner/taskrun/rollout_creator.go@@ -32,14 +32,14 @@ func NewRolloutCreator(store *store.Store, bus *bus.Bus, webhookManager *webhook}// Run starts the rollout creator listening to the channel.-func (rc *RolloutCreator) Run(ctx context.Context, wg *sync.WaitGroup, rolloutCreationChan chan int64) {+func (rc *RolloutCreator) Run(ctx context.Context, wg *sync.WaitGroup, rolloutCreationChan chan bus.PlanRef) {defer wg.Done()slog.Debug("Rollout creator started")for {select {- case planID := <-rolloutCreationChan:- rc.tryCreateRollout(ctx, planID)+ case ref := <-rolloutCreationChan:+ rc.tryCreateRollout(ctx, ref)case <-ctx.Done():slog.Debug("Rollout creator stopped")return@@ -48,8 +48,9 @@ func (rc *RolloutCreator) Run(ctx context.Context, wg *sync.WaitGroup, rolloutCr}// tryCreateRollout attempts to create a rollout for the given plan.-func (rc *RolloutCreator) tryCreateRollout(ctx context.Context, planID int64) {- plan, err := rc.store.GetPlan(ctx, &store.FindPlanMessage{UID: &planID})+func (rc *RolloutCreator) tryCreateRollout(ctx context.Context, ref bus.PlanRef) {+ planID := ref.PlanID+ plan, err := rc.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: ref.ProjectID, UID: &planID})if err != nil {slog.Error("failed to get plan for rollout creation",slog.Int("plan_id", int(planID)),@@ -128,7 +129,7 @@ func (rc *RolloutCreator) tryCreateRollout(ctx context.Context, planID int64) {}// Check plan check status (must have no errors)- planCheckRun, err := rc.store.GetPlanCheckRun(ctx, planID)+ planCheckRun, err := rc.store.GetPlanCheckRun(ctx, plan.ProjectID, planID)if err != nil {slog.Error("failed to get plan check run for rollout creation",slog.Int("plan_id", int(planID)),diff --git a/backend/runner/taskrun/running_scheduler.go b/backend/runner/taskrun/running_scheduler.goindex 2d8472357c..d5fb180760 100644--- a/backend/runner/taskrun/running_scheduler.go+++ b/backend/runner/taskrun/running_scheduler.go@@ -10,6 +10,7 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/webhook"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/store"@@ -57,7 +58,7 @@ func (s *Scheduler) scheduleRunningTaskRuns(ctx context.Context) error {}for _, c := range claimed {- if err := s.executeTaskRun(ctx, c.TaskRunUID, c.TaskUID); err != nil {+ if err := s.executeTaskRun(ctx, c.ProjectID, c.TaskRunUID, c.TaskUID); err != nil {slog.Error("failed to execute task run", slog.Int("id", c.TaskRunUID), log.BBError(err))}}@@ -66,8 +67,8 @@ func (s *Scheduler) scheduleRunningTaskRuns(ctx context.Context) error {}// executeTaskRun executes a task run that is already in RUNNING status.-func (s *Scheduler) executeTaskRun(ctx context.Context, taskRunUID, taskUID int) error {- task, err := s.store.GetTaskByID(ctx, taskUID)+func (s *Scheduler) executeTaskRun(ctx context.Context, projectID string, taskRunUID, taskUID int) error {+ task, err := s.store.GetTaskByID(ctx, projectID, taskUID)if err != nil {return errors.Wrapf(err, "failed to get task")}@@ -83,9 +84,10 @@ func (s *Scheduler) executeTaskRun(ctx context.Context, taskRunUID, taskUID int)log.BBError(err),)taskRunStatusPatch := &store.TaskRunStatusPatch{- ID: taskRunUID,- Updater: "",- Status: storepb.TaskRun_FAILED,+ ID: taskRunUID,+ ProjectID: task.ProjectID,+ Updater: "",+ Status: storepb.TaskRun_FAILED,ResultProto: &storepb.TaskRunResult{Detail: err.Error(),},@@ -102,7 +104,7 @@ func (s *Scheduler) executeTaskRun(ctx context.Context, taskRunUID, taskUID int)}// Update started_at- if err := s.store.UpdateTaskRunStartAt(ctx, taskRunUID); err != nil {+ if err := s.store.UpdateTaskRunStartAt(ctx, task.ProjectID, taskRunUID); err != nil {return errors.Wrapf(err, "failed to update task run start at")}@@ -120,13 +122,14 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *stslog.Error("Task scheduler V2 runTaskRunOnce PANIC RECOVER", log.BBError(err), log.BBStack("panic-stack"))}}()+ taskRunRef := bus.TaskRunRef{ProjectID: task.ProjectID, ID: taskRunUID}defer func() {- s.bus.RunningTaskRunsCancelFunc.Delete(taskRunUID)+ s.bus.RunningTaskRunsCancelFunc.Delete(taskRunRef)}()driverCtx, cancel := context.WithCancel(ctx)defer cancel()- s.bus.RunningTaskRunsCancelFunc.Store(taskRunUID, cancel)+ s.bus.RunningTaskRunsCancelFunc.Store(taskRunRef, cancel)result, err := RunExecutorOnce(ctx, driverCtx, executor, task, taskRunUID)@@ -138,6 +141,7 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *st)taskRunStatusPatch := &store.TaskRunStatusPatch{ID: taskRunUID,+ ProjectID: task.ProjectID,Updater: "",Status: storepb.TaskRun_CANCELED,ResultProto: &storepb.TaskRunResult{},@@ -159,9 +163,10 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *stlog.BBError(err),)taskRunStatusPatch := &store.TaskRunStatusPatch{- ID: taskRunUID,- Updater: "",- Status: storepb.TaskRun_FAILED,+ ID: taskRunUID,+ ProjectID: task.ProjectID,+ Updater: "",+ Status: storepb.TaskRun_FAILED,ResultProto: &storepb.TaskRunResult{Detail: err.Error(),},@@ -175,12 +180,12 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *st}// Immediately try to send PIPELINE_FAILED webhook (HA-safe atomic claim)- claimed, err := s.store.ClaimPipelineFailureNotification(ctx, task.PlanID)+ claimed, err := s.store.ClaimPipelineFailureNotification(ctx, task.ProjectID, task.PlanID)if err != nil {slog.Error("failed to claim pipeline failure notification", log.BBError(err))} else if claimed {// Get plan and project for webhook- plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{UID: &task.PlanID})+ plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: task.ProjectID, UID: &task.PlanID})if err != nil || plan == nil {slog.Error("failed to get plan for failure webhook", log.BBError(err))} else {@@ -206,6 +211,7 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *st// Success casetaskRunStatusPatch := &store.TaskRunStatusPatch{ID: taskRunUID,+ ProjectID: task.ProjectID,Updater: "",Status: storepb.TaskRun_DONE,ResultProto: result,@@ -219,7 +225,7 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *st}// Signal to check if plan is complete and successful (may send PIPELINE_COMPLETED)- s.bus.PlanCompletionCheckChan <- task.PlanID+ s.bus.PlanCompletionCheckChan <- bus.PlanRef{ProjectID: task.ProjectID, PlanID: task.PlanID}}// validateTaskFreshness checks for state drift between task creation and execution time.@@ -242,7 +248,7 @@ func (s *Scheduler) validateTaskFreshness(ctx context.Context, task *store.TaskMreturn errors.Errorf("target database %q on instance %q has been deleted", task.GetDatabaseName(), task.InstanceID)}- plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{UID: &task.PlanID})+ plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: task.ProjectID, UID: &task.PlanID})if err != nil {return errors.Wrapf(err, "failed to get plan for drift validation")}diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.goindex 5e066516d2..c148a59670 100644--- a/backend/runner/taskrun/scheduler.go+++ b/backend/runner/taskrun/scheduler.go@@ -93,8 +93,8 @@ func (s *Scheduler) runTaskCompletionListener(ctx context.Context) {select {case <-ctx.Done():return- case planID := <-s.bus.PlanCompletionCheckChan:- s.checkPlanCompletion(ctx, planID)+ case ref := <-s.bus.PlanCompletionCheckChan:+ s.checkPlanCompletion(ctx, ref)}}}@@ -103,9 +103,16 @@ func (s *Scheduler) runTaskCompletionListener(ctx context.Context) {// If so, sends PIPELINE_COMPLETED webhook and auto-resolves issues for deferred rollout plans.// Deferred rollout plans (exportDataConfig, createDatabaseConfig) auto-resolve when tasks complete.// Called when tasks are marked DONE/SKIPPED, or when tasks are skipped/canceled via API.-func (s *Scheduler) checkPlanCompletion(ctx context.Context, planID int64) {+func (s *Scheduler) checkPlanCompletion(ctx context.Context, ref bus.PlanRef) {+ planID := ref.PlanID+ plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{ProjectID: ref.ProjectID, UID: &planID})+ if err != nil || plan == nil {+ slog.Error("failed to get plan for completion check", log.BBError(err))+ return+ }+// Get all tasks for this plan- tasks, err := s.store.ListTasks(ctx, &store.TaskFind{PlanID: &planID})+ tasks, err := s.store.ListTasks(ctx, &store.TaskFind{ProjectID: ref.ProjectID, PlanID: &planID})if err != nil {slog.Error("failed to list tasks for plan completion check", log.BBError(err))return@@ -128,7 +135,7 @@ func (s *Scheduler) checkPlanCompletion(ctx context.Context, planID int64) {}// All tasks complete and successful - try to claim completion notification- claimed, err := s.store.ClaimPipelineCompletionNotification(ctx, planID)+ claimed, err := s.store.ClaimPipelineCompletionNotification(ctx, ref.ProjectID, planID)if err != nil {slog.Error("failed to claim pipeline completion notification", log.BBError(err))return@@ -137,13 +144,6 @@ func (s *Scheduler) checkPlanCompletion(ctx context.Context, planID int64) {return // Already sent}- // Get plan and project for webhook- plan, err := s.store.GetPlan(ctx, &store.FindPlanMessage{UID: &planID})- if err != nil || plan == nil {- slog.Error("failed to get plan for completion webhook", log.BBError(err))- return- }-project, err := s.store.GetProject(ctx, &store.FindProjectMessage{ResourceID: &plan.ProjectID})if err != nil || project == nil {slog.Error("failed to get project for completion webhook", log.BBError(err))diff --git a/backend/store/plan.go b/backend/store/plan.goindex 32cf3a3bf9..a313ed24eb 100644--- a/backend/store/plan.go+++ b/backend/store/plan.go@@ -35,9 +35,8 @@ type PlanMessage struct {// FindPlanMessage is the message to find a plan.type FindPlanMessage struct {- UID *int64- ProjectID *string- ProjectIDs *[]string+ UID *int64+ ProjectID stringHasRollout *bool@@ -49,7 +48,9 @@ type FindPlanMessage struct {// UpdatePlanMessage is the message to update a plan.type UpdatePlanMessage struct {- UID int64+ UID int64+ ProjectID string+Name *stringDescription *string// Config replaces the entire plan config.@@ -123,8 +124,8 @@ func (s *Store) ListPlans(ctx context.Context, find *FindPlanMessage) ([]*PlanMeplan.deletedFROM planLEFT JOIN issue on plan.id = issue.plan_id- WHERE TRUE- `)+ WHERE plan.project = ?+ `, find.ProjectID)if filterQ := find.FilterQ; filterQ != nil {q.And("?", filterQ)@@ -132,16 +133,6 @@ func (s *Store) ListPlans(ctx context.Context, find *FindPlanMessage) ([]*PlanMeif v := find.UID; v != nil {q.And("plan.id = ?", *v)}- if v := find.ProjectID; v != nil {- q.And("plan.project = ?", *v)- }- if v := find.ProjectIDs; v != nil {- if len(*v) == 0 {- q.And("FALSE")- } else {- q.And("plan.project = ANY(?)", *v)- }- }if v := find.HasRollout; v != nil {if *v {q.And("plan.config->>'hasRollout' = ?", "true")@@ -226,8 +217,8 @@ func (s *Store) UpdatePlan(ctx context.Context, patch *UpdatePlanMessage) (*Planargs = append(args, config)}- args = append(args, patch.UID)- q := qb.Q().Space(fmt.Sprintf("UPDATE plan SET %s WHERE id = ? RETURNING id, creator, created_at, updated_at, project, name, description, config, deleted", strings.Join(set, ", ")), args...)+ args = append(args, patch.UID, patch.ProjectID)+ q := qb.Q().Space(fmt.Sprintf("UPDATE plan SET %s WHERE id = ? AND project = ? RETURNING id, creator, created_at, updated_at, project, name, description, config, deleted", strings.Join(set, ", ")), args...)query, finalArgs, err := q.ToSQL()if err != nil {diff --git a/backend/store/plan_check_run.go b/backend/store/plan_check_run.goindex e8caaee817..76141efe9a 100644--- a/backend/store/plan_check_run.go+++ b/backend/store/plan_check_run.go@@ -34,7 +34,8 @@ type PlanCheckRunMessage struct {CreatedAt time.TimeUpdatedAt time.Time- PlanUID int64+ ProjectID string+ PlanUID int64Status PlanCheckRunStatusResult *storepb.PlanCheckRunResult@@ -42,6 +43,7 @@ type PlanCheckRunMessage struct {// FindPlanCheckRunMessage is the message for finding plan check runs.type FindPlanCheckRunMessage struct {+ ProjectID stringPlanUID *int64PlanUIDs *[]int64UIDs *[]int@@ -58,14 +60,14 @@ func (s *Store) CreatePlanCheckRun(ctx context.Context, create *PlanCheckRunMess}query := `- INSERT INTO plan_check_run (plan_id, status, result)- VALUES ($1, $2, $3)- ON CONFLICT (plan_id) DO UPDATE SET+ INSERT INTO plan_check_run (project, plan_id, status, result)+ VALUES ($1, $2, $3, $4)+ ON CONFLICT (project, plan_id) DO UPDATE SETstatus = EXCLUDED.status,result = EXCLUDED.result,updated_at = now()`- if _, err := s.GetDB().ExecContext(ctx, query, create.PlanUID, PlanCheckRunStatusAvailable, result); err != nil {+ if _, err := s.GetDB().ExecContext(ctx, query, create.ProjectID, create.PlanUID, PlanCheckRunStatusAvailable, result); err != nil {return errors.Wrapf(err, "failed to upsert plan check run")}return nil@@ -74,15 +76,16 @@ func (s *Store) CreatePlanCheckRun(ctx context.Context, create *PlanCheckRunMess// ListPlanCheckRuns returns a list of plan check runs based on find.func (s *Store) ListPlanCheckRuns(ctx context.Context, find *FindPlanCheckRunMessage) ([]*PlanCheckRunMessage, error) {q := qb.Q().Space(`-SELECT- plan_check_run.id,- plan_check_run.created_at,- plan_check_run.updated_at,- plan_check_run.plan_id,- plan_check_run.status,- plan_check_run.result-FROM plan_check_run-WHERE TRUE`)+ SELECT+ plan_check_run.id,+ plan_check_run.created_at,+ plan_check_run.updated_at,+ plan_check_run.project,+ plan_check_run.plan_id,+ plan_check_run.status,+ plan_check_run.result+ FROM plan_check_run+ WHERE plan_check_run.project = ?`, find.ProjectID)if v := find.PlanUID; v != nil {q.Space("AND plan_check_run.plan_id = ?", *v)}@@ -123,6 +126,7 @@ WHERE TRUE`)&planCheckRun.UID,&planCheckRun.CreatedAt,&planCheckRun.UpdatedAt,+ &planCheckRun.ProjectID,&planCheckRun.PlanUID,&planCheckRun.Status,&result,@@ -143,8 +147,8 @@ WHERE TRUE`)}// GetPlanCheckRun returns the plan check run for a plan.-func (s *Store) GetPlanCheckRun(ctx context.Context, planUID int64) (*PlanCheckRunMessage, error) {- runs, err := s.ListPlanCheckRuns(ctx, &FindPlanCheckRunMessage{PlanUID: &planUID})+func (s *Store) GetPlanCheckRun(ctx context.Context, projectID string, planUID int64) (*PlanCheckRunMessage, error) {+ runs, err := s.ListPlanCheckRuns(ctx, &FindPlanCheckRunMessage{ProjectID: projectID, PlanUID: &planUID})if err != nil {return nil, err}@@ -155,18 +159,18 @@ func (s *Store) GetPlanCheckRun(ctx context.Context, planUID int64) (*PlanCheckR}// UpdatePlanCheckRun updates a plan check run.-func (s *Store) UpdatePlanCheckRun(ctx context.Context, status PlanCheckRunStatus, result *storepb.PlanCheckRunResult, uid int) error {+func (s *Store) UpdatePlanCheckRun(ctx context.Context, projectID string, status PlanCheckRunStatus, result *storepb.PlanCheckRunResult, uid int) error {resultBytes, err := protojson.Marshal(result)if err != nil {return errors.Wrapf(err, "failed to marshal result %v", result)}q := qb.Q().Space(`- UPDATE plan_check_run- SET- updated_at = ?,- status = ?,- result = ?- WHERE id = ?`, time.Now(), status, resultBytes, uid)+ UPDATE plan_check_run+ SET+ updated_at = ?,+ status = ?,+ result = ?+ WHERE id = ? AND project = ?`, time.Now(), status, resultBytes, uid, projectID)query, args, err := q.ToSQL()if err != nil {return errors.Wrapf(err, "failed to build sql")@@ -178,13 +182,13 @@ func (s *Store) UpdatePlanCheckRun(ctx context.Context, status PlanCheckRunStatu}// BatchCancelPlanCheckRuns updates the status of planCheckRuns to CANCELED.-func (s *Store) BatchCancelPlanCheckRuns(ctx context.Context, planCheckRunUIDs []int) error {+func (s *Store) BatchCancelPlanCheckRuns(ctx context.Context, projectID string, planCheckRunUIDs []int) error {q := qb.Q().Space(`UPDATE plan_check_runSETstatus = ?,updated_at = ?- WHERE id = ANY(?)`, PlanCheckRunStatusCanceled, time.Now(), planCheckRunUIDs)+ WHERE id = ANY(?) AND project = ?`, PlanCheckRunStatusCanceled, time.Now(), planCheckRunUIDs, projectID)query, args, err := q.ToSQL()if err != nil {return errors.Wrapf(err, "failed to build sql")@@ -197,8 +201,9 @@ func (s *Store) BatchCancelPlanCheckRuns(ctx context.Context, planCheckRunUIDs [// ClaimedPlanCheckRun represents a plan check run that was atomically claimed.type ClaimedPlanCheckRun struct {- UID int- PlanUID int64+ UID int+ ProjectID string+ PlanUID int64}// FailStalePlanCheckRuns marks RUNNING plan check runs as FAILED if they have been running@@ -238,7 +243,7 @@ func (s *Store) ClaimAvailablePlanCheckRuns(ctx context.Context) ([]*ClaimedPlanWHERE status = ?FOR UPDATE SKIP LOCKED)- RETURNING id, plan_id+ RETURNING id, project, plan_id`, PlanCheckRunStatusRunning, PlanCheckRunStatusAvailable)query, args, err := q.ToSQL()@@ -255,7 +260,7 @@ func (s *Store) ClaimAvailablePlanCheckRuns(ctx context.Context) ([]*ClaimedPlanvar claimed []*ClaimedPlanCheckRunfor rows.Next() {var c ClaimedPlanCheckRun- if err := rows.Scan(&c.UID, &c.PlanUID); err != nil {+ if err := rows.Scan(&c.UID, &c.ProjectID, &c.PlanUID); err != nil {return nil, err}claimed = append(claimed, &c)diff --git a/backend/store/plan_webhook_delivery.go b/backend/store/plan_webhook_delivery.goindex 30836e531b..1782a41315 100644--- a/backend/store/plan_webhook_delivery.go+++ b/backend/store/plan_webhook_delivery.go@@ -7,25 +7,26 @@ import (// ResetPlanWebhookDelivery deletes the webhook delivery record for a plan.// Called when user clicks BatchRunTasks to enable new notifications on retry.-func (s *Store) ResetPlanWebhookDelivery(ctx context.Context, planID int64) error {- query := `DELETE FROM plan_webhook_delivery WHERE plan_id = $1`- _, err := s.GetDB().ExecContext(ctx, query, planID)+func (s *Store) ResetPlanWebhookDelivery(ctx context.Context, projectID string, planID int64) error {+ query := `DELETE FROM plan_webhook_delivery WHERE project = $1 AND plan_id = $2`+ _, err := s.GetDB().ExecContext(ctx, query, projectID, planID)return err}// ClaimPipelineFailureNotification attempts to claim the right to send PIPELINE_FAILED webhook.// Returns true if claimed (should send), false if already sent or claimed by another replica.// HA-safe: PRIMARY KEY constraint prevents duplicate sends across replicas.-func (s *Store) ClaimPipelineFailureNotification(ctx context.Context, planID int64) (bool, error) {+func (s *Store) ClaimPipelineFailureNotification(ctx context.Context, projectID string, planID int64) (bool, error) {+ // RETURNING is needed to distinguish insert-success (row returned) from ON CONFLICT DO NOTHING (sql.ErrNoRows).query := `- INSERT INTO plan_webhook_delivery (plan_id, event_type)- VALUES ($1, 'PIPELINE_FAILED')- ON CONFLICT (plan_id) DO NOTHING+ INSERT INTO plan_webhook_delivery (project, plan_id, event_type)+ VALUES ($1, $2, 'PIPELINE_FAILED')+ ON CONFLICT (project, plan_id) DO NOTHINGRETURNING plan_id`var id int64- err := s.GetDB().QueryRowContext(ctx, query, planID).Scan(&id)+ err := s.GetDB().QueryRowContext(ctx, query, projectID, planID).Scan(&id)if err == sql.ErrNoRows {return false, nil // Already exists}@@ -35,16 +36,16 @@ func (s *Store) ClaimPipelineFailureNotification(ctx context.Context, planID int// ClaimPipelineCompletionNotification attempts to claim the right to send PIPELINE_COMPLETED webhook.// Returns true if claimed (should send), false if already sent or claimed by another replica.// HA-safe: PRIMARY KEY constraint prevents duplicate sends across replicas.-func (s *Store) ClaimPipelineCompletionNotification(ctx context.Context, planID int64) (bool, error) {+func (s *Store) ClaimPipelineCompletionNotification(ctx context.Context, projectID string, planID int64) (bool, error) {query := `- INSERT INTO plan_webhook_delivery (plan_id, event_type)- VALUES ($1, 'PIPELINE_COMPLETED')- ON CONFLICT (plan_id) DO NOTHING+ INSERT INTO plan_webhook_delivery (project, plan_id, event_type)+ VALUES ($1, $2, 'PIPELINE_COMPLETED')+ ON CONFLICT (project, plan_id) DO NOTHINGRETURNING plan_id`var id int64- err := s.GetDB().QueryRowContext(ctx, query, planID).Scan(&id)+ err := s.GetDB().QueryRowContext(ctx, query, projectID, planID).Scan(&id)if err == sql.ErrNoRows {return false, nil // Already exists}diff --git a/backend/store/signal.go b/backend/store/signal.goindex 5e1ed9b7e0..bf6438335b 100644--- a/backend/store/signal.go+++ b/backend/store/signal.go@@ -13,10 +13,11 @@ import (const SignalChannel = "bytebase_signal"// SendSignal sends a notification to the bytebase_signal channel.-func (s *Store) SendSignal(ctx context.Context, signalType storepb.Signal_Type, uid int32) error {+func (s *Store) SendSignal(ctx context.Context, signalType storepb.Signal_Type, projectID string, uid int32) error {payload, err := protojson.Marshal(&storepb.Signal{- Type: signalType,- Uid: uid,+ Type: signalType,+ Uid: uid,+ Project: projectID,})if err != nil {return errors.Wrap(err, "failed to marshal signal payload")diff --git a/backend/store/task.go b/backend/store/task.goindex 7d8f7b4a90..39a6878a3a 100644--- a/backend/store/task.go+++ b/backend/store/task.go@@ -19,6 +19,7 @@ type TaskMessage struct {ID int// Related fields+ ProjectID stringPlanID int64InstanceID stringEnvironment string // The environment ID (was stage_id). Could be empty if the task does not have an environment.@@ -52,6 +53,7 @@ type TaskFind struct {IDs *[]int// Related fields+ ProjectID stringPlanID *int64PlanIDs *[]int64Environment *string@@ -65,8 +67,8 @@ type TaskFind struct {}// GetTaskByID gets a task by ID.-func (s *Store) GetTaskByID(ctx context.Context, id int) (*TaskMessage, error) {- tasks, err := s.ListTasks(ctx, &TaskFind{ID: &id})+func (s *Store) GetTaskByID(ctx context.Context, projectID string, id int) (*TaskMessage, error) {+ tasks, err := s.ListTasks(ctx, &TaskFind{ProjectID: projectID, ID: &id})if err != nil {return nil, errors.Wrapf(err, "failed to get Task with ID %d", id)}@@ -80,7 +82,7 @@ func (s *Store) GetTaskByID(ctx context.Context, id int) (*TaskMessage, error) {// Get a blocking task in the pipeline.// A task is blocked by a task with a smaller schema version within the same pipeline.-func (s *Store) FindBlockingTaskByVersion(ctx context.Context, planUID int64, instanceID, databaseName string, version string) (*int, error) {+func (s *Store) FindBlockingTaskByVersion(ctx context.Context, projectID string, planUID int64, instanceID, databaseName string, version string) (*int, error) {myVersion, err := model.NewVersion(version)if err != nil {return nil, err@@ -103,12 +105,12 @@ func (s *Store) FindBlockingTaskByVersion(ctx context.Context, planUID int64, in), 'NOT_STARTED') AS status) AS latest_task_run ON TRUE- WHERE task.plan_id = ? AND task.instance = ? AND task.db_name = ?+ WHERE task.project = ? AND task.plan_id = ? AND task.instance = ? AND task.db_name = ?AND task.payload->>'schemaVersion' IS NOT NULLAND (task.payload->>'skipped')::BOOLEAN IS NOT TRUEAND latest_task_run.status != 'DONE'AND COALESCE(issue.status, 'OPEN') = 'OPEN'- ORDER BY task.id ASC`, planUID, instanceID, databaseName)+ ORDER BY task.id ASC`, projectID, planUID, instanceID, databaseName)query, args, err := q.ToSQL()if err != nil {return nil, errors.Wrapf(err, "failed to build sql")@@ -141,6 +143,7 @@ func (s *Store) FindBlockingTaskByVersion(ctx context.Context, planUID int64, in// createTasksTx creates tasks in a transaction.func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMessage) ([]*TaskMessage, error) {var (+ projects []stringplanIDs []int64instances []stringdatabases []*string@@ -156,6 +159,7 @@ func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMeif err != nil {return nil, errors.Wrapf(err, "failed to marshal payload")}+ projects = append(projects, create.ProjectID)planIDs = append(planIDs, create.PlanID)instances = append(instances, create.InstanceID)databases = append(databases, create.DatabaseName)@@ -166,6 +170,7 @@ func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMeq := qb.Q().Space(`INSERT INTO task (+ project,plan_id,instance,db_name,@@ -173,13 +178,15 @@ func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMetype,payload) SELECT+ unnest(CAST(? AS TEXT[])),unnest(CAST(? AS BIGINT[])),unnest(CAST(? AS TEXT[])),unnest(CAST(? AS TEXT[])),unnest(CAST(? AS TEXT[])),unnest(CAST(? AS TEXT[])),unnest(CAST(? AS JSONB[]))- RETURNING id, plan_id, instance, db_name, environment, type, payload`,+ RETURNING id, project, plan_id, instance, db_name, environment, type, payload`,+ projects,planIDs,instances,databases,@@ -204,6 +211,7 @@ func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMevar typeString stringif err := rows.Scan(&task.ID,+ &task.ProjectID,&task.PlanID,&task.InstanceID,&task.DatabaseName,@@ -236,6 +244,7 @@ func (*Store) listTasksImpl(ctx context.Context, txn *sql.Tx, find *TaskFind) ([q := qb.Q().Space(`SELECTtask.id,+ task.project,task.plan_id,task.instance,task.db_name,@@ -256,7 +265,7 @@ func (*Store) listTasksImpl(ctx context.Context, txn *sql.Tx, find *TaskFind) ([ORDER BY task_run.id DESCLIMIT 1) AS latest_task_run ON TRUE- WHERE TRUE`, storepb.TaskRun_NOT_STARTED.String())+ WHERE task.project = ?`, storepb.TaskRun_NOT_STARTED.String(), find.ProjectID)if v := find.ID; v != nil {q.Space("AND task.id = ?", *v)}@@ -312,6 +321,7 @@ func (*Store) listTasksImpl(ctx context.Context, txn *sql.Tx, find *TaskFind) ([var typeString stringif err := rows.Scan(&task.ID,+ &task.ProjectID,&task.PlanID,&task.InstanceID,&task.DatabaseName,@@ -367,11 +377,11 @@ func (s *Store) ListTasks(ctx context.Context, find *TaskFind) ([]*TaskMessage,}// BatchSkipTasks batch skip tasks.-func (s *Store) BatchSkipTasks(ctx context.Context, taskUIDs []int, comment string) error {+func (s *Store) BatchSkipTasks(ctx context.Context, projectID string, taskUIDs []int, comment string) error {q := qb.Q().Space(`UPDATE taskSET payload = payload || jsonb_build_object('skipped', ?::BOOLEAN) || jsonb_build_object('skippedReason', ?::TEXT)- WHERE id = ANY(?)`, true, comment, taskUIDs)+ WHERE id = ANY(?) AND project = ?`, true, comment, taskUIDs, projectID)query, args, err := q.ToSQL()if err != nil {return errors.Wrapf(err, "failed to build sql")@@ -385,7 +395,7 @@ func (s *Store) BatchSkipTasks(ctx context.Context, taskUIDs []int, comment stri}// CreateTasks creates tasks for a plan.-func (s *Store) CreateTasks(ctx context.Context, planUID int64, tasks []*TaskMessage) ([]*TaskMessage, error) {+func (s *Store) CreateTasks(ctx context.Context, projectID string, planUID int64, tasks []*TaskMessage) ([]*TaskMessage, error) {tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, errors.Wrapf(err, "failed to begin tx")@@ -394,7 +404,8 @@ func (s *Store) CreateTasks(ctx context.Context, planUID int64, tasks []*TaskMes// Check existing tasks to avoid duplicatesexistingTasks, err := s.listTasksImpl(ctx, tx, &TaskFind{- PlanID: &planUID,+ ProjectID: projectID,+ PlanID: &planUID,})if err != nil {return nil, errors.Wrapf(err, "failed to list existing tasks")@@ -432,6 +443,7 @@ func (s *Store) CreateTasks(ctx context.Context, planUID int64, tasks []*TaskMesif _, ok := createdTasks[k]; ok {continue}+ taskCreate.ProjectID = projectIDtaskCreate.PlanID = planUIDtaskCreateList = append(taskCreateList, taskCreate)}diff --git a/backend/store/task_run.go b/backend/store/task_run.goindex 3554057f99..18e297f4d0 100644--- a/backend/store/task_run.go+++ b/backend/store/task_run.go@@ -36,6 +36,7 @@ type TaskRunMessage struct {type FindTaskRunMessage struct {UID *intUIDs *[]int+ ProjectID stringTaskUID *intEnvironment *stringPlanUID *int64@@ -44,7 +45,8 @@ type FindTaskRunMessage struct {// TaskRunStatusPatch is the API message for patching a task run.type TaskRunStatusPatch struct {- ID int+ ID int+ ProjectID string// Standard fieldsUpdater string@@ -70,13 +72,11 @@ func (s *Store) ListTaskRuns(ctx context.Context, find *FindTaskRunMessage) ([]*task_run.payload,task.plan_id,task.environment,- project.resource_id+ task_run.projectFROM task_runLEFT JOIN task ON task.id = task_run.task_id- LEFT JOIN plan ON plan.id = task.plan_id- LEFT JOIN project ON project.resource_id = plan.project- WHERE TRUE- `)+ WHERE task_run.project = ?+ `, find.ProjectID)if v := find.UID; v != nil {q.And("task_run.id = ?", *v)@@ -188,8 +188,8 @@ func (s *Store) GetTaskRunV1(ctx context.Context, find *FindTaskRunMessage) (*Ta}// GetTaskRunByUID gets a task run by uid.-func (s *Store) GetTaskRunByUID(ctx context.Context, uid int) (*TaskRunMessage, error) {- return s.GetTaskRunV1(ctx, &FindTaskRunMessage{UID: &uid})+func (s *Store) GetTaskRunByUID(ctx context.Context, projectID string, uid int) (*TaskRunMessage, error) {+ return s.GetTaskRunV1(ctx, &FindTaskRunMessage{ProjectID: projectID, UID: &uid})}// UpdateTaskRunStatus updates task run status.@@ -206,7 +206,7 @@ func (s *Store) UpdateTaskRunStatus(ctx context.Context, patch *TaskRunStatusPat}// Get the plan ID for cache invalidation- q := qb.Q().Space("SELECT plan_id FROM task WHERE id = ?", taskRun.TaskUID)+ q := qb.Q().Space("SELECT plan_id FROM task WHERE id = ? AND project = ?", taskRun.TaskUID, patch.ProjectID)sql, args, err := q.ToSQL()if err != nil {return nil, errors.Wrapf(err, "failed to build sql")@@ -227,6 +227,7 @@ func (s *Store) UpdateTaskRunStatus(ctx context.Context, patch *TaskRunStatusPattype ClaimedTaskRun struct {TaskRunUID intTaskUID int+ ProjectID string}// ClaimAvailableTaskRuns atomically claims all AVAILABLE task runs by updating them to RUNNING@@ -241,7 +242,7 @@ func (s *Store) ClaimAvailableTaskRuns(ctx context.Context, replicaID string) ([WHERE task_run.status = ?FOR UPDATE SKIP LOCKED)- RETURNING id, task_id+ RETURNING id, task_id, project`, storepb.TaskRun_RUNNING.String(), replicaID, storepb.TaskRun_AVAILABLE.String())query, args, err := q.ToSQL()@@ -258,7 +259,7 @@ func (s *Store) ClaimAvailableTaskRuns(ctx context.Context, replicaID string) ([var claimed []*ClaimedTaskRunfor rows.Next() {var c ClaimedTaskRun- if err := rows.Scan(&c.TaskRunUID, &c.TaskUID); err != nil {+ if err := rows.Scan(&c.TaskRunUID, &c.TaskUID, &c.ProjectID); err != nil {return nil, err}claimed = append(claimed, &c)@@ -269,14 +270,14 @@ func (s *Store) ClaimAvailableTaskRuns(ctx context.Context, replicaID string) ([return claimed, nil}-func (s *Store) UpdateTaskRunStartAt(ctx context.Context, taskRunID int) error {+func (s *Store) UpdateTaskRunStartAt(ctx context.Context, projectID string, taskRunID int) error {// Get the pipeline ID for cache invalidationq := qb.Q().Space(`UPDATE task_runSET started_at = now(), updated_at = now()- WHERE id = ?- RETURNING (SELECT plan_id FROM task WHERE task.id = task_run.task_id)- `, taskRunID)+ WHERE id = ? AND project = ?+ RETURNING (SELECT plan_id FROM task WHERE task.id = task_run.task_id AND task.project = task_run.project)+ `, taskRunID, projectID)query, args, err := q.ToSQL()if err != nil {@@ -302,9 +303,11 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creator string, creatvar taskUIDs []intvar runAts []*time.Time+ var projects []stringfor _, create := range creates {taskUIDs = append(taskUIDs, create.TaskUID)runAts = append(runAts, create.RunAt)+ projects = append(projects, create.ProjectID)}// Convert empty string to NULL for system-created task runs@@ -335,6 +338,7 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creator string, creatq := qb.Q().Space(`INSERT INTO task_run (creator,+ project,task_id,run_at,attempt,@@ -343,6 +347,7 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creator string, creat)SELECT?,+ tasks.project,tasks.task_id,tasks.run_at,COALESCE((SELECT MAX(attempt) + 1 FROM task_run WHERE task_run.task_id = tasks.task_id), 0) as attempt,@@ -350,6 +355,7 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creator string, creat?FROM (SELECT+ unnest(CAST(? AS TEXT[])) AS project,unnest(CAST(? AS INTEGER[])) AS task_id,unnest(CAST(? AS TIMESTAMPTZ[])) AS run_at) tasks@@ -358,8 +364,8 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creator string, creatWHERE task_run.task_id = tasks.task_idAND task_run.status IN (?, ?, ?, ?))- ON CONFLICT (task_id, attempt) DO NOTHING- `, creatorPtr, storepb.TaskRun_PENDING.String(), payloadStr, taskUIDs, runAts,+ ON CONFLICT (project, task_id, attempt) DO NOTHING+ `, creatorPtr, storepb.TaskRun_PENDING.String(), payloadStr, projects, taskUIDs, runAts,storepb.TaskRun_PENDING.String(), storepb.TaskRun_AVAILABLE.String(), storepb.TaskRun_RUNNING.String(), storepb.TaskRun_DONE.String())query, args, err := q.ToSQL()@@ -393,7 +399,7 @@ func (*Store) patchTaskRunStatusImpl(ctx context.Context, txn *sql.Tx, patch *Ta}q := qb.Q().Space("UPDATE task_run SET ?", set).- Space("WHERE id = ?", patch.ID).+ Space("WHERE id = ? AND project = ?", patch.ID, patch.ProjectID).Space("RETURNING id, creator, created_at, updated_at, task_id, status, result")query, args, err := q.ToSQL()@@ -436,15 +442,15 @@ func (*Store) patchTaskRunStatusImpl(ctx context.Context, txn *sql.Tx, patch *Ta}// BatchCancelTaskRuns updates the status of taskRuns to CANCELED.-func (s *Store) BatchCancelTaskRuns(ctx context.Context, taskRunIDs []int) error {+func (s *Store) BatchCancelTaskRuns(ctx context.Context, projectID string, taskRunIDs []int) error {if len(taskRunIDs) == 0 {return nil}q := qb.Q().Space(`UPDATE task_runSET status = ?, updated_at = now()- WHERE id = ANY(?)- `, storepb.TaskRun_CANCELED.String(), taskRunIDs)+ WHERE id = ANY(?) AND project = ?+ `, storepb.TaskRun_CANCELED.String(), taskRunIDs, projectID)query, args, err := q.ToSQL()if err != nil {@@ -490,7 +496,7 @@ func (s *Store) FailStaleTaskRuns(ctx context.Context, stalenessThreshold time.D}// UpdateTaskRunPayload updates the payload column for a task run.-func (s *Store) UpdateTaskRunPayload(ctx context.Context, taskRunID int, payload *storepb.TaskRunPayload) error {+func (s *Store) UpdateTaskRunPayload(ctx context.Context, projectID string, taskRunID int, payload *storepb.TaskRunPayload) error {payloadBytes, err := protojson.Marshal(payload)if err != nil {return errors.Wrapf(err, "failed to marshal task run payload")@@ -503,8 +509,8 @@ func (s *Store) UpdateTaskRunPayload(ctx context.Context, taskRunID int, payloadq := qb.Q().Space(`UPDATE task_runSET payload = ?, updated_at = now()- WHERE id = ?- `, payloadStr, taskRunID)+ WHERE id = ? AND project = ?+ `, payloadStr, taskRunID, projectID)query, args, err := q.ToSQL()if err != nil {@@ -516,3 +522,86 @@ func (s *Store) UpdateTaskRunPayload(ctx context.Context, taskRunID int, payload}return nil}++// ListTaskRunsByStatus lists task runs by status across all projects.+// This is for system schedulers that need cross-project queries.+func (s *Store) ListTaskRunsByStatus(ctx context.Context, statuses []storepb.TaskRun_Status) ([]*TaskRunMessage, error) {+ var statusStrings []string+ for _, status := range statuses {+ statusStrings = append(statusStrings, status.String())+ }+ q := qb.Q().Space(`+ SELECT+ task_run.id,+ task_run.creator,+ task_run.created_at,+ task_run.updated_at,+ task_run.task_id,+ task_run.status,+ task_run.started_at,+ task_run.run_at,+ task_run.result,+ task_run.payload,+ task.plan_id,+ task.environment,+ task_run.project+ FROM task_run+ LEFT JOIN task ON task.id = task_run.task_id+ WHERE task_run.status = ANY(?)+ ORDER BY task_run.id ASC+ `, statusStrings)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := s.GetDB().QueryContext(ctx, query, args...)+ if err != nil {+ return nil, err+ }+ defer rows.Close()++ var taskRuns []*TaskRunMessage+ for rows.Next() {+ var taskRun TaskRunMessage+ var resultJSON string+ var payloadJSON string+ var statusString string+ if err := rows.Scan(+ &taskRun.ID,+ &taskRun.CreatorEmail,+ &taskRun.CreatedAt,+ &taskRun.UpdatedAt,+ &taskRun.TaskUID,+ &statusString,+ &taskRun.StartedAt,+ &taskRun.RunAt,+ &resultJSON,+ &payloadJSON,+ &taskRun.PlanUID,+ &taskRun.Environment,+ &taskRun.ProjectID,+ ); err != nil {+ return nil, err+ }+ if statusValue, ok := storepb.TaskRun_Status_value[statusString]; ok {+ taskRun.Status = storepb.TaskRun_Status(statusValue)+ }+ resultProto := &storepb.TaskRunResult{}+ if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(resultJSON), resultProto); err != nil {+ return nil, errors.Wrapf(err, "failed to unmarshal result")+ }+ taskRun.ResultProto = resultProto+ payloadProto := &storepb.TaskRunPayload{}+ if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(payloadJSON), payloadProto); err != nil {+ return nil, errors.Wrapf(err, "failed to unmarshal payload")+ }+ taskRun.PayloadProto = payloadProto+ taskRuns = append(taskRuns, &taskRun)+ }+ if err := rows.Err(); err != nil {+ return nil, err+ }+ return taskRuns, nil+}diff --git a/backend/store/task_run_log.go b/backend/store/task_run_log.goindex 0001ce3404..cbe7b40662 100644--- a/backend/store/task_run_log.go+++ b/backend/store/task_run_log.go@@ -19,13 +19,13 @@ type TaskRunLog struct {Payload *storepb.TaskRunLog}-func (s *Store) CreateTaskRunLogS(ctx context.Context, taskRunUID int, t time.Time, replicaID string, e *storepb.TaskRunLog) {- if err := s.CreateTaskRunLog(ctx, taskRunUID, t, replicaID, e); err != nil {+func (s *Store) CreateTaskRunLogS(ctx context.Context, projectID string, taskRunUID int, t time.Time, replicaID string, e *storepb.TaskRunLog) {+ if err := s.CreateTaskRunLog(ctx, projectID, taskRunUID, t, replicaID, e); err != nil {slog.Error("failed to create task run log", log.BBError(err))}}-func (s *Store) CreateTaskRunLog(ctx context.Context, taskRunUID int, t time.Time, replicaID string, e *storepb.TaskRunLog) error {+func (s *Store) CreateTaskRunLog(ctx context.Context, projectID string, taskRunUID int, t time.Time, replicaID string, e *storepb.TaskRunLog) error {e.ReplicaId = replicaIDp, err := protojson.Marshal(e)if err != nil {@@ -34,15 +34,17 @@ func (s *Store) CreateTaskRunLog(ctx context.Context, taskRunUID int, t time.Timq := qb.Q().Space(`INSERT INTO task_run_log (+ project,task_run_id,created_at,payload) VALUES (+ ?,?,?,?)- `, taskRunUID, t, p)+ `, projectID, taskRunUID, t, p)sql, args, err := q.ToSQL()if err != nil {@@ -55,15 +57,15 @@ func (s *Store) CreateTaskRunLog(ctx context.Context, taskRunUID int, t time.Timreturn nil}-func (s *Store) ListTaskRunLogs(ctx context.Context, taskRunUID int) ([]*TaskRunLog, error) {+func (s *Store) ListTaskRunLogs(ctx context.Context, projectID string, taskRunUID int) ([]*TaskRunLog, error) {q := qb.Q().Space(`SELECTcreated_at,payloadFROM task_run_log- WHERE task_run_log.task_run_id = ?+ WHERE task_run_log.project = ? AND task_run_log.task_run_id = ?ORDER BY created_at- `, taskRunUID)+ `, projectID, taskRunUID)sql, args, err := q.ToSQL()if err != nil {diff --git a/docs/plans/saas/01.prepare-for-saas.md b/docs/plans/saas/01.prepare-for-saas.mdindex d16e6216e8..ee0739b3bf 100644--- a/docs/plans/saas/01.prepare-for-saas.md+++ b/docs/plans/saas/01.prepare-for-saas.md@@ -264,7 +264,7 @@ For SaaS, tables need different ID strategies depending on their scope:| `oauth2_authorization_code` | `code text` | Workspace (via client) | Add `workspace_id` || `oauth2_refresh_token` | `token_hash text` | Workspace (via client) | Add `workspace_id` || **Project-scoped (per-project auto-increment id)** | | | |-| `project_webhook` | `serial` | Project | Per-project auto-increment id |+| `project_webhook` | `serial` | Project | Drop `id`, use `resource_id` as PK (no composite PK needed — `id` is unused, all lookups use `resource_id`) || `plan` | `bigserial` | Project | Per-project auto-increment id || `issue` | `serial` | Project | Per-project auto-increment id || `worksheet` | `serial` | Project | Per-project auto-increment id |@@ -273,7 +273,7 @@ For SaaS, tables need different ID strategies depending on their scope:| `task` | `serial` | Project (via plan) | Add project ref || `task_run` | `serial` | Project (via task) | Add project ref || `task_run_log` | `(task_run_id, created_at)` | Project (via task_run) | Add project ref |-| `issue_comment` | `bigserial` | Project (via issue) | Add project ref |+| `issue_comment` | `bigserial` | Project (via issue) | Drop `id`, use `resource_id` as PK (no composite PK needed — `id` is unused, all lookups use `resource_id`) || `worksheet_organizer` | `serial` | Project (via worksheet) | Add project ref || `db_group` | `(project, resource_id)` | Project | None (already has project) || `release` | `(project, train, iteration)` | Project | None (already has project) |@@ -350,7 +350,8 @@ issue(id) → issue(project, id)-- is nullable and references plan(id). After Step 2a, need composite FK.-- UNIQUE(plan_id) → UNIQUE(project, plan_id)-issue_comment(id) → issue_comment(project, id)+issue_comment: drop `id` column, use `resource_id` as PK+ -- `id` is unused: no FK references it, all lookups use `resource_id`-- add `project` column, backfill from issue-- issue_comment.issue_id FK → (project, issue_id) REFERENCES issue(project, id)```@@ -372,8 +373,9 @@ worksheet_organizer(worksheet_id, principal) → worksheet_organizer(project, wo**Step 2d: `project_webhook`**```-project_webhook(id) → project_webhook(project, id)- -- already has `project` column, just change PK+project_webhook: drop `id` column, use `resource_id` as PK+ -- `id` is unused: no FK references it, all lookups use `resource_id`+ -- already has `project` column, no other schema changes needed```**Step 2e: Instance-scoped tables**@@ -404,7 +406,8 @@ Convert `serial`/`bigserial` to `bigint` (drop the global sequence default). Imp3. **`SELECT COALESCE(MAX(id), 0) + 1`** with advisory lock on `(project, table_name)`.Tables affected:-- Project-scoped: `plan`, `issue`, `project_webhook`, `worksheet`, `plan_check_run`, `task`, `task_run`, `issue_comment`+- Project-scoped: `plan`, `issue`, `worksheet`, `plan_check_run`, `task`, `task_run`+ - (`project_webhook` and `issue_comment` use `resource_id` as PK — no per-project auto-increment needed)- Instance-scoped: `revision`, `sync_history`, `changelog`#### Step 4: Cross-Workspace Tables — Text UUID PKsdiff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex 32503bb9eb..d4efb78c28 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -4634,6 +4634,7 @@ Signal represents a notification payload sent via PostgreSQL NOTIFY for HA coord| ----- | ---- | ----- | ----------- || type | [Signal.Type](#bytebase-store-Signal-Type) | | || uid | [int32](#int32) | | |+| project | [string](#string) | | |diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.htmlindex 6380bcabdb..5035fee7b9 100644--- a/proto/gen/grpc-doc/store/index.html+++ b/proto/gen/grpc-doc/store/index.html@@ -12745,6 +12745,13 @@ Default to this mode.</p></td><td><p> </p></td></tr>+ <tr>+ <td>project</td>+ <td><a href="#string">string</a></td>+ <td></td>+ <td><p> </p></td>+ </tr>+</tbody></table>diff --git a/proto/store/store/signal.proto b/proto/store/store/signal.protoindex 903db0faa1..6f20bb65d8 100644--- a/proto/store/store/signal.proto+++ b/proto/store/store/signal.proto@@ -15,4 +15,5 @@ message Signal {Type type = 1;int32 uid = 2;+ string project = 3;}