Skip to content

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

chore: add force option to migration (#1552)

Danny Xu5d0de684045c
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 569e46bd29..769c271510 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -253,6 +253,10 @@ type MigrationInfo struct {
// SemanticVersionSuffix should be set to timestamp format of "20060102150405" (common.DefaultMigrationVersion) if UseSemanticVersion is set.
// Since stored version should be unique, we have to append a suffix if we allow users to baseline to the same semantic version for fixing schema drift.
SemanticVersionSuffix string
+ // Force is used to execute migration disregarding any migration history with PENDING or FAILED status.
+ // This applies to BASELINE and MIGRATE types of migrations because most of these migrations are retriable.
+ // We don't use force option for DATA type of migrations yet till there's customer needs.
+ Force bool
}
// ParseMigrationInfo matches filePath against filePathTemplate
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 5e6a11df01..7c9f995bd0 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -207,11 +207,21 @@ func BeginMigration(ctx context.Context, executor MigrationExecutor, m *db.Migra
return -1, common.Errorf(common.MigrationAlreadyApplied,
fmt.Errorf("database %q has already applied version %s", m.Database, m.Version))
case db.Pending:
- return -1, common.Errorf(common.MigrationPending,
- fmt.Errorf("database %q version %s migration is already in progress", m.Database, m.Version))
+ err := fmt.Errorf("database %q version %s migration is already in progress", m.Database, m.Version)
+ log.Debug(err.Error())
+ // For force migration, we will ignore the existing migration history and continue to migration.
+ if m.Force {
+ return int64(list[0].ID), nil
+ }
+ return -1, common.Errorf(common.MigrationPending, err)
case db.Failed:
- return -1, common.Errorf(common.MigrationFailed,
- fmt.Errorf("database %q version %s migration has failed, please check your database to make sure things are fine and then start a new migration using a new version ", m.Database, m.Version))
+ err := fmt.Errorf("database %q version %s migration has failed, please check your database to make sure things are fine and then start a new migration using a new version ", m.Database, m.Version)
+ log.Debug(err.Error())
+ // For force migration, we will ignore the existing migration history and continue to migration.
+ if m.Force {
+ return int64(list[0].ID), nil
+ }
+ return -1, common.Errorf(common.MigrationFailed, err)
}
}
diff --git a/server/task_executor.go b/server/task_executor.go
index b6e0928223..0ac049131f 100644
--- a/server/task_executor.go
+++ b/server/task_executor.go
@@ -117,6 +117,11 @@ func preMigration(ctx context.Context, server *Server, task *api.Task, migration
if mi.Type != db.Baseline && statement == "" {
return nil, fmt.Errorf("empty statement")
}
+ // We will force migration for baseline and migrate type of migrations.
+ // This usually happens when the previous attempt fails and the client retries the migration.
+ if mi.Type == db.Baseline || mi.Type == db.Migrate {
+ mi.Force = true
+ }
return mi, nil
}
diff --git a/server/task_executor_database_create.go b/server/task_executor_database_create.go
index 097027fd91..29050be82a 100644
--- a/server/task_executor_database_create.go
+++ b/server/task_executor_database_create.go
@@ -59,6 +59,7 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser
Type: db.Baseline,
Description: "Create database",
CreateDatabase: true,
+ Force: true,
}
creator, err := server.store.GetPrincipalByID(ctx, task.CreatorID)
if err != nil {