Skip to content

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

fix: add sleep time between issues in tests (#1060)

Danny Xu325f828561e6
diff --git a/bin/bb/cmd/migrate.go b/bin/bb/cmd/migrate.go
index c35d366dec..d2e433376c 100644
--- a/bin/bb/cmd/migrate.go
+++ b/bin/bb/cmd/migrate.go
@@ -8,8 +8,8 @@ import (
"os"
"os/user"
"strings"
- "time"
+ "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
// install mysql driver.
@@ -126,7 +126,7 @@ func migrateDatabase(ctx context.Context, databaseType, username, password, host
// TODO(d): support semantic versioning.
if _, _, err := driver.ExecuteMigration(ctx, &db.MigrationInfo{
ReleaseVersion: version,
- Version: defaultMigrationVersion(),
+ Version: common.DefaultMigrationVersion(),
Database: database,
Source: db.LIBRARY,
Type: db.Migrate,
@@ -139,7 +139,3 @@ func migrateDatabase(ctx context.Context, databaseType, username, password, host
}
return nil
}
-
-func defaultMigrationVersion() string {
- return time.Now().Format("20060102150405")
-}
diff --git a/common/util.go b/common/util.go
index 43ddb14bcd..db496fca52 100644
--- a/common/util.go
+++ b/common/util.go
@@ -5,6 +5,7 @@ import (
"path"
"sort"
"strings"
+ "time"
)
// FindString returns the search index of sorted strings.
@@ -48,3 +49,10 @@ func GetPostgresDataDir(dataDir string) string {
func GetPostgresSocketDir() string {
return "/tmp"
}
+
+// DefaultMigrationVersion returns the default migration version string.
+// Use the concatenation of current time in second to guarantee uniqueness in a monotonic increasing way.
+// We cannot add task ID because tenant mode databases should use the same migration version string when applying a schema update.
+func DefaultMigrationVersion() string {
+ return time.Now().Format("20060102150405")
+}
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 8bc6f9bd7d..739fc5e4a9 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -236,9 +236,9 @@ type MigrationInfo struct {
// UseSemanticVersion is whether version is a semantic version.
// When UseSemanticVersion is set, version should be set to the format specified in Semantic Versioning 2.0.0 (https://semver.org/).
// For example, for setting non-semantic version "hello", the values should be Version = "hello", UseSemanticVersion = false, SemanticVersionSuffix = "".
- // For setting semantic version "1.2.0", the values should be Version = "1.2.0", UseSemanticVersion = true, SemanticVersionSuffix = "20060102150405".
+ // For setting semantic version "1.2.0", the values should be Version = "1.2.0", UseSemanticVersion = true, SemanticVersionSuffix = "20060102150405" (common.DefaultMigrationVersion).
UseSemanticVersion bool
- // SemanticVersionSuffix should be set to timestamp format of "20060102150405" if UseSemanticVersion is set.
+ // SemanticVersionSuffix should be set to timestamp format of "20060102150405" (common.DefaultMigrationVersion) if UseSemanticVersion is set.
// Since stored version should be unique, we have to append a suffix if we allow users to baseline to the same semantic version for fixing schema drift.
SemanticVersionSuffix string
}
diff --git a/server/issue.go b/server/issue.go
index 976cc56755..32e804e399 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -648,7 +648,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
schemaVersion, schema = sv, s
}
if schemaVersion == "" {
- schemaVersion = defaultMigrationVersionFromTaskID()
+ schemaVersion = common.DefaultMigrationVersion()
}
payload := api.TaskDatabaseCreatePayload{}
@@ -771,7 +771,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project ID: %v", issueCreate.ProjectID)).SetInternal(err)
}
- schemaVersion := defaultMigrationVersionFromTaskID()
+ schemaVersion := common.DefaultMigrationVersion()
// Tenant mode project pipeline has its own generation.
if project.TenantMode == api.TenantModeTenant {
if !s.feature(api.FeatureMultiTenancy) {
diff --git a/server/task.go b/server/task.go
index fabec3638f..f6e828a125 100644
--- a/server/task.go
+++ b/server/task.go
@@ -105,7 +105,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
payload.Statement = *taskPatch.Statement
// We should update the schema version if we've updated the SQL, otherwise we will
// get migration history version conflict if the previous task has been attempted.
- payload.SchemaVersion = defaultMigrationVersionFromTaskID()
+ payload.SchemaVersion = common.DefaultMigrationVersion()
bytes, err := json.Marshal(payload)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to construct updated task payload").SetInternal(err)
@@ -121,7 +121,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
payload.Statement = *taskPatch.Statement
// We should update the schema version if we've updated the SQL, otherwise we will
// get migration history version conflict if the previous task has been attempted.
- payload.SchemaVersion = defaultMigrationVersionFromTaskID()
+ payload.SchemaVersion = common.DefaultMigrationVersion()
bytes, err := json.Marshal(payload)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to construct updated task payload").SetInternal(err)
diff --git a/server/task_executor.go b/server/task_executor.go
index 29c85ef11e..5060118ce3 100644
--- a/server/task_executor.go
+++ b/server/task_executor.go
@@ -7,7 +7,6 @@ import (
"path/filepath"
"strconv"
"strings"
- "time"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
@@ -28,12 +27,6 @@ type TaskExecutor interface {
RunOnce(ctx context.Context, server *Server, task *api.Task) (terminated bool, result *api.TaskRunResultPayload, 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() string {
- return time.Now().Format("20060102150405")
-}
-
func runMigration(ctx context.Context, l *zap.Logger, server *Server, task *api.Task, migrationType db.MigrationType, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent) ( /*terminated*/ bool /*result*/, *api.TaskRunResultPayload, error) {
if task.Database == nil {
msg := "missing database when updating schema"
diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.go
index 23aaf76531..f4c7998d91 100644
--- a/server/task_executor_database_restore.go
+++ b/server/task_executor_database_restore.go
@@ -10,6 +10,7 @@ import (
"strconv"
"github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
"go.uber.org/zap"
)
@@ -176,7 +177,7 @@ func createBranchMigrationHistory(ctx context.Context, server *Server, sourceDat
// TODO(d): support semantic versioning.
m := &db.MigrationInfo{
ReleaseVersion: server.version,
- Version: defaultMigrationVersionFromTaskID(),
+ Version: common.DefaultMigrationVersion(),
Namespace: targetDatabase.Name,
Database: targetDatabase.Name,
Environment: targetDatabase.Instance.Environment.Name,
diff --git a/store/pg_engine.go b/store/pg_engine.go
index b63acc39e2..e8e52c6f5c 100644
--- a/store/pg_engine.go
+++ b/store/pg_engine.go
@@ -313,7 +313,7 @@ func migrate(ctx context.Context, d dbdriver.Driver, curVer *semver.Version, mod
ReleaseVersion: serverVersion,
UseSemanticVersion: true,
Version: cutoffSchemaVersion.String(),
- SemanticVersionSuffix: time.Now().Format("20060102150405"),
+ SemanticVersionSuffix: common.DefaultMigrationVersion(),
Namespace: databaseName,
Database: databaseName,
Environment: "", /* unused in execute migration */
@@ -375,7 +375,7 @@ func migrate(ctx context.Context, d dbdriver.Driver, curVer *semver.Version, mod
ReleaseVersion: serverVersion,
UseSemanticVersion: true,
Version: pv.version.String(),
- SemanticVersionSuffix: time.Now().Format("20060102150405"),
+ SemanticVersionSuffix: common.DefaultMigrationVersion(),
Namespace: databaseName,
Database: databaseName,
Environment: "", /* unused in execute migration */
diff --git a/tests/tests.go b/tests/tests.go
index 9fd219028b..5ec185f879 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -676,6 +676,9 @@ func getAggregatedTaskStatus(issue *api.Issue) (api.TaskStatus, error) {
// waitIssuePipeline waits for pipeline to finish and approves tasks when necessary.
func (ctl *controller) waitIssuePipeline(id int) (api.TaskStatus, error) {
+ // Sleep for two seconds between issues so that we don't get migration version conflict because we are using second-level timestamp for the version string. We choose sleep because it mimics the user's behavior.
+ time.Sleep(2 * time.Second)
+
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()