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: instance-scoped tables use resource_id as PK (#19616)
ecmadao86c45cf0cd8d
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.goindex ff7387bb80..4dbcd24bc2 100644--- a/backend/api/v1/database_service.go+++ b/backend/api/v1/database_service.go@@ -358,10 +358,10 @@ func (s *DatabaseService) UpdateDatabase(ctx context.Context, req *connect.Requereturn nil, errors.Errorf("instance %q not found", databaseMessage.InstanceID)}if _, err := s.store.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: databaseMessage.InstanceID,- DatabaseName: databaseMessage.DatabaseName,- Status: store.ChangelogStatusDone,- SyncHistoryUID: &syncHistory,+ InstanceID: databaseMessage.InstanceID,+ DatabaseName: databaseMessage.DatabaseName,+ Status: store.ChangelogStatusDone,+ SyncHistory: &syncHistory,Payload: &storepb.ChangelogPayload{GitCommit: s.profile.GitCommit,}}); err != nil {@@ -731,6 +731,7 @@ func (s *DatabaseService) getSourceDBMetadata(ctx context.Context, request *v1pb}changelog, err := s.store.GetChangelog(ctx, &store.FindChangelogMessage{+ InstanceID: instanceID,ResourceID: &changelogID,})if err != nil {@@ -740,14 +741,14 @@ func (s *DatabaseService) getSourceDBMetadata(ctx context.Context, request *v1pbreturn nil, errors.Errorf("changelog %q not found", changelogID)}- // Use SyncHistoryUID to get historical metadata- if changelog.SyncHistoryUID != nil {- syncHistory, err := s.store.GetSyncHistoryByUID(ctx, *changelog.SyncHistoryUID)+ // Use SyncHistory to get historical metadata+ if changelog.SyncHistory != nil {+ syncHistory, err := s.store.GetSyncHistory(ctx, *changelog.SyncHistory)if err != nil {return nil, err}if syncHistory == nil {- return nil, errors.Errorf("sync history %d not found", *changelog.SyncHistoryUID)+ return nil, errors.Errorf("sync history %s not found", *changelog.SyncHistory)}// Get instance to determine engine and case sensitivity@@ -811,6 +812,7 @@ func (s *DatabaseService) getTargetDBMetadata(ctx context.Context, request *v1pb}changelog, err := s.store.GetChangelog(ctx, &store.FindChangelogMessage{+ InstanceID: instanceID,ResourceID: &changelogID,})if err != nil {@@ -820,14 +822,14 @@ func (s *DatabaseService) getTargetDBMetadata(ctx context.Context, request *v1pbreturn nil, errors.Errorf("changelog %q not found", changelogID)}- // Use SyncHistoryUID to get historical metadata- if changelog.SyncHistoryUID != nil {- syncHistory, err := s.store.GetSyncHistoryByUID(ctx, *changelog.SyncHistoryUID)+ // Use SyncHistory to get historical metadata+ if changelog.SyncHistory != nil {+ syncHistory, err := s.store.GetSyncHistory(ctx, *changelog.SyncHistory)if err != nil {return nil, err}if syncHistory == nil {- return nil, errors.Errorf("sync history %d not found", *changelog.SyncHistoryUID)+ return nil, errors.Errorf("sync history %s not found", *changelog.SyncHistory)}// Get instance to determine engine and case sensitivitydiff --git a/backend/api/v1/database_service_changelog.go b/backend/api/v1/database_service_changelog.goindex 42c3895fb4..327c47991a 100644--- a/backend/api/v1/database_service_changelog.go+++ b/backend/api/v1/database_service_changelog.go@@ -116,7 +116,7 @@ func (s *DatabaseService) ListChangelogs(ctx context.Context, req *connect.RequelimitPlusOne := offset.limit + 1find := &store.FindChangelogMessage{- InstanceID: &database.InstanceID,+ InstanceID: database.InstanceID,DatabaseName: &database.DatabaseName,Limit: &limitPlusOne,Offset: &offset.offset,@@ -156,6 +156,7 @@ func (s *DatabaseService) GetChangelog(ctx context.Context, req *connect.Request}find := &store.FindChangelogMessage{+ InstanceID: instanceID,ResourceID: &changelogID,}if req.Msg.View == v1pb.ChangelogView_CHANGELOG_VIEW_FULL {@@ -206,7 +207,7 @@ func (*DatabaseService) convertToChangelog(d *store.DatabaseMessage, c *store.ChPlanTitle: c.PlanTitle,}- if v := c.SyncHistoryUID; v != nil {+ if c.SyncHistory != nil {cl.Schema = c.Schemacl.SchemaSize = int64(len(cl.Schema))}diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.goindex 5227238976..2ca96bed01 100644--- a/backend/api/v1/release_service_check.go+++ b/backend/api/v1/release_service_check.go@@ -184,7 +184,7 @@ loop:finalMetadata := model.NewDatabaseMetadata(clonedMetadata, nil, nil, engine, store.IsObjectCaseSensitive(instance))// Batch fetch all revisions for this databaserevisions, err := s.store.ListRevisions(ctx, &store.FindRevisionMessage{- InstanceID: &database.InstanceID,+ InstanceID: database.InstanceID,DatabaseName: &database.DatabaseName,Type: common.NewP(storepb.SchemaChangeType_VERSIONED),Versions: &releaseFileVersions,@@ -361,7 +361,7 @@ func (s *ReleaseService) checkReleaseDeclarative(ctx context.Context, files []*vengine := instance.Metadata.GetEngine()revisions, err := s.store.ListRevisions(ctx, &store.FindRevisionMessage{- InstanceID: &database.InstanceID,+ InstanceID: database.InstanceID,DatabaseName: &database.DatabaseName,Type: common.NewP(storepb.SchemaChangeType_DECLARATIVE),Limit: common.NewP(1),diff --git a/backend/api/v1/revision_service.go b/backend/api/v1/revision_service.goindex df4561e9c1..2b46b2eaad 100644--- a/backend/api/v1/revision_service.go+++ b/backend/api/v1/revision_service.go@@ -64,7 +64,7 @@ func (s *RevisionService) ListRevisions(limitPlusOne := offset.limit + 1find := &store.FindRevisionMessage{- InstanceID: &database.InstanceID,+ InstanceID: database.InstanceID,DatabaseName: &database.DatabaseName,Limit: &limitPlusOne,Offset: &offset.offset,diff --git a/backend/component/sampleinstance/manager.go b/backend/component/sampleinstance/manager.goindex 3b0958931b..21bd4caaec 100644--- a/backend/component/sampleinstance/manager.go+++ b/backend/component/sampleinstance/manager.go@@ -292,10 +292,10 @@ func (m *Manager) generateInstance(}if _, err := m.store.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: testDatabase.InstanceID,- DatabaseName: dbName,- Status: store.ChangelogStatusDone,- SyncHistoryUID: &syncHistory,+ InstanceID: testDatabase.InstanceID,+ DatabaseName: dbName,+ Status: store.ChangelogStatusDone,+ SyncHistory: &syncHistory,Payload: &storepb.ChangelogPayload{GitCommit: m.profile.GitCommit,},diff --git a/backend/migrator/migration/3.17/0004##project_webhook_drop_id.sql b/backend/migrator/migration/3.17/0004##project_webhook_drop_id.sqlnew file mode 100644index 0000000000..ca3e669a55--- /dev/null+++ b/backend/migrator/migration/3.17/0004##project_webhook_drop_id.sql@@ -0,0 +1,10 @@+-- Drop unused `id` column from project_webhook, use `resource_id` as PK.+-- `id` has no FK references; all lookups use `resource_id`.++DROP INDEX IF EXISTS idx_project_webhook_unique_resource_id;+ALTER TABLE project_webhook DROP COLUMN IF EXISTS id;++ALTER TABLE project_webhook DROP CONSTRAINT IF EXISTS project_webhook_pkey;+ALTER TABLE project_webhook ADD PRIMARY KEY (resource_id);++DROP SEQUENCE IF EXISTS project_webhook_id_seq;diff --git a/backend/migrator/migration/3.17/0005##instance_scoped_composite_pk.sql b/backend/migrator/migration/3.17/0005##instance_scoped_composite_pk.sqlnew file mode 100644index 0000000000..e99b178644--- /dev/null+++ b/backend/migrator/migration/3.17/0005##instance_scoped_composite_pk.sql@@ -0,0 +1,44 @@+-- Step 2e: Switch revision, sync_history, changelog to resource_id PKs.++-- Phase A: Drop FKs+ALTER TABLE changelog DROP CONSTRAINT IF EXISTS changelog_sync_history_id_fkey;++-- Phase B: revision — promote resource_id to PK+DROP INDEX IF EXISTS idx_revision_unique_resource_id;+ALTER TABLE revision DROP CONSTRAINT IF EXISTS revision_pkey;+ALTER TABLE revision ADD PRIMARY KEY (resource_id);++-- Phase C: sync_history — add resource_id, promote to PK+ALTER TABLE sync_history ADD COLUMN IF NOT EXISTS resource_id text;+UPDATE sync_history SET resource_id = gen_random_uuid()::text WHERE resource_id IS NULL;+ALTER TABLE sync_history ALTER COLUMN resource_id SET NOT NULL;+ALTER TABLE sync_history ALTER COLUMN resource_id SET DEFAULT gen_random_uuid()::text;+ALTER TABLE sync_history DROP CONSTRAINT IF EXISTS sync_history_pkey;+ALTER TABLE sync_history ADD PRIMARY KEY (resource_id);++-- Phase D: changelog — promote resource_id to PK+DROP INDEX IF EXISTS idx_changelog_unique_resource_id;+ALTER TABLE changelog DROP CONSTRAINT IF EXISTS changelog_pkey;+ALTER TABLE changelog ADD PRIMARY KEY (resource_id);++-- Phase E: changelog — add sync_history text column, backfill, drop old sync_history_id+ALTER TABLE changelog ADD COLUMN IF NOT EXISTS sync_history text;+UPDATE changelog SET sync_history = sync_history.resource_id+ FROM sync_history WHERE changelog.sync_history IS NULL AND changelog.sync_history_id = sync_history.id;+ALTER TABLE changelog DROP COLUMN IF EXISTS sync_history_id;++-- Phase F: Re-add FK from changelog.sync_history to sync_history.resource_id+DO $$ BEGIN+ IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'changelog_sync_history_fkey' AND conrelid = 'changelog'::regclass) THEN+ ALTER TABLE changelog ADD CONSTRAINT changelog_sync_history_fkey+ FOREIGN KEY (sync_history) REFERENCES sync_history(resource_id);+ END IF;+END $$;++-- Phase G: Drop unused id columns and sequences+ALTER TABLE revision DROP COLUMN IF EXISTS id;+ALTER TABLE sync_history DROP COLUMN IF EXISTS id;+ALTER TABLE changelog DROP COLUMN IF EXISTS id;+DROP SEQUENCE IF EXISTS revision_id_seq;+DROP SEQUENCE IF EXISTS sync_history_id_seq;+DROP SEQUENCE IF EXISTS changelog_id_seq;diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sqlindex 6a00bf6ff9..93918c2852 100644--- a/backend/migrator/migration/LATEST.sql+++ b/backend/migrator/migration/LATEST.sql@@ -108,17 +108,13 @@ CREATE TABLE policy (-- Project HookCREATE TABLE project_webhook (- id serial PRIMARY KEY,- resource_id text NOT NULL DEFAULT gen_random_uuid()::text,+ resource_id text PRIMARY KEY 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;-- InstanceCREATE TABLE instance (@@ -468,8 +464,7 @@ CREATE TABLE review_config ();CREATE TABLE revision (- id bigserial PRIMARY KEY,- resource_id text NOT NULL DEFAULT gen_random_uuid()::text,+ resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,instance text NOT NULL,db_name text NOT NULL,created_at timestamptz NOT NULL DEFAULT now(),@@ -481,16 +476,12 @@ 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;CREATE INDEX idx_revision_instance_db_name_type_version ON revision(instance, db_name, (payload->>'type'), version);CREATE TABLE sync_history (- id bigserial PRIMARY KEY,+ resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,created_at timestamptz NOT NULL DEFAULT now(),instance text NOT NULL,db_name text NOT NULL,@@ -500,27 +491,20 @@ CREATE TABLE sync_history (CONSTRAINT sync_history_instance_db_name_fkey FOREIGN KEY(instance, db_name) REFERENCES db(instance, name));-ALTER SEQUENCE sync_history_id_seq RESTART WITH 101;-CREATE INDEX idx_sync_history_instance_db_name_created_at ON sync_history (instance, db_name, created_at);CREATE TABLE changelog (- id bigserial PRIMARY KEY,- resource_id text NOT NULL DEFAULT gen_random_uuid()::text,+ resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text,created_at timestamptz NOT NULL DEFAULT now(),instance text NOT NULL,db_name text NOT NULL,status text NOT NULL CONSTRAINT changelog_status_check CHECK (status IN ('PENDING', 'DONE', 'FAILED')),- sync_history_id bigint REFERENCES sync_history(id),+ sync_history text REFERENCES sync_history(resource_id),-- Stored as ChangelogPayload (proto/store/store/changelog.proto)payload jsonb NOT NULL DEFAULT '{}',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);CREATE TABLE release (diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.goindex 0eaf9bec0e..54d82a8355 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.3"), *files[len(files)-1].version)- require.Equal(t, "migration/3.17/0003##worksheet_composite_pk.sql", files[len(files)-1].path)+ 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)}func TestVersionUnique(t *testing.T) {diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.goindex d165ed86ce..c64b91233f 100644--- a/backend/runner/schemasync/syncer.go+++ b/backend/runner/schemasync/syncer.go@@ -366,17 +366,17 @@ func (s *Syncer) SyncInstance(ctx context.Context, instance *store.InstanceMessa}// doSyncDatabaseSchema is the core implementation that syncs the schema for a database and optionally creates a sync history record.-func (s *Syncer) doSyncDatabaseSchema(ctx context.Context, database *store.DatabaseMessage, createSyncHistory bool) (syncHistoryID int64, retErr error) {+func (s *Syncer) doSyncDatabaseSchema(ctx context.Context, database *store.DatabaseMessage, createSyncHistory bool) (syncHistoryResourceID string, retErr error) {instance, err := s.store.GetInstance(ctx, &store.FindInstanceMessage{ResourceID: &database.InstanceID})if err != nil {- return 0, errors.Wrapf(err, "failed to get instance %q", database.InstanceID)+ return "", errors.Wrapf(err, "failed to get instance %q", database.InstanceID)}if instance == nil {- return 0, errors.Errorf("instance %q not found", database.InstanceID)+ return "", errors.Errorf("instance %q not found", database.InstanceID)}driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, database, db.ConnectionContext{})if err != nil {- return 0, err+ return "", err}defer driver.Close(ctx)// Sync database schema@@ -384,11 +384,11 @@ func (s *Syncer) doSyncDatabaseSchema(ctx context.Context, database *store.Databdefer cancelFunc()syncedDatabaseMetadata, err := driver.SyncDBSchema(deadlineCtx)if err != nil {- return 0, errors.Wrapf(err, "failed to sync database schema for database %q", database.DatabaseName)+ return "", errors.Wrapf(err, "failed to sync database schema for database %q", database.DatabaseName)}var schemaBuf bytes.Bufferif err := driver.Dump(deadlineCtx, &schemaBuf, syncedDatabaseMetadata); err != nil {- return 0, errors.Wrapf(err, "failed to dump database schema for database %q", database.DatabaseName)+ return "", errors.Wrapf(err, "failed to dump database schema for database %q", database.DatabaseName)}rawDump := schemaBuf.Bytes()@@ -397,7 +397,7 @@ func (s *Syncer) doSyncDatabaseSchema(ctx context.Context, database *store.DatabDatabaseName: database.DatabaseName,})if err != nil {- return 0, errors.Wrapf(err, "failed to get database schema for database %q", database.DatabaseName)+ return "", errors.Wrapf(err, "failed to get database schema for database %q", database.DatabaseName)}// If the schema does not exist, then we create a new one.// This happens when creating a new database in the test.@@ -424,7 +424,7 @@ func (s *Syncer) doSyncDatabaseSchema(ctx context.Context, database *store.DatabDeleted: proto.Bool(false),MetadataUpdates: metadataUpdates,}); err != nil {- return 0, errors.Wrapf(err, "failed to update database %q for instance %q", database.DatabaseName, database.InstanceID)+ return "", errors.Wrapf(err, "failed to update database %q for instance %q", database.DatabaseName, database.InstanceID)}if err := s.store.UpsertDBSchema(ctx,@@ -436,28 +436,28 @@ func (s *Syncer) doSyncDatabaseSchema(ctx context.Context, database *store.Databslog.Error("unsupported Unicode escape sequence", slog.String("metadata", string(metadataBytes)), slog.String("raw_dump", string(rawDump)))}}- return 0, errors.Wrapf(err, "failed to upsert database schema for database %q", database.DatabaseName)+ return "", errors.Wrapf(err, "failed to upsert database schema for database %q", database.DatabaseName)}// Create sync history if requestedif createSyncHistory {- id, err := s.store.CreateSyncHistory(ctx, database.InstanceID, database.DatabaseName, syncedDatabaseMetadata, string(rawDump))+ resourceID, err := s.store.CreateSyncHistory(ctx, database.InstanceID, database.DatabaseName, syncedDatabaseMetadata, string(rawDump))if err != nil {if strings.Contains(err.Error(), "escape sequence") {if metadataBytes, err := protojson.Marshal(syncedDatabaseMetadata); err == nil {slog.Error("unsupported Unicode escape sequence", slog.String("metadata", string(metadataBytes)), slog.String("raw_dump", string(rawDump)))}}- return 0, errors.Wrapf(err, "failed to insert sync history for database %q", database.DatabaseName)+ return "", errors.Wrapf(err, "failed to insert sync history for database %q", database.DatabaseName)}- return id, nil+ return resourceID, nil}- return 0, nil+ return "", nil}// SyncDatabaseSchemaToHistory will sync the schema for a database and create a sync history record.-func (s *Syncer) SyncDatabaseSchemaToHistory(ctx context.Context, database *store.DatabaseMessage) (int64, error) {+func (s *Syncer) SyncDatabaseSchemaToHistory(ctx context.Context, database *store.DatabaseMessage) (string, error) {return s.doSyncDatabaseSchema(ctx, database, true)}diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.goindex cd65eedabf..4b7a537a83 100644--- a/backend/runner/taskrun/database_migrate_executor.go+++ b/backend/runner/taskrun/database_migrate_executor.go@@ -131,7 +131,7 @@ func (exec *DatabaseMigrateExecutor) ensureBaselineChangelog(ctx context.Context// Check if this database has any existing changelogslimit := 1existingChangelogs, err := exec.store.ListChangelogs(ctx, &store.FindChangelogMessage{- InstanceID: &database.InstanceID,+ InstanceID: database.InstanceID,DatabaseName: &database.DatabaseName,Limit: &limit,})@@ -141,16 +141,16 @@ func (exec *DatabaseMigrateExecutor) ensureBaselineChangelog(ctx context.Context// If no changelogs exist, create a baseline with the current schemaif len(existingChangelogs) == 0 {- baselineSyncHistoryUID, err := exec.schemaSyncer.SyncDatabaseSchemaToHistory(ctx, database)+ baselineSyncHistory, err := exec.schemaSyncer.SyncDatabaseSchemaToHistory(ctx, database)if err != nil {return errors.Wrapf(err, "failed to sync database schema for baseline")}_, err = exec.store.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: database.InstanceID,- DatabaseName: database.DatabaseName,- Status: store.ChangelogStatusDone,- SyncHistoryUID: &baselineSyncHistoryUID,+ InstanceID: database.InstanceID,+ DatabaseName: database.DatabaseName,+ Status: store.ChangelogStatusDone,+ SyncHistory: &baselineSyncHistory,Payload: &storepb.ChangelogPayload{GitCommit: exec.profile.GitCommit,},@@ -237,10 +237,10 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, d// Begin migration - create pending changelogchangelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: database.InstanceID,- DatabaseName: database.DatabaseName,- Status: store.ChangelogStatusPending,- SyncHistoryUID: nil,+ InstanceID: database.InstanceID,+ DatabaseName: database.DatabaseName,+ Status: store.ChangelogStatusPending,+ SyncHistory: nil,Payload: &storepb.ChangelogPayload{TaskRun: common.FormatTaskRun(database.ProjectID, task.PlanID, task.Environment, task.ID, taskRunUID),GitCommit: exec.profile.GitCommit,@@ -265,7 +265,7 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, dslog.Error("failed to sync database schema", log.BBError(err))} else {opts.LogDatabaseSyncEnd("")- update.SyncHistoryUID = &syncHistory+ update.SyncHistory = &syncHistory}}if migrationErr == nil {@@ -354,10 +354,10 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, driv// Begin migration - create pending changelogchangelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: database.InstanceID,- DatabaseName: database.DatabaseName,- Status: store.ChangelogStatusPending,- SyncHistoryUID: nil,+ InstanceID: database.InstanceID,+ DatabaseName: database.DatabaseName,+ Status: store.ChangelogStatusPending,+ SyncHistory: nil,Payload: &storepb.ChangelogPayload{TaskRun: common.FormatTaskRun(database.ProjectID, task.PlanID, task.Environment, task.ID, taskRunUID),GitCommit: exec.profile.GitCommit,@@ -419,7 +419,7 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, drivslog.Error("failed to sync database schema", log.BBError(err))} else {opts.LogDatabaseSyncEnd("")- update.SyncHistoryUID = &syncHistory+ update.SyncHistory = &syncHistory}if migrationErr == nil {status := store.ChangelogStatusDone@@ -442,7 +442,7 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, drivfunc (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) {// Get existing revisions for this databaserevisions, err := exec.store.ListRevisions(ctx, &store.FindRevisionMessage{- InstanceID: &task.InstanceID,+ InstanceID: task.InstanceID,DatabaseName: task.DatabaseName,})if err != nil {@@ -461,10 +461,10 @@ func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, dr// Create pending changelog for the entire releasechangelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: database.InstanceID,- DatabaseName: database.DatabaseName,- Status: store.ChangelogStatusPending,- SyncHistoryUID: nil,+ InstanceID: database.InstanceID,+ DatabaseName: database.DatabaseName,+ Status: store.ChangelogStatusPending,+ SyncHistory: nil,Payload: &storepb.ChangelogPayload{TaskRun: taskRunName,GitCommit: exec.profile.GitCommit,@@ -572,7 +572,7 @@ func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, drslog.Error("failed to sync database schema", log.BBError(err))} else {opts.LogDatabaseSyncEnd("")- update.SyncHistoryUID = &syncHistory+ update.SyncHistory = &syncHistory}if migrationErr == nil {status := store.ChangelogStatusDone@@ -674,10 +674,10 @@ func (exec *DatabaseMigrateExecutor) runDeclarativeRelease(ctx context.Context,// Begin migration - create pending changelogchangelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: database.InstanceID,- DatabaseName: database.DatabaseName,- Status: store.ChangelogStatusPending,- SyncHistoryUID: nil,+ InstanceID: database.InstanceID,+ DatabaseName: database.DatabaseName,+ Status: store.ChangelogStatusPending,+ SyncHistory: nil,Payload: &storepb.ChangelogPayload{TaskRun: common.FormatTaskRun(database.ProjectID, task.PlanID, task.Environment, task.ID, taskRunUID),GitCommit: exec.profile.GitCommit,@@ -703,7 +703,7 @@ func (exec *DatabaseMigrateExecutor) runDeclarativeRelease(ctx context.Context,slog.Error("failed to sync database schema", log.BBError(err))} else {opts.LogDatabaseSyncEnd("")- update.SyncHistoryUID = &syncHistory+ update.SyncHistory = &syncHistory}if migrationErr == nil {status := store.ChangelogStatusDonediff --git a/backend/store/changelog.go b/backend/store/changelog.goindex 21a5511fc9..6e49db5d5f 100644--- a/backend/store/changelog.go+++ b/backend/store/changelog.go@@ -27,8 +27,8 @@ type ChangelogMessage struct {DatabaseName stringPayload *storepb.ChangelogPayload- SyncHistoryUID *int64- Status ChangelogStatus+ SyncHistory *string+ Status ChangelogStatus// output onlyResourceID string@@ -39,8 +39,8 @@ type ChangelogMessage struct {}type FindChangelogMessage struct {+ InstanceID stringResourceID *string- InstanceID *stringDatabaseName *stringStatus *ChangelogStatus@@ -58,9 +58,9 @@ type FindChangelogMessage struct {type UpdateChangelogMessage struct {ResourceID string- SyncHistoryUID *int64- Status *ChangelogStatus- DumpVersion *int32+ SyncHistory *string+ Status *ChangelogStatus+ DumpVersion *int32}func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (string, error) {@@ -74,7 +74,7 @@ func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (instance,db_name,status,- sync_history_id,+ sync_history,payload) VALUES (?,@@ -84,7 +84,7 @@ func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (?)RETURNING resource_id- `, create.InstanceID, create.DatabaseName, create.Status, create.SyncHistoryUID, p)+ `, create.InstanceID, create.DatabaseName, create.Status, create.SyncHistory, p)query, args, err := q.ToSQL()if err != nil {@@ -101,8 +101,8 @@ func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (func (s *Store) UpdateChangelog(ctx context.Context, update *UpdateChangelogMessage) error {set := qb.Q()- if v := update.SyncHistoryUID; v != nil {- set.Comma("sync_history_id = ?", *v)+ if v := update.SyncHistory; v != nil {+ set.Comma("sync_history = ?", *v)}if v := update.Status; v != nil {set.Comma("status = ?", *v)@@ -143,27 +143,24 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)changelog.instance,changelog.db_name,changelog.status,- changelog.sync_history_id,- ` + schemaExpr + `,+ changelog.sync_history,+ `+schemaExpr+`,changelog.payload,COALESCE(plan.name, '')FROM changelog- LEFT JOIN sync_history sh_cur ON sh_cur.id = changelog.sync_history_id+ LEFT JOIN sync_history sh_cur ON sh_cur.resource_id = changelog.sync_historyLEFT JOIN LATERAL (SELECT task.plan_idFROM taskWHERE task.id = (regexp_match(changelog.payload->>'taskRun', 'tasks/(\d+)/'))[1]::int) task_info ON TRUELEFT JOIN plan ON plan.id = task_info.plan_id- WHERE TRUE- `)+ WHERE changelog.instance = ?+ `, find.InstanceID)if v := find.ResourceID; v != nil {q.And("changelog.resource_id = ?", *v)}- if v := find.InstanceID; v != nil {- q.And("changelog.instance = ?", *v)- }if v := find.DatabaseName; v != nil {q.And("changelog.db_name = ?", *v)}@@ -171,7 +168,7 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)q.And("changelog.status = ?", string(*v))}if find.HasSyncHistory {- q.And("changelog.sync_history_id IS NOT NULL")+ q.And("changelog.sync_history IS NOT NULL")}if v := find.CreatedAtBefore; v != nil {q.And("changelog.created_at <= ?", *v)@@ -212,7 +209,7 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)&c.InstanceID,&c.DatabaseName,&c.Status,- &c.SyncHistoryUID,+ &c.SyncHistory,&c.Schema,&payload,&c.PlanTitle,diff --git a/backend/store/revision.go b/backend/store/revision.goindex 7c48846357..1347426ea8 100644--- a/backend/store/revision.go+++ b/backend/store/revision.go@@ -26,8 +26,8 @@ type RevisionMessage struct {}type FindRevisionMessage struct {+ InstanceID stringResourceID *string- InstanceID *stringDatabaseName *stringType *storepb.SchemaChangeType@@ -54,15 +54,12 @@ func (s *Store) ListRevisions(ctx context.Context, find *FindRevisionMessage) ([version,payloadFROM revision- WHERE TRUE- `)+ WHERE instance = ?+ `, find.InstanceID)if v := find.ResourceID; v != nil {q.And("resource_id = ?", *v)}- if v := find.InstanceID; v != nil {- q.And("instance = ?", *v)- }if v := find.DatabaseName; v != nil {q.And("db_name = ?", *v)}@@ -132,8 +129,8 @@ func (s *Store) ListRevisions(ctx context.Context, find *FindRevisionMessage) ([func (s *Store) GetRevision(ctx context.Context, resourceID, instanceID, databaseName string) (*RevisionMessage, error) {revisions, err := s.ListRevisions(ctx, &FindRevisionMessage{+ InstanceID: instanceID,ResourceID: &resourceID,- InstanceID: &instanceID,DatabaseName: &databaseName,ShowDeleted: true,})diff --git a/backend/store/sync_history.go b/backend/store/sync_history.goindex 8798867f62..ad2338f0ab 100644--- a/backend/store/sync_history.go+++ b/backend/store/sync_history.go@@ -14,7 +14,7 @@ import ()type SyncHistory struct {- UID int64+ ResourceID stringInstanceID stringDatabaseName stringSchema string@@ -23,18 +23,18 @@ type SyncHistory struct {CreatedAt time.Time}-func (s *Store) GetSyncHistoryByUID(ctx context.Context, uid int64) (*SyncHistory, error) {+func (s *Store) GetSyncHistory(ctx context.Context, resourceID string) (*SyncHistory, error) {q := qb.Q().Space(`SELECT- id,+ resource_id,created_at,instance,db_name,metadata,raw_dumpFROM sync_history- WHERE id = ?- `, uid)+ WHERE resource_id = ?+ `, resourceID)query, args, err := q.ToSQL()if err != nil {@@ -47,7 +47,7 @@ func (s *Store) GetSyncHistoryByUID(ctx context.Context, uid int64) (*SyncHistorvar m []byteif err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(- &h.UID,+ &h.ResourceID,&h.CreatedAt,&h.InstanceID,&h.DatabaseName,@@ -67,14 +67,13 @@ func (s *Store) GetSyncHistoryByUID(ctx context.Context, uid int64) (*SyncHistorreturn &h, nil}-// UpsertDBSchema upserts a database schema.-func (s *Store) CreateSyncHistory(ctx context.Context, instanceID, databaseName string, metadata *storepb.DatabaseSchemaMetadata, schema string) (int64, error) {+func (s *Store) CreateSyncHistory(ctx context.Context, instanceID, databaseName string, metadata *storepb.DatabaseSchemaMetadata, schema string) (string, error) {// Sanitize schema to prevent storing invalid UTF-8 bytes from external databases.schema = strings.ToValidUTF8(schema, "")metadataBytes, err := protojson.Marshal(metadata)if err != nil {- return 0, errors.Wrapf(err, "failed to marshal")+ return "", errors.Wrapf(err, "failed to marshal")}q := qb.Q().Space(`@@ -85,18 +84,18 @@ func (s *Store) CreateSyncHistory(ctx context.Context, instanceID, databaseNameraw_dump)VALUES (?, ?, ?, ?)- RETURNING id+ RETURNING resource_id`, instanceID, databaseName, metadataBytes, schema)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}diff --git a/docs/plans/saas/01.prepare-for-saas.md b/docs/plans/saas/01.prepare-for-saas.mdindex bd4b942280..4ca164aba2 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 | 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 |+| `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 || **Workspace-scoped (need `workspace_id`)** | | | || `idp` | `resource_id text` | Workspace | Add `workspace_id` || `project` | `resource_id text` | Workspace | Add `workspace_id` |@@ -396,22 +396,36 @@ project_webhook: drop `id` column, use `resource_id` as PKTables: `revision`, `sync_history`, `changelog`+Instead of composite PKs `(instance, id)`, promote `resource_id` to PK for all three tables. This is simpler — avoids composite FK rewiring and matches the pattern used for `project_webhook` and `issue_comment`.+Schema changes:```-revision(id) → revision(instance, id)- -- revision already has `instance` column, just change PK--sync_history(id) → sync_history(instance, id)- -- sync_history already has `instance` column, just change PK--changelog(id) → changelog(instance, id)- -- changelog already has `instance` column, just change PK- -- changelog.sync_history_id FK → (instance, sync_history_id) REFERENCES sync_history(instance, id)+revision: drop `id` as PK, use `resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text`+ -- `id` is kept as `bigserial` (no PK) for backward compat but not used as PK+ -- drop unique index on resource_id (now covered by PK)++sync_history: add `resource_id text` column, promote to PK+ -- `id` is kept as `bigserial` (no PK) for backward compat+ -- add `resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text`+ -- backfill existing rows with gen_random_uuid()::text++changelog: drop `id` as PK, use `resource_id text PRIMARY KEY DEFAULT gen_random_uuid()::text`+ -- `id` is kept as `bigserial` (no PK) for backward compat+ -- drop unique index on resource_id (now covered by PK)+ -- replace `sync_history_id bigint` with `sync_history text REFERENCES sync_history(resource_id)`+ -- backfill: UPDATE changelog SET sync_history = sync_history.resource_id FROM sync_history WHERE changelog.sync_history_id = sync_history.id+ -- drop `sync_history_id` column after backfill```+Store layer changes:+- `sync_history.go`: `GetSyncHistoryByUID(ctx, instanceID, 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-After Step 2, all scoped tables have composite PKs `(project, id)` or `(instance, id)`. Now switch from global sequences to per-scope auto-increment.+After 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.Convert `serial`/`bigserial` to `bigint` (drop the global sequence default). Implement per-scope ID generation — options:@@ -423,7 +437,7 @@ Tables affected:- Project-scoped: `plan`, `issue`, `plan_check_run`, `task`, `task_run`- (`project_webhook` and `issue_comment` use `resource_id` as PK — no per-project auto-increment needed)- (`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: `revision`, `sync_history`, `changelog`+- 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@@ -436,7 +450,7 @@ For tables with no natural scope, replace `serial`/`bigserial` with `text` PK us| `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_id` stays internal after Step 2e, `task_run.result` JSONB field `exportArchiveUid` needs proto/data migration).+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).### JSONB / Proto Fields Storing Integer IDs