Skip to content

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

feat: rollback generation status (#4716)

p0ny80f22663cb85
diff --git a/backend/legacyapi/task.go b/backend/legacyapi/task.go
index 372e2ad998..c7967876a8 100644
--- a/backend/legacyapi/task.go
+++ b/backend/legacyapi/task.go
@@ -164,6 +164,18 @@ type TaskDatabaseSchemaUpdateGhostCutoverPayload struct {
SkippedReason string `json:"skippedReason,omitempty"`
}
+// RollbackSQLStatus is the status of a rollback SQL generation task.
+type RollbackSQLStatus string
+
+const (
+ // RollbackSQLStatusPending means the rollback SQL generation task is pending.
+ RollbackSQLStatusPending RollbackSQLStatus = "PENDING"
+ // RollbackSQLStatusDone means the rollback SQL generation task finished and has no error.
+ RollbackSQLStatusDone RollbackSQLStatus = "DONE"
+ // RollbackSQLStatusFailed means the rollback SQL generation task failed.
+ RollbackSQLStatusFailed RollbackSQLStatus = "FAILED"
+)
+
// TaskDatabaseDataUpdatePayload is the task payload for database data update (DML).
type TaskDatabaseDataUpdatePayload struct {
// Common fields
@@ -179,6 +191,8 @@ type TaskDatabaseDataUpdatePayload struct {
// Build the RollbackStatement if RollbackEnabled.
RollbackEnabled bool `json:"rollbackEnabled,omitempty"`
+ // RollbackSQLStatus is the status of the rollback generation.
+ RollbackSQLStatus RollbackSQLStatus `json:"rollbackSqlStatus,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"`
@@ -333,6 +347,7 @@ type TaskPatch struct {
Statement *string `jsonapi:"attr,statement"`
SchemaVersion *string
RollbackEnabled *bool `jsonapi:"attr,rollbackEnabled"`
+ RollbackSQLStatus *RollbackSQLStatus
RollbackStatement *string
RollbackError *string
}
diff --git a/backend/runner/rollbackrun/runner.go b/backend/runner/rollbackrun/runner.go
index 2eaf01e367..07801c1fae 100644
--- a/backend/runner/rollbackrun/runner.go
+++ b/backend/runner/rollbackrun/runner.go
@@ -68,7 +68,7 @@ func (r *Runner) retryGenerateRollbackSQL(ctx context.Context) {
find := &api.TaskFind{
StatusList: &[]api.TaskStatus{api.TaskDone},
TypeList: &[]api.TaskType{api.TaskDatabaseDataUpdate},
- Payload: "(task.payload->>'rollbackEnabled')::BOOLEAN IS TRUE AND task.payload->>'threadID'!='' AND COALESCE(task.payload->>'rollbackError', '')='' AND COALESCE(task.payload->>'rollbackStatement', '')=''",
+ Payload: "(task.payload->>'rollbackEnabled')::BOOLEAN IS TRUE AND task.payload->>'threadID'!='' AND task.payload->>'rollbackSqlStatus'='PENDING'",
}
taskList, err := r.store.ListTasks(ctx, find)
if err != nil {
@@ -97,26 +97,26 @@ func (r *Runner) generateRollbackSQL(ctx context.Context, task *store.TaskMessag
log.Error("Invalid database data update payload", zap.Error(err))
return
}
- payload.RollbackStatement = ""
- payload.RollbackError = ""
+
+ var rollbackSQLStatus api.RollbackSQLStatus
+ var rollbackStatement, rollbackError string
rollbackSQL, err := r.generateRollbackSQLImpl(ctx, task, payload)
if err != nil {
log.Error("Failed to generate rollback SQL statement", zap.Error(err))
- payload.RollbackError = err.Error()
+ rollbackSQLStatus = api.RollbackSQLStatusFailed
+ rollbackError = err.Error()
+ } else {
+ rollbackSQLStatus = api.RollbackSQLStatusDone
+ rollbackStatement = rollbackSQL
}
- payload.RollbackStatement = rollbackSQL
- payloadBytes, err := json.Marshal(payload)
- if err != nil {
- log.Error("Failed to marshal task payload", zap.Error(err))
- return
- }
- payloadString := string(payloadBytes)
patch := &api.TaskPatch{
- ID: task.ID,
- UpdaterID: api.SystemBotID,
- Payload: &payloadString,
+ ID: task.ID,
+ UpdaterID: api.SystemBotID,
+ RollbackSQLStatus: &rollbackSQLStatus,
+ RollbackStatement: &rollbackStatement,
+ RollbackError: &rollbackError,
}
if _, err := r.store.UpdateTaskV2(ctx, patch); err != nil {
log.Error("Failed to patch task with the MySQL thread ID", zap.Int("taskID", task.ID))
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index 0dfd99598a..f20e282350 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -352,13 +352,19 @@ func (s *Scheduler) PatchTask(ctx context.Context, task *store.TaskMessage, task
}
}
- // Reset rollbackStatement and rollbackError because we are trying to build
+ // Reset because we are trying to build
// the rollbackStatement again and there could be previous runs.
if taskPatch.RollbackEnabled != nil && *taskPatch.RollbackEnabled {
empty := ""
+ pending := api.RollbackSQLStatusPending
+ taskPatch.RollbackSQLStatus = &pending
taskPatch.RollbackStatement = &empty
taskPatch.RollbackError = &empty
}
+ // if *taskPatch.RollbackEnabled == false, we don't reset
+ // 1. they are meaningless anyway if rollback is disabled
+ // 2. they will be reset when enabling
+ // 3. we cancel the generation after writing to db. The rollback sql generation may finish and write to db, too.
taskPatched, err := s.store.UpdateTaskV2(ctx, taskPatch)
if err != nil {
@@ -371,8 +377,9 @@ func (s *Scheduler) PatchTask(ctx context.Context, task *store.TaskMessage, task
}
}
+ // enqueue or cancel after it's written to the database.
if taskPatch.RollbackEnabled != nil {
- // Enqueue the task if it's done.
+ // Enqueue the rollback sql generation if the task done.
if *taskPatch.RollbackEnabled && taskPatched.Status == api.TaskDone {
s.stateCfg.RollbackGenerate.Store(taskPatched.ID, taskPatched)
} else {
diff --git a/backend/server/issue.go b/backend/server/issue.go
index 5b2b8eddf4..93d222a011 100644
--- a/backend/server/issue.go
+++ b/backend/server/issue.go
@@ -596,13 +596,11 @@ func (s *Server) getPipelineCreateForDatabaseRollback(ctx context.Context, issue
}
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, "Rollback SQL generation is not enabled.")
+ case taskPayload.RollbackSQLStatus == api.RollbackSQLStatusPending:
return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Rollback SQL generation for task %d is still in progress", taskID))
- case taskPayload.RollbackStatement == "" && taskPayload.RollbackError != "":
+ case taskPayload.RollbackSQLStatus == api.RollbackSQLStatusFailed:
return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Rollback SQL generation for task %d has already failed: %s", taskID, taskPayload.RollbackError))
- case taskPayload.RollbackStatement != "" && taskPayload.RollbackError != "":
- return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Invalid task payload: RollbackStatement=%q, RollbackError=%q", taskPayload.RollbackStatement, taskPayload.RollbackError))
}
issueCreateContext := &api.MigrationContext{
@@ -1100,11 +1098,12 @@ 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,
- RollbackEnabled: d.RollbackEnabled,
+ Statement: d.Statement,
+ SheetID: d.SheetID,
+ SchemaVersion: schemaVersion,
+ VCSPushEvent: vcsPushEvent,
+ RollbackEnabled: d.RollbackEnabled,
+ RollbackSQLStatus: api.RollbackSQLStatusPending,
}
bytes, err := json.Marshal(payload)
if err != nil {
diff --git a/backend/store/task.go b/backend/store/task.go
index 4dbe463f17..15f153bd7d 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -420,8 +420,8 @@ func (s *Store) UpdateTaskV2(ctx context.Context, patch *api.TaskPatch) (*TaskMe
if (patch.Statement != nil || patch.SchemaVersion != nil) && patch.Payload != nil {
return nil, errors.Errorf("cannot set both statement/schemaVersion and payload for TaskPatch")
}
- if (patch.RollbackEnabled != nil || patch.RollbackStatement != nil || patch.RollbackError != nil) && patch.Payload != nil {
- return nil, errors.Errorf("cannot set both rollbackEnabled/rollbackStatement/rollbackError payload for TaskPatch")
+ if (patch.RollbackEnabled != nil || patch.RollbackSQLStatus != nil || patch.RollbackStatement != nil || patch.RollbackError != nil) && patch.Payload != nil {
+ return nil, errors.Errorf("cannot set both rollbackEnabled/rollbackSQLStatus/rollbackStatement/rollbackError payload for TaskPatch")
}
var payloadSet []string
if v := patch.Statement; v != nil {
@@ -433,6 +433,9 @@ func (s *Store) UpdateTaskV2(ctx context.Context, patch *api.TaskPatch) (*TaskMe
if v := patch.RollbackEnabled; v != nil {
payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('rollbackEnabled', to_jsonb($%d::BOOLEAN))`, len(args)+1)), append(args, *v)
}
+ if v := patch.RollbackSQLStatus; v != nil {
+ payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('rollbackSqlStatus', to_jsonb($%d::TEXT))`, len(args)+1)), append(args, *v)
+ }
if v := patch.RollbackStatement; v != nil {
payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('rollbackStatement', to_jsonb($%d::TEXT))`, len(args)+1)), append(args, *v)
}