Skip to content

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

feat: idempotent mysql PITR (#2491)

Yilong Lid2fff000f72a
diff --git a/plugin/db/mysql/restore.go b/plugin/db/mysql/restore.go
index 1529b08f20..04ed9c5b24 100644
--- a/plugin/db/mysql/restore.go
+++ b/plugin/db/mysql/restore.go
@@ -238,16 +238,20 @@ func (driver *Driver) RestoreBackupToDatabase(ctx context.Context, backup io.Rea
// It's the first step of the PITR process.
func (driver *Driver) RestoreBackupToPITRDatabase(ctx context.Context, backup io.Reader, databaseName string, suffixTs int64) error {
pitrDatabaseName := util.GetPITRDatabaseName(databaseName, suffixTs)
- stmt := fmt.Sprintf("CREATE DATABASE `%s`;", pitrDatabaseName)
+ // If there's already a PITR database, it means there's a failed trial before this task execution.
+ // We need to clean up the dirty state and start clean for idempotent task execution.
+ stmt := fmt.Sprintf("DROP DATABASE IF EXISTS %s; CREATE DATABASE `%s`;", pitrDatabaseName, pitrDatabaseName)
db, err := driver.GetDBConnection(ctx, "")
if err != nil {
- return err
+ return errors.Wrapf(err, "failed to get connection to restore backup")
}
if _, err := db.ExecContext(ctx, stmt); err != nil {
- return err
+ return errors.Wrapf(err, "failed to create the PITR database %s", pitrDatabaseName)
}
- // Create the pitr database.
- return driver.RestoreBackupToDatabase(ctx, backup, pitrDatabaseName)
+ if err := driver.RestoreBackupToDatabase(ctx, backup, pitrDatabaseName); err != nil {
+ return errors.Wrapf(err, "failed to restore backup to the PITR database %s", pitrDatabaseName)
+ }
+ return nil
}
// GetBinlogReplayList returns the path list of the binlog that need be replayed.