Skip to content

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

refactor: migrate API-exposed IDs from integer to string resource_id (#19592)

ecmadao273155ccbfed
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index 560bdc7024..ff7387bb80 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -725,19 +725,19 @@ func (s *DatabaseService) DiffSchema(ctx context.Context, req *connect.Request[v
func (s *DatabaseService) getSourceDBMetadata(ctx context.Context, request *v1pb.DiffSchemaRequest) (*model.DatabaseMetadata, error) {
if strings.Contains(request.Name, common.ChangelogPrefix) {
- instanceID, databaseName, changelogUID, err := common.GetInstanceDatabaseChangelogUID(request.Name)
+ instanceID, databaseName, changelogID, err := common.GetInstanceDatabaseChangelogID(request.Name)
if err != nil {
return nil, err
}
changelog, err := s.store.GetChangelog(ctx, &store.FindChangelogMessage{
- UID: &changelogUID,
+ ResourceID: &changelogID,
})
if err != nil {
return nil, err
}
if changelog == nil {
- return nil, errors.Errorf("changelog %d not found", changelogUID)
+ return nil, errors.Errorf("changelog %q not found", changelogID)
}
// Use SyncHistoryUID to get historical metadata
@@ -805,19 +805,19 @@ func (s *DatabaseService) getTargetDBMetadata(ctx context.Context, request *v1pb
// If the change history id is set, use the schema of the change history as the target.
if changeHistoryID != "" {
- instanceID, databaseName, changelogUID, err := common.GetInstanceDatabaseChangelogUID(changeHistoryID)
+ instanceID, databaseName, changelogID, err := common.GetInstanceDatabaseChangelogID(changeHistoryID)
if err != nil {
return nil, err
}
changelog, err := s.store.GetChangelog(ctx, &store.FindChangelogMessage{
- UID: &changelogUID,
+ ResourceID: &changelogID,
})
if err != nil {
return nil, err
}
if changelog == nil {
- return nil, errors.Errorf("changelog %d not found", changelogUID)
+ return nil, errors.Errorf("changelog %q not found", changelogID)
}
// Use SyncHistoryUID to get historical metadata
@@ -907,7 +907,7 @@ func (s *DatabaseService) getParserEngine(ctx context.Context, request *v1pb.Dif
var instanceID string
if strings.Contains(request.Name, common.ChangelogPrefix) {
- insID, _, _, err := common.GetInstanceDatabaseChangelogUID(request.Name)
+ insID, _, _, err := common.GetInstanceDatabaseChangelogID(request.Name)
if err != nil {
return storepb.Engine_ENGINE_UNSPECIFIED, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("%v", err.Error()))
}
diff --git a/backend/api/v1/database_service_changelog.go b/backend/api/v1/database_service_changelog.go
index 5eca72368c..42c3895fb4 100644
--- a/backend/api/v1/database_service_changelog.go
+++ b/backend/api/v1/database_service_changelog.go
@@ -150,13 +150,13 @@ func (s *DatabaseService) ListChangelogs(ctx context.Context, req *connect.Reque
}
func (s *DatabaseService) GetChangelog(ctx context.Context, req *connect.Request[v1pb.GetChangelogRequest]) (*connect.Response[v1pb.Changelog], error) {
- instanceID, databaseName, changelogUID, err := common.GetInstanceDatabaseChangelogUID(req.Msg.Name)
+ instanceID, databaseName, changelogID, err := common.GetInstanceDatabaseChangelogID(req.Msg.Name)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
find := &store.FindChangelogMessage{
- UID: &changelogUID,
+ ResourceID: &changelogID,
}
if req.Msg.View == v1pb.ChangelogView_CHANGELOG_VIEW_FULL {
find.ShowFull = true
@@ -167,7 +167,7 @@ func (s *DatabaseService) GetChangelog(ctx context.Context, req *connect.Request
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list changelogs"))
}
if changelog == nil {
- return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("changelog %d not found", changelogUID))
+ return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("changelog %q not found", changelogID))
}
database, err := s.store.GetDatabase(ctx, &store.FindDatabaseMessage{
@@ -197,7 +197,7 @@ func (s *DatabaseService) convertToChangelogs(_ context.Context, d *store.Databa
func (*DatabaseService) convertToChangelog(d *store.DatabaseMessage, c *store.ChangelogMessage) *v1pb.Changelog {
cl := &v1pb.Changelog{
- Name: common.FormatChangelog(d.InstanceID, d.DatabaseName, c.UID),
+ Name: common.FormatChangelog(d.InstanceID, d.DatabaseName, c.ResourceID),
CreateTime: timestamppb.New(c.CreatedAt),
Status: convertToChangelogStatus(c.Status),
Schema: "",
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.go
index b550649938..7ff9c3ff29 100644
--- a/backend/api/v1/issue_service.go
+++ b/backend/api/v1/issue_service.go
@@ -1136,14 +1136,14 @@ func (s *IssueService) UpdateIssueComment(ctx context.Context, req *connect.Requ
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid parent %q: %v", req.Msg.Parent, err))
}
- _, commentIssueUID, issueCommentUID, err := common.GetProjectIDIssueUIDIssueCommentUID(req.Msg.IssueComment.Name)
+ _, commentIssueUID, issueCommentID, err := common.GetProjectIDIssueUIDIssueCommentID(req.Msg.IssueComment.Name)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid comment name %q: %v", req.Msg.IssueComment.Name, err))
}
if parentIssueUID != commentIssueUID {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("issue comment %q does not belong to parent %q", req.Msg.IssueComment.Name, req.Msg.Parent))
}
- issueComment, err := s.store.GetIssueComment(ctx, &store.FindIssueCommentMessage{UID: &issueCommentUID, IssueUID: &parentIssueUID})
+ issueComment, err := s.store.GetIssueComment(ctx, &store.FindIssueCommentMessage{ResourceID: &issueCommentID, IssueUID: &parentIssueUID})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to get issue comment: %v", err))
}
@@ -1164,7 +1164,7 @@ func (s *IssueService) UpdateIssueComment(ctx context.Context, req *connect.Requ
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("issue comment not found"))
}
update := &store.UpdateIssueCommentMessage{
- UID: issueCommentUID,
+ ResourceID: issueCommentID,
}
for _, path := range req.Msg.UpdateMask.Paths {
switch path {
@@ -1181,7 +1181,7 @@ func (s *IssueService) UpdateIssueComment(ctx context.Context, req *connect.Requ
}
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to update the issue comment with error: %v", err.Error()))
}
- issueComment, err = s.store.GetIssueComment(ctx, &store.FindIssueCommentMessage{UID: &issueCommentUID})
+ issueComment, err = s.store.GetIssueComment(ctx, &store.FindIssueCommentMessage{ResourceID: &issueCommentID})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to get issue comment: %v", err))
}
diff --git a/backend/api/v1/issue_service_converter.go b/backend/api/v1/issue_service_converter.go
index 465ca5c5ea..5819cc4dee 100644
--- a/backend/api/v1/issue_service_converter.go
+++ b/backend/api/v1/issue_service_converter.go
@@ -296,7 +296,7 @@ func convertToIssueComment(issueName string, ic *store.IssueCommentMessage) *v1p
Comment: ic.Payload.Comment,
CreateTime: timestamppb.New(ic.CreatedAt),
UpdateTime: timestamppb.New(ic.UpdatedAt),
- Name: fmt.Sprintf("%s/%s%d", issueName, common.IssueCommentNamePrefix, ic.UID),
+ Name: fmt.Sprintf("%s/%s%s", issueName, common.IssueCommentNamePrefix, ic.ResourceID),
Creator: common.FormatUserEmail(ic.CreatorEmail),
}
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 61f85f9646..e63b164ec6 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log/slog"
- "strconv"
"strings"
"time"
@@ -774,10 +773,6 @@ func (s *ProjectService) UpdateWebhook(ctx context.Context, req *connect.Request
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
- webhookIDInt, err := strconv.Atoi(webhookID)
- if err != nil {
- return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid webhook id %q", webhookID))
- }
project, err := s.store.GetProject(ctx, &store.FindProjectMessage{
ResourceID: &projectID,
@@ -797,8 +792,8 @@ func (s *ProjectService) UpdateWebhook(ctx context.Context, req *connect.Request
}
webhook, err := s.store.GetProjectWebhook(ctx, &store.FindProjectWebhookMessage{
- ProjectID: &project.ResourceID,
- ID: &webhookIDInt,
+ ProjectID: &project.ResourceID,
+ ResourceID: &webhookID,
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
@@ -851,7 +846,7 @@ func (s *ProjectService) UpdateWebhook(ctx context.Context, req *connect.Request
Payload: updatedPayload,
}
- if _, err := s.store.UpdateProjectWebhook(ctx, project.ResourceID, webhook.ID, update); err != nil {
+ if _, err := s.store.UpdateProjectWebhook(ctx, project.ResourceID, webhook.ResourceID, update); err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
@@ -870,10 +865,6 @@ func (s *ProjectService) RemoveWebhook(ctx context.Context, req *connect.Request
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
- webhookIDInt, err := strconv.Atoi(webhookID)
- if err != nil {
- return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid webhook id %q", webhookID))
- }
project, err := s.store.GetProject(ctx, &store.FindProjectMessage{
ResourceID: &projectID,
@@ -889,8 +880,8 @@ func (s *ProjectService) RemoveWebhook(ctx context.Context, req *connect.Request
}
webhook, err := s.store.GetProjectWebhook(ctx, &store.FindProjectWebhookMessage{
- ProjectID: &project.ResourceID,
- ID: &webhookIDInt,
+ ProjectID: &project.ResourceID,
+ ResourceID: &webhookID,
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
@@ -899,7 +890,7 @@ func (s *ProjectService) RemoveWebhook(ctx context.Context, req *connect.Request
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("webhook %q not found", req.Msg.Webhook.Url))
}
- if err := s.store.DeleteProjectWebhook(ctx, project.ResourceID, webhook.ID); err != nil {
+ if err := s.store.DeleteProjectWebhook(ctx, project.ResourceID, webhook.ResourceID); err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
@@ -969,7 +960,7 @@ func (s *ProjectService) TestWebhook(ctx context.Context, req *connect.Request[v
Title: fmt.Sprintf("Test webhook %q", webhook.Payload.GetTitle()),
TitleZh: fmt.Sprintf("测试 webhook %q", webhook.Payload.GetTitle()),
Description: "This is a test",
- Link: fmt.Sprintf("%s/projects/%s/webhooks/%s", externalURL, project.ResourceID, fmt.Sprintf("%s-%d", slug.Make(webhook.Payload.GetTitle()), webhook.ID)),
+ Link: fmt.Sprintf("%s/projects/%s/webhooks/%s", externalURL, project.ResourceID, fmt.Sprintf("%s-%s", slug.Make(webhook.Payload.GetTitle()), webhook.ResourceID)),
ActorID: userMessage.ID,
ActorName: userMessage.Name,
ActorEmail: userMessage.Email,
diff --git a/backend/api/v1/project_service_converter.go b/backend/api/v1/project_service_converter.go
index c252527ec8..95b122d4ac 100644
--- a/backend/api/v1/project_service_converter.go
+++ b/backend/api/v1/project_service_converter.go
@@ -251,7 +251,7 @@ func convertToProject(projectMessage *store.ProjectMessage) *v1pb.Project {
var projectWebhooks []*v1pb.Webhook
for _, webhook := range projectMessage.Webhooks {
projectWebhooks = append(projectWebhooks, &v1pb.Webhook{
- Name: fmt.Sprintf("%s/%s%d", common.FormatProject(projectMessage.ResourceID), common.WebhookIDPrefix, webhook.ID),
+ Name: fmt.Sprintf("%s/%s%s", common.FormatProject(projectMessage.ResourceID), common.WebhookIDPrefix, webhook.ResourceID),
Type: convertToV1WebhookType(webhook.Payload.GetType()),
Title: webhook.Payload.GetTitle(),
Url: webhook.Payload.GetUrl(),
diff --git a/backend/api/v1/revision_service.go b/backend/api/v1/revision_service.go
index 805b743ff8..d950563d87 100644
--- a/backend/api/v1/revision_service.go
+++ b/backend/api/v1/revision_service.go
@@ -97,9 +97,9 @@ func (s *RevisionService) GetRevision(
req *connect.Request[v1pb.GetRevisionRequest],
) (*connect.Response[v1pb.Revision], error) {
request := req.Msg
- instanceID, databaseName, revisionUID, err := common.GetInstanceDatabaseRevisionID(request.Name)
+ instanceID, databaseName, revisionID, err := common.GetInstanceDatabaseRevisionID(request.Name)
if err != nil {
- return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "failed to get revision UID from %v", request.Name))
+ return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "failed to get revision ID from %v", request.Name))
}
database, err := s.store.GetDatabase(ctx, &store.FindDatabaseMessage{
InstanceID: &instanceID,
@@ -113,7 +113,7 @@ func (s *RevisionService) GetRevision(
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("database not found"))
}
- revision, err := s.store.GetRevision(ctx, revisionUID, instanceID, databaseName)
+ revision, err := s.store.GetRevision(ctx, revisionID, instanceID, databaseName)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get revision %v", request.Name))
}
@@ -271,7 +271,7 @@ func (s *RevisionService) DeleteRevision(
req *connect.Request[v1pb.DeleteRevisionRequest],
) (*connect.Response[emptypb.Empty], error) {
request := req.Msg
- instanceID, databaseName, revisionUID, err := common.GetInstanceDatabaseRevisionID(request.Name)
+ instanceID, databaseName, revisionID, err := common.GetInstanceDatabaseRevisionID(request.Name)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "failed to parse %v", request.Name))
}
@@ -279,7 +279,7 @@ func (s *RevisionService) DeleteRevision(
if !ok {
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("user not found"))
}
- if err := s.store.DeleteRevision(ctx, revisionUID, instanceID, databaseName, user.Email); err != nil {
+ if err := s.store.DeleteRevision(ctx, revisionID, instanceID, databaseName, user.Email); err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to delete revision %v", request.Name))
}
return connect.NewResponse(&emptypb.Empty{}), nil
@@ -297,7 +297,7 @@ func convertToRevisions(parent string, projectID string, revisions []*store.Revi
func convertToRevision(parent string, projectID string, revision *store.RevisionMessage) *v1pb.Revision {
taskRunName := revision.Payload.TaskRun
r := &v1pb.Revision{
- Name: fmt.Sprintf("%s/%s%d", parent, common.RevisionNamePrefix, revision.UID),
+ Name: fmt.Sprintf("%s/%s%s", parent, common.RevisionNamePrefix, revision.ResourceID),
Release: revision.Payload.Release,
CreateTime: timestamppb.New(revision.CreatedAt),
Sheet: common.FormatSheet(projectID, revision.Payload.SheetSha256),
diff --git a/backend/common/resource_name.go b/backend/common/resource_name.go
index c91b974aaa..40d468345d 100644
--- a/backend/common/resource_name.go
+++ b/backend/common/resource_name.go
@@ -145,31 +145,23 @@ func GetInstanceDatabaseID(name string) (string, string, error) {
return tokens[0], tokens[1], nil
}
-// GetInstanceDatabaseRevisionID returns the instance ID, database ID, and revision UID from a resource name.
-func GetInstanceDatabaseRevisionID(name string) (string, string, int64, error) {
+// GetInstanceDatabaseRevisionID returns the instance ID, database ID, and revision ID from a resource name.
+func GetInstanceDatabaseRevisionID(name string) (string, string, string, error) {
tokens, err := GetNameParentTokens(name, InstanceNamePrefix, DatabaseIDPrefix, RevisionNamePrefix)
if err != nil {
- return "", "", 0, err
- }
- revisionUID, err := strconv.ParseInt(tokens[2], 10, 64)
- if err != nil {
- return "", "", 0, errors.Wrapf(err, "failed to convert %q to int64", tokens[2])
+ return "", "", "", err
}
- return tokens[0], tokens[1], revisionUID, nil
+ return tokens[0], tokens[1], tokens[2], nil
}
-// GetInstanceDatabaseChangelogUID returns the instance ID, database ID, and changelog UID from a resource name.
-func GetInstanceDatabaseChangelogUID(name string) (string, string, int64, error) {
+// GetInstanceDatabaseChangelogID returns the instance ID, database ID, and changelog ID from a resource name.
+func GetInstanceDatabaseChangelogID(name string) (string, string, string, error) {
// the name should be instances/{instance-id}/databases/{database-id}/changelogs/{changelog-id}
tokens, err := GetNameParentTokens(name, InstanceNamePrefix, DatabaseIDPrefix, ChangelogPrefix)
if err != nil {
- return "", "", 0, err
- }
- changelogUID, err := strconv.ParseInt(tokens[2], 10, 64)
- if err != nil {
- return "", "", 0, errors.Wrapf(err, "failed to convert %q to int64", tokens[2])
+ return "", "", "", err
}
- return tokens[0], tokens[1], changelogUID, nil
+ return tokens[0], tokens[1], tokens[2], nil
}
// GetUserEmail returns the user email from a resource name.
@@ -212,21 +204,17 @@ func GetProjectIDIssueUID(name string) (string, int, error) {
return tokens[0], issueUID, nil
}
-// GetProjectIDIssueUIDIssueCommentUID returns the project ID, issue UID and issue comment UID from the issue comment name.
-func GetProjectIDIssueUIDIssueCommentUID(name string) (string, int, 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) {
tokens, err := GetNameParentTokens(name, ProjectNamePrefix, IssueNamePrefix, IssueCommentNamePrefix)
if err != nil {
- return "", 0, 0, err
+ return "", 0, "", err
}
issueUID, err := strconv.Atoi(tokens[1])
if err != nil {
- return "", 0, 0, errors.Errorf("invalid issue ID %q", tokens[1])
- }
- issueCommentUID, err := strconv.Atoi(tokens[2])
- if err != nil {
- return "", 0, 0, errors.Errorf("invalid issue comment ID %q", tokens[2])
+ return "", 0, "", errors.Errorf("invalid issue ID %q", tokens[1])
}
- return tokens[0], issueUID, issueCommentUID, nil
+ return tokens[0], issueUID, tokens[2], nil
}
// GetProjectIDPlanID returns the project ID and plan ID from a resource name.
@@ -624,12 +612,12 @@ func FormatReleaseFile(release string, fileID string) string {
return fmt.Sprintf("%s/%s%s", release, FileNamePrefix, fileID)
}
-func FormatRevision(instanceID, databaseID string, revisionUID int64) string {
- return fmt.Sprintf("%s/%s%d", FormatDatabase(instanceID, databaseID), RevisionNamePrefix, revisionUID)
+func FormatRevision(instanceID, databaseID string, revisionID string) string {
+ return fmt.Sprintf("%s/%s%s", FormatDatabase(instanceID, databaseID), RevisionNamePrefix, revisionID)
}
-func FormatChangelog(instanceID, databaseID string, changelogUID int64) string {
- return fmt.Sprintf("%s/%s%d", FormatDatabase(instanceID, databaseID), ChangelogPrefix, changelogUID)
+func FormatChangelog(instanceID, databaseID string, changelogID string) string {
+ return fmt.Sprintf("%s/%s%s", FormatDatabase(instanceID, databaseID), ChangelogPrefix, changelogID)
}
func FormatPlan(projectID string, planUID int64) string {
diff --git a/backend/migrator/migration/3.16/0003##add_resource_id_to_api_tables.sql b/backend/migrator/migration/3.16/0003##add_resource_id_to_api_tables.sql
new file mode 100644
index 0000000000..f375e6781f
--- /dev/null
+++ b/backend/migrator/migration/3.16/0003##add_resource_id_to_api_tables.sql
@@ -0,0 +1,22 @@
+-- Add resource_id column to API-exposed tables that currently use integer IDs.
+-- Use gen_random_uuid() as default for existing rows and new inserts.
+
+ALTER TABLE plan ADD COLUMN resource_id text NOT NULL DEFAULT gen_random_uuid()::text;
+ALTER TABLE task ADD COLUMN resource_id text NOT NULL DEFAULT gen_random_uuid()::text;
+ALTER TABLE task_run ADD COLUMN resource_id text NOT NULL DEFAULT gen_random_uuid()::text;
+ALTER TABLE issue ADD COLUMN resource_id text NOT NULL DEFAULT gen_random_uuid()::text;
+ALTER TABLE issue_comment ADD COLUMN resource_id text NOT NULL DEFAULT gen_random_uuid()::text;
+ALTER TABLE revision ADD COLUMN resource_id text NOT NULL DEFAULT gen_random_uuid()::text;
+ALTER TABLE changelog ADD COLUMN resource_id text NOT NULL DEFAULT gen_random_uuid()::text;
+ALTER TABLE worksheet ADD COLUMN resource_id text NOT NULL DEFAULT gen_random_uuid()::text;
+ALTER TABLE project_webhook ADD COLUMN resource_id text NOT NULL DEFAULT gen_random_uuid()::text;
+
+CREATE UNIQUE INDEX idx_plan_unique_resource_id ON plan(resource_id);
+CREATE UNIQUE INDEX idx_task_unique_resource_id ON task(resource_id);
+CREATE UNIQUE INDEX idx_task_run_unique_resource_id ON task_run(resource_id);
+CREATE UNIQUE INDEX idx_issue_unique_resource_id ON issue(resource_id);
+CREATE UNIQUE INDEX idx_issue_comment_unique_resource_id ON issue_comment(resource_id);
+CREATE UNIQUE INDEX idx_revision_unique_resource_id ON revision(resource_id);
+CREATE UNIQUE INDEX idx_changelog_unique_resource_id ON changelog(resource_id);
+CREATE UNIQUE INDEX idx_worksheet_unique_resource_id ON worksheet(resource_id);
+CREATE UNIQUE INDEX idx_project_webhook_unique_resource_id ON project_webhook(resource_id);
diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sql
index e06588f5cf..0adc58e34e 100644
--- a/backend/migrator/migration/LATEST.sql
+++ b/backend/migrator/migration/LATEST.sql
@@ -109,12 +109,14 @@ CREATE TABLE policy (
-- Project Hook
CREATE TABLE project_webhook (
id serial PRIMARY KEY,
+ resource_id text NOT NULL DEFAULT gen_random_uuid()::text,
project text NOT NULL REFERENCES project(resource_id),
-- Stored as ProjectWebhook (proto/store/store/project_webhook.proto)
payload jsonb NOT NULL DEFAULT '{}'
);
CREATE INDEX idx_project_webhook_project ON project_webhook(project);
+CREATE UNIQUE INDEX idx_project_webhook_unique_resource_id ON project_webhook(resource_id);
ALTER SEQUENCE project_webhook_id_seq RESTART WITH 101;
@@ -165,6 +167,7 @@ CREATE TABLE sheet_blob (
-- plan table stores the plan for a project
CREATE TABLE plan (
id bigserial PRIMARY KEY,
+ 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,6 +182,7 @@ 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;
@@ -211,6 +215,7 @@ CREATE TABLE plan_webhook_delivery (
-- task table stores the task for a plan
CREATE TABLE task (
id serial PRIMARY KEY,
+ resource_id text NOT NULL DEFAULT gen_random_uuid()::text,
plan_id bigint NOT NULL REFERENCES plan(id),
instance text NOT NULL REFERENCES instance(resource_id),
environment text,
@@ -221,12 +226,14 @@ CREATE TABLE task (
);
CREATE INDEX idx_task_plan_id_environment ON task(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 run
CREATE TABLE task_run (
id serial PRIMARY KEY,
+ 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(),
@@ -244,6 +251,7 @@ 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 (task_id, attempt);
@@ -276,6 +284,7 @@ CREATE TABLE task_run_log (
-- issue
CREATE TABLE issue (
id serial PRIMARY KEY,
+ 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(),
@@ -299,6 +308,7 @@ CREATE UNIQUE INDEX idx_issue_unique_plan_id ON issue(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;
@@ -333,6 +343,7 @@ ALTER SEQUENCE audit_log_id_seq RESTART WITH 101;
CREATE TABLE issue_comment (
id bigserial PRIMARY KEY,
+ 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(),
@@ -342,6 +353,7 @@ CREATE TABLE issue_comment (
);
CREATE INDEX idx_issue_comment_issue_id ON issue_comment(issue_id);
+CREATE UNIQUE INDEX idx_issue_comment_unique_resource_id ON issue_comment(resource_id);
ALTER SEQUENCE issue_comment_id_seq RESTART WITH 101;
@@ -366,6 +378,7 @@ ALTER SEQUENCE query_history_id_seq RESTART WITH 101;
-- worksheet table stores worksheets in SQL Editor.
CREATE TABLE worksheet (
id serial PRIMARY KEY,
+ 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(),
@@ -381,6 +394,7 @@ CREATE TABLE worksheet (
);
CREATE INDEX idx_worksheet_creator_project ON worksheet(creator, project);
+CREATE UNIQUE INDEX idx_worksheet_unique_resource_id ON worksheet(resource_id);
ALTER SEQUENCE worksheet_id_seq RESTART WITH 101;
@@ -439,6 +453,7 @@ CREATE TABLE review_config (
CREATE TABLE revision (
id bigserial PRIMARY KEY,
+ resource_id text NOT NULL DEFAULT gen_random_uuid()::text,
instance text NOT NULL,
db_name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
@@ -450,6 +465,8 @@ CREATE TABLE revision (
CONSTRAINT revision_instance_db_name_fkey FOREIGN KEY(instance, db_name) REFERENCES db(instance, name)
);
+CREATE UNIQUE INDEX idx_revision_unique_resource_id ON revision(resource_id);
+
ALTER SEQUENCE revision_id_seq RESTART WITH 101;
CREATE UNIQUE INDEX idx_revision_unique_instance_db_name_type_version_deleted_at_null ON revision(instance, db_name, (payload->>'type'), version) WHERE deleted_at IS NULL;
@@ -473,6 +490,7 @@ CREATE INDEX idx_sync_history_instance_db_name_created_at ON sync_history (insta
CREATE TABLE changelog (
id bigserial PRIMARY KEY,
+ resource_id text NOT NULL DEFAULT gen_random_uuid()::text,
created_at timestamptz NOT NULL DEFAULT now(),
instance text NOT NULL,
db_name text NOT NULL,
@@ -483,6 +501,8 @@ CREATE TABLE changelog (
CONSTRAINT changelog_instance_db_name_fkey FOREIGN KEY(instance, db_name) REFERENCES db(instance, name)
);
+CREATE UNIQUE INDEX idx_changelog_unique_resource_id ON changelog(resource_id);
+
ALTER SEQUENCE changelog_id_seq RESTART WITH 101;
CREATE INDEX idx_changelog_instance_db_name ON changelog (instance, db_name);
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index 19e1f3af57..8e99aae3cb 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.2"), *files[len(files)-1].version)
- require.Equal(t, "migration/3.16/0002##drop_unused_id_columns.sql", files[len(files)-1].path)
+ require.Equal(t, semver.MustParse("3.16.3"), *files[len(files)-1].version)
+ require.Equal(t, "migration/3.16/0003##add_resource_id_to_api_tables.sql", files[len(files)-1].path)
}
func TestVersionUnique(t *testing.T) {
diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.go
index c3a776b6a3..ecce2fbe65 100644
--- a/backend/runner/taskrun/database_migrate_executor.go
+++ b/backend/runner/taskrun/database_migrate_executor.go
@@ -236,7 +236,7 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, d
}
// Begin migration - create pending changelog
- changelogUID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{
+ changelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{
InstanceID: database.InstanceID,
DatabaseName: database.DatabaseName,
Status: store.ChangelogStatusPending,
@@ -255,7 +255,7 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, d
// Dump after migration and update changelog
update := &store.UpdateChangelogMessage{
- UID: changelogUID,
+ ResourceID: changelogID,
}
if needDump {
opts.LogDatabaseSyncStart()
@@ -353,7 +353,7 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, driv
}()
// Begin migration - create pending changelog
- changelogUID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{
+ changelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{
InstanceID: database.InstanceID,
DatabaseName: database.DatabaseName,
Status: store.ChangelogStatusPending,
@@ -410,7 +410,7 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, driv
// Dump after migration and update changelog
update := &store.UpdateChangelogMessage{
- UID: changelogUID,
+ ResourceID: changelogID,
}
opts.LogDatabaseSyncStart()
syncHistory, err := exec.schemaSyncer.SyncDatabaseSchemaToHistory(ctx, database)
@@ -460,7 +460,7 @@ func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, dr
taskRunName := common.FormatTaskRun(database.ProjectID, task.PlanID, task.Environment, task.ID, taskRunUID)
// Create pending changelog for the entire release
- changelogUID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{
+ changelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{
InstanceID: database.InstanceID,
DatabaseName: database.DatabaseName,
Status: store.ChangelogStatusPending,
@@ -563,7 +563,7 @@ func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, dr
// Update changelog after all files are processed
update := &store.UpdateChangelogMessage{
- UID: changelogUID,
+ ResourceID: changelogID,
}
opts.LogDatabaseSyncStart()
syncHistory, err := exec.schemaSyncer.SyncDatabaseSchemaToHistory(ctx, database)
@@ -673,7 +673,7 @@ func (exec *DatabaseMigrateExecutor) runDeclarativeRelease(ctx context.Context,
opts.LogComputeDiffEnd("")
// Begin migration - create pending changelog
- changelogUID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{
+ changelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{
InstanceID: database.InstanceID,
DatabaseName: database.DatabaseName,
Status: store.ChangelogStatusPending,
@@ -694,7 +694,7 @@ func (exec *DatabaseMigrateExecutor) runDeclarativeRelease(ctx context.Context,
// Dump after migration and update changelog
update := &store.UpdateChangelogMessage{
- UID: changelogUID,
+ ResourceID: changelogID,
}
opts.LogDatabaseSyncStart()
syncHistory, err := exec.schemaSyncer.SyncDatabaseSchemaToHistory(ctx, database)
diff --git a/backend/store/changelog.go b/backend/store/changelog.go
index cddbc5949b..21a5511fc9 100644
--- a/backend/store/changelog.go
+++ b/backend/store/changelog.go
@@ -31,15 +31,15 @@ type ChangelogMessage struct {
Status ChangelogStatus
// output only
- UID int64
- CreatedAt time.Time
+ ResourceID string
+ CreatedAt time.Time
Schema string
PlanTitle string
}
type FindChangelogMessage struct {
- UID *int64
+ ResourceID *string
InstanceID *string
DatabaseName *string
@@ -56,17 +56,17 @@ type FindChangelogMessage struct {
}
type UpdateChangelogMessage struct {
- UID int64
+ ResourceID string
SyncHistoryUID *int64
Status *ChangelogStatus
DumpVersion *int32
}
-func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (int64, error) {
+func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (string, error) {
p, err := protojson.Marshal(create.Payload)
if err != nil {
- return 0, errors.Wrapf(err, "failed to marshal")
+ return "", errors.Wrapf(err, "failed to marshal")
}
q := qb.Q().Space(`
@@ -83,20 +83,20 @@ func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (
?,
?
)
- RETURNING id
+ RETURNING resource_id
`, create.InstanceID, create.DatabaseName, create.Status, create.SyncHistoryUID, p)
query, args, err := q.ToSQL()
if err != nil {
- return 0, errors.Wrapf(err, "failed to build sql")
+ return "", errors.Wrapf(err, "failed to build sql")
}
- var id int64
- if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&id); err != nil {
- return 0, errors.Wrapf(err, "failed to insert")
+ var resourceID string
+ if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&resourceID); err != nil {
+ return "", errors.Wrapf(err, "failed to insert")
}
- return id, nil
+ return resourceID, nil
}
func (s *Store) UpdateChangelog(ctx context.Context, update *UpdateChangelogMessage) error {
@@ -115,7 +115,7 @@ func (s *Store) UpdateChangelog(ctx context.Context, update *UpdateChangelogMess
return errors.Errorf("update nothing")
}
- query, args, err := qb.Q().Space("UPDATE changelog SET ? WHERE id = ?", set, update.UID).ToSQL()
+ query, args, err := qb.Q().Space("UPDATE changelog SET ? WHERE resource_id = ?", set, update.ResourceID).ToSQL()
if err != nil {
return errors.Wrapf(err, "failed to build sql")
}
@@ -138,7 +138,7 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)
q := qb.Q().Space(`
SELECT
- changelog.id,
+ changelog.resource_id,
changelog.created_at,
changelog.instance,
changelog.db_name,
@@ -158,8 +158,8 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)
WHERE TRUE
`)
- if v := find.UID; v != nil {
- q.And("changelog.id = ?", *v)
+ if v := find.ResourceID; v != nil {
+ q.And("changelog.resource_id = ?", *v)
}
if v := find.InstanceID; v != nil {
q.And("changelog.instance = ?", *v)
@@ -180,7 +180,7 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)
q.And("changelog.created_at >= ?", *v)
}
- q.Space("ORDER BY changelog.id DESC")
+ q.Space("ORDER BY changelog.created_at DESC")
if v := find.Limit; v != nil {
q.Space("LIMIT ?", *v)
}
@@ -207,7 +207,7 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)
var payload []byte
if err := rows.Scan(
- &c.UID,
+ &c.ResourceID,
&c.CreatedAt,
&c.InstanceID,
&c.DatabaseName,
diff --git a/backend/store/issue_comment.go b/backend/store/issue_comment.go
index 05c1e66bfe..fd3f302ff3 100644
--- a/backend/store/issue_comment.go
+++ b/backend/store/issue_comment.go
@@ -13,7 +13,7 @@ import (
)
type IssueCommentMessage struct {
- UID int
+ ResourceID string
CreatedAt time.Time
UpdatedAt time.Time
IssueUID int
@@ -22,15 +22,15 @@ type IssueCommentMessage struct {
}
type FindIssueCommentMessage struct {
- UID *int
- IssueUID *int
+ ResourceID *string
+ IssueUID *int
Limit *int
Offset *int
}
type UpdateIssueCommentMessage struct {
- UID int
+ ResourceID string
Comment *string
}
@@ -52,7 +52,7 @@ func (s *Store) GetIssueComment(ctx context.Context, find *FindIssueCommentMessa
func (s *Store) ListIssueComment(ctx context.Context, find *FindIssueCommentMessage) ([]*IssueCommentMessage, error) {
q := qb.Q().Space(`
SELECT
- id,
+ resource_id,
creator,
created_at,
updated_at,
@@ -63,14 +63,14 @@ func (s *Store) ListIssueComment(ctx context.Context, find *FindIssueCommentMess
WHERE TRUE
`)
- if v := find.UID; v != nil {
- q.And("id = ?", *v)
+ if v := find.ResourceID; v != nil {
+ q.And("resource_id = ?", *v)
}
if v := find.IssueUID; v != nil {
q.And("issue_id = ?", *v)
}
- q.Space("ORDER BY id ASC")
+ q.Space("ORDER BY created_at ASC")
if v := find.Limit; v != nil {
q.Space("LIMIT ?", *v)
}
@@ -96,7 +96,7 @@ func (s *Store) ListIssueComment(ctx context.Context, find *FindIssueCommentMess
}
var p []byte
if err := rows.Scan(
- &ic.UID,
+ &ic.ResourceID,
&ic.CreatorEmail,
&ic.CreatedAt,
&ic.UpdatedAt,
@@ -146,14 +146,14 @@ func (s *Store) CreateIssueComments(ctx context.Context, creator string, creates
// For single comment, use RETURNING to get the created comment details.
if len(creates) == 1 {
- q.Space("RETURNING id, created_at, updated_at")
+ q.Space("RETURNING resource_id, created_at, updated_at")
query, args, err := q.ToSQL()
if err != nil {
return nil, errors.Wrapf(err, "failed to build sql")
}
create := creates[0]
- if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&create.UID, &create.CreatedAt, &create.UpdatedAt); err != nil {
+ if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&create.ResourceID, &create.CreatedAt, &create.UpdatedAt); err != nil {
return nil, errors.Wrapf(err, "failed to insert")
}
create.CreatorEmail = creator
@@ -180,7 +180,7 @@ func (s *Store) UpdateIssueComment(ctx context.Context, patch *UpdateIssueCommen
q.Join(", ", "payload = payload || jsonb_build_object('comment',?::TEXT)", *v)
}
- q.Space("WHERE id = ?", patch.UID)
+ q.Space("WHERE resource_id = ?", patch.ResourceID)
query, args, err := q.ToSQL()
if err != nil {
diff --git a/backend/store/project_webhook.go b/backend/store/project_webhook.go
index 6b8cf7ae69..1ccb0a85d3 100644
--- a/backend/store/project_webhook.go
+++ b/backend/store/project_webhook.go
@@ -16,10 +16,10 @@ import (
type ProjectWebhookMessage struct {
// Output only fields.
//
- // ID is the unique identifier of the project webhook.
- ID int
- ProjectID string
- Payload *storepb.ProjectWebhook
+ // ResourceID is the unique identifier of the project webhook.
+ ResourceID string
+ ProjectID string
+ Payload *storepb.ProjectWebhook
}
// UpdateProjectWebhookMessage is the message for updating project webhooks.
@@ -30,7 +30,7 @@ type UpdateProjectWebhookMessage struct {
// FindProjectWebhookMessage is the message for finding project webhooks,
// if all fields are nil, it will list all project webhooks.
type FindProjectWebhookMessage struct {
- ID *int
+ ResourceID *string
ProjectID *string
ProjectIDs []string
URL *string
@@ -59,7 +59,7 @@ func (s *Store) CreateProjectWebhook(ctx context.Context, projectID string, crea
payload
)
VALUES (?, ?)
- RETURNING id
+ RETURNING resource_id
`, projectID, payload)
query, args, err := q.ToSQL()
@@ -68,7 +68,7 @@ func (s *Store) CreateProjectWebhook(ctx context.Context, projectID string, crea
}
if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(
- &projectWebhook.ID,
+ &projectWebhook.ResourceID,
); err != nil {
if err == sql.ErrNoRows {
return nil, common.FormatDBErrorEmptyRowWithQuery(query)
@@ -84,15 +84,15 @@ func (s *Store) CreateProjectWebhook(ctx context.Context, projectID string, crea
func (s *Store) ListProjectWebhooks(ctx context.Context, find *FindProjectWebhookMessage) ([]*ProjectWebhookMessage, error) {
q := qb.Q().Space(`
SELECT
- id,
+ resource_id,
project,
payload
FROM project_webhook
WHERE TRUE
`)
- if v := find.ID; v != nil {
- q.And("id = ?", *v)
+ if v := find.ResourceID; v != nil {
+ q.And("resource_id = ?", *v)
}
if v := find.ProjectID; v != nil {
q.And("project = ?", *v)
@@ -123,7 +123,7 @@ func (s *Store) ListProjectWebhooks(ctx context.Context, find *FindProjectWebhoo
var payload []byte
if err := rows.Scan(
- &projectWebhook.ID,
+ &projectWebhook.ResourceID,
&projectWebhook.ProjectID,
&payload,
); err != nil {
@@ -171,7 +171,7 @@ func (s *Store) GetProjectWebhook(ctx context.Context, find *FindProjectWebhookM
}
// UpdateProjectWebhook updates an instance of ProjectWebhook.
-func (s *Store) UpdateProjectWebhook(ctx context.Context, projectResourceID string, projectWebhookID int, update *UpdateProjectWebhookMessage) (*ProjectWebhookMessage, error) {
+func (s *Store) UpdateProjectWebhook(ctx context.Context, projectResourceID string, webhookResourceID string, update *UpdateProjectWebhookMessage) (*ProjectWebhookMessage, error) {
var payload []byte
if update.Payload != nil {
p, err := protojson.Marshal(update.Payload)
@@ -184,9 +184,9 @@ func (s *Store) UpdateProjectWebhook(ctx context.Context, projectResourceID stri
q := qb.Q().Space(`
UPDATE project_webhook
SET payload = ?
- WHERE id = ?
- RETURNING id, project, payload
- `, payload, projectWebhookID)
+ WHERE resource_id = ?
+ RETURNING resource_id, project, payload
+ `, payload, webhookResourceID)
query, args, err := q.ToSQL()
if err != nil {
@@ -199,12 +199,12 @@ func (s *Store) UpdateProjectWebhook(ctx context.Context, projectResourceID stri
var returnedPayload []byte
// Execute update query with RETURNING.
if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(
- &projectWebhook.ID,
+ &projectWebhook.ResourceID,
&projectWebhook.ProjectID,
&returnedPayload,
); err != nil {
if err == sql.ErrNoRows {
- return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("project hook ID not found: %d", projectWebhookID)}
+ return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("project hook not found: %s", webhookResourceID)}
}
return nil, err
}
@@ -217,9 +217,9 @@ func (s *Store) UpdateProjectWebhook(ctx context.Context, projectResourceID stri
return &projectWebhook, nil
}
-// DeleteProjectWebhook deletes an existing projectWebhook by projectUID and url.
-func (s *Store) DeleteProjectWebhook(ctx context.Context, projectResourceID string, projectWebhookUID int) error {
- q := qb.Q().Space("DELETE FROM project_webhook WHERE id = ?", projectWebhookUID)
+// DeleteProjectWebhook deletes an existing projectWebhook by resource ID.
+func (s *Store) DeleteProjectWebhook(ctx context.Context, projectResourceID string, webhookResourceID string) error {
+ q := qb.Q().Space("DELETE FROM project_webhook WHERE resource_id = ?", webhookResourceID)
query, args, err := q.ToSQL()
if err != nil {
diff --git a/backend/store/revision.go b/backend/store/revision.go
index 729c4908c8..7c48846357 100644
--- a/backend/store/revision.go
+++ b/backend/store/revision.go
@@ -19,14 +19,14 @@ type RevisionMessage struct {
Payload *storepb.RevisionPayload
// output only
- UID int64
- CreatedAt time.Time
- Deleter *string
- DeletedAt *time.Time
+ ResourceID string
+ CreatedAt time.Time
+ Deleter *string
+ DeletedAt *time.Time
}
type FindRevisionMessage struct {
- UID *int64
+ ResourceID *string
InstanceID *string
DatabaseName *string
Type *storepb.SchemaChangeType
@@ -45,7 +45,7 @@ type FindRevisionMessage struct {
func (s *Store) ListRevisions(ctx context.Context, find *FindRevisionMessage) ([]*RevisionMessage, error) {
q := qb.Q().Space(`
SELECT
- id,
+ resource_id,
instance,
db_name,
created_at,
@@ -57,8 +57,8 @@ func (s *Store) ListRevisions(ctx context.Context, find *FindRevisionMessage) ([
WHERE TRUE
`)
- if v := find.UID; v != nil {
- q.And("id = ?", *v)
+ if v := find.ResourceID; v != nil {
+ q.And("resource_id = ?", *v)
}
if v := find.InstanceID; v != nil {
q.And("instance = ?", *v)
@@ -104,7 +104,7 @@ func (s *Store) ListRevisions(ctx context.Context, find *FindRevisionMessage) ([
}
var p []byte
if err := rows.Scan(
- &r.UID,
+ &r.ResourceID,
&r.InstanceID,
&r.DatabaseName,
&r.CreatedAt,
@@ -130,9 +130,9 @@ func (s *Store) ListRevisions(ctx context.Context, find *FindRevisionMessage) ([
return revisions, nil
}
-func (s *Store) GetRevision(ctx context.Context, uid int64, instanceID, databaseName string) (*RevisionMessage, error) {
+func (s *Store) GetRevision(ctx context.Context, resourceID, instanceID, databaseName string) (*RevisionMessage, error) {
revisions, err := s.ListRevisions(ctx, &FindRevisionMessage{
- UID: &uid,
+ ResourceID: &resourceID,
InstanceID: &instanceID,
DatabaseName: &databaseName,
ShowDeleted: true,
@@ -141,10 +141,10 @@ func (s *Store) GetRevision(ctx context.Context, uid int64, instanceID, database
return nil, err
}
if len(revisions) == 0 {
- return nil, errors.Errorf("revision not found: %d", uid)
+ return nil, errors.Errorf("revision not found: %s", resourceID)
}
if len(revisions) > 1 {
- return nil, errors.Errorf("found multiple revisions for uid: %d", uid)
+ return nil, errors.Errorf("found multiple revisions for resource_id: %s", resourceID)
}
return revisions[0], nil
}
@@ -162,7 +162,7 @@ func (s *Store) CreateRevision(ctx context.Context, revision *RevisionMessage) (
$3,
$4
)
- RETURNING id, created_at
+ RETURNING resource_id, created_at
`
p, err := protojson.Marshal(revision.Payload)
@@ -170,28 +170,25 @@ func (s *Store) CreateRevision(ctx context.Context, revision *RevisionMessage) (
return nil, errors.Wrapf(err, "failed to marshal revision payload")
}
- var id int64
if err := s.GetDB().QueryRowContext(ctx, query,
revision.InstanceID,
revision.DatabaseName,
revision.Version,
p,
- ).Scan(&id, &revision.CreatedAt); err != nil {
+ ).Scan(&revision.ResourceID, &revision.CreatedAt); err != nil {
return nil, errors.Wrapf(err, "failed to query and scan")
}
- revision.UID = id
-
return revision, nil
}
-func (s *Store) DeleteRevision(ctx context.Context, uid int64, instanceID, databaseName string, deleter string) error {
+func (s *Store) DeleteRevision(ctx context.Context, resourceID, instanceID, databaseName string, deleter string) error {
query :=
`UPDATE revision
SET deleter = $1, deleted_at = now()
- WHERE id = $2 AND instance = $3 AND db_name = $4`
+ WHERE resource_id = $2 AND instance = $3 AND db_name = $4`
- if _, err := s.GetDB().ExecContext(ctx, query, deleter, uid, instanceID, databaseName); err != nil {
+ if _, err := s.GetDB().ExecContext(ctx, query, deleter, resourceID, instanceID, databaseName); err != nil {
return errors.Wrapf(err, "failed to exec")
}
diff --git a/docs/plans/saas/01.prepare-for-saas.md b/docs/plans/saas/01.prepare-for-saas.md
index cdc1565014..5732554721 100644
--- a/docs/plans/saas/01.prepare-for-saas.md
+++ b/docs/plans/saas/01.prepare-for-saas.md
@@ -234,6 +234,80 @@ Sequential integer IDs (`serial`/`bigserial`) have two problems for SaaS:
String IDs (e.g., UUIDs or nanoids) are opaque, non-sequential, and safe to expose in URLs.
+### ID Strategy Investigation
+
+We evaluated four approaches to replace sequential integer IDs for SaaS. Key considerations:
+
+- **Security**: IDs must be non-enumerable and leak no cross-tenant information.
+- **Performance**: Indexed lookup speed matters for high-frequency queries.
+- **Migration cost**: FK chains (`plan → task → task_run → task_run_log`) make schema migration expensive.
+- **Consistency**: The codebase already has tables using text PKs (`project`, `instance`, `role`, `idp`, `user_group`, `review_config`, `access_grant`).
+
+#### Plan 1: Keep Integer IDs (`bigserial`)
+
+Change `serial` to `bigserial` where needed. IDs remain globally unique via PostgreSQL sequences (sequences are database-level, so even in a shared multi-tenant database, no two rows get the same ID).
+
+| Pros | Cons |
+|------|------|
+| Zero migration cost -- no schema or FK changes | Sequential IDs are guessable (`plans/101`, `plans/102`) |
+| Best query performance (8-byte integer index) | Leaks information: total count, creation rate, cross-tenant ordering |
+| Simplest implementation | Inconsistent with existing text-PK tables |
+
+**Verdict**: Fails the security requirement. Not suitable for SaaS.
+
+#### Plan 2: Text UUID Replacing Integer ID (Chosen)
+
+Add a `resource_id text NOT NULL DEFAULT gen_random_uuid()::text` column, migrate all store/API references from `id` to `resource_id`, migrate FK columns from integer to text, then drop the old `id` column. The `resource_id` becomes the new PK.
+
+| Pros | Cons |
+|------|------|
+| Opaque, non-sequential, non-enumerable | ~30-40% slower indexed lookups vs bigint |
+| Consistent with existing text-PK tables | Large migration: FK chain is 4 levels deep |
+| Clean single-identity-per-row design | One-time cost to migrate all FK references |
+| Shard-safe -- no coordination needed for ID generation | Larger storage (36 bytes vs 8 bytes per ID) |
+| No custom ID generator required (`gen_random_uuid()` built-in) | |
+
+**Verdict**: Best long-term design. One-time migration cost is worth the architectural simplicity.
+
+#### Plan 3: Dual ID (Keep Integer `id` + Add Text `resource_id`)
+
+Add `resource_id` for API exposure but keep integer `id` as PK and for all internal FK references. API layer maps between the two.
+
+| Pros | Cons |
+|------|------|
+| No FK migration needed | Permanent dual-identity complexity |
+| Internal queries stay on fast integer indexes | Every table has two identity columns forever |
+| Incremental migration possible | API layer needs constant mapping between `id` and `resource_id` |
+| | If you already have a globally unique UUID, the integer `id` serves no purpose |
+| | Adds confusion about which ID to use in which context |
+
+**Verdict**: Avoids migration pain but creates permanent schema complexity. If the UUID is already globally unique, keeping the integer `id` alongside it is unnecessary overhead.
+
+#### Plan 4: Random `bigint`
+
+Replace `serial`/`bigserial` with a random 64-bit integer generator (e.g., a custom PL/pgSQL function or application-level snowflake/KSUID). Keeps integer type but makes values non-sequential.
+
+| Pros | Cons |
+|------|------|
+| Same performance as integer indexes | Needs custom ID generator (collision handling, uniqueness guarantees) |
+| Non-sequential, harder to enumerate | Still leaks that IDs are integers (narrower space than UUID) |
+| No type change -- FK columns stay integer | Inconsistent with existing text-PK tables |
+| | 64-bit space has non-trivial collision probability at scale |
+
+**Verdict**: Good performance but requires custom infrastructure and remains inconsistent with the existing text-PK pattern.
+
+#### Decision: Plan 2
+
+We chose Plan 2 (text UUID replacing integer `id`) because:
+
+1. **Consistency** -- Aligns with the 10+ tables that already use text PKs. One ID pattern across the entire schema.
+2. **Simplicity** -- Single identity per row. No mapping layer, no "which ID do I use?" confusion.
+3. **Security** -- UUIDs are cryptographically random, non-enumerable, and leak no information.
+4. **No custom infrastructure** -- PostgreSQL's built-in `gen_random_uuid()` handles generation.
+5. **Future-proof** -- Text UUIDs are inherently shard-safe if we ever split into multiple databases.
+
+The ~30-40% slower indexed lookups are acceptable given our query patterns (most hot paths filter by project/instance first, not by ID alone). The one-time FK migration cost is significant but bounded -- once complete, the schema is permanently simpler.
+
### Tables That Expose Integer IDs in API
These 9 tables currently expose their integer `id` in API resource names: