Skip to content

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

refactor: use sheet *SheetMessage in migration functions (#18584)

p0ny78559a7415aa
diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.go
index 807ffe1386..1af45fd0e3 100644
--- a/backend/runner/taskrun/database_migrate_executor.go
+++ b/backend/runner/taskrun/database_migrate_executor.go
@@ -69,7 +69,6 @@ func (exec *DatabaseMigrateExecutor) runMigrationWithPriorBackup(ctx context.Con
if sheet == nil {
return true, nil, errors.Errorf("sheet not found: %s", task.Payload.GetSheetSha256())
}
- statement := sheet.Statement
// Handle prior backup if enabled.
// TransformDMLToSelect will automatically filter out DDL statements,
@@ -99,7 +98,7 @@ func (exec *DatabaseMigrateExecutor) runMigrationWithPriorBackup(ctx context.Con
// Check if we should skip backup or not.
if common.EngineSupportPriorBackup(database.Engine) {
var backupErr error
- priorBackupDetail, backupErr = exec.backupData(ctx, driverCtx, statement, task.Payload, task, instance, database)
+ priorBackupDetail, backupErr = exec.backupData(ctx, driverCtx, sheet.Statement, task.Payload, task, instance, database)
if backupErr != nil {
exec.store.CreateTaskRunLogS(ctx, taskRunUID, time.Now(), exec.profile.DeployID, &storepb.TaskRunLog{
Type: storepb.TaskRunLog_PRIOR_BACKUP_END,
@@ -127,7 +126,7 @@ func (exec *DatabaseMigrateExecutor) runMigrationWithPriorBackup(ctx context.Con
}
}
- terminated, result, err := runMigration(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, statement, task.Payload.GetSchemaVersion(), &sheet.Sha256)
+ terminated, result, err := runMigration(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, sheet, task.Payload.GetSchemaVersion())
if result != nil {
// Save prior backup detail to task run result.
result.PriorBackupDetail = priorBackupDetail
@@ -143,7 +142,6 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, driv
if sheet == nil {
return true, nil, errors.Errorf("sheet not found: %s", task.Payload.GetSheetSha256())
}
- statement := sheet.Statement
flags := task.Payload.GetFlags()
instance, err := exec.store.GetInstance(ctx, &store.FindInstanceMessage{ResourceID: &task.InstanceID})
@@ -235,7 +233,7 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, driv
}
}
- return runMigrationWithFunc(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, statement, task.Payload.GetSchemaVersion(), &sheet.Sha256, execFunc)
+ return runMigrationWithFunc(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, sheet, task.Payload.GetSchemaVersion(), execFunc)
}
func (exec *DatabaseMigrateExecutor) shouldSkipBackupError(ctx context.Context, task *store.TaskMessage) (bool, error) {
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index d243bcb62d..5d8def156a 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -62,9 +62,9 @@ type migrateContext struct {
profile *config.Profile
dbFactory *dbfactory.DBFactory
- instance *store.InstanceMessage
- database *store.DatabaseMessage
- sheetSha256 string
+ instance *store.InstanceMessage
+ database *store.DatabaseMessage
+ sheet *store.SheetMessage
task *store.TaskMessage
taskRunUID int
@@ -114,8 +114,8 @@ func getUseDatabaseOwner(ctx context.Context, stores *store.Store, instance *sto
return project.Setting.PostgresDatabaseTenantMode, nil
}
-func runMigration(ctx context.Context, driverCtx context.Context, store *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State, syncer *schemasync.Syncer, profile *config.Profile, task *store.TaskMessage, taskRunUID int, statement string, schemaVersion string, sheetID *string) (terminated bool, result *storepb.TaskRunResult, err error) {
- return runMigrationWithFunc(ctx, driverCtx, store, dbFactory, stateCfg, syncer, profile, task, taskRunUID, statement, schemaVersion, sheetID, nil /* default */)
+func runMigration(ctx context.Context, driverCtx context.Context, store *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State, syncer *schemasync.Syncer, profile *config.Profile, task *store.TaskMessage, taskRunUID int, sheet *store.SheetMessage, schemaVersion string) (terminated bool, result *storepb.TaskRunResult, err error) {
+ return runMigrationWithFunc(ctx, driverCtx, store, dbFactory, stateCfg, syncer, profile, task, taskRunUID, sheet, schemaVersion, nil /* default */)
}
func runMigrationWithFunc(
@@ -128,28 +128,27 @@ func runMigrationWithFunc(
profile *config.Profile,
task *store.TaskMessage,
taskRunUID int,
- statement string,
+ sheet *store.SheetMessage,
schemaVersion string,
- sheetID *string,
execFunc execFuncType,
) (terminated bool, result *storepb.TaskRunResult, err error) {
- mc, err := getMigrationInfo(ctx, store, profile, syncer, task, schemaVersion, sheetID, taskRunUID, dbFactory)
+ mc, err := getMigrationInfo(ctx, store, profile, syncer, task, schemaVersion, sheet, taskRunUID, dbFactory)
if err != nil {
return true, nil, err
}
// Pre-compute whether schema dump is needed.
// Skip dump for pure DML statements (INSERT, UPDATE, DELETE) as they don't change schema.
- mc.needDump = computeNeedDump(task.Type, mc.database.Engine, statement)
+ mc.needDump = computeNeedDump(task.Type, mc.database.Engine, sheet.Statement)
- skipped, err := doMigrationWithFunc(ctx, driverCtx, store, stateCfg, profile, statement, mc, execFunc)
+ skipped, err := doMigrationWithFunc(ctx, driverCtx, store, stateCfg, profile, sheet.Statement, mc, execFunc)
if err != nil {
return true, nil, err
}
return postMigration(ctx, store, mc, skipped)
}
-func getMigrationInfo(ctx context.Context, stores *store.Store, profile *config.Profile, syncer *schemasync.Syncer, task *store.TaskMessage, schemaVersion string, sheetID *string, taskRunUID int, dbFactory *dbfactory.DBFactory) (*migrateContext, error) {
+func getMigrationInfo(ctx context.Context, stores *store.Store, profile *config.Profile, syncer *schemasync.Syncer, task *store.TaskMessage, schemaVersion string, sheet *store.SheetMessage, taskRunUID int, dbFactory *dbfactory.DBFactory) (*migrateContext, error) {
instance, err := stores.GetInstance(ctx, &store.FindInstanceMessage{ResourceID: &task.InstanceID})
if err != nil {
return nil, err
@@ -175,6 +174,7 @@ func getMigrationInfo(ctx context.Context, stores *store.Store, profile *config.
dbFactory: dbFactory,
instance: instance,
database: database,
+ sheet: sheet,
task: task,
version: schemaVersion,
taskRunName: common.FormatTaskRun(pipeline.ProjectID, task.PipelineID, task.Environment, task.ID, taskRunUID),
@@ -194,10 +194,6 @@ func getMigrationInfo(ctx context.Context, stores *store.Store, profile *config.
return nil, errors.Errorf("task type %s is unexpected", task.Type)
}
- if sheetID != nil {
- mc.sheetSha256 = *sheetID
- }
-
if isChangeDatabaseTask(task) {
if f := task.Payload.GetTaskReleaseSource().GetFile(); f != "" {
project, release, _, err := common.GetProjectReleaseUIDFile(f)
@@ -433,7 +429,7 @@ func beginMigration(ctx context.Context, stores *store.Store, mc *migrateContext
Payload: &storepb.ChangelogPayload{
TaskRun: mc.taskRunName,
Revision: 0,
- SheetSha256: mc.sheetSha256,
+ SheetSha256: mc.sheet.Sha256,
Version: mc.version,
Type: changelogType,
GitCommit: mc.profile.GitCommit,
@@ -474,7 +470,7 @@ func endMigration(ctx context.Context, storeInstance *store.Store, mc *migrateCo
Payload: &storepb.RevisionPayload{
Release: mc.release.release,
File: mc.release.file,
- SheetSha256: mc.sheetSha256,
+ SheetSha256: mc.sheet.Sha256,
TaskRun: mc.taskRunName,
Type: storepb.SchemaChangeType_VERSIONED,
},
diff --git a/backend/runner/taskrun/schema_update_sdl_executor.go b/backend/runner/taskrun/schema_update_sdl_executor.go
index f35a5015b7..1c55370e3c 100644
--- a/backend/runner/taskrun/schema_update_sdl_executor.go
+++ b/backend/runner/taskrun/schema_update_sdl_executor.go
@@ -58,7 +58,6 @@ func (exec *SchemaDeclareExecutor) RunOnce(ctx context.Context, driverCtx contex
if sheet == nil {
return true, nil, errors.Errorf("sheet not found: %s", task.Payload.GetSheetSha256())
}
- sheetContent := sheet.Statement
execFunc := func(ctx context.Context, execStatement string, driver db.Driver, opts db.ExecuteOptions) error {
opts.LogComputeDiffStart()
@@ -77,7 +76,7 @@ func (exec *SchemaDeclareExecutor) RunOnce(ctx context.Context, driverCtx contex
return nil
}
- return runMigrationWithFunc(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, sheetContent, task.Payload.GetSchemaVersion(), &sheet.Sha256, execFunc)
+ return runMigrationWithFunc(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, sheet, task.Payload.GetSchemaVersion(), execFunc)
}
func diff(ctx context.Context, s *store.Store, instance *store.InstanceMessage, database *store.DatabaseMessage, sheetContent string) (string, error) {