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: remove driver.MigrationType (#17014)
p0ny06e9641052d9
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.goindex b32702fd35..0c63722a9f 100644--- a/backend/plugin/db/driver.go+++ b/backend/plugin/db/driver.go@@ -68,53 +68,6 @@ var (type driverFunc func() Driver-// MigrationType is the type of a migration.-type MigrationType string--const (- // Baseline is the migration type for BASELINE.- // Used for establishing schema baseline, this is used when- // 1. Onboard the database into Bytebase since Bytebase needs to know the current database schema.- // 2. Had schema drift and need to re-establish the baseline.- Baseline MigrationType = "BASELINE"- // Migrate is the migration type for MIGRATE.- // Used for DDL change including CREATE DATABASE.- Migrate MigrationType = "MIGRATE"- // MigrateSDL is the migration type via state-based schema migration.- // Used for schema change including CREATE DATABASE.- MigrateSDL MigrationType = "MIGRATE_SDL"- // Data is the migration type for DATA.- // Used for DML change.- Data MigrationType = "DATA"-)--// GetVersionTypeSuffix returns the suffix used for schema version string from GitOps.-func (t MigrationType) GetVersionTypeSuffix() string {- switch t {- case Migrate:- return "ddl"- case Data:- return "dml"- case MigrateSDL:- return "sdl"- case Baseline:- return "baseline"- default:- return ""- }-}--func (t MigrationType) NeedDump() bool {- switch t {- case Baseline, Migrate, MigrateSDL:- return true- case Data:- return false- default:- return false- }-}-// MigrationStatus is the status of migration.type MigrationStatus stringdiff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.goindex faf5adadf2..bb068082e5 100644--- a/backend/runner/schemasync/syncer.go+++ b/backend/runner/schemasync/syncer.go@@ -575,7 +575,7 @@ func (s *Syncer) getSchemaDrifted(ctx context.Context, instance *store.InstanceMlist, err := s.store.ListChangelogs(ctx, &store.FindChangelogMessage{InstanceID: &database.InstanceID,DatabaseName: &database.DatabaseName,- TypeList: []string{string(db.Migrate), string(db.Baseline)},+ TypeList: []string{storepb.ChangelogPayload_BASELINE.String(), storepb.ChangelogPayload_MIGRATE.String(), storepb.ChangelogPayload_MIGRATE_GHOST.String(), storepb.ChangelogPayload_MIGRATE_SDL.String()},HasSyncHistory: true,Limit: &limit,ShowFull: true,diff --git a/backend/runner/taskrun/data_update_executor.go b/backend/runner/taskrun/data_update_executor.goindex 246e829835..e4ca7e4c3a 100644--- a/backend/runner/taskrun/data_update_executor.go+++ b/backend/runner/taskrun/data_update_executor.go@@ -127,7 +127,7 @@ func (exec *DataUpdateExecutor) RunOnce(ctx context.Context, driverCtx context.C}}- terminated, result, err := runMigration(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, db.Data, statement, task.Payload.GetSchemaVersion(), &sheetID)+ terminated, result, err := runMigration(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, statement, task.Payload.GetSchemaVersion(), &sheetID)if result != nil {// Save prior backup detail to task run result.result.PriorBackupDetail = priorBackupDetaildiff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.goindex 27a2641e87..8455a6c739 100644--- a/backend/runner/taskrun/executor.go+++ b/backend/runner/taskrun/executor.go@@ -89,11 +89,10 @@ type migrateContext struct {// changelog uidchangelog int64- migrateType db.MigrationTypechangeHistoryPayload *storepb.InstanceChangeHistoryPayload}-func getMigrationInfo(ctx context.Context, stores *store.Store, profile *config.Profile, syncer *schemasync.Syncer, task *store.TaskMessage, migrationType db.MigrationType, schemaVersion string, sheetID *int, 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, sheetID *int, taskRunUID int, dbFactory *dbfactory.DBFactory) (*migrateContext, error) {instance, err := stores.GetInstanceV2(ctx, &store.FindInstanceMessage{ResourceID: &task.InstanceID})if err != nil {return nil, err@@ -123,7 +122,21 @@ func getMigrationInfo(ctx context.Context, stores *store.Store, profile *config.version: schemaVersion,taskRunName: common.FormatTaskRun(pipeline.ProjectID, task.PipelineID, task.Environment, task.ID, taskRunUID),taskRunUID: taskRunUID,- migrateType: migrationType,+ }++ switch task.Type {+ case+ storepb.Task_TASK_TYPE_UNSPECIFIED,+ storepb.Task_DATABASE_EXPORT,+ storepb.Task_DATABASE_CREATE:+ return nil, errors.Errorf("task type %s is unexpected", task.Type)+ case+ storepb.Task_DATABASE_DATA_UPDATE,+ storepb.Task_DATABASE_SCHEMA_UPDATE,+ storepb.Task_DATABASE_SCHEMA_UPDATE_GHOST,+ storepb.Task_DATABASE_SCHEMA_UPDATE_SDL:+ default:+ return nil, errors.Errorf("task type %s is unexpected", task.Type)}if sheetID != nil {@@ -273,7 +286,7 @@ func doMigrationWithFunc(slog.Debug("Start migration...",slog.String("instance", instance.ResourceID),slog.String("database", database.DatabaseName),- slog.String("type", string(mc.migrateType)),+ slog.String("type", string(mc.task.Type.String())),slog.String("statement", statementRecord),)@@ -336,9 +349,6 @@ func postMigration(ctx context.Context, stores *store.Store, mc *migrateContext,}detail := fmt.Sprintf("Applied migration version %s to database %q.", mc.version, database.DatabaseName)- if mc.migrateType == db.Baseline {- detail = fmt.Sprintf("Established baseline version %s for database %q.", mc.version, database.DatabaseName)- }return true, &storepb.TaskRunResult{Detail: detail,@@ -347,8 +357,8 @@ func postMigration(ctx context.Context, stores *store.Store, mc *migrateContext,}, nil}-func runMigrationWithFunc(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, migrationType db.MigrationType, statement string, schemaVersion string, sheetID *int, execFunc execFuncType) (terminated bool, result *storepb.TaskRunResult, err error) {- mc, err := getMigrationInfo(ctx, store, profile, syncer, task, migrationType, schemaVersion, sheetID, taskRunUID, dbFactory)+func runMigrationWithFunc(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 *int, execFunc execFuncType) (terminated bool, result *storepb.TaskRunResult, err error) {+ mc, err := getMigrationInfo(ctx, store, profile, syncer, task, schemaVersion, sheetID, taskRunUID, dbFactory)if err != nil {return true, nil, err}@@ -360,8 +370,8 @@ func runMigrationWithFunc(ctx context.Context, driverCtx context.Context, storereturn postMigration(ctx, store, mc, skipped)}-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, migrationType db.MigrationType, statement string, schemaVersion string, sheetID *int) (terminated bool, result *storepb.TaskRunResult, err error) {- return runMigrationWithFunc(ctx, driverCtx, store, dbFactory, stateCfg, syncer, profile, task, taskRunUID, migrationType, 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, statement string, schemaVersion string, sheetID *int) (terminated bool, result *storepb.TaskRunResult, err error) {+ return runMigrationWithFunc(ctx, driverCtx, store, dbFactory, stateCfg, syncer, profile, task, taskRunUID, statement, schemaVersion, sheetID, nil /* default */)}// executeMigrationWithFunc executes the migration with custom migration function.@@ -387,13 +397,11 @@ func executeMigrationWithFunc(ctx context.Context, driverCtx context.Context, s}()// Phase 2 - Executing migration.- if mc.migrateType != db.Baseline {- // Database secrets feature has been removed- // To avoid leak the rendered statement, the error message should use the original statement and not the rendered statement.- // Database secrets feature removed - using original statement directly- if err := execFunc(driverCtx, statement); err != nil {- return false, err- }+ // Database secrets feature has been removed+ // To avoid leak the rendered statement, the error message should use the original statement and not the rendered statement.+ // Database secrets feature removed - using original statement directly+ if err := execFunc(driverCtx, statement); err != nil {+ return false, err}return false, nil@@ -427,7 +435,7 @@ func beginMigration(ctx context.Context, stores *store.Store, mc *migrateContext// sync historyvar syncHistoryPrevUID *int64- if mc.migrateType.NeedDump() {+ if needDump(mc.task.Type) {opts.LogDatabaseSyncStart()syncHistoryPrev, err := mc.syncer.SyncDatabaseSchemaToHistory(ctx, mc.database)if err != nil {@@ -469,7 +477,7 @@ func endMigration(ctx context.Context, storeInstance *store.Store, mc *migrateCoUID: mc.changelog,}- if mc.migrateType.NeedDump() {+ if needDump(mc.task.Type) {syncHistory, err := mc.syncer.SyncDatabaseSchemaToHistory(ctx, mc.database)if err != nil {return errors.Wrapf(err, "failed to sync database metadata and schema")@@ -589,3 +597,22 @@ func isChangeDatabaseTask(taskType storepb.Task_Type) bool {return false}}++func needDump(taskType storepb.Task_Type) bool {+ //exhaustive:enforce+ switch taskType {+ case+ storepb.Task_DATABASE_SCHEMA_UPDATE,+ storepb.Task_DATABASE_SCHEMA_UPDATE_SDL,+ storepb.Task_DATABASE_SCHEMA_UPDATE_GHOST:+ return true+ case+ storepb.Task_TASK_TYPE_UNSPECIFIED,+ storepb.Task_DATABASE_CREATE,+ storepb.Task_DATABASE_DATA_UPDATE,+ storepb.Task_DATABASE_EXPORT:+ return false+ default:+ return false+ }+}diff --git a/backend/runner/taskrun/schema_update_executor.go b/backend/runner/taskrun/schema_update_executor.goindex 5f4a0e8ba5..f5672c7218 100644--- a/backend/runner/taskrun/schema_update_executor.go+++ b/backend/runner/taskrun/schema_update_executor.go@@ -11,7 +11,6 @@ import ("github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/enterprise"storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/runner/schemasync""github.com/bytebase/bytebase/backend/store")@@ -55,7 +54,7 @@ func (exec *SchemaUpdateExecutor) RunOnce(ctx context.Context, driverCtx contextreturn true, nil, err}- terminated, result, err := runMigration(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, db.Migrate, statement, task.Payload.GetSchemaVersion(), &sheetID)+ terminated, result, err := runMigration(ctx, driverCtx, exec.store, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, statement, task.Payload.GetSchemaVersion(), &sheetID)// sync database schema anywaysexec.store.CreateTaskRunLogS(ctx, taskRunUID, time.Now(), exec.profile.DeployID, &storepb.TaskRunLog{Type: storepb.TaskRunLog_DATABASE_SYNC_START,diff --git a/backend/runner/taskrun/schema_update_ghost_executor.go b/backend/runner/taskrun/schema_update_ghost_executor.goindex dcfcde7866..5f624f57a1 100644--- a/backend/runner/taskrun/schema_update_ghost_executor.go+++ b/backend/runner/taskrun/schema_update_ghost_executor.go@@ -149,7 +149,7 @@ func (exec *SchemaUpdateGhostExecutor) RunOnce(ctx context.Context, driverCtx co}}- terminated, result, err := runMigrationWithFunc(ctx, driverCtx, exec.s, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, db.Migrate, statement, task.Payload.GetSchemaVersion(), &sheetID, execFunc)+ terminated, result, err := runMigrationWithFunc(ctx, driverCtx, exec.s, exec.dbFactory, exec.stateCfg, exec.schemaSyncer, exec.profile, task, taskRunUID, statement, task.Payload.GetSchemaVersion(), &sheetID, execFunc)// sync database schema anywaysexec.s.CreateTaskRunLogS(ctx, taskRunUID, time.Now(), exec.profile.DeployID, &storepb.TaskRunLog{Type: storepb.TaskRunLog_DATABASE_SYNC_START,