Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
test(PITR): add integration test of pitr to a new database (#2053)
h3n4l6eb3fcd1ce94
diff --git a/plugin/db/mysql/restore.go b/plugin/db/mysql/restore.goindex b9ad6ea108..8f64ee703d 100644--- a/plugin/db/mysql/restore.go+++ b/plugin/db/mysql/restore.go@@ -92,9 +92,14 @@ func newBinlogCoordinate(binlogFileName string, pos int64) (binlogCoordinate, erreturn binlogCoordinate{Seq: seq, Pos: pos}, nil}-// ReplayBinlog replays the binlog for `originDatabase` from `startBinlogInfo.Position` to `targetTs`.+// ReplayBinlog replays the binlog for `originDatabase` from `startBinlogInfo.Position` to `targetTs`, read binlog from `driver.binlogDir``.func (driver *Driver) replayBinlog(ctx context.Context, originalDatabase, targetDatabase string, startBinlogInfo api.BinlogInfo, targetTs int64) error {- replayBinlogPaths, err := GetBinlogReplayList(startBinlogInfo, driver.binlogDir)+ return driver.replayBinlogReadFromDir(ctx, originalDatabase, targetDatabase, startBinlogInfo, targetTs, driver.binlogDir)+}++// replayBinlogReadFromDir replays the binlog for `originDatabase` from `startBinlogInfo.Position` to `targetTs`, read binlog from `binlogDir`.+func (driver *Driver) replayBinlogReadFromDir(ctx context.Context, originalDatabase, targetDatabase string, startBinlogInfo api.BinlogInfo, targetTs int64, binlogDir string) error {+ replayBinlogPaths, err := GetBinlogReplayList(startBinlogInfo, binlogDir)if err != nil {return err}@@ -196,15 +201,15 @@ func (driver *Driver) GetReplayedBinlogBytes() int64 {}// ReplayBinlogToDatabase replays the binlog of originDatabaseName to the targetDatabaseName.-func (driver *Driver) ReplayBinlogToDatabase(ctx context.Context, originDatabaseName, targetDatabaseName string, startBinlogInfo api.BinlogInfo, targetTs int64) error {- return driver.replayBinlog(ctx, originDatabaseName, targetDatabaseName, startBinlogInfo, targetTs)+func (driver *Driver) ReplayBinlogToDatabase(ctx context.Context, originDatabaseName, targetDatabaseName string, startBinlogInfo api.BinlogInfo, targetTs int64, binlogDir string) error {+ return driver.replayBinlogReadFromDir(ctx, originDatabaseName, targetDatabaseName, startBinlogInfo, targetTs, binlogDir)}// ReplayBinlogToPITRDatabase replays binlog to the PITR database.// It's the second step of the PITR process.func (driver *Driver) ReplayBinlogToPITRDatabase(ctx context.Context, databaseName string, startBinlogInfo api.BinlogInfo, suffixTs, targetTs int64) error {pitrDatabaseName := util.GetPITRDatabaseName(databaseName, suffixTs)- return driver.ReplayBinlogToDatabase(ctx, databaseName, pitrDatabaseName, startBinlogInfo, targetTs)+ return driver.replayBinlog(ctx, databaseName, pitrDatabaseName, startBinlogInfo, targetTs)}// RestoreBackupToDatabase create the database named `databaseName` and restores a full backup to the given database.diff --git a/server/issue.go b/server/issue.goindex 963ac394f2..40d473e968 100644--- a/server/issue.go+++ b/server/issue.go@@ -907,19 +907,17 @@ func (s *Server) createPITRTaskList(ctx context.Context, originDatabase *api.Dat}restoreTaskCreate := api.TaskCreate{- Name: fmt.Sprintf("Restore PITR database %q", originDatabase.Name),- Status: api.TaskPendingApproval,- Type: api.TaskDatabaseRestorePITRRestore,- Payload: string(bytesRestore),- BackupID: c.BackupID,+ Name: fmt.Sprintf("Restore PITR database %q", originDatabase.Name),+ Status: api.TaskPendingApproval,+ Type: api.TaskDatabaseRestorePITRRestore,+ InstanceID: originDatabase.InstanceID,+ DatabaseID: &originDatabase.ID,+ Payload: string(bytesRestore),+ BackupID: c.BackupID,}if payloadRestore.TargetInstanceID != nil {- restoreTaskCreate.InstanceID = c.CreateDatabaseCtx.InstanceIDrestoreTaskCreate.DatabaseName = c.CreateDatabaseCtx.DatabaseName- } else {- restoreTaskCreate.InstanceID = originDatabase.InstanceID- restoreTaskCreate.DatabaseID = &originDatabase.ID}taskCreateList = append(taskCreateList, restoreTaskCreate)diff --git a/server/task_executor_pitr_restore.go b/server/task_executor_pitr_restore.goindex be6cd3311d..4ad49c91d9 100644--- a/server/task_executor_pitr_restore.go+++ b/server/task_executor_pitr_restore.go@@ -182,7 +182,13 @@ func (exec *PITRRestoreTaskExecutor) doPITRRestore(ctx context.Context, server *targetDriver := sourceDriverif payload.TargetInstanceID != nil {- targetDriver.Close(ctx)+ targetInstance, err := server.store.GetInstanceByID(ctx, *payload.TargetInstanceID)+ if err != nil {+ return nil, err+ }+ if targetDriver, err = server.getAdminDatabaseDriver(ctx, targetInstance, ""); err != nil {+ return nil, err+ }}// DB.Close is idempotent, so we can feel free to assign sourceDriver to targetDriver first.defer targetDriver.Close(ctx)@@ -253,7 +259,7 @@ func (exec *PITRRestoreTaskExecutor) doPITRRestore(ctx context.Context, server *zap.Error(err))return nil, errors.Wrap(err, "failed to restore full backup in the new database")}- if err := mysqlTargetDriver.ReplayBinlogToDatabase(ctx, task.Database.Name, *payload.DatabaseName, startBinlogInfo, targetTs); err != nil {+ if err := mysqlTargetDriver.ReplayBinlogToDatabase(ctx, task.Database.Name, *payload.DatabaseName, startBinlogInfo, targetTs, mysqlSourceDriver.GetBinlogDir()); err != nil {log.Error("failed to perform a PITR restore in the new database",zap.Int("issueID", issue.ID),zap.String("databaseName", *payload.DatabaseName),diff --git a/tests/backup_restore_test.go b/tests/backup_restore_test.goindex f94e0b8046..b50f306fbc 100644--- a/tests/backup_restore_test.go+++ b/tests/backup_restore_test.go@@ -391,6 +391,85 @@ func TestPITR(t *testing.T) {log.Debug("Second PITR done.")})+ t.Run("Restore To New Database In Another Instance", func(t *testing.T) {+ port := getTestPort(t.Name())+ sourceMySQLDB, database, cleanFn := setUpForPITRTest(t, ctl, port, prodEnvironment.ID, project)+ defer cleanFn()++ dstPort := port + 1+ _, dstStopFn := resourcemysql.SetupTestInstance(t, dstPort)+ defer dstStopFn()+ dstConnCfg := getMySQLConnectionConfig(strconv.Itoa(dstPort), "")++ dstInstance, err := ctl.addInstance(api.InstanceCreate{+ // The target instance must be within the same environment as the instance of the original database now.+ EnvironmentID: prodEnvironment.ID,+ Name: "DestinationInstance",+ Engine: db.MySQL,+ Host: dstConnCfg.Host,+ Port: dstConnCfg.Port,+ Username: dstConnCfg.Username,+ })+ a.NoError(err)++ insertRangeData(t, sourceMySQLDB, numRowsTime0, numRowsTime1)++ ctxUpdateRow, cancelUpdateRow := context.WithCancel(ctx)+ cancelUpdateRow()++ targetTs := startUpdateRow(ctxUpdateRow, t, database.Name, port) + 1++ dropColumnStmt := `ALTER TABLE tbl1 DROP COLUMN id;`+ log.Debug("mimics schema migration", zap.String("statement", dropColumnStmt))+ _, err = sourceMySQLDB.ExecContext(ctx, dropColumnStmt)+ a.NoError(err)++ labels, err := marshalLabels(nil, dstInstance.Environment.Name)+ a.NoError(err)++ targetDatabaseName := "new_database"+ pitrIssueCtx, err := json.Marshal(&api.PITRContext{+ DatabaseID: database.ID,+ PointInTimeTs: &targetTs,+ CreateDatabaseCtx: &api.CreateDatabaseContext{+ InstanceID: dstInstance.ID,+ DatabaseName: targetDatabaseName,+ Labels: labels,+ CharacterSet: "utf8mb4",+ Collation: "utf8mb4_general_ci",+ },+ })+ a.NoError(err)++ issue, err := ctl.createIssue(api.IssueCreate{+ ProjectID: project.ID,+ Name: fmt.Sprintf("Restore database %s to the time %d", database.Name, targetTs),+ Type: api.IssueDatabaseRestorePITR,+ AssigneeID: project.Creator.ID,+ CreateContext: string(pitrIssueCtx),+ })+ a.NoError(err)++ // Create database task+ status, err := ctl.waitIssueNextTaskWithTaskApproval(issue.ID)+ a.NoError(err)+ a.Equal(api.TaskDone, status)++ // Restore task+ status, err = ctl.waitIssueNextTaskWithTaskApproval(issue.ID)+ a.NoError(err)+ a.Equal(api.TaskDone, status)++ cancelUpdateRow()++ targetDB, err := connectTestMySQL(dstPort, targetDatabaseName)+ a.NoError(err)++ validateTbl0(t, targetDB, targetDatabaseName, numRowsTime1)+ validateTbl1(t, targetDB, targetDatabaseName, numRowsTime1)+ validateTableUpdateRow(t, targetDB, targetDatabaseName)+ })+t.Run("Invalid Time Point", func(t *testing.T) {a := require.New(t)port := getTestPort(t.Name())diff --git a/tests/tests.go b/tests/tests.goindex f6c372f5ac..b4011fe9bd 100644--- a/tests/tests.go+++ b/tests/tests.go@@ -140,6 +140,7 @@ func getTestPort(testName string) int {"TestPITR/Case_Sensitive","TestPITR/Invalid_Time_Point","TestPITR/PITR_Twice",+ "TestPITR/Restore_To_New_Database_In_Another_Instance","TestCheckEngineInnoDB","TestCheckServerVersionAndBinlogForPITR",