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: auto-increased id in the project-scope (#19624)
ecmadao63ec07d67437
diff --git a/backend/api/v1/access_grant_service.go b/backend/api/v1/access_grant_service.goindex 510db01678..cb9c2b5f22 100644--- a/backend/api/v1/access_grant_service.go+++ b/backend/api/v1/access_grant_service.go@@ -427,7 +427,7 @@ func convertToAccessGrant(msg *store.AccessGrantMessage) *v1pb.AccessGrant {ag.Query = p.Queryag.Unmask = p.Unmaskif p.IssueId != 0 {- ag.Issue = common.FormatIssue(msg.ProjectID, int(p.IssueId))+ ag.Issue = common.FormatIssue(msg.ProjectID, p.IssueId)}if ag.Expiration == nil && p.RequestedDuration != nil {ag.Expiration = &v1pb.AccessGrant_Ttl{diff --git a/backend/api/v1/issue_hook.go b/backend/api/v1/issue_hook.goindex b1995bee8a..7bc7ab2a30 100644--- a/backend/api/v1/issue_hook.go+++ b/backend/api/v1/issue_hook.go@@ -51,7 +51,7 @@ func postCreateIssue(if err := approval.FindAndApplyApprovalTemplate(ctx, stores, webhookManager, licenseService, issue); err != nil {slog.Error("failed to find approval template",- slog.String("project", issue.ProjectID), slog.Int("issue_uid", issue.UID),+ slog.String("project", issue.ProjectID), slog.Int64("issue_uid", issue.UID),slog.String("issue_title", issue.Title),log.BBError(err))}diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.goindex 4c6578e69b..62768fb4ea 100644--- a/backend/api/v1/issue_service.go+++ b/backend/api/v1/issue_service.go@@ -984,7 +984,7 @@ func (s *IssueService) BatchUpdateIssuesStatus(ctx context.Context, req *connect// Parse issue names and validate all issues belong to the same project.var projectID string- issueUIDs := make([]int, 0, len(req.Msg.Issues))+ issueUIDs := make([]int64, 0, len(req.Msg.Issues))for i, issueName := range req.Msg.Issues {issueProjectID, issueUID, err := common.GetProjectIDIssueUID(issueName)if err != nil {diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.goindex b4e097d13e..05f2c28828 100644--- a/backend/api/v1/plan_service.go+++ b/backend/api/v1/plan_service.go@@ -345,7 +345,7 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[vif updatedIssue.Type == storepb.Issue_DATABASE_EXPORT {if err := approval.FindAndApplyApprovalTemplate(ctx, s.store, s.webhookManager, s.licenseService, updatedIssue); err != nil {slog.Error("failed to find approval template after plan update",- slog.String("project", updatedIssue.ProjectID), slog.Int("issue_uid", updatedIssue.UID),+ slog.String("project", updatedIssue.ProjectID), slog.Int64("issue_uid", updatedIssue.UID),slog.String("issue_title", updatedIssue.Title),log.BBError(err))// Continue anyway - non-fatal error@@ -502,12 +502,12 @@ func (s *PlanService) CancelPlanCheckRun(ctx context.Context, request *connect.R}// Broadcast cancel signal to all replicas for HA.- if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_PLAN_CHECK_RUN, projectID, int32(planCheckRun.UID)); err != nil {+ if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_PLAN_CHECK_RUN, projectID, 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, projectID, []int{planCheckRun.UID}); err != nil {+ if err := s.store.BatchCancelPlanCheckRuns(ctx, projectID, []int64{planCheckRun.UID}); err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to cancel plan check run"))}diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex 3c43eebc05..07a4ff2e6b 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -740,8 +740,8 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Request}// Parse requested task IDs and group by their environment- taskEnvironments := map[string][]int{}- taskIDsToRunMap := map[int]bool{}+ taskEnvironments := map[string][]int64{}+ taskIDsToRunMap := map[int64]bool{}for _, task := range request.Tasks {_, _, stageID, taskID, err := common.GetProjectIDPlanIDStageIDTaskID(task)if err != nil {@@ -817,7 +817,14 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.RequesttaskRunCreates = append(taskRunCreates, create)}slices.SortFunc(taskRunCreates, func(a, b *store.TaskRunMessage) int {- return a.TaskUID - b.TaskUID+ switch {+ case a.TaskUID < b.TaskUID:+ return -1+ case a.TaskUID > b.TaskUID:+ return 1+ default:+ return 0+ }})if err := s.store.CreatePendingTaskRuns(ctx, user.Email, taskRunCreates...); err != nil {@@ -871,7 +878,7 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, req *connect.Requesreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list tasks"))}- taskByID := make(map[int]*store.TaskMessage)+ taskByID := make(map[int64]*store.TaskMessage)for _, task := range tasks {taskByID[task.ID] = task}@@ -880,7 +887,7 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, req *connect.Requesif !ok {return nil, connect.NewError(connect.CodeInternal, errors.New("user not found"))}- var taskUIDs []int+ var taskUIDs []int64environmentSet := map[string]struct{}{}for _, task := range request.Tasks {_, _, _, taskID, err := common.GetProjectIDPlanIDStageIDTaskID(task)@@ -980,7 +987,7 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, req *connect.Rreturn nil, connect.NewError(connect.CodePermissionDenied, errors.New("Not allowed to cancel tasks"))}- var taskRunIDs []int+ var taskRunIDs []int64for _, taskRun := range request.TaskRuns {_, _, _, _, taskRunID, err := common.GetProjectIDPlanIDStageIDTaskIDTaskRunID(taskRun)if err != nil {@@ -1013,7 +1020,7 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, req *connect.RcancelFunc.(context.CancelFunc)()}// Broadcast cancel signal to all replicas for HA.- if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_TASK_RUN, projectID, int32(taskRun.ID)); err != nil {+ if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_TASK_RUN, projectID, taskRun.ID); err != nil {slog.Warn("failed to send cancel signal", log.BBError(err))}}diff --git a/backend/api/v1/rollout_service_converter.go b/backend/api/v1/rollout_service_converter.goindex 1d82fc6b02..b32d6fdf89 100644--- a/backend/api/v1/rollout_service_converter.go+++ b/backend/api/v1/rollout_service_converter.go@@ -191,7 +191,14 @@ func convertToRollout(project *store.ProjectMessage, plan *store.PlanMessage, taenvTasks := tasksByEnv[env]// Sort tasks by ID within each stage.slices.SortFunc(envTasks, func(a, b *store.TaskMessage) int {- return a.ID - b.ID+ switch {+ case a.ID < b.ID:+ return -1+ case a.ID > b.ID:+ return 1+ default:+ return 0+ }})// Convert tasks to v1pb.Task.diff --git a/backend/common/resource_name.go b/backend/common/resource_name.goindex 249ce5094a..c53c8f7a17 100644--- a/backend/common/resource_name.go+++ b/backend/common/resource_name.go@@ -192,12 +192,12 @@ func GetIdentityProviderID(name string) (string, error) {}// GetProjectIDIssueUID returns the project ID and issue UID from the issue name.-func GetProjectIDIssueUID(name string) (string, int, error) {+func GetProjectIDIssueUID(name string) (string, int64, error) {tokens, err := GetNameParentTokens(name, ProjectNamePrefix, IssueNamePrefix)if err != nil {return "", 0, err}- issueUID, err := strconv.Atoi(tokens[1])+ issueUID, err := strconv.ParseInt(tokens[1], 10, 64)if err != nil {return "", 0, errors.Errorf("invalid issue ID %q", tokens[1])}@@ -205,12 +205,12 @@ func GetProjectIDIssueUID(name string) (string, int, error) {}// GetProjectIDIssueUIDIssueCommentID returns the project ID, issue UID and issue comment ID from the issue comment name.-func GetProjectIDIssueUIDIssueCommentID(name string) (string, int, string, error) {+func GetProjectIDIssueUIDIssueCommentID(name string) (string, int64, string, error) {tokens, err := GetNameParentTokens(name, ProjectNamePrefix, IssueNamePrefix, IssueCommentNamePrefix)if err != nil {return "", 0, "", err}- issueUID, err := strconv.Atoi(tokens[1])+ issueUID, err := strconv.ParseInt(tokens[1], 10, 64)if err != nil {return "", 0, "", errors.Errorf("invalid issue ID %q", tokens[1])}@@ -231,16 +231,16 @@ func GetProjectIDPlanID(name string) (string, int64, error) {}// GetProjectIDPlanIDPlanCheckRunID returns the project ID, plan ID and plan check run ID from a resource name.-func GetProjectIDPlanIDPlanCheckRunID(name string) (string, int, int, error) {+func GetProjectIDPlanIDPlanCheckRunID(name string) (string, int64, int64, error) {tokens, err := GetNameParentTokens(name, ProjectNamePrefix, PlanPrefix, PlanCheckRunPrefix)if err != nil {return "", 0, 0, err}- planID, err := strconv.Atoi(tokens[1])+ planID, err := strconv.ParseInt(tokens[1], 10, 64)if err != nil {return "", 0, 0, errors.Errorf("invalid plan ID %q", tokens[1])}- planCheckRunID, err := strconv.Atoi(tokens[2])+ planCheckRunID, err := strconv.ParseInt(tokens[2], 10, 64)if err != nil {return "", 0, 0, errors.Errorf("invalid plan check run ID %q", tokens[2])}@@ -259,7 +259,7 @@ func GetProjectIDPlanIDFromPlanCheckRun(name string) (string, int64, error) {if err != nil {return "", 0, err}- return projectID, int64(planID), nil+ return projectID, planID, nil}// GetProjectIDPlanIDFromRolloutName returns the project ID and plan ID from a resource name.@@ -297,7 +297,7 @@ func GetProjectIDPlanIDMaybeStageID(name string) (string, int64, *string, error)}// GetProjectIDPlanIDStageIDMaybeTaskID returns the project ID, plan ID, and maybe stage ID and maybe task ID from a resource name.-func GetProjectIDPlanIDStageIDMaybeTaskID(name string) (string, int64, string, *int, error) {+func GetProjectIDPlanIDStageIDMaybeTaskID(name string) (string, int64, string, *int64, error) {parts := strings.Split(name, "/rollout")if len(parts) != 2 {return "", 0, "", nil, errors.Errorf("invalid rollout task name %q", name)@@ -315,9 +315,9 @@ func GetProjectIDPlanIDStageIDMaybeTaskID(name string) (string, int64, string, *}stageID := suffixParts[1]- var maybeTaskID *int+ var maybeTaskID *int64if suffixParts[3] != "-" {- taskID, err := strconv.Atoi(suffixParts[3])+ taskID, err := strconv.ParseInt(suffixParts[3], 10, 64)if err != nil {return "", 0, "", nil, errors.Errorf("invalid task ID %q", suffixParts[3])}@@ -327,7 +327,7 @@ func GetProjectIDPlanIDStageIDMaybeTaskID(name string) (string, int64, string, *}// GetProjectIDPlanIDMaybeStageIDMaybeTaskID returns the project ID, plan ID, and maybe stage ID and maybe task ID from a resource name.-func GetProjectIDPlanIDMaybeStageIDMaybeTaskID(name string) (string, int64, *string, *int, error) {+func GetProjectIDPlanIDMaybeStageIDMaybeTaskID(name string) (string, int64, *string, *int64, error) {parts := strings.Split(name, "/rollout")if len(parts) != 2 {return "", 0, nil, nil, errors.Errorf("invalid rollout task name %q", name)@@ -348,9 +348,9 @@ func GetProjectIDPlanIDMaybeStageIDMaybeTaskID(name string) (string, int64, *strif suffixParts[1] != "-" {maybeStageID = &suffixParts[1]}- var maybeTaskID *int+ var maybeTaskID *int64if suffixParts[3] != "-" {- taskID, err := strconv.Atoi(suffixParts[3])+ taskID, err := strconv.ParseInt(suffixParts[3], 10, 64)if err != nil {return "", 0, nil, nil, errors.Errorf("invalid task ID %q", suffixParts[3])}@@ -360,7 +360,7 @@ func GetProjectIDPlanIDMaybeStageIDMaybeTaskID(name string) (string, int64, *str}// GetProjectIDPlanIDStageIDTaskID returns the project ID, plan ID, stage ID, and task ID from a resource name.-func GetProjectIDPlanIDStageIDTaskID(name string) (string, int64, string, int, error) {+func GetProjectIDPlanIDStageIDTaskID(name string) (string, int64, string, int64, error) {parts := strings.Split(name, "/rollout")if len(parts) != 2 {return "", 0, "", 0, errors.Errorf("invalid rollout task name %q", name)@@ -378,7 +378,7 @@ func GetProjectIDPlanIDStageIDTaskID(name string) (string, int64, string, int, e}stageID := suffixParts[1]- taskID, err := strconv.Atoi(suffixParts[3])+ taskID, err := strconv.ParseInt(suffixParts[3], 10, 64)if err != nil {return "", 0, "", 0, errors.Errorf("invalid task ID %q", suffixParts[3])}@@ -386,7 +386,7 @@ func GetProjectIDPlanIDStageIDTaskID(name string) (string, int64, string, int, e}// GetProjectIDPlanIDStageIDTaskIDTaskRunID returns the project ID, plan ID, stage ID, task ID and task run ID from a resource name.-func GetProjectIDPlanIDStageIDTaskIDTaskRunID(name string) (string, int64, string, int, int, error) {+func GetProjectIDPlanIDStageIDTaskIDTaskRunID(name string) (string, int64, string, int64, int64, error) {parts := strings.Split(name, "/rollout")if len(parts) != 2 {return "", 0, "", 0, 0, errors.Errorf("invalid rollout task run name %q", name)@@ -404,11 +404,11 @@ func GetProjectIDPlanIDStageIDTaskIDTaskRunID(name string) (string, int64, strin}stageID := suffixParts[1]- taskID, err := strconv.Atoi(suffixParts[3])+ taskID, err := strconv.ParseInt(suffixParts[3], 10, 64)if err != nil {return "", 0, "", 0, 0, errors.Errorf("invalid task ID %q", suffixParts[3])}- taskRunID, err := strconv.Atoi(suffixParts[5])+ taskRunID, err := strconv.ParseInt(suffixParts[5], 10, 64)if err != nil {return "", 0, "", 0, 0, errors.Errorf("invalid task run ID %q", suffixParts[5])}@@ -572,7 +572,7 @@ func FormatSheet(projectID string, sheetSha256 string) string {return fmt.Sprintf("%s/%s%s", FormatProject(projectID), SheetIDPrefix, sheetSha256)}-func FormatIssue(projectID string, issueUID int) string {+func FormatIssue(projectID string, issueUID int64) string {return fmt.Sprintf("%s/%s%d", FormatProject(projectID), IssueNamePrefix, issueUID)}@@ -597,13 +597,13 @@ func FormatStage(projectID string, planUID int64, stageID string) string {}// stageID is task environmentID.-func FormatTask(projectID string, planUID int64, stageID string, taskUID int) string {+func FormatTask(projectID string, planUID int64, stageID string, taskUID int64) string {// stageUID is now environmentIDreturn fmt.Sprintf("%s/%s%d", FormatStage(projectID, planUID, stageID), TaskPrefix, taskUID)}// stageID is task environmentID.-func FormatTaskRun(projectID string, planUID int64, stageID string, taskUID, taskRunUID int) string {+func FormatTaskRun(projectID string, planUID int64, stageID string, taskUID, taskRunUID int64) string {return fmt.Sprintf("%s/%s%d", FormatTask(projectID, planUID, stageID, taskUID), TaskRunPrefix, taskRunUID)}diff --git a/backend/component/bus/bus.go b/backend/component/bus/bus.goindex dcbf673e83..798bfb5647 100644--- a/backend/component/bus/bus.go+++ b/backend/component/bus/bus.go@@ -14,19 +14,19 @@ type PlanRef struct {// TaskRunRef identifies a task run by project and ID.type TaskRunRef struct {ProjectID string- ID int+ ID int64}// IssueRef identifies an issue by project and UID.type IssueRef struct {ProjectID string- UID int+ UID int64}// PlanCheckRunRef identifies a plan check run by project and UID.type PlanCheckRunRef struct {ProjectID string- UID int+ UID int64}// Bus is the message bus for all in-memory communication within the server.diff --git a/backend/component/ghost/config.go b/backend/component/ghost/config.goindex e5943afe48..a89a469f3f 100644--- a/backend/component/ghost/config.go+++ b/backend/component/ghost/config.go@@ -203,7 +203,7 @@ func GetUserFlags(flags map[string]string) (*UserFlags, error) {}// NewMigrationContext is the context for gh-ost migration.-func NewMigrationContext(ctx context.Context, taskID int, database *store.DatabaseMessage, dataSource *storepb.DataSource, tableName string, tmpTableNameSuffix string, statement string, noop bool, flags map[string]string, serverIDOffset uint) (*ghostbase.MigrationContext, error) {+func NewMigrationContext(ctx context.Context, taskID int64, database *store.DatabaseMessage, dataSource *storepb.DataSource, tableName string, tmpTableNameSuffix string, statement string, noop bool, flags map[string]string, serverIDOffset uint) (*ghostbase.MigrationContext, error) {password, err := secretcomp.ReplaceExternalSecret(ctx, dataSource.GetPassword(), dataSource.GetExternalSecret())if err != nil {return nil, errdiff --git a/backend/component/webhook/event.go b/backend/component/webhook/event.goindex 3503c14b67..1bf3274f0c 100644--- a/backend/component/webhook/event.go+++ b/backend/component/webhook/event.go@@ -47,7 +47,7 @@ func NewRollout(r *store.PlanMessage) *Rollout {}type Issue struct {- UID int+ UID int64Status stringType stringTitle stringdiff --git a/backend/generated-go/store/signal.pb.go b/backend/generated-go/store/signal.pb.goindex 99c2afe198..fb98021019 100644--- a/backend/generated-go/store/signal.pb.go+++ b/backend/generated-go/store/signal.pb.go@@ -75,7 +75,7 @@ func (Signal_Type) EnumDescriptor() ([]byte, []int) {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"`+ Uid int64 `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@@ -118,7 +118,7 @@ func (x *Signal) GetType() Signal_Type {return Signal_TYPE_UNSPECIFIED}-func (x *Signal) GetUid() int32 {+func (x *Signal) GetUid() int64 {if x != nil {return x.Uid}@@ -139,7 +139,7 @@ const file_store_signal_proto_rawDesc = "" +"\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\x12\x18\n" ++ "\x03uid\x18\x02 \x01(\x03R\x03uid\x12\x18\n" +"\aproject\x18\x03 \x01(\tR\aproject\"L\n" +"\x04Type\x12\x14\n" +"\x10TYPE_UNSPECIFIED\x10\x00\x12\x19\n" +diff --git a/backend/migrator/migration/3.17/0006##per_project_auto_increment.sql b/backend/migrator/migration/3.17/0006##per_project_auto_increment.sqlnew file mode 100644index 0000000000..c7a88341e6--- /dev/null+++ b/backend/migrator/migration/3.17/0006##per_project_auto_increment.sql@@ -0,0 +1,33 @@+-- Remove global sequence defaults, drop sequences, unify id columns to bigint.+-- Drop unused resource_id columns from plan, task, task_run, issue.+-- NOTE: DROP DEFAULT / DROP COLUMN IF EXISTS / DROP INDEX IF EXISTS are all idempotent.++-- Drop unused resource_id columns and their unique indexes.+DROP INDEX IF EXISTS idx_plan_unique_resource_id;+DROP INDEX IF EXISTS idx_task_unique_resource_id;+DROP INDEX IF EXISTS idx_task_run_unique_resource_id;+DROP INDEX IF EXISTS idx_issue_unique_resource_id;+ALTER TABLE plan DROP COLUMN IF EXISTS resource_id;+ALTER TABLE task DROP COLUMN IF EXISTS resource_id;+ALTER TABLE task_run DROP COLUMN IF EXISTS resource_id;+ALTER TABLE issue DROP COLUMN IF EXISTS resource_id;++-- Remove global sequence defaults and unify id columns to bigint.+ALTER TABLE plan ALTER COLUMN id DROP DEFAULT;+ALTER TABLE task ALTER COLUMN id DROP DEFAULT;+ALTER TABLE task ALTER COLUMN id SET DATA TYPE bigint;+ALTER TABLE task_run ALTER COLUMN id DROP DEFAULT;+ALTER TABLE task_run ALTER COLUMN id SET DATA TYPE bigint;+ALTER TABLE plan_check_run ALTER COLUMN id DROP DEFAULT;+ALTER TABLE plan_check_run ALTER COLUMN id SET DATA TYPE bigint;+ALTER TABLE issue ALTER COLUMN id DROP DEFAULT;+ALTER TABLE issue ALTER COLUMN id SET DATA TYPE bigint;+ALTER TABLE worksheet ALTER COLUMN id DROP DEFAULT;+ALTER TABLE worksheet ALTER COLUMN id SET DATA TYPE bigint;++DROP SEQUENCE IF EXISTS plan_id_seq;+DROP SEQUENCE IF EXISTS task_id_seq;+DROP SEQUENCE IF EXISTS task_run_id_seq;+DROP SEQUENCE IF EXISTS plan_check_run_id_seq;+DROP SEQUENCE IF EXISTS issue_id_seq;+DROP SEQUENCE IF EXISTS worksheet_id_seq;diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sqlindex 93918c2852..d830ab03c3 100644--- a/backend/migrator/migration/LATEST.sql+++ b/backend/migrator/migration/LATEST.sql@@ -163,7 +163,6 @@ CREATE TABLE sheet_blob (-- plan table stores the plan for a projectCREATE TABLE plan (id bigserial,- resource_id text NOT NULL DEFAULT gen_random_uuid()::text,deleted boolean NOT NULL DEFAULT FALSE,creator text NOT NULL,created_at timestamptz NOT NULL DEFAULT now(),@@ -179,7 +178,6 @@ CREATE TABLE plan (CREATE INDEX idx_plan_project ON plan(project);CREATE INDEX idx_plan_creator ON plan(creator);CREATE INDEX idx_plan_config_has_rollout ON plan ((config->>'hasRollout'));-CREATE UNIQUE INDEX idx_plan_unique_resource_id ON plan(resource_id);ALTER SEQUENCE plan_id_seq RESTART WITH 101;@@ -218,7 +216,6 @@ CREATE TABLE plan_webhook_delivery (-- task table stores the task for a planCREATE TABLE task (id serial,- resource_id text NOT NULL DEFAULT gen_random_uuid()::text,project text NOT NULL REFERENCES project(resource_id),plan_id bigint NOT NULL,instance text NOT NULL REFERENCES instance(resource_id),@@ -232,14 +229,12 @@ CREATE TABLE task ();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,- 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(),@@ -260,7 +255,6 @@ CREATE TABLE task_run ();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(project, task_id, attempt);@@ -295,7 +289,6 @@ CREATE TABLE task_run_log (-- issueCREATE TABLE issue (id serial,- resource_id text NOT NULL DEFAULT gen_random_uuid()::text,creator text NOT NULL,created_at timestamptz NOT NULL DEFAULT now(),updated_at timestamptz NOT NULL DEFAULT now(),@@ -321,7 +314,6 @@ CREATE UNIQUE INDEX idx_issue_unique_plan_id ON issue(project, plan_id);CREATE INDEX idx_issue_creator ON issue(creator);CREATE INDEX idx_issue_ts_vector ON issue USING GIN(ts_vector);-CREATE UNIQUE INDEX idx_issue_unique_resource_id ON issue(resource_id);ALTER SEQUENCE issue_id_seq RESTART WITH 101;diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.goindex 54d82a8355..a2451ec03e 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.17.5"), *files[len(files)-1].version)- require.Equal(t, "migration/3.17/0005##instance_scoped_composite_pk.sql", files[len(files)-1].path)+ require.Equal(t, semver.MustParse("3.17.6"), *files[len(files)-1].version)+ require.Equal(t, "migration/3.17/0006##per_project_auto_increment.sql", files[len(files)-1].path)}func TestVersionUnique(t *testing.T) {diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.goindex e8e65b1326..e918994786 100644--- a/backend/plugin/db/driver.go+++ b/backend/plugin/db/driver.go@@ -104,7 +104,7 @@ type ConnectionContext struct {// MessageBuffer is used for logging messages from the database server.MessageBuffer []*v1pb.QueryResult_Message// TaskRunUID is set when executing a task run, used to set application_name for connection identification.- TaskRunUID *int+ TaskRunUID *int64}// AppendMessage appends a message to the message buffer.diff --git a/backend/plugin/webhook/webhook.go b/backend/plugin/webhook/webhook.goindex 9049c87dca..b8d8be5f57 100644--- a/backend/plugin/webhook/webhook.go+++ b/backend/plugin/webhook/webhook.go@@ -43,7 +43,7 @@ const (// Issue object of issue.type Issue struct {- ID int+ ID int64Name stringStatus stringType stringdiff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.goindex f2b2635c29..ff4a4a8596 100644--- a/backend/runner/approval/runner.go+++ b/backend/runner/approval/runner.go@@ -83,7 +83,7 @@ func (r *Runner) processIssue(ctx context.Context, ref bus.IssueRef) {issue, err := r.store.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{ref.ProjectID}, UID: &uid})if err != nil {slog.Error("failed to get issue for approval check",- slog.String("project", ref.ProjectID), slog.Int("issue_uid", ref.UID), log.BBError(err))+ slog.String("project", ref.ProjectID), slog.Int64("issue_uid", ref.UID), log.BBError(err))return}if issue == nil {@@ -98,7 +98,7 @@ func (r *Runner) processIssue(ctx context.Context, ref bus.IssueRef) {if err := findApprovalTemplateForIssue(ctx, r.store, r.webhookManager, r.licenseService, issue, approvalSetting); err != nil {slog.Error("failed to find approval template",- slog.String("project", ref.ProjectID), slog.Int("issue_uid", ref.UID),+ slog.String("project", ref.ProjectID), slog.Int64("issue_uid", ref.UID),slog.String("issue_title", issue.Title),log.BBError(err))// Don't persist error - user can rerun plan check to retry@@ -113,7 +113,7 @@ func (r *Runner) processIssue(ctx context.Context, ref bus.IssueRef) {issue, err = r.store.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{ref.ProjectID}, UID: &uid})if err != nil {slog.Error("failed to re-fetch issue after approval finding",- slog.String("project", ref.ProjectID), slog.Int("issue_uid", ref.UID), log.BBError(err))+ slog.String("project", ref.ProjectID), slog.Int64("issue_uid", ref.UID), log.BBError(err))return}if issue == nil {@@ -123,7 +123,7 @@ func (r *Runner) processIssue(ctx context.Context, ref bus.IssueRef) {approved, err := utils.CheckIssueApproved(issue)if err != nil {slog.Error("failed to check if issue is approved",- slog.String("project", ref.ProjectID), slog.Int("issue_uid", ref.UID), log.BBError(err))+ slog.String("project", ref.ProjectID), slog.Int64("issue_uid", ref.UID), log.BBError(err))return}if approved {diff --git a/backend/runner/notifylistener/listener.go b/backend/runner/notifylistener/listener.goindex a72769941c..5c794f44bf 100644--- a/backend/runner/notifylistener/listener.go+++ b/backend/runner/notifylistener/listener.go@@ -90,12 +90,12 @@ func (l *Listener) handleNotification(payload string) {switch signal.Type {case storepb.Signal_CANCEL_PLAN_CHECK_RUN:- ref := bus.PlanCheckRunRef{ProjectID: signal.Project, UID: int(signal.Uid)}+ ref := bus.PlanCheckRunRef{ProjectID: signal.Project, UID: signal.Uid}if cancel, ok := l.bus.RunningPlanCheckRunsCancelFunc.Load(ref); ok {cancel.(context.CancelFunc)()}case storepb.Signal_CANCEL_TASK_RUN:- ref := bus.TaskRunRef{ProjectID: signal.Project, ID: int(signal.Uid)}+ ref := bus.TaskRunRef{ProjectID: signal.Project, ID: signal.Uid}if cancel, ok := l.bus.RunningTaskRunsCancelFunc.Load(ref); ok {cancel.(context.CancelFunc)()}diff --git a/backend/runner/plancheck/ghost_sync_executor.go b/backend/runner/plancheck/ghost_sync_executor.goindex 780fde4f69..af9e9b2760 100644--- a/backend/runner/plancheck/ghost_sync_executor.go+++ b/backend/runner/plancheck/ghost_sync_executor.go@@ -142,7 +142,7 @@ func (e *GhostSyncExecutor) RunForTarget(ctx context.Context, target *CheckTargeif err != nil {return nil, common.Wrapf(err, common.Internal, "failed to generate secure random number")}- migrationContext, err := ghost.NewMigrationContext(ctx, int(randomInt.Int64()), database, adminDataSource, tableName, fmt.Sprintf("_dryrun_%d", time.Now().Unix()), statement, true, target.GhostFlags, 20000000)+ migrationContext, err := ghost.NewMigrationContext(ctx, randomInt.Int64(), database, adminDataSource, tableName, fmt.Sprintf("_dryrun_%d", time.Now().Unix()), statement, true, target.GhostFlags, 20000000)if err != nil {return nil, common.Wrapf(err, common.Internal, "failed to create migration context")}diff --git a/backend/runner/plancheck/scheduler.go b/backend/runner/plancheck/scheduler.goindex 9d5a0804b9..e266eee338 100644--- a/backend/runner/plancheck/scheduler.go+++ b/backend/runner/plancheck/scheduler.go@@ -90,7 +90,7 @@ func (s *Scheduler) runOnce(ctx context.Context) {}}-func (s *Scheduler) runPlanCheckRun(ctx context.Context, projectID string, uid int, planUID int64) {+func (s *Scheduler) runPlanCheckRun(ctx context.Context, projectID string, uid int64, planUID int64) {ctxWithCancel, cancel := context.WithCancel(ctx)defer cancel()planCheckRef := bus.PlanCheckRunRef{ProjectID: projectID, UID: uid}@@ -152,7 +152,7 @@ func (s *Scheduler) runPlanCheckRun(ctx context.Context, projectID string, uid i}}-func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, projectID string, uid int, planUID int64, results []*storepb.PlanCheckRunResult_Result) {+func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, projectID string, uid int64, planUID int64, results []*storepb.PlanCheckRunResult_Result) {result := &storepb.PlanCheckRunResult{Results: results,}@@ -181,7 +181,7 @@ func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, projectID string,}}-func (s *Scheduler) markPlanCheckRunFailed(ctx context.Context, projectID string, uid int, reason string) {+func (s *Scheduler) markPlanCheckRunFailed(ctx context.Context, projectID string, uid int64, reason string) {result := &storepb.PlanCheckRunResult{Error: reason,}@@ -195,7 +195,7 @@ func (s *Scheduler) markPlanCheckRunFailed(ctx context.Context, projectID string}}-func (s *Scheduler) markPlanCheckRunCanceled(ctx context.Context, projectID string, uid int, reason string) {+func (s *Scheduler) markPlanCheckRunCanceled(ctx context.Context, projectID string, uid int64, reason string) {result := &storepb.PlanCheckRunResult{Error: reason,}diff --git a/backend/runner/taskrun/data_export_executor.go b/backend/runner/taskrun/data_export_executor.goindex a1b61bb7a0..18c0a9988b 100644--- a/backend/runner/taskrun/data_export_executor.go+++ b/backend/runner/taskrun/data_export_executor.go@@ -45,7 +45,7 @@ type DataExportExecutor struct {}// RunOnce will run the data export task executor once.-func (exec *DataExportExecutor) RunOnce(ctx context.Context, _ context.Context, task *store.TaskMessage, _ int) (*storepb.TaskRunResult, error) {+func (exec *DataExportExecutor) RunOnce(ctx context.Context, _ context.Context, task *store.TaskMessage, _ int64) (*storepb.TaskRunResult, error) {issue, err := exec.store.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{task.ProjectID}, PlanUID: &task.PlanID})if err != nil {return nil, errors.Wrapf(err, "failed to get issue")diff --git a/backend/runner/taskrun/database_create_executor.go b/backend/runner/taskrun/database_create_executor.goindex 5337f42aa4..28119547da 100644--- a/backend/runner/taskrun/database_create_executor.go+++ b/backend/runner/taskrun/database_create_executor.go@@ -33,7 +33,7 @@ type DatabaseCreateExecutor struct {}// RunOnce will run the database create task executor once.-func (exec *DatabaseCreateExecutor) RunOnce(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, _ int) (*storepb.TaskRunResult, error) {+func (exec *DatabaseCreateExecutor) RunOnce(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, _ int64) (*storepb.TaskRunResult, error) {sheet, err := exec.store.GetSheetFull(ctx, task.Payload.GetSheetSha256())if err != nil {return nil, errors.Wrapf(err, "failed to get sheet: %s", task.Payload.GetSheetSha256())diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.goindex 4b7a537a83..3782597d77 100644--- a/backend/runner/taskrun/database_migrate_executor.go+++ b/backend/runner/taskrun/database_migrate_executor.go@@ -52,7 +52,7 @@ type DatabaseMigrateExecutor struct {}// RunOnce will run the database migration task executor once.-func (exec *DatabaseMigrateExecutor) RunOnce(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int) (*storepb.TaskRunResult, error) {+func (exec *DatabaseMigrateExecutor) RunOnce(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int64) (*storepb.TaskRunResult, error) {// Fetch instance, database, and project (common to all migration types)instance, err := exec.store.GetInstance(ctx, &store.FindInstanceMessage{ResourceID: &task.InstanceID})if err != nil {@@ -163,7 +163,7 @@ func (exec *DatabaseMigrateExecutor) ensureBaselineChangelog(ctx context.Contextreturn nil}-func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int, sheet *store.SheetMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {+func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int64, sheet *store.SheetMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {// Handle prior backup if enabled.// TransformDMLToSelect will automatically filter out DDL statements,// so this works correctly for mixed DDL+DML statements.@@ -288,7 +288,7 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, d}, nil}-func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int, sheet *store.SheetMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {+func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int64, sheet *store.SheetMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {// Parse ghost flags from sheet directiveflags, err := ghost.ParseGhostDirective(sheet.Statement)if err != nil {@@ -439,7 +439,7 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, drivreturn &storepb.TaskRunResult{}, nil}-func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int, release *store.ReleaseMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {+func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int64, release *store.ReleaseMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {// Get existing revisions for this databaserevisions, err := exec.store.ListRevisions(ctx, &store.FindRevisionMessage{InstanceID: task.InstanceID,@@ -603,7 +603,7 @@ func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, drreturn &storepb.TaskRunResult{}, nil}-func (exec *DatabaseMigrateExecutor) runDeclarativeRelease(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int, release *store.ReleaseMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {+func (exec *DatabaseMigrateExecutor) runDeclarativeRelease(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int64, release *store.ReleaseMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {// Declarative releases should have exactly one fileif len(release.Payload.Files) == 0 {return nil, errors.Errorf("no files found in declarative release")diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.goindex a13918fc12..b6fda23298 100644--- a/backend/runner/taskrun/executor.go+++ b/backend/runner/taskrun/executor.go@@ -17,11 +17,11 @@ type Executor interface {// If err is non-nil, the task will be marked as failed.// driverCtx is used by the database driver so that we can cancel the query// while having the ability to cleanup migration history etc.- RunOnce(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int) (result *storepb.TaskRunResult, err error)+ RunOnce(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int64) (result *storepb.TaskRunResult, err error)}// RunExecutorOnce wraps a TaskExecutor.RunOnce call with panic recovery.-func RunExecutorOnce(ctx context.Context, driverCtx context.Context, exec Executor, task *store.TaskMessage, taskRunUID int) (result *storepb.TaskRunResult, err error) {+func RunExecutorOnce(ctx context.Context, driverCtx context.Context, exec Executor, task *store.TaskMessage, taskRunUID int64) (result *storepb.TaskRunResult, err error) {defer func() {if r := recover(); r != nil {panicErr, ok := r.(error)diff --git a/backend/runner/taskrun/pending_scheduler.go b/backend/runner/taskrun/pending_scheduler.goindex 07bca546e4..6dc0d4c625 100644--- a/backend/runner/taskrun/pending_scheduler.go+++ b/backend/runner/taskrun/pending_scheduler.go@@ -117,7 +117,7 @@ func (s *Scheduler) schedulePendingTaskRun(ctx context.Context, taskRun *store.Treturn nil}-func (s *Scheduler) storeParallelLimitCause(ctx context.Context, projectID string, taskRunID int) {+func (s *Scheduler) storeParallelLimitCause(ctx context.Context, projectID string, taskRunID int64) {payload := &storepb.TaskRunPayload{SchedulerInfo: &storepb.SchedulerInfo{ReportTime: timestamppb.Now(),@@ -185,8 +185,8 @@ type planKey struct {// 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[planKey]int // (project, planID) -> active count+ activeByDatabase map[string]int64 // dbKey -> blocking taskID (first found)+ activeCountByPlan map[planKey]int // (project, planID) -> active count// Tracks promotions this roundpromotedDBs map[string]bool@@ -200,7 +200,7 @@ func newSchedulingContext(ctx context.Context, s *store.Store) (*schedulingConte}sc := &schedulingContext{- activeByDatabase: make(map[string]int),+ activeByDatabase: make(map[string]int64),activeCountByPlan: make(map[planKey]int),promotedDBs: make(map[string]bool),promotedCounts: make(map[planKey]int),@@ -211,7 +211,7 @@ func newSchedulingContext(ctx context.Context, s *store.Store) (*schedulingConte}// Group task run task UIDs by project for correct project-scoped lookups.- taskIDsByProject := make(map[string][]int)+ taskIDsByProject := make(map[string][]int64)for _, tr := range activeTaskRuns {taskIDsByProject[tr.ProjectID] = append(taskIDsByProject[tr.ProjectID], tr.TaskUID)}@@ -219,7 +219,7 @@ func newSchedulingContext(ctx context.Context, s *store.Store) (*schedulingConte// Batch fetch tasks per project.type taskKey struct {projectID string- id int+ id int64}taskByKey := make(map[taskKey]*store.TaskMessage)for projectID, ids := range taskIDsByProject {@@ -251,7 +251,7 @@ func newSchedulingContext(ctx context.Context, s *store.Store) (*schedulingContereturn sc, nil}-func (sc *schedulingContext) checkDatabaseMutualExclusion(task *store.TaskMessage) (canProceed bool, blockingTaskID *int) {+func (sc *schedulingContext) checkDatabaseMutualExclusion(task *store.TaskMessage) (canProceed bool, blockingTaskID *int64) {if task.DatabaseName == nil || !isSequentialTask(task) {return true, nil}diff --git a/backend/runner/taskrun/running_scheduler.go b/backend/runner/taskrun/running_scheduler.goindex d5fb180760..605e06253d 100644--- a/backend/runner/taskrun/running_scheduler.go+++ b/backend/runner/taskrun/running_scheduler.go@@ -59,7 +59,7 @@ func (s *Scheduler) scheduleRunningTaskRuns(ctx context.Context) error {for _, c := range claimed {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))+ slog.Error("failed to execute task run", slog.Int64("id", c.TaskRunUID), log.BBError(err))}}@@ -67,7 +67,7 @@ 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, projectID string, taskRunUID, taskUID int) error {+func (s *Scheduler) executeTaskRun(ctx context.Context, projectID string, taskRunUID, taskUID int64) error {task, err := s.store.GetTaskByID(ctx, projectID, taskUID)if err != nil {return errors.Wrapf(err, "failed to get task")@@ -79,7 +79,7 @@ func (s *Scheduler) executeTaskRun(ctx context.Context, projectID string, taskRu// Validate task freshness before execution.if err := s.validateTaskFreshness(ctx, task); err != nil {slog.Warn("task run blocked by drift validation",- slog.Int("id", task.ID),+ slog.Int64("id", task.ID),slog.String("type", task.Type.String()),log.BBError(err),)@@ -112,7 +112,7 @@ func (s *Scheduler) executeTaskRun(ctx context.Context, projectID string, taskRureturn nil}-func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *store.TaskMessage, executor Executor) {+func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int64, task *store.TaskMessage, executor Executor) {defer func() {if r := recover(); r != nil {err, ok := r.(error)@@ -135,7 +135,7 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *stif err != nil && errors.Is(err, context.Canceled) {slog.Warn("task run is canceled",- slog.Int("id", task.ID),+ slog.Int64("id", task.ID),slog.String("type", task.Type.String()),log.BBError(err),)@@ -148,7 +148,7 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *st}if _, err := s.store.UpdateTaskRunStatus(ctx, taskRunStatusPatch); err != nil {slog.Error("Failed to mark task as CANCELED",- slog.Int("id", task.ID),+ slog.Int64("id", task.ID),log.BBError(err),)return@@ -158,7 +158,7 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *stif err != nil {slog.Warn("task run failed",- slog.Int("id", task.ID),+ slog.Int64("id", task.ID),slog.String("type", task.Type.String()),log.BBError(err),)@@ -173,7 +173,7 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *st}if _, err := s.store.UpdateTaskRunStatus(ctx, taskRunStatusPatch); err != nil {slog.Error("Failed to mark task as FAILED",- slog.Int("id", task.ID),+ slog.Int64("id", task.ID),log.BBError(err),)return@@ -218,7 +218,7 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *st}if _, err := s.store.UpdateTaskRunStatus(ctx, taskRunStatusPatch); err != nil {slog.Error("Failed to mark task as DONE",- slog.Int("id", task.ID),+ slog.Int64("id", task.ID),log.BBError(err),)returndiff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.goindex 234bbe6869..2f57df434a 100644--- a/backend/runner/taskrun/scheduler.go+++ b/backend/runner/taskrun/scheduler.go@@ -205,8 +205,8 @@ func (s *Scheduler) autoResolveIssue(ctx context.Context, projectID string, plannewStatus := storepb.Issue_DONEif _, err := s.store.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{Status: &newStatus}); err != nil {- slog.Error("failed to auto-resolve issue", slog.String("project", projectID), slog.Int("issueUID", issue.UID), log.BBError(err))+ slog.Error("failed to auto-resolve issue", slog.String("project", projectID), slog.Int64("issueUID", issue.UID), log.BBError(err))return}- slog.Info("auto-resolved deferred rollout issue", slog.String("project", projectID), slog.Int("issueUID", issue.UID), slog.Int64("planID", planID))+ slog.Info("auto-resolved deferred rollout issue", slog.String("project", projectID), slog.Int64("issueUID", issue.UID), slog.Int64("planID", planID))}diff --git a/backend/store/changelog.go b/backend/store/changelog.goindex 6e49db5d5f..a3b45101f8 100644--- a/backend/store/changelog.go+++ b/backend/store/changelog.go@@ -150,11 +150,12 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)FROM changelogLEFT JOIN sync_history sh_cur ON sh_cur.resource_id = changelog.sync_historyLEFT JOIN LATERAL (- SELECT task.plan_id+ SELECT task.project, task.plan_idFROM task- WHERE task.id = (regexp_match(changelog.payload->>'taskRun', 'tasks/(\d+)/'))[1]::int+ WHERE task.project = (regexp_match(changelog.payload->>'taskRun', 'projects/([^/]+)/'))[1]+ AND task.id = (regexp_match(changelog.payload->>'taskRun', 'tasks/(\d+)/'))[1]::int) task_info ON TRUE- LEFT JOIN plan ON plan.id = task_info.plan_id+ LEFT JOIN plan ON plan.project = task_info.project AND plan.id = task_info.plan_idWHERE changelog.instance = ?`, find.InstanceID)diff --git a/backend/store/id.go b/backend/store/id.gonew file mode 100644index 0000000000..2219dc9f28--- /dev/null+++ b/backend/store/id.go@@ -0,0 +1,44 @@+package store++import (+ "context"+ "database/sql"++ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/common/qb"+)++// idMinValue is the minimum ID value for per-project auto-increment.+// Matches the old ALTER SEQUENCE ... RESTART WITH 101 convention,+// reserving IDs below 101 for seed/test data.+const idMinValue int64 = 101++// nextProjectID returns the next per-project auto-increment ID for the given table.+// Must be called within a transaction. Locks the project row to serialize concurrent inserts.+// Returns at least idMinValue (101) for new projects.+func nextProjectID(ctx context.Context, tx *sql.Tx, table, projectID string) (int64, error) {+ if _, err := tx.ExecContext(ctx,+ "SELECT 1 FROM project WHERE resource_id = $1 FOR UPDATE", projectID); err != nil {+ return 0, errors.Wrapf(err, "failed to lock project %s", projectID)+ }+ var maxID int64++ q := qb.Q()+ q.Space(`+ SELECT COALESCE(MAX(id), 0) FROM ? WHERE project = ?+ `, qb.Q().Space(table), projectID)+ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")+ }++ if err := tx.QueryRowContext(ctx, query, args...).Scan(&maxID); err != nil {+ return 0, errors.Wrapf(err, "failed to get max id for %s", table)+ }+ nextID := maxID + 1+ if nextID < idMinValue {+ nextID = idMinValue+ }+ return nextID, nil+}diff --git a/backend/store/instance.go b/backend/store/instance.goindex 2a0d109633..051b58f1be 100644--- a/backend/store/instance.go+++ b/backend/store/instance.go@@ -547,7 +547,7 @@ func (s *Store) DeleteInstance(ctx context.Context, resourceID string) error {DELETE FROM task_run_logWHERE task_run_id IN (SELECT tr.id FROM task_run tr- JOIN task t ON tr.task_id = t.id+ JOIN task t ON t.project = tr.project AND t.id = tr.task_idWHERE t.instance = ?)`, resourceID)diff --git a/backend/store/issue.go b/backend/store/issue.goindex 12b099e3ca..b267466eb3 100644--- a/backend/store/issue.go+++ b/backend/store/issue.go@@ -41,7 +41,7 @@ type IssueMessage struct {PlanUID *int64// The following fields are output only and not used for create().- UID int+ UID int64CreatedAt time.TimeUpdatedAt time.Time}@@ -61,7 +61,7 @@ type FindIssueMessage struct {// Required fieldProjectIDs []string- UID *int+ UID *int64PlanUID *int64PlanUIDs *[]int64CreatorID *string@@ -133,8 +133,20 @@ func (s *Store) CreateIssue(ctx context.Context, create *IssueMessage) (*IssueMe}tsVector := getTSVector(fmt.Sprintf("%s %s", create.Title, create.Description))+ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to begin tx")+ }+ defer tx.Rollback()++ nextID, err := nextProjectID(ctx, tx, "issue", create.ProjectID)+ if err != nil {+ return nil, err+ }+q := qb.Q().Space(`INSERT INTO issue (+ id,creator,project,plan_id,@@ -145,8 +157,8 @@ func (s *Store) CreateIssue(ctx context.Context, create *IssueMessage) (*IssueMepayload,ts_vector)- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)- RETURNING id`,+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,+ nextID,create.CreatorEmail,create.ProjectID,create.PlanUID,@@ -163,15 +175,20 @@ func (s *Store) CreateIssue(ctx context.Context, create *IssueMessage) (*IssueMereturn nil, errors.Wrapf(err, "failed to build sql")}- if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&create.UID); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return nil, err}+ if err := tx.Commit(); err != nil {+ return nil, errors.Wrapf(err, "failed to commit tx")+ }++ create.UID = nextIDreturn s.GetIssue(ctx, &FindIssueMessage{ProjectIDs: []string{create.ProjectID}, UID: &create.UID})}// UpdateIssue updates an issue.-func (s *Store) UpdateIssue(ctx context.Context, projectID string, uid int, patch *UpdateIssueMessage) (*IssueMessage, error) {+func (s *Store) UpdateIssue(ctx context.Context, projectID string, uid int64, patch *UpdateIssueMessage) (*IssueMessage, error) {oldIssue, err := s.GetIssue(ctx, &FindIssueMessage{ProjectIDs: []string{projectID}, UID: &uid})if err != nil {return nil, err@@ -390,7 +407,7 @@ func (s *Store) ListIssues(ctx context.Context, find *FindIssueMessage) ([]*Issu// BatchUpdateIssueStatuses updates the status of multiple issues.// Returns a map of issueUID -> old status for the updated issues.-func (s *Store) BatchUpdateIssueStatuses(ctx context.Context, projectID string, issueUIDs []int, newStatus storepb.Issue_Status) (map[int]storepb.Issue_Status, error) {+func (s *Store) BatchUpdateIssueStatuses(ctx context.Context, projectID string, issueUIDs []int64, newStatus storepb.Issue_Status) (map[int64]storepb.Issue_Status, error) {tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, errors.Wrapf(err, "failed to begin transaction")@@ -410,9 +427,9 @@ func (s *Store) BatchUpdateIssueStatuses(ctx context.Context, projectID string,}defer rows.Close()- oldStatuses := make(map[int]storepb.Issue_Status)+ oldStatuses := make(map[int64]storepb.Issue_Status)for rows.Next() {- var issueID int+ var issueID int64var statusString stringif err := rows.Scan(&issueID, &statusString); err != nil {return nil, errors.Wrapf(err, "failed to scan issue")diff --git a/backend/store/issue_comment.go b/backend/store/issue_comment.goindex fd606cce68..ae9200d768 100644--- a/backend/store/issue_comment.go+++ b/backend/store/issue_comment.go@@ -17,7 +17,7 @@ type IssueCommentMessage struct {ResourceID stringCreatedAt time.TimeUpdatedAt time.Time- IssueUID int+ IssueUID int64Payload *storepb.IssueCommentPayloadCreatorEmail string}@@ -25,7 +25,7 @@ type IssueCommentMessage struct {type FindIssueCommentMessage struct {ProjectID stringResourceID *string- IssueUID *int+ IssueUID *int64Limit *intOffset *int@@ -133,7 +133,7 @@ func (s *Store) CreateIssueComments(ctx context.Context, creator string, creates// Prepare all payloads.projectIDs := make([]string, 0, len(creates))- issueIDs := make([]int, 0, len(creates))+ issueIDs := make([]int64, 0, len(creates))payloads := make([][]byte, 0, len(creates))for _, create := range creates {payload, err := protojson.Marshal(create.Payload)diff --git a/backend/store/plan.go b/backend/store/plan.goindex a313ed24eb..b844dbc064 100644--- a/backend/store/plan.go+++ b/backend/store/plan.go@@ -67,29 +67,44 @@ func (s *Store) CreatePlan(ctx context.Context, plan *PlanMessage, creator strinreturn nil, errors.Wrap(err, "failed to marshal plan config")}+ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return nil, errors.Wrap(err, "failed to begin tx")+ }+ defer tx.Rollback()++ nextID, err := nextProjectID(ctx, tx, "plan", plan.ProjectID)+ if err != nil {+ return nil, err+ }+q := qb.Q().Space(`INSERT INTO plan (+ id,creator,project,name,description,config) VALUES (- ?, ?, ?, ?, ?- ) RETURNING id, created_at, updated_at- `, creator, plan.ProjectID, plan.Name, plan.Description, config)+ ?, ?, ?, ?, ?, ?+ ) RETURNING created_at, updated_at+ `, nextID, creator, plan.ProjectID, plan.Name, plan.Description, config)query, args, err := q.ToSQL()if err != nil {return nil, errors.Wrap(err, "failed to build sql")}- var id int64- if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&id, &plan.CreatedAt, &plan.UpdatedAt); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&plan.CreatedAt, &plan.UpdatedAt); err != nil {return nil, errors.Wrap(err, "failed to insert plan")}- plan.UID = id+ if err := tx.Commit(); err != nil {+ return nil, errors.Wrap(err, "failed to commit tx")+ }++ plan.UID = nextIDplan.Creator = creatorreturn plan, nil}@@ -104,7 +119,7 @@ func (s *Store) GetPlan(ctx context.Context, find *FindPlanMessage) (*PlanMessagreturn nil, nil}if len(plans) > 1 {- return nil, errors.Errorf("expect to find one plan, found %d", len(plans))+ return nil, errors.Errorf("expect to find one plan, found %d by filter %v", len(plans), find)}return plans[0], nil}@@ -123,7 +138,7 @@ func (s *Store) ListPlans(ctx context.Context, find *FindPlanMessage) ([]*PlanMeplan.config,plan.deletedFROM plan- LEFT JOIN issue on plan.id = issue.plan_id+ LEFT JOIN issue on plan.project = issue.project AND plan.id = issue.plan_idWHERE plan.project = ?`, find.ProjectID)diff --git a/backend/store/plan_check_run.go b/backend/store/plan_check_run.goindex 76141efe9a..72b1a66fdd 100644--- a/backend/store/plan_check_run.go+++ b/backend/store/plan_check_run.go@@ -30,7 +30,7 @@ const (// PlanCheckRunMessage is the message for a plan check run.type PlanCheckRunMessage struct {- UID int+ UID int64CreatedAt time.TimeUpdatedAt time.Time@@ -46,7 +46,7 @@ type FindPlanCheckRunMessage struct {ProjectID stringPlanUID *int64PlanUIDs *[]int64- UIDs *[]int+ UIDs *[]int64Status *[]PlanCheckRunStatusResultStatus *[]storepb.Advice_Status}@@ -59,17 +59,32 @@ func (s *Store) CreatePlanCheckRun(ctx context.Context, create *PlanCheckRunMessreturn errors.Wrapf(err, "failed to marshal result")}+ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return errors.Wrapf(err, "failed to begin tx")+ }+ defer tx.Rollback()++ nextID, err := nextProjectID(ctx, tx, "plan_check_run", create.ProjectID)+ if err != nil {+ return err+ }+query := `- INSERT INTO plan_check_run (project, plan_id, status, result)- VALUES ($1, $2, $3, $4)+ INSERT INTO plan_check_run (id, project, plan_id, status, result)+ VALUES ($1, $2, $3, $4, $5)ON CONFLICT (project, plan_id) DO UPDATE SETstatus = EXCLUDED.status,result = EXCLUDED.result,updated_at = now()`- if _, err := s.GetDB().ExecContext(ctx, query, create.ProjectID, create.PlanUID, PlanCheckRunStatusAvailable, result); err != nil {+ if _, err := tx.ExecContext(ctx, query, nextID, create.ProjectID, create.PlanUID, PlanCheckRunStatusAvailable, result); err != nil {return errors.Wrapf(err, "failed to upsert plan check run")}++ if err := tx.Commit(); err != nil {+ return errors.Wrapf(err, "failed to commit tx")+ }return nil}@@ -159,7 +174,7 @@ func (s *Store) GetPlanCheckRun(ctx context.Context, projectID string, planUID i}// UpdatePlanCheckRun updates a plan check run.-func (s *Store) UpdatePlanCheckRun(ctx context.Context, projectID string, status PlanCheckRunStatus, result *storepb.PlanCheckRunResult, uid int) error {+func (s *Store) UpdatePlanCheckRun(ctx context.Context, projectID string, status PlanCheckRunStatus, result *storepb.PlanCheckRunResult, uid int64) error {resultBytes, err := protojson.Marshal(result)if err != nil {return errors.Wrapf(err, "failed to marshal result %v", result)@@ -182,7 +197,7 @@ func (s *Store) UpdatePlanCheckRun(ctx context.Context, projectID string, status}// BatchCancelPlanCheckRuns updates the status of planCheckRuns to CANCELED.-func (s *Store) BatchCancelPlanCheckRuns(ctx context.Context, projectID string, planCheckRunUIDs []int) error {+func (s *Store) BatchCancelPlanCheckRuns(ctx context.Context, projectID string, planCheckRunUIDs []int64) error {q := qb.Q().Space(`UPDATE plan_check_runSET@@ -201,7 +216,7 @@ func (s *Store) BatchCancelPlanCheckRuns(ctx context.Context, projectID string,// ClaimedPlanCheckRun represents a plan check run that was atomically claimed.type ClaimedPlanCheckRun struct {- UID int+ UID int64ProjectID stringPlanUID int64}diff --git a/backend/store/project.go b/backend/store/project.goindex 3ee9668660..789cbdd22b 100644--- a/backend/store/project.go+++ b/backend/store/project.go@@ -393,8 +393,8 @@ func (s *Store) DeleteProject(ctx context.Context, resourceID string) error {q = qb.Q().Space("DELETE FROM task_run_log")q.Space("WHERE task_run_id IN (")q.Space("SELECT tr.id FROM task_run tr")- q.Space("JOIN task t ON tr.task_id = t.id")- q.Space("JOIN plan p ON t.plan_id = p.id")+ q.Space("JOIN task t ON t.project = tr.project AND t.id = tr.task_id")+ q.Space("JOIN plan p ON p.project = t.project AND p.id = t.plan_id")q.Space("WHERE p.project = ?)", resourceID)sql, args, err = q.ToSQL()if err != nil {@@ -408,7 +408,7 @@ func (s *Store) DeleteProject(ctx context.Context, resourceID string) error {q = qb.Q().Space("DELETE FROM task_run")q.Space("WHERE task_id IN (")q.Space("SELECT t.id FROM task t")- q.Space("JOIN plan p ON t.plan_id = p.id")+ q.Space("JOIN plan p ON p.project = t.project AND p.id = t.plan_id")q.Space("WHERE p.project = ?)", resourceID)sql, args, err = q.ToSQL()if err != nil {diff --git a/backend/store/rollout_filter.go b/backend/store/rollout_filter.goindex 2c06cfac93..94fb53d2b2 100644--- a/backend/store/rollout_filter.go+++ b/backend/store/rollout_filter.go@@ -69,7 +69,7 @@ func GetListRolloutFilter(filter string) (*qb.Query, error) {storeTaskType := convertV1ToStoreTaskType(v1TaskType)taskTypes = append(taskTypes, storeTaskType.String())}- return qb.Q().Space("EXISTS (SELECT 1 FROM task WHERE task.plan_id = plan.id AND task.type = ANY(?))", taskTypes), nil+ return qb.Q().Space("EXISTS (SELECT 1 FROM task WHERE task.project = plan.project AND task.plan_id = plan.id AND task.type = ANY(?))", taskTypes), nildefault:return nil, errors.Errorf("unsupported variable %q", variable)}@@ -86,7 +86,7 @@ func GetListRolloutFilter(filter string) (*qb.Query, error) {if err != nil {return nil, errors.Errorf("failed to parse time %v, error: %v", value, err)}- updatedAtSubquery := `COALESCE((SELECT MAX(task_run.updated_at) FROM task JOIN task_run ON task_run.task_id = task.id WHERE task.plan_id = plan.id), plan.created_at)`+ updatedAtSubquery := `COALESCE((SELECT MAX(task_run.updated_at) FROM task JOIN task_run ON task_run.project = task.project AND task_run.task_id = task.id WHERE task.project = plan.project AND task.plan_id = plan.id), plan.created_at)`if functionName == celoperators.GreaterEquals {return qb.Q().Space(updatedAtSubquery+" >= ?", t), nil}diff --git a/backend/store/rollout_filter_test.go b/backend/store/rollout_filter_test.goindex 3756b81913..e36401354d 100644--- a/backend/store/rollout_filter_test.go+++ b/backend/store/rollout_filter_test.go@@ -9,7 +9,7 @@ import (func TestGetListRolloutFilter(t *testing.T) {// The subquery used for update_time filtering- updatedAtSubquery := `COALESCE((SELECT MAX(task_run.updated_at) FROM task JOIN task_run ON task_run.task_id = task.id WHERE task.plan_id = plan.id), plan.created_at)`+ updatedAtSubquery := `COALESCE((SELECT MAX(task_run.updated_at) FROM task JOIN task_run ON task_run.project = task.project AND task_run.task_id = task.id WHERE task.project = plan.project AND task.plan_id = plan.id), plan.created_at)`tests := []struct {name string@@ -29,7 +29,7 @@ func TestGetListRolloutFilter(t *testing.T) {{name: "task_type in filter",filter: `task_type in ["DATABASE_MIGRATE", "DATABASE_EXPORT"]`,- wantSQL: "(EXISTS (SELECT 1 FROM task WHERE task.plan_id = plan.id AND task.type = ANY($1)))",+ wantSQL: "(EXISTS (SELECT 1 FROM task WHERE task.project = plan.project AND task.plan_id = plan.id AND task.type = ANY($1)))",wantArgs: []any{[]string{"DATABASE_MIGRATE", "DATABASE_EXPORT"}},wantErr: false,},@@ -66,7 +66,7 @@ func TestGetListRolloutFilter(t *testing.T) {{name: "AND condition with task_type and update_time",filter: `task_type in ["DATABASE_MIGRATE"] && update_time >= "2024-01-01T00:00:00Z"`,- wantSQL: "((EXISTS (SELECT 1 FROM task WHERE task.plan_id = plan.id AND task.type = ANY($1)) AND " + updatedAtSubquery + " >= $2))",+ wantSQL: "((EXISTS (SELECT 1 FROM task WHERE task.project = plan.project AND task.plan_id = plan.id AND task.type = ANY($1)) AND " + updatedAtSubquery + " >= $2))",wantArgs: []any{[]string{"DATABASE_MIGRATE"}, func() time.Time {ts, _ := time.Parse(time.RFC3339, "2024-01-01T00:00:00Z")return tsdiff --git a/backend/store/signal.go b/backend/store/signal.goindex bf6438335b..a86819eca5 100644--- a/backend/store/signal.go+++ b/backend/store/signal.go@@ -13,7 +13,7 @@ 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, projectID string, uid int32) error {+func (s *Store) SendSignal(ctx context.Context, signalType storepb.Signal_Type, projectID string, uid int64) error {payload, err := protojson.Marshal(&storepb.Signal{Type: signalType,Uid: uid,diff --git a/backend/store/task.go b/backend/store/task.goindex 39a6878a3a..e3db4963a7 100644--- a/backend/store/task.go+++ b/backend/store/task.go@@ -16,7 +16,7 @@ import (// TaskMessage is the message for tasks.type TaskMessage struct {- ID int+ ID int64// Related fieldsProjectID string@@ -49,8 +49,8 @@ func (t *TaskMessage) GetDatabaseName() string {// TaskFind is the API message for finding tasks.type TaskFind struct {- ID *int- IDs *[]int+ ID *int64+ IDs *[]int64// Related fieldsProjectID string@@ -67,7 +67,7 @@ type TaskFind struct {}// GetTaskByID gets a task by ID.-func (s *Store) GetTaskByID(ctx context.Context, projectID string, id int) (*TaskMessage, error) {+func (s *Store) GetTaskByID(ctx context.Context, projectID string, id int64) (*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)@@ -82,7 +82,7 @@ func (s *Store) GetTaskByID(ctx context.Context, projectID string, id int) (*Tas// 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, projectID string, 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) (*int64, error) {myVersion, err := model.NewVersion(version)if err != nil {return nil, err@@ -92,14 +92,14 @@ func (s *Store) FindBlockingTaskByVersion(ctx context.Context, projectID string,task.id,task.payload->>'schemaVersion'FROM task- LEFT JOIN plan ON plan.id = task.plan_id- LEFT JOIN issue ON issue.plan_id = plan.id+ LEFT JOIN plan ON plan.project = task.project AND plan.id = task.plan_id+ LEFT JOIN issue ON issue.project = plan.project AND issue.plan_id = plan.idLEFT JOIN LATERAL (SELECT COALESCE((SELECTtask_run.statusFROM task_run- WHERE task_run.task_id = task.id+ WHERE task_run.project = task.project AND task_run.task_id = task.idORDER BY task_run.id DESCLIMIT 1), 'NOT_STARTED'@@ -121,7 +121,7 @@ func (s *Store) FindBlockingTaskByVersion(ctx context.Context, projectID string,}defer rows.Close()for rows.Next() {- var id int+ var id int64var v stringif err := rows.Scan(&id, &v); err != nil {return nil, errors.Wrapf(err, "failed to scan rows")@@ -142,7 +142,18 @@ func (s *Store) FindBlockingTaskByVersion(ctx context.Context, projectID string,// createTasksTx creates tasks in a transaction.func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMessage) ([]*TaskMessage, error) {+ if len(creates) == 0 {+ return nil, nil+ }++ projectID := creates[0].ProjectID+ baseID, err := nextProjectID(ctx, txn, "task", projectID)+ if err != nil {+ return nil, err+ }+var (+ ids []int64projects []stringplanIDs []int64instances []string@@ -151,7 +162,7 @@ func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMetypes []stringpayloads [][]byte)- for _, create := range creates {+ for i, create := range creates {if create.Payload == nil {create.Payload = &storepb.Task{}}@@ -159,6 +170,7 @@ func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMeif err != nil {return nil, errors.Wrapf(err, "failed to marshal payload")}+ ids = append(ids, baseID+int64(i))projects = append(projects, create.ProjectID)planIDs = append(planIDs, create.PlanID)instances = append(instances, create.InstanceID)@@ -170,6 +182,7 @@ func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMeq := qb.Q().Space(`INSERT INTO task (+ id,project,plan_id,instance,@@ -178,6 +191,7 @@ func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMetype,payload) SELECT+ unnest(CAST(? AS BIGINT[])),unnest(CAST(? AS TEXT[])),unnest(CAST(? AS BIGINT[])),unnest(CAST(? AS TEXT[])),@@ -186,6 +200,7 @@ func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMeunnest(CAST(? AS TEXT[])),unnest(CAST(? AS JSONB[]))RETURNING id, project, plan_id, instance, db_name, environment, type, payload`,+ ids,projects,planIDs,instances,@@ -377,7 +392,7 @@ func (s *Store) ListTasks(ctx context.Context, find *TaskFind) ([]*TaskMessage,}// BatchSkipTasks batch skip tasks.-func (s *Store) BatchSkipTasks(ctx context.Context, projectID string, taskUIDs []int, comment string) error {+func (s *Store) BatchSkipTasks(ctx context.Context, projectID string, taskUIDs []int64, comment string) error {q := qb.Q().Space(`UPDATE taskSET payload = payload || jsonb_build_object('skipped', ?::BOOLEAN) || jsonb_build_object('skippedReason', ?::TEXT)diff --git a/backend/store/task_run.go b/backend/store/task_run.goindex 18e297f4d0..304e5764b3 100644--- a/backend/store/task_run.go+++ b/backend/store/task_run.go@@ -15,7 +15,7 @@ import (// TaskRunMessage is message for task run.type TaskRunMessage struct {- TaskUID int+ TaskUID int64Environment string // Refer to the task's environment.PlanUID int64Status storepb.TaskRun_Status@@ -23,7 +23,7 @@ type TaskRunMessage struct {PayloadProto *storepb.TaskRunPayload// Output only.- ID int+ ID int64CreatorEmail stringCreatedAt time.TimeUpdatedAt time.Time@@ -34,10 +34,10 @@ type TaskRunMessage struct {// FindTaskRunMessage is the message for finding task runs.type FindTaskRunMessage struct {- UID *int- UIDs *[]int+ UID *int64+ UIDs *[]int64ProjectID string- TaskUID *int+ TaskUID *int64Environment *stringPlanUID *int64Status *[]storepb.TaskRun_Status@@ -45,7 +45,7 @@ type FindTaskRunMessage struct {// TaskRunStatusPatch is the API message for patching a task run.type TaskRunStatusPatch struct {- ID int+ ID int64ProjectID string// Standard fields@@ -74,7 +74,7 @@ func (s *Store) ListTaskRuns(ctx context.Context, find *FindTaskRunMessage) ([]*task.environment,task_run.projectFROM task_run- LEFT JOIN task ON task.id = task_run.task_id+ LEFT JOIN task ON task.project = task_run.project AND task.id = task_run.task_idWHERE task_run.project = ?`, find.ProjectID)@@ -188,7 +188,7 @@ func (s *Store) GetTaskRunV1(ctx context.Context, find *FindTaskRunMessage) (*Ta}// GetTaskRunByUID gets a task run by uid.-func (s *Store) GetTaskRunByUID(ctx context.Context, projectID string, uid int) (*TaskRunMessage, error) {+func (s *Store) GetTaskRunByUID(ctx context.Context, projectID string, uid int64) (*TaskRunMessage, error) {return s.GetTaskRunV1(ctx, &FindTaskRunMessage{ProjectID: projectID, UID: &uid})}@@ -225,8 +225,8 @@ func (s *Store) UpdateTaskRunStatus(ctx context.Context, patch *TaskRunStatusPat// ClaimedTaskRun represents a claimed task run with its task UID.type ClaimedTaskRun struct {- TaskRunUID int- TaskUID int+ TaskRunUID int64+ TaskUID int64ProjectID string}@@ -270,7 +270,7 @@ func (s *Store) ClaimAvailableTaskRuns(ctx context.Context, replicaID string) ([return claimed, nil}-func (s *Store) UpdateTaskRunStartAt(ctx context.Context, projectID string, taskRunID int) error {+func (s *Store) UpdateTaskRunStartAt(ctx context.Context, projectID string, taskRunID int64) error {// Get the pipeline ID for cache invalidationq := qb.Q().Space(`UPDATE task_run@@ -301,7 +301,7 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creator string, creatreturn nil}- var taskUIDs []int+ var taskUIDs []int64var runAts []*time.Timevar projects []stringfor _, create := range creates {@@ -330,13 +330,47 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creator string, creat}}+ projectID := creates[0].ProjectID++ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return errors.Wrapf(err, "failed to begin tx")+ }+ defer tx.Rollback()++ baseID, err := nextProjectID(ctx, tx, "task_run", projectID)+ if err != nil {+ return err+ }+// Single query that:- // 1. Filters out tasks with existing PENDING/RUNNING/DONE task runs (idempotent)- // 2. Calculates next attempt for each remaining task- // 3. Inserts task runs- // 4. Uses ON CONFLICT DO NOTHING to handle race conditions+ // 1. Assigns per-project IDs using ROW_NUMBER() + baseID+ // 2. Filters out tasks with existing PENDING/RUNNING/DONE task runs (idempotent)+ // 3. Calculates next attempt for each remaining task+ // 4. Inserts task runs+ // 5. Uses ON CONFLICT DO NOTHING to handle race conditionsq := qb.Q().Space(`+ WITH candidates AS (+ SELECT+ (ROW_NUMBER() OVER ()) + ? - 1 AS new_id,+ tasks.project,+ tasks.task_id,+ tasks.run_at+ FROM (+ SELECT+ unnest(CAST(? AS TEXT[])) AS project,+ unnest(CAST(? AS BIGINT[])) AS task_id,+ unnest(CAST(? AS TIMESTAMPTZ[])) AS run_at+ ) tasks+ WHERE NOT EXISTS (+ SELECT 1 FROM task_run+ WHERE task_run.task_id = tasks.task_id+ AND task_run.project = tasks.project+ AND task_run.status IN (?, ?, ?, ?)+ )+ )INSERT INTO task_run (+ id,creator,project,task_id,@@ -346,37 +380,33 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creator string, creatpayload)SELECT+ candidates.new_id,?,- 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,+ candidates.project,+ candidates.task_id,+ candidates.run_at,+ COALESCE((SELECT MAX(attempt) + 1 FROM task_run WHERE task_run.task_id = candidates.task_id AND task_run.project = candidates.project), 0),?,?- FROM (- SELECT- unnest(CAST(? AS TEXT[])) AS project,- unnest(CAST(? AS INTEGER[])) AS task_id,- unnest(CAST(? AS TIMESTAMPTZ[])) AS run_at- ) tasks- WHERE NOT EXISTS (- SELECT 1 FROM task_run- WHERE task_run.task_id = tasks.task_id- AND task_run.status IN (?, ?, ?, ?)- )+ FROM candidatesON 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())+ `, baseID, projects, taskUIDs, runAts,+ storepb.TaskRun_PENDING.String(), storepb.TaskRun_AVAILABLE.String(), storepb.TaskRun_RUNNING.String(), storepb.TaskRun_DONE.String(),+ creatorPtr, storepb.TaskRun_PENDING.String(), payloadStr)query, args, err := q.ToSQL()if err != nil {return errors.Wrapf(err, "failed to build sql")}- if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to create pending task runs")}+ if err := tx.Commit(); err != nil {+ return errors.Wrapf(err, "failed to commit tx")+ }+return nil}@@ -442,7 +472,7 @@ 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, projectID string, taskRunIDs []int) error {+func (s *Store) BatchCancelTaskRuns(ctx context.Context, projectID string, taskRunIDs []int64) error {if len(taskRunIDs) == 0 {return nil}@@ -496,7 +526,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, projectID string, taskRunID int, payload *storepb.TaskRunPayload) error {+func (s *Store) UpdateTaskRunPayload(ctx context.Context, projectID string, taskRunID int64, payload *storepb.TaskRunPayload) error {payloadBytes, err := protojson.Marshal(payload)if err != nil {return errors.Wrapf(err, "failed to marshal task run payload")@@ -546,7 +576,7 @@ func (s *Store) ListTaskRunsByStatus(ctx context.Context, statuses []storepb.Tastask.environment,task_run.projectFROM task_run- LEFT JOIN task ON task.id = task_run.task_id+ LEFT JOIN task ON task.project = task_run.project AND task.id = task_run.task_idWHERE task_run.status = ANY(?)ORDER BY task_run.id ASC`, statusStrings)diff --git a/backend/store/task_run_log.go b/backend/store/task_run_log.goindex cbe7b40662..aa5a0dbd9f 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, projectID string, taskRunUID int, t time.Time, replicaID string, e *storepb.TaskRunLog) {+func (s *Store) CreateTaskRunLogS(ctx context.Context, projectID string, taskRunUID int64, 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, projectID string, taskRunUID int, t time.Time, replicaID string, e *storepb.TaskRunLog) error {+func (s *Store) CreateTaskRunLog(ctx context.Context, projectID string, taskRunUID int64, t time.Time, replicaID string, e *storepb.TaskRunLog) error {e.ReplicaId = replicaIDp, err := protojson.Marshal(e)if err != nil {@@ -57,7 +57,7 @@ func (s *Store) CreateTaskRunLog(ctx context.Context, projectID string, taskRunUreturn nil}-func (s *Store) ListTaskRunLogs(ctx context.Context, projectID string, taskRunUID int) ([]*TaskRunLog, error) {+func (s *Store) ListTaskRunLogs(ctx context.Context, projectID string, taskRunUID int64) ([]*TaskRunLog, error) {q := qb.Q().Space(`SELECTcreated_at,diff --git a/docs/plans/saas/01.prepare-for-saas.md b/docs/plans/saas/01.prepare-for-saas.mdindex 4ca164aba2..0cc3f9364d 100644--- a/docs/plans/saas/01.prepare-for-saas.md+++ b/docs/plans/saas/01.prepare-for-saas.md@@ -247,9 +247,9 @@ For SaaS, tables need different ID strategies depending on their scope:| `instance` | `resource_id text` | Global | None || `db` | `(instance, name)` | Instance | None || `db_schema` | `(instance, db_name)` | Instance | None |-| `revision` | `bigserial` | Instance | Drop `id` as PK, use `resource_id text` as PK |-| `sync_history` | `bigserial` | Instance | Add `resource_id text`, use as PK |-| `changelog` | `bigserial` | Instance | Drop `id` as PK, use `resource_id text` as PK; replace `sync_history_id` with `sync_history text` FK |+| `revision` | `bigserial` | Instance | Per-instance auto-increment id, add FK to `instance(resource_id)` |+| `sync_history` | `bigserial` | Instance | Per-instance auto-increment id |+| `changelog` | `bigserial` | Instance | Per-instance auto-increment id || **Workspace-scoped (need `workspace_id`)** | | | || `idp` | `resource_id text` | Workspace | Add `workspace_id` || `project` | `resource_id text` | Workspace | Add `workspace_id` |@@ -418,12 +418,12 @@ changelog: drop `id` as PK, use `resource_id text PRIMARY KEY DEFAULT gen_random```Store layer changes:-- `sync_history.go`: `GetSyncHistoryByUID(ctx, instanceID, uid)` → `GetSyncHistory(ctx, resourceID)`, `CreateSyncHistory` returns `(string, error)` (resource_id)+- `sync_history.go`: `GetSyncHistoryByUID(ctx, uid)` → `GetSyncHistory(ctx, resourceID)`, `CreateSyncHistory` returns `(string, error)` (resource_id)- `changelog.go`: `SyncHistoryUID *int64` → `SyncHistory *string` (text FK), `FindChangelogMessage.InstanceID` becomes required (`string` not `*string`)- `revision.go`: `FindRevisionMessage.InstanceID` becomes required (`string` not `*string`)- `schemasync/syncer.go`: `SyncDatabaseSchemaToHistory` returns `(string, error)` instead of `(int64, error)`-#### Step 3: Per-Project / Per-Instance Auto-Increment IDs+#### Step 3: Per-Project Auto-Increment IDsAfter Step 2, project-scoped tables have composite PKs `(project, id)`. Instance-scoped tables (`revision`, `sync_history`, `changelog`) use `resource_id` (uuid) as PK instead. Now switch project-scoped tables from global sequences to per-project auto-increment.@@ -439,24 +439,39 @@ Tables affected:- (`worksheet` uses `resource_id` (UUID) as the external identifier — per-project auto-increment not needed for API, but `id` still auto-increments via sequence for the composite PK)- Instance-scoped: none — `revision`, `sync_history`, `changelog` now use `resource_id` (uuid) as PK (Step 2e), no per-instance auto-increment needed-#### Step 4: Cross-Workspace Tables — Text UUID PKs+#### Step 4: Cross-Workspace Tables — UUID PKs-For tables with no natural scope, replace `serial`/`bigserial` with `text` PK using `gen_random_uuid()`:+For tables with no natural scope, add `resource_id text` column and promote to PK. Skip `instance_change_history` — it's only used for Bytebase's own metadata migration and doesn't need the change.-| Table | Current | After |-|---|---|---|-| `audit_log` | `id bigserial PRIMARY KEY` | `id text PRIMARY KEY DEFAULT gen_random_uuid()::text` |-| `query_history` | `id bigserial PRIMARY KEY` | `id text PRIMARY KEY DEFAULT gen_random_uuid()::text`, fix `project_id` to proper FK |-| `instance_change_history` | `id bigserial PRIMARY KEY` | `id text PRIMARY KEY DEFAULT gen_random_uuid()::text` |-| `export_archive` | `id serial PRIMARY KEY` | `id text PRIMARY KEY DEFAULT gen_random_uuid()::text` |--Also update any FK references to these tables (e.g., `changelog.sync_history` is already a text FK to `sync_history(resource_id)` after Step 2e, `task_run.result` JSONB field `exportArchiveUid` needs proto/data migration).+| Table | Current PK | After PK | Notes |+|---|---|---|---|+| `audit_log` | `id bigserial` | `resource_id text` | Old `id` kept as non-PK |+| `query_history` | `id bigserial` | `resource_id text` | Old `id` kept as non-PK; change resource name from `queryHistories/{id}` to `projects/{project}/queryHistories/{id}` |+| `export_archive` | `id serial` | `resource_id text` | Old `id` kept as non-PK until JSONB migration completes |++Migration approach (same for all three tables):+1. `ALTER TABLE <table> ADD COLUMN resource_id text DEFAULT gen_random_uuid()::text`+2. Backfill: `UPDATE <table> SET resource_id = gen_random_uuid()::text WHERE resource_id IS NULL`+3. `ALTER TABLE <table> ALTER COLUMN resource_id SET NOT NULL`+4. Drop old PK on `id`, add PK on `resource_id`+5. Old `id` column stays as non-PK (drop later in cleanup)++**export_archive JSONB migration**: `TaskRunResult.export_archive_uid` (int32) → `TaskRunResult.export_archive_id` (string). Requires:+- Proto: rename field `export_archive_uid` → `export_archive_id`, change type `int32` → `string`+- DML: `UPDATE task_run SET result = jsonb_set(result - 'exportArchiveUid', '{exportArchiveId}', to_jsonb(ea.resource_id)) FROM export_archive ea WHERE (result->>'exportArchiveUid')::int = ea.id` (old `id` column is still present for the join)+- Go code: update all references from `ExportArchiveUid` → `ExportArchiveId`+- After JSONB migration is complete, old `id` column on `export_archive` can be dropped++**query_history resource name change**: `queryHistories/{id}` → `projects/{project}/queryHistories/{id}`+- Proto: update `QueryHistory.name` format comment+- API: update `SearchQueryHistories` to format/parse with project prefix+- Store: `query_history` already has `project_id` column, no schema change needed for this### JSONB / Proto Fields Storing Integer IDs| JSONB Location | Proto Field | Type | References | Needs migration? ||---|---|---|---|---|-| `task_run.result` | `TaskRunResult.export_archive_uid` | `int32` | `export_archive.id` | **Yes** — `export_archive.id` changes to text uuid in Step 4 |+| `task_run.result` | `TaskRunResult.export_archive_uid` | `int32` | `export_archive.id` | **Yes (Step 4)** — rename to `export_archive_id`, change to `string`, DML to backfill with `export_archive.resource_id` || `access_grant.payload` | `AccessGrantPayload.issue_id` | `int64` | `issue.id` | No — `issue.id` stays integer (per-project bigint) || PG NOTIFY channel | `Signal.uid` | `int32` | `task_run.id` or `plan_check_run.id` | **Yes (Step 3)** — IDs are only unique per project after Step 3, so Signal must include `project` alongside `uid` to uniquely identify the resource |diff --git a/frontend/src/views/DatabaseDetail/ChangelogDetail.vue b/frontend/src/views/DatabaseDetail/ChangelogDetail.vueindex d5534916e2..1b93ba3aca 100644--- a/frontend/src/views/DatabaseDetail/ChangelogDetail.vue+++ b/frontend/src/views/DatabaseDetail/ChangelogDetail.vue@@ -1,28 +1,30 @@<template>- <NBreadcrumb class="mb-4">- <NBreadcrumbItem @click="router.push(`/${props.project}/databases`)">- {{ $t("common.databases") }}- </NBreadcrumbItem>- <NBreadcrumbItem @click="router.push(databaseV1Url(database))">- {{ extractDatabaseResourceName(database.name).databaseName }}- </NBreadcrumbItem>- <NBreadcrumbItem- @click="router.push(`${databaseV1Url(database)}#changelog`)"- >- {{ $t("changelog.self") }}- </NBreadcrumbItem>- <NBreadcrumbItem :clickable="false">- {{ changelogId }}- </NBreadcrumbItem>- </NBreadcrumb>- <ChangelogDetailView- v-if="ready"- :instance="instance"- :database="database.name"- :changelog-id="changelogId"- />- <div v-else class="flex justify-center items-center py-10">- <BBSpin />+ <div>+ <NBreadcrumb class="mb-4">+ <NBreadcrumbItem @click="router.push(`/${props.project}/databases`)">+ {{ $t("common.databases") }}+ </NBreadcrumbItem>+ <NBreadcrumbItem @click="router.push(databaseV1Url(database))">+ {{ extractDatabaseResourceName(database.name).databaseName }}+ </NBreadcrumbItem>+ <NBreadcrumbItem+ @click="router.push(`${databaseV1Url(database)}#changelog`)"+ >+ {{ $t("changelog.self") }}+ </NBreadcrumbItem>+ <NBreadcrumbItem :clickable="false">+ {{ changelogId }}+ </NBreadcrumbItem>+ </NBreadcrumb>+ <ChangelogDetailView+ v-if="ready"+ :instance="instance"+ :database="database.name"+ :changelog-id="changelogId"+ />+ <div v-else class="flex justify-center items-center py-10">+ <BBSpin />+ </div></div></template>diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex 06e4d68aa8..cf7c1ee169 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -4633,7 +4633,7 @@ Signal represents a notification payload sent via PostgreSQL NOTIFY for HA coord| Field | Type | Label | Description || ----- | ---- | ----- | ----------- || type | [Signal.Type](#bytebase-store-Signal-Type) | | |-| uid | [int32](#int32) | | |+| uid | [int64](#int64) | | || project | [string](#string) | | |diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.htmlindex 1d3cfa1a20..231176f15b 100644--- a/proto/gen/grpc-doc/store/index.html+++ b/proto/gen/grpc-doc/store/index.html@@ -12740,7 +12740,7 @@ Default to this mode.</p></td><tr><td>uid</td>- <td><a href="#int32">int32</a></td>+ <td><a href="#int64">int64</a></td><td></td><td><p> </p></td></tr>diff --git a/proto/store/store/signal.proto b/proto/store/store/signal.protoindex 6f20bb65d8..b91fb671b1 100644--- a/proto/store/store/signal.proto+++ b/proto/store/store/signal.proto@@ -14,6 +14,6 @@ message Signal {}Type type = 1;- int32 uid = 2;+ int64 uid = 2;string project = 3;}