Skip to content

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

Do restore database first, then do adding branch migration history record and path database to add the source backup id info

天舟498d41df5408
diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.go
index e6f5b0ca63..bee5fba8db 100644
--- a/server/task_executor_database_restore.go
+++ b/server/task_executor_database_restore.go
@@ -76,14 +76,6 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Se
}
return true, "", fmt.Errorf("failed to find target database %q in instance %q: %w", targetDatabase.Name, task.Instance.Name, err)
}
- databasePatch := &api.DatabasePatch{
- ID: targetDatabase.ID,
- UpdaterId: api.SYSTEM_BOT_ID,
- SourceBackupId: &backup.ID,
- }
- if _, err = server.DatabaseService.PatchDatabase(context.Background(), databasePatch); err != nil {
- return true, "failed to patch database source backup ID", err
- }
exec.l.Debug("Start database restore from backup...",
zap.String("source_instance", sourceDatabase.Instance.Name),
@@ -93,16 +85,30 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Se
zap.String("backup", backup.Name),
)
- // Branch migration history.
- if err := branchMigrationHistoryIfNeeded(ctx, server, sourceDatabase, targetDatabase, backup, task, exec.l); err != nil {
+ // Restore the database to the target database.
+ if err := restoreDatabase(targetDatabase, backup, server.dataDir); err != nil {
return true, "", err
}
- // Restore the database to the target database.
- if err := restoreDatabase(targetDatabase, backup, server.dataDir); err != nil {
+ // TODO(tianzhou): This should be done in the same transaction as restoreDatabase to guarantee consistency.
+ // For now, we do this after restoreDatabase, since this one is unlikely to fail.
+ if err := branchMigrationHistoryIfNeeded(ctx, server, sourceDatabase, targetDatabase, backup, task, exec.l); err != nil {
return true, "", err
}
+ // Patch the backup id after we successfully restore the database using the backup.
+ // restoringDatabase is changing the customer database instance, while here we are changing our own meta db,
+ // and since we can't guarantee cross database transaction consistency, there is always a chance to have
+ // inconsistent data. We choose to do Patch afterwards since this one is unlikely to fail.
+ databasePatch := &api.DatabasePatch{
+ ID: targetDatabase.ID,
+ UpdaterId: api.SYSTEM_BOT_ID,
+ SourceBackupId: &backup.ID,
+ }
+ if _, err = server.DatabaseService.PatchDatabase(context.Background(), databasePatch); err != nil {
+ return true, "", fmt.Errorf("failed to patch database source backup ID after restore: %w", err)
+ }
+
return true, fmt.Sprintf("Restored database %q from backup %q", targetDatabase.Name, backup.Name), nil
}