Skip to content

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

chore: store sheet ID to migration history (#6862)

Danny Xu70be4493d27b
diff --git a/backend/migrator/migrator.go b/backend/migrator/migrator.go
index b19e45393e..1daff7f942 100644
--- a/backend/migrator/migrator.go
+++ b/backend/migrator/migrator.go
@@ -356,7 +356,7 @@ func migrate(ctx context.Context, storeInstance *store.Store, metadataDriver dbd
Description: fmt.Sprintf("Migrate version %s server version %s with files %s.", pv.version, serverVersion, pv.filename),
Force: true,
}
- if _, _, err := utils.ExecuteMigrationDefault(ctx, storeInstance, metadataDriver, mi, string(buf), dbdriver.ExecuteOptions{}); err != nil {
+ if _, _, err := utils.ExecuteMigrationDefault(ctx, storeInstance, metadataDriver, mi, string(buf), nil, dbdriver.ExecuteOptions{}); err != nil {
return err
}
retVersion = pv.version
@@ -445,7 +445,7 @@ func migrateDev(ctx context.Context, storeInstance *store.Store, metadataDriver
Description: fmt.Sprintf("Migrate version %s server version %s with files %s.", m.version, serverVersion, m.filename),
Force: true,
}
- if _, _, err := utils.ExecuteMigrationDefault(ctx, storeInstance, metadataDriver, mi, m.statement, dbdriver.ExecuteOptions{}); err != nil {
+ if _, _, err := utils.ExecuteMigrationDefault(ctx, storeInstance, metadataDriver, mi, m.statement, nil, dbdriver.ExecuteOptions{}); err != nil {
return err
}
}
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index cb801798f5..cc05293b92 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -380,6 +380,7 @@ type MigrationHistory struct {
Version string
Description string
Statement string
+ SheetID *int
Schema string
SchemaPrev string
ExecutionDurationNs int64
diff --git a/backend/runner/taskrun/data_update_executor.go b/backend/runner/taskrun/data_update_executor.go
index d31b85db3c..8cfc9b0668 100644
--- a/backend/runner/taskrun/data_update_executor.go
+++ b/backend/runner/taskrun/data_update_executor.go
@@ -49,5 +49,5 @@ func (exec *DataUpdateExecutor) RunOnce(ctx context.Context, task *store.TaskMes
if err != nil {
return true, nil, err
}
- return runMigration(ctx, exec.store, exec.dbFactory, exec.activityManager, exec.license, exec.stateCfg, exec.profile, task, db.Data, statement, payload.SchemaVersion, payload.VCSPushEvent)
+ return runMigration(ctx, exec.store, exec.dbFactory, exec.activityManager, exec.license, exec.stateCfg, exec.profile, task, db.Data, statement, payload.SchemaVersion, &payload.SheetID, payload.VCSPushEvent)
}
diff --git a/backend/runner/taskrun/database_create_executor.go b/backend/runner/taskrun/database_create_executor.go
index 713c9d327c..b74cb63f56 100644
--- a/backend/runner/taskrun/database_create_executor.go
+++ b/backend/runner/taskrun/database_create_executor.go
@@ -274,7 +274,7 @@ func (exec *DatabaseCreateExecutor) createInitialSchema(ctx context.Context, env
mi.IssueIDInt = &issue.UID
}
- if _, _, err := utils.ExecuteMigrationDefault(ctx, exec.store, driver, mi, schema, db.ExecuteOptions{}); err != nil {
+ if _, _, err := utils.ExecuteMigrationDefault(ctx, exec.store, driver, mi, schema, nil, db.ExecuteOptions{}); err != nil {
return "", "", err
}
return schemaVersion, schema, nil
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index 2ba74045bc..fb44a1baf7 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -143,7 +143,7 @@ func getMigrationInfo(ctx context.Context, stores *store.Store, profile config.P
return mi, nil
}
-func executeMigration(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State, task *store.TaskMessage, statement string, mi *db.MigrationInfo) (string, string, error) {
+func executeMigration(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State, task *store.TaskMessage, statement string, sheetID *int, mi *db.MigrationInfo) (string, string, error) {
instance, err := stores.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &task.InstanceID})
if err != nil {
return "", "", err
@@ -185,7 +185,7 @@ func executeMigration(ctx context.Context, stores *store.Store, dbFactory *dbfac
opts.EndTransactionFunc = getSetOracleTransactionIDFunc(ctx, task, stores)
}
- migrationID, schema, err := utils.ExecuteMigrationDefault(ctx, stores, driver, mi, statement, opts)
+ migrationID, schema, err := utils.ExecuteMigrationDefault(ctx, stores, driver, mi, statement, sheetID, opts)
if err != nil {
return "", "", err
}
@@ -568,13 +568,13 @@ func isWriteBack(ctx context.Context, stores *store.Store, license enterpriseAPI
return branch, nil
}
-func runMigration(ctx context.Context, store *store.Store, dbFactory *dbfactory.DBFactory, activityManager *activity.Manager, license enterpriseAPI.LicenseService, stateCfg *state.State, profile config.Profile, task *store.TaskMessage, migrationType db.MigrationType, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent) (terminated bool, result *api.TaskRunResultPayload, err error) {
+func runMigration(ctx context.Context, store *store.Store, dbFactory *dbfactory.DBFactory, activityManager *activity.Manager, license enterpriseAPI.LicenseService, stateCfg *state.State, profile config.Profile, task *store.TaskMessage, migrationType db.MigrationType, statement, schemaVersion string, sheetID *int, vcsPushEvent *vcsPlugin.PushEvent) (terminated bool, result *api.TaskRunResultPayload, err error) {
mi, err := getMigrationInfo(ctx, store, profile, task, migrationType, statement, schemaVersion, vcsPushEvent)
if err != nil {
return true, nil, err
}
- migrationID, schema, err := executeMigration(ctx, store, dbFactory, stateCfg, task, statement, mi)
+ migrationID, schema, err := executeMigration(ctx, store, dbFactory, stateCfg, task, statement, sheetID, mi)
if err != nil {
return true, nil, err
}
diff --git a/backend/runner/taskrun/pitr_cutover_executor.go b/backend/runner/taskrun/pitr_cutover_executor.go
index 5008d3ac33..0d91cef254 100644
--- a/backend/runner/taskrun/pitr_cutover_executor.go
+++ b/backend/runner/taskrun/pitr_cutover_executor.go
@@ -155,7 +155,7 @@ func (exec *PITRCutoverExecutor) pitrCutover(ctx context.Context, dbFactory *dbf
}
defer driver.Close(ctx)
- if _, _, err := utils.ExecuteMigrationDefault(ctx, exec.store, driver, m, "" /* pitr cutover */, db.ExecuteOptions{}); err != nil {
+ if _, _, err := utils.ExecuteMigrationDefault(ctx, exec.store, driver, m, "" /* pitr cutover */, nil, db.ExecuteOptions{}); err != nil {
log.Error("Failed to add migration history record", zap.Error(err))
return true, nil, errors.Wrap(err, "failed to add migration history record")
}
diff --git a/backend/runner/taskrun/pitr_restore_executor.go b/backend/runner/taskrun/pitr_restore_executor.go
index c822a3f4ce..60cef2d477 100644
--- a/backend/runner/taskrun/pitr_restore_executor.go
+++ b/backend/runner/taskrun/pitr_restore_executor.go
@@ -555,7 +555,7 @@ func createBranchMigrationHistory(ctx context.Context, stores *store.Store, dbFa
}
defer targetDriver.Close(ctx)
- migrationID, _, err := utils.ExecuteMigrationDefault(ctx, stores, targetDriver, m, "", db.ExecuteOptions{})
+ migrationID, _, err := utils.ExecuteMigrationDefault(ctx, stores, targetDriver, m, "", nil, db.ExecuteOptions{})
if err != nil {
return "", "", errors.Wrap(err, "failed to create migration history")
}
diff --git a/backend/runner/taskrun/schema_baseline_executor.go b/backend/runner/taskrun/schema_baseline_executor.go
index f7ddfb41f2..e3aa226ac0 100644
--- a/backend/runner/taskrun/schema_baseline_executor.go
+++ b/backend/runner/taskrun/schema_baseline_executor.go
@@ -59,7 +59,7 @@ func (exec *SchemaBaselineExecutor) RunOnce(ctx context.Context, task *store.Tas
return true, nil, err
}
- terminated, result, err := runMigration(ctx, exec.store, exec.dbFactory, exec.activityManager, exec.license, exec.stateCfg, exec.profile, task, db.Baseline, "" /* statement */, payload.SchemaVersion, nil /* vcsPushEvent */)
+ terminated, result, err := runMigration(ctx, exec.store, exec.dbFactory, exec.activityManager, exec.license, exec.stateCfg, exec.profile, task, db.Baseline, "" /* statement */, payload.SchemaVersion, nil /* sheetID */, nil /* vcsPushEvent */)
if err := exec.schemaSyncer.SyncDatabaseSchema(ctx, database, true /* force */); err != nil {
log.Error("failed to sync database schema",
zap.String("instanceName", instance.ResourceID),
diff --git a/backend/runner/taskrun/schema_update_executor.go b/backend/runner/taskrun/schema_update_executor.go
index 7528678950..58e4111d97 100644
--- a/backend/runner/taskrun/schema_update_executor.go
+++ b/backend/runner/taskrun/schema_update_executor.go
@@ -64,7 +64,7 @@ func (exec *SchemaUpdateExecutor) RunOnce(ctx context.Context, task *store.TaskM
return true, nil, err
}
- terminated, result, err := runMigration(ctx, exec.store, exec.dbFactory, exec.activityManager, exec.license, exec.stateCfg, exec.profile, task, db.Migrate, statement, payload.SchemaVersion, payload.VCSPushEvent)
+ terminated, result, err := runMigration(ctx, exec.store, exec.dbFactory, exec.activityManager, exec.license, exec.stateCfg, exec.profile, task, db.Migrate, statement, payload.SchemaVersion, &payload.SheetID, payload.VCSPushEvent)
if err := exec.schemaSyncer.SyncDatabaseSchema(ctx, database, true /* force */); err != nil {
log.Error("failed to sync database schema",
zap.String("instanceName", instance.ResourceID),
diff --git a/backend/runner/taskrun/schema_update_ghost_cutover_executor.go b/backend/runner/taskrun/schema_update_ghost_cutover_executor.go
index e58c706f04..6d4cd16c54 100644
--- a/backend/runner/taskrun/schema_update_ghost_cutover_executor.go
+++ b/backend/runner/taskrun/schema_update_ghost_cutover_executor.go
@@ -98,7 +98,7 @@ func (e *SchemaUpdateGhostCutoverExecutor) RunOnce(ctx context.Context, task *st
sharedGhost := value.(sharedGhostState)
// not using the rendered statement here because we want to avoid leaking the rendered statement
- terminated, result, err := cutover(ctx, e.store, e.dbFactory, e.activityManager, e.license, e.profile, task, statement, payload.SchemaVersion, payload.VCSPushEvent, postponeFilename, sharedGhost.migrationContext, sharedGhost.errCh)
+ terminated, result, err := cutover(ctx, e.store, e.dbFactory, e.activityManager, e.license, e.profile, task, statement, payload.SheetID, payload.SchemaVersion, payload.VCSPushEvent, postponeFilename, sharedGhost.migrationContext, sharedGhost.errCh)
if err := e.schemaSyncer.SyncDatabaseSchema(ctx, database, true /* force */); err != nil {
log.Error("failed to sync database schema",
zap.String("instanceName", instance.ResourceID),
@@ -110,7 +110,7 @@ func (e *SchemaUpdateGhostCutoverExecutor) RunOnce(ctx context.Context, task *st
return terminated, result, err
}
-func cutover(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFactory, activityManager *activity.Manager, license enterpriseAPI.LicenseService, profile config.Profile, task *store.TaskMessage, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent, postponeFilename string, migrationContext *base.MigrationContext, errCh <-chan error) (terminated bool, result *api.TaskRunResultPayload, err error) {
+func cutover(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFactory, activityManager *activity.Manager, license enterpriseAPI.LicenseService, profile config.Profile, task *store.TaskMessage, statement string, sheetID int, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent, postponeFilename string, migrationContext *base.MigrationContext, errCh <-chan error) (terminated bool, result *api.TaskRunResultPayload, err error) {
statement = strings.TrimSpace(statement)
instance, err := stores.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &task.InstanceID})
if err != nil {
@@ -146,7 +146,7 @@ func cutover(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFa
return true, nil, err
}
defer driver.Close(ctx)
- migrationID, schema, err := utils.ExecuteMigrationWithFunc(ctx, stores, driver, mi, statement, execFunc)
+ migrationID, schema, err := utils.ExecuteMigrationWithFunc(ctx, stores, driver, mi, statement, &sheetID, execFunc)
if err != nil {
return true, nil, err
}
diff --git a/backend/runner/taskrun/schema_update_sdl_executor.go b/backend/runner/taskrun/schema_update_sdl_executor.go
index ab68c5a5c5..4c66983fc3 100644
--- a/backend/runner/taskrun/schema_update_sdl_executor.go
+++ b/backend/runner/taskrun/schema_update_sdl_executor.go
@@ -74,7 +74,7 @@ func (exec *SchemaUpdateSDLExecutor) RunOnce(ctx context.Context, task *store.Ta
if err != nil {
return true, nil, errors.Wrap(err, "invalid database schema diff")
}
- terminated, result, err := runMigration(ctx, exec.store, exec.dbFactory, exec.activityManager, exec.license, exec.stateCfg, exec.profile, task, db.MigrateSDL, ddl, payload.SchemaVersion, payload.VCSPushEvent)
+ terminated, result, err := runMigration(ctx, exec.store, exec.dbFactory, exec.activityManager, exec.license, exec.stateCfg, exec.profile, task, db.MigrateSDL, ddl, payload.SchemaVersion, &payload.SheetID, payload.VCSPushEvent)
if err := exec.schemaSyncer.SyncDatabaseSchema(ctx, database, true /* force */); err != nil {
log.Error("failed to sync database schema",
diff --git a/backend/store/instance_change_history.go b/backend/store/instance_change_history.go
index 75ddebc70f..a5a8c6d17d 100644
--- a/backend/store/instance_change_history.go
+++ b/backend/store/instance_change_history.go
@@ -34,6 +34,7 @@ type InstanceChangeHistoryMessage struct {
Version string
Description string
Statement string
+ SheetID *int
Schema string
SchemaPrev string
ExecutionDurationNs int64
@@ -73,6 +74,7 @@ type UpdateInstanceChangeHistoryMessage struct {
Status *db.MigrationStatus
ExecutionDurationNs *int64
Schema *string
+ Sheet *int
}
// CreateInstanceChangeHistory creates instance change history in batch.
@@ -118,6 +120,7 @@ func (*Store) createInstanceChangeHistoryImpl(ctx context.Context, tx *Tx, creat
description,
statement,
"schema",
+ sheet_id,
schema_prev,
execution_duration_ns,
payload,
@@ -146,11 +149,12 @@ func (*Store) createInstanceChangeHistoryImpl(ctx context.Context, tx *Tx, creat
create.Description,
create.Statement,
create.Schema,
+ create.SheetID,
create.SchemaPrev,
create.ExecutionDurationNs,
payload,
)
- const countToPayload = 17
+ const countToPayload = 18
var valueStr []string
for i := 0; i < countToPayload; i++ {
valueStr = append(valueStr, fmt.Sprintf("$%d", count))
@@ -208,6 +212,7 @@ func (*Store) createInstanceChangeHistoryImpl(ctx context.Context, tx *Tx, creat
Description: create.Description,
Statement: create.Statement,
Schema: create.Schema,
+ SheetID: create.SheetID,
SchemaPrev: create.SchemaPrev,
ExecutionDurationNs: create.ExecutionDurationNs,
Payload: create.Payload,
@@ -257,6 +262,7 @@ func convertInstanceChangeHistoryToMigrationHistory(change *InstanceChangeHistor
Description: change.Description,
Statement: change.Statement,
Schema: change.Schema,
+ SheetID: change.SheetID,
SchemaPrev: change.SchemaPrev,
ExecutionDurationNs: change.ExecutionDurationNs,
IssueID: issueID,
@@ -372,6 +378,7 @@ func (s *Store) ListInstanceChangeHistory(ctx context.Context, find *FindInstanc
%s,
%s,
%s,
+ instance_change_history.sheet_id,
instance_change_history.execution_duration_ns,
instance_change_history.payload,
COALESCE(instance.resource_id, ''),
@@ -403,7 +410,7 @@ func (s *Store) ListInstanceChangeHistory(ctx context.Context, find *FindInstanc
for rows.Next() {
var changeHistory InstanceChangeHistoryMessage
var rowStatus, payload string
- var instanceID, databaseID, issueID sql.NullInt32
+ var instanceID, databaseID, issueID, sheetID sql.NullInt32
if err := rows.Scan(
&changeHistory.UID,
&rowStatus,
@@ -424,6 +431,7 @@ func (s *Store) ListInstanceChangeHistory(ctx context.Context, find *FindInstanc
&changeHistory.Statement,
&changeHistory.Schema,
&changeHistory.SchemaPrev,
+ &sheetID,
&changeHistory.ExecutionDurationNs,
&payload,
&changeHistory.InstanceID,
@@ -443,6 +451,10 @@ func (s *Store) ListInstanceChangeHistory(ctx context.Context, find *FindInstanc
n := int(issueID.Int32)
changeHistory.IssueUID = &n
}
+ if sheetID.Valid {
+ n := int(sheetID.Int32)
+ changeHistory.SheetID = &n
+ }
changeHistory.Payload = &storepb.InstanceChangeHistoryPayload{}
if err := protojson.Unmarshal([]byte(payload), changeHistory.Payload); err != nil {
return nil, err
@@ -540,7 +552,7 @@ func (*Store) getNextInstanceChangeHistorySequence(ctx context.Context, tx *Tx,
// CreatePendingInstanceChangeHistory creates an instance change history.
// it deprecates the old InsertPendingHistory.
-func (s *Store) CreatePendingInstanceChangeHistory(ctx context.Context, prevSchema string, m *db.MigrationInfo, storedVersion, statement string) (string, error) {
+func (s *Store) CreatePendingInstanceChangeHistory(ctx context.Context, prevSchema string, m *db.MigrationInfo, storedVersion, statement string, sheetID *int) (string, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return "", err
@@ -564,6 +576,7 @@ func (s *Store) CreatePendingInstanceChangeHistory(ctx context.Context, prevSche
Version: storedVersion,
Description: m.Description,
Statement: statement,
+ SheetID: sheetID,
Schema: prevSchema,
SchemaPrev: prevSchema,
ExecutionDurationNs: 0,
@@ -582,6 +595,7 @@ func (s *Store) CreatePendingInstanceChangeHistory(ctx context.Context, prevSche
// ListInstanceChangeHistoryForMigrator finds the instance change history for the migrator,
// the users are not composed.
+// The sheet_id is not loaded.
func (s *Store) ListInstanceChangeHistoryForMigrator(ctx context.Context, find *FindInstanceChangeHistoryMessage) ([]*InstanceChangeHistoryMessage, error) {
where, args := []string{"TRUE"}, []any{}
if v := find.ID; v != nil {
diff --git a/backend/utils/utils.go b/backend/utils/utils.go
index 4930f0175f..f08e964fef 100644
--- a/backend/utils/utils.go
+++ b/backend/utils/utils.go
@@ -475,18 +475,18 @@ func passCheck(taskCheckRunList []*store.TaskCheckRunMessage, checkType api.Task
}
// ExecuteMigrationDefault executes migration.
-func ExecuteMigrationDefault(ctx context.Context, store *store.Store, driver db.Driver, mi *db.MigrationInfo, statement string, opts db.ExecuteOptions) (migrationHistoryID string, updatedSchema string, resErr error) {
+func ExecuteMigrationDefault(ctx context.Context, store *store.Store, driver db.Driver, mi *db.MigrationInfo, statement string, sheetID *int, opts db.ExecuteOptions) (migrationHistoryID string, updatedSchema string, resErr error) {
execFunc := func(execStatement string) error {
if _, err := driver.Execute(ctx, execStatement, false /* createDatabase */, opts); err != nil {
return err
}
return nil
}
- return ExecuteMigrationWithFunc(ctx, store, driver, mi, statement, execFunc)
+ return ExecuteMigrationWithFunc(ctx, store, driver, mi, statement, sheetID, execFunc)
}
// ExecuteMigrationWithFunc executes the migration with custom migration function.
-func ExecuteMigrationWithFunc(ctx context.Context, s *store.Store, driver db.Driver, m *db.MigrationInfo, statement string, execFunc func(execStatement string) error) (migrationHistoryID string, updatedSchema string, resErr error) {
+func ExecuteMigrationWithFunc(ctx context.Context, s *store.Store, driver db.Driver, m *db.MigrationInfo, statement string, sheetID *int, execFunc func(execStatement string) error) (migrationHistoryID string, updatedSchema string, resErr error) {
var prevSchemaBuf bytes.Buffer
// Don't record schema if the database hasn't existed yet or is schemaless, e.g. MongoDB.
// For baseline migration, we also record the live schema to detect the schema drift.
@@ -495,7 +495,7 @@ func ExecuteMigrationWithFunc(ctx context.Context, s *store.Store, driver db.Dri
return "", "", err
}
- insertedID, err := BeginMigration(ctx, s, m, prevSchemaBuf.String(), statement)
+ insertedID, err := BeginMigration(ctx, s, m, prevSchemaBuf.String(), statement, sheetID)
if err != nil {
if common.ErrorCode(err) == common.MigrationAlreadyApplied {
return insertedID, prevSchemaBuf.String(), nil
@@ -558,7 +558,7 @@ func ExecuteMigrationWithFunc(ctx context.Context, s *store.Store, driver db.Dri
}
// BeginMigration checks before executing migration and inserts a migration history record with pending status.
-func BeginMigration(ctx context.Context, store *store.Store, m *db.MigrationInfo, prevSchema string, statement string) (string, error) {
+func BeginMigration(ctx context.Context, store *store.Store, m *db.MigrationInfo, prevSchema, statement string, sheetID *int) (string, error) {
// Convert version to stored version.
storedVersion, err := util.ToStoredVersion(m.UseSemanticVersion, m.Version, m.SemanticVersionSuffix)
if err != nil {
@@ -605,7 +605,7 @@ func BeginMigration(ctx context.Context, store *store.Store, m *db.MigrationInfo
// Thus we sort of doing a 2-phase commit, where we first write a PENDING migration record, and after migration completes, we then
// update the record to DONE together with the updated schema.
statementRecord, _ := common.TruncateString(statement, common.MaxSheetSize)
- insertedID, err := store.CreatePendingInstanceChangeHistory(ctx, prevSchema, m, storedVersion, statementRecord)
+ insertedID, err := store.CreatePendingInstanceChangeHistory(ctx, prevSchema, m, storedVersion, statementRecord, sheetID)
if err != nil {
return "", err
}