Skip to content

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

feat: add a switch to control whether to build the rollback sql (#4639)

p0ny6feede777b4d
diff --git a/backend/legacyapi/issue.go b/backend/legacyapi/issue.go
index 28a7ce38c9..01a254ad50 100644
--- a/backend/legacyapi/issue.go
+++ b/backend/legacyapi/issue.go
@@ -153,6 +153,8 @@ type MigrationDetail struct {
// SchemaVersion is parsed from VCS file name.
// It is automatically generated in the UI workflow.
SchemaVersion string `json:"schemaVersion"`
+ // If RollbackEnabled, build the rollbackStatement of the task.
+ RollbackEnabled bool `json:"rollbackEnabled"`
}
// MigrationContext is the issue create context for database migration such as Migrate, Data.
diff --git a/backend/legacyapi/task.go b/backend/legacyapi/task.go
index faea5a0aca..c2740799ab 100644
--- a/backend/legacyapi/task.go
+++ b/backend/legacyapi/task.go
@@ -177,6 +177,8 @@ type TaskDatabaseDataUpdatePayload struct {
// MySQL rollback SQL related.
+ // Build the RollbackStatement if RollbackEnabled.
+ RollbackEnabled bool `json:"rollbackEnabled,omitempty"`
// ThreadID is the ID of the connection executing the migration.
// We use it to filter the binlog events of the migration transaction.
ThreadID string `json:"threadId,omitempty"`
diff --git a/backend/runner/rollbackrun/runner.go b/backend/runner/rollbackrun/runner.go
index 3d945fca48..434ad4a8eb 100644
--- a/backend/runner/rollbackrun/runner.go
+++ b/backend/runner/rollbackrun/runner.go
@@ -64,7 +64,7 @@ func (r *Runner) retryGenerateRollbackSQL(ctx context.Context) {
find := &api.TaskFind{
StatusList: &[]api.TaskStatus{api.TaskDone},
TypeList: &[]api.TaskType{api.TaskDatabaseDataUpdate},
- Payload: "task.payload->>'threadID'!='' AND task.payload->>'rollbackError' IS NULL AND task.payload->>'rollbackStatement' IS NULL",
+ Payload: "(task.payload->>'rollbackEnabled')::BOOLEAN IS TRUE AND task.payload->>'threadID'!='' AND task.payload->>'rollbackError' IS NULL AND task.payload->>'rollbackStatement' IS NULL",
}
taskList, err := r.store.ListTasks(ctx, find)
if err != nil {
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index 0aa9008bfe..e878b9ed45 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -192,8 +192,15 @@ func executeMigration(ctx context.Context, stores *store.Store, dbFactory *dbfac
if err != nil {
return "", "", errors.Wrap(err, "failed to update the task payload for MySQL rollback SQL")
}
- // The runner will periodically scan the map to generate rollback SQL asynchronously.
- stateCfg.RollbackGenerateMap.Store(task.ID, updatedTask)
+
+ payload := &api.TaskDatabaseDataUpdatePayload{}
+ if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {
+ return "", "", errors.Wrap(err, "invalid database data update payload")
+ }
+ if payload.RollbackEnabled {
+ // The runner will periodically scan the map to generate rollback SQL asynchronously.
+ stateCfg.RollbackGenerateMap.Store(task.ID, updatedTask)
+ }
}
return migrationID, schema, nil
diff --git a/backend/server/issue.go b/backend/server/issue.go
index b82d423aa4..5b2b8eddf4 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -595,6 +595,8 @@ func (s *Server) getPipelineCreateForDatabaseRollback(ctx context.Context, issue
return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to unmarshal the task payload with ID %d", taskID)).SetInternal(err)
}
switch {
+ case !taskPayload.RollbackEnabled:
+ return nil, echo.NewHTTPError(http.StatusBadRequest, "Rollback SQL is not requested to build.")
case taskPayload.RollbackStatement == "" && taskPayload.RollbackError == "":
return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Rollback SQL generation for task %d is still in progress", taskID))
case taskPayload.RollbackStatement == "" && taskPayload.RollbackError != "":
@@ -1098,10 +1100,11 @@ func getUpdateTask(database *store.DatabaseMessage, instance *store.InstanceMess
taskName = fmt.Sprintf("DML(data) for database %q", database.DatabaseName)
taskType = api.TaskDatabaseDataUpdate
payload := api.TaskDatabaseDataUpdatePayload{
- Statement: d.Statement,
- SheetID: d.SheetID,
- SchemaVersion: schemaVersion,
- VCSPushEvent: vcsPushEvent,
+ Statement: d.Statement,
+ SheetID: d.SheetID,
+ SchemaVersion: schemaVersion,
+ VCSPushEvent: vcsPushEvent,
+ RollbackEnabled: d.RollbackEnabled,
}
bytes, err := json.Marshal(payload)
if err != nil {
diff --git a/backend/tests/rollback_test.go b/backend/tests/rollback_test.go
index 9caf910d2f..f7cd49cd49 100644
--- a/backend/tests/rollback_test.go
+++ b/backend/tests/rollback_test.go
@@ -171,6 +171,7 @@ func TestCreateRollbackIssueMySQL(t *testing.T) {
DELETE FROM t WHERE id = 1;
UPDATE t SET name = 'unknown\nunknown';
`,
+ RollbackEnabled: true,
},
},
})