Skip to content

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

Extract the function to generate the default migration version from taskId

天舟8708b801234c
diff --git a/server/task_executor.go b/server/task_executor.go
index 76a4d78366..470631f186 100644
--- a/server/task_executor.go
+++ b/server/task_executor.go
@@ -2,6 +2,9 @@ package server
import (
"context"
+ "strconv"
+ "strings"
+ "time"
"github.com/bytebase/bytebase/api"
)
@@ -12,3 +15,9 @@ type TaskExecutor interface {
// usually indicates a transient error and will make scheduler retry later.
RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, detail string, err error)
}
+
+// defaultMigrationVersion returns the default migration version string
+// Use the concatenation of current time and the task id to guarantee uniqueness in a monotonic increasing way.
+func defaultMigrationVersionFromTaskId(taskId int) string {
+ return strings.Join([]string{time.Now().Format("20060102150405"), strconv.Itoa(taskId)}, ".")
+}
diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.go
index 86465d7e0e..eaf82faf1b 100644
--- a/server/task_executor_database_restore.go
+++ b/server/task_executor_database_restore.go
@@ -8,8 +8,6 @@ import (
"os"
"path/filepath"
"strconv"
- "strings"
- "time"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/bin/bb/connect"
@@ -167,7 +165,7 @@ func branchMigrationHistoryIfNeeded(ctx context.Context, server *Server, sourceD
issueId = strconv.Itoa(issue.ID)
}
m := &db.MigrationInfo{
- Version: strings.Join([]string{time.Now().Format("20060102150405"), strconv.Itoa(task.ID)}, "."),
+ Version: defaultMigrationVersionFromTaskId(task.ID),
Namespace: targetDatabase.Name,
Database: targetDatabase.Name,
Environment: targetDatabase.Instance.Environment.Name,
diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.go
index 9317297657..2e15a2ce65 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -6,7 +6,6 @@ import (
"fmt"
"strconv"
"strings"
- "time"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/db"
@@ -62,8 +61,7 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
} else {
mi.Creator = creator.Name
}
- // Use the concatenation of current time and the task id to guarantee uniqueness in an ascending way.
- mi.Version = strings.Join([]string{time.Now().Format("20060102150405"), strconv.Itoa(task.ID)}, ".")
+ mi.Version = defaultMigrationVersionFromTaskId(task.ID)
mi.Database = databaseName
mi.Namespace = databaseName
mi.Description = task.Name