Skip to content

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

Add a branch migration history on database restore.

spinningbotd344fa355b5b
diff --git a/db/driver.go b/db/driver.go
index 4e85a94f59..632ed6e917 100644
--- a/db/driver.go
+++ b/db/driver.go
@@ -110,6 +110,7 @@ type MigrationType string
const (
Baseline MigrationType = "BASELINE"
Sql MigrationType = "SQL"
+ Branch MigrationType = "BRANCH"
)
func (e MigrationType) String() string {
@@ -118,6 +119,8 @@ func (e MigrationType) String() string {
return "BASELINE"
case Sql:
return "SQL"
+ case Branch:
+ return "BRANCH"
}
return "UNKNOWN"
}
diff --git a/db/mysql_migration_schema.sql b/db/mysql_migration_schema.sql
index 331a6fe8d9..fd8ad50a46 100644
--- a/db/mysql_migration_schema.sql
+++ b/db/mysql_migration_schema.sql
@@ -53,7 +53,7 @@ CREATE TABLE bytebase.migration_history (
sequence INTEGER UNSIGNED NOT NULL,
-- We call it engine because maybe we could load history from other migration tool.
`engine` ENUM('UI', 'VCS') NOT NULL,
- `type` ENUM('BASELINE', 'SQL') NOT NULL,
+ `type` ENUM('BASELINE', 'SQL', 'BRANCH') NOT NULL,
version TEXT NOT NULL,
description TEXT NOT NULL,
-- Recorded the migration statement
diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.go
index d6ddedcbcc..fd1c7b6656 100644
--- a/server/task_executor_database_restore.go
+++ b/server/task_executor_database_restore.go
@@ -6,6 +6,9 @@ import (
"encoding/json"
"fmt"
"os"
+ "strconv"
+ "strings"
+ "time"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/bin/bb/connect"
@@ -80,7 +83,7 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Se
// Fork migration history.
if backup.MigrationHistoryVersion != "" {
- if err := forkMigrationHistory(backup.Database, task.Database, backup.MigrationHistoryVersion, exec.l); err != nil {
+ if err := forkMigrationHistory(backup.Database, task.Database, backup, task, exec.l); err != nil {
return true, "", err
}
}
@@ -115,7 +118,7 @@ func restoreDatabase(database *api.Database, backup *api.Backup) error {
// forkMigrationHistory will fork the migration history from source database to target database based on backup migration history version.
// This is needed only when backup migration history version is not empty.
-func forkMigrationHistory(sourceDatabase, targetDatabase *api.Database, migrationHistoryVersion string, logger *zap.Logger) error {
+func forkMigrationHistory(sourceDatabase, targetDatabase *api.Database, backup *api.Backup, task *api.Task, logger *zap.Logger) error {
sourceDriver, err := getDatabaseDriver(sourceDatabase, logger)
if err != nil {
return err
@@ -142,16 +145,15 @@ func forkMigrationHistory(sourceDatabase, targetDatabase *api.Database, migratio
history.Namespace = targetDatabase.Name
forkList = append(forkList, history)
// Fork the history up to the backup version.
- if history.Version == migrationHistoryVersion {
+ if history.Version == backup.MigrationHistoryVersion {
break
}
}
- // TODO(spinningbot): add a new BRANCH migration history.
for _, history := range forkList {
m := &db.MigrationInfo{
Version: history.Version,
- Namespace: history.Namespace,
+ Namespace: targetDatabase.Name,
Database: targetDatabase.Name,
Environment: targetDatabase.Instance.Environment.Name,
Engine: history.Engine,
@@ -166,6 +168,23 @@ func forkMigrationHistory(sourceDatabase, targetDatabase *api.Database, migratio
}
}
+ // Add a branch migration history record.
+ // TODO(spinningbot): fill in the new issue ID.
+ m := &db.MigrationInfo{
+ Version: strings.Join([]string{time.Now().Format("20060102150405"), strconv.Itoa(task.ID)}, "."),
+ Namespace: targetDatabase.Name,
+ Database: targetDatabase.Name,
+ Environment: targetDatabase.Instance.Environment.Name,
+ Engine: db.MigrationEngine(targetDatabase.Project.WorkflowType),
+ Type: db.Branch,
+ Description: fmt.Sprintf("Branched from backup %q of database %q.", backup.Name, sourceDatabase.Name),
+ Creator: task.Creator.Name,
+ IssueId: "TODO: newIssueID",
+ Payload: "",
+ }
+ if err := targetDriver.ExecuteMigration(context.Background(), m, ""); err != nil {
+ return err
+ }
return nil
}