Skip to content

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

feat(pg): tenant mode migration should use the same migration version string (#765)

Danny Xu794959b5b101
diff --git a/api/task.go b/api/task.go
index cc0982cd69..f36c4be25d 100644
--- a/api/task.go
+++ b/api/task.go
@@ -92,6 +92,7 @@ type TaskDatabaseSchemaUpdatePayload struct {
MigrationType db.MigrationType `json:"migrationType,omitempty"`
Statement string `json:"statement,omitempty"`
RollbackStatement string `json:"rollbackStatement,omitempty"`
+ SchemaVersion string `json:"schemaVersion,omitempty"`
VCSPushEvent *vcs.PushEvent `json:"pushEvent,omitempty"`
}
@@ -99,6 +100,7 @@ type TaskDatabaseSchemaUpdatePayload struct {
type TaskDatabaseDataUpdatePayload struct {
Statement string `json:"statement,omitempty"`
RollbackStatement string `json:"rollbackStatement,omitempty"`
+ SchemaVersion string `json:"schemaVersion,omitempty"`
VCSPushEvent *vcs.PushEvent `json:"pushEvent,omitempty"`
}
diff --git a/server/issue.go b/server/issue.go
index 58625cd69a..e0684850ce 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -603,6 +603,9 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
}
schemaVersion, schema = sv, s
}
+ if schemaVersion == "" {
+ schemaVersion = defaultMigrationVersionFromTaskID()
+ }
payload := api.TaskDatabaseCreatePayload{}
payload.ProjectID = issueCreate.ProjectID
@@ -721,6 +724,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()
// Tenant mode project pipeline has its own generation.
if project.TenantMode == api.TenantModeTenant {
if !s.feature(api.FeatureMultiTenancy) {
@@ -768,7 +772,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
if policy.Value == api.PipelineApprovalValueManualNever {
taskStatus = api.TaskPending
}
- taskCreate, err := getUpdateTask(database, m.MigrationType, m.VCSPushEvent, d, taskStatus)
+ taskCreate, err := getUpdateTask(database, m.MigrationType, m.VCSPushEvent, d, schemaVersion, taskStatus)
if err != nil {
return nil, err
}
@@ -815,7 +819,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
taskStatus = api.TaskPending
}
- taskCreate, err := getUpdateTask(database, m.MigrationType, m.VCSPushEvent, d, taskStatus)
+ taskCreate, err := getUpdateTask(database, m.MigrationType, m.VCSPushEvent, d, schemaVersion, taskStatus)
if err != nil {
return nil, err
}
@@ -863,7 +867,7 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
return createdPipeline, nil
}
-func getUpdateTask(database *api.Database, migrationType db.MigrationType, vcsPushEvent *vcs.PushEvent, d *api.UpdateSchemaDetail, taskStatus api.TaskStatus) (*api.TaskCreate, error) {
+func getUpdateTask(database *api.Database, migrationType db.MigrationType, vcsPushEvent *vcs.PushEvent, d *api.UpdateSchemaDetail, schemaVersion string, taskStatus api.TaskStatus) (*api.TaskCreate, error) {
taskName := fmt.Sprintf("Establish %q baseline", database.Name)
if migrationType == db.Migrate {
taskName = fmt.Sprintf("Update %q schema", database.Name)
@@ -873,6 +877,7 @@ func getUpdateTask(database *api.Database, migrationType db.MigrationType, vcsPu
payload := api.TaskDatabaseSchemaUpdatePayload{}
payload.MigrationType = migrationType
payload.Statement = d.Statement
+ payload.SchemaVersion = schemaVersion
if d.RollbackStatement != "" {
payload.RollbackStatement = d.RollbackStatement
}
diff --git a/server/task_executor.go b/server/task_executor.go
index 885873ad72..6ec72897da 100644
--- a/server/task_executor.go
+++ b/server/task_executor.go
@@ -30,11 +30,11 @@ type TaskExecutor interface {
// 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)}, ".")
+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 string, vcsPushEvent *vcs.PushEvent) (terminated bool, result *api.TaskRunResultPayload, err error) {
+func runMigration(ctx context.Context, l *zap.Logger, server *Server, task *api.Task, migrationType db.MigrationType, statement, schemaVersion string, vcsPushEvent *vcs.PushEvent) (terminated bool, result *api.TaskRunResultPayload, err error) {
if task.Database == nil {
msg := "missing database when updating schema"
if migrationType == db.Data {
@@ -62,7 +62,7 @@ func runMigration(ctx context.Context, l *zap.Logger, server *Server, task *api.
} else {
mi.Creator = creator.Name
}
- mi.Version = defaultMigrationVersionFromTaskID(task.ID)
+ mi.Version = schemaVersion
mi.Database = databaseName
mi.Namespace = databaseName
mi.Description = task.Name
diff --git a/server/task_executor_data_update.go b/server/task_executor_data_update.go
index 2df1a89257..dcd1976a4e 100644
--- a/server/task_executor_data_update.go
+++ b/server/task_executor_data_update.go
@@ -41,5 +41,5 @@ func (exec *DataUpdateTaskExecutor) RunOnce(ctx context.Context, server *Server,
return true, nil, fmt.Errorf("invalid database data update payload: %w", err)
}
- return runMigration(ctx, exec.l, server, task, db.Data, payload.Statement, payload.VCSPushEvent)
+ return runMigration(ctx, exec.l, server, task, db.Data, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent)
}
diff --git a/server/task_executor_database_create.go b/server/task_executor_database_create.go
index 8e303c300e..54747f8b10 100644
--- a/server/task_executor_database_create.go
+++ b/server/task_executor_database_create.go
@@ -66,13 +66,9 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser
)
// Create a baseline migration history upon creating the database.
- version := payload.SchemaVersion
- if version == "" {
- version = defaultMigrationVersionFromTaskID(task.ID)
- }
mi := &db.MigrationInfo{
ReleaseVersion: server.version,
- Version: version,
+ Version: payload.SchemaVersion,
Namespace: payload.DatabaseName,
Database: payload.DatabaseName,
Environment: instance.Environment.Name,
diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.go
index 554dc49762..a8d791423c 100644
--- a/server/task_executor_database_restore.go
+++ b/server/task_executor_database_restore.go
@@ -179,7 +179,7 @@ func createBranchMigrationHistory(ctx context.Context, server *Server, sourceDat
}
m := &db.MigrationInfo{
ReleaseVersion: server.version,
- Version: defaultMigrationVersionFromTaskID(task.ID),
+ Version: defaultMigrationVersionFromTaskID(),
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 c08ed9c731..fdde80129c 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -40,5 +40,5 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
return true, nil, fmt.Errorf("invalid database schema update payload: %w", err)
}
- return runMigration(ctx, exec.l, server, task, payload.MigrationType, payload.Statement, payload.VCSPushEvent)
+ return runMigration(ctx, exec.l, server, task, payload.MigrationType, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent)
}
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index aa6d73ba07..773010a7e0 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -237,6 +237,9 @@ func TestSchemaAndDataUpdate(t *testing.T) {
if len(diff) != 0 {
t.Fatalf("migration history %v got %v, want %v, diff %v", i, got, want, diff)
}
+ if history.Version == "" {
+ t.Fatalf("empty migration history version for migration %v", i)
+ }
}
// Create a manual backup.
@@ -595,5 +598,14 @@ func TestVCS(t *testing.T) {
if len(diff) != 0 {
t.Fatalf("migration history %v got %v, want %v, diff %v", i, got, want, diff)
}
+ if history.Version == "" {
+ t.Fatalf("empty migration history version for migration %v", i)
+ }
+ }
+ if histories[0].Version != "ver2" {
+ t.Fatalf("invalid migration(0) history version, want ver2 got %v", histories[0].Version)
+ }
+ if histories[1].Version != "ver1" {
+ t.Fatalf("invalid migration(0) history version, want ver1 got %v", histories[0].Version)
}
}
diff --git a/tests/tenant_test.go b/tests/tenant_test.go
index 535a39a36e..bd1e2edecc 100644
--- a/tests/tenant_test.go
+++ b/tests/tenant_test.go
@@ -224,6 +224,31 @@ func TestTenant(t *testing.T) {
t.Fatalf("SQL result want %q, got %q, diff %q", bookSchemaSQLResult, result, pretty.Diff(bookSchemaSQLResult, result))
}
}
+
+ // Query migration history
+ instances := append(stagingInstances, prodInstances...)
+ var hm1 map[string]bool = map[string]bool{}
+ var hm2 map[string]bool = map[string]bool{}
+ for _, instance := range instances {
+ histories, err := ctl.getInstanceMigrationHistory(db.MigrationHistoryFind{ID: &instance.ID, Database: &databaseName})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(histories) != 2 {
+ t.Fatalf("invalid number of migration histories, want 2, got %v", len(histories))
+ }
+ if histories[0].Version == "" {
+ t.Fatalf("empty migration history(0) version")
+ }
+ if histories[1].Version == "" {
+ t.Fatalf("empty migration history(1) version")
+ }
+ hm1[histories[0].Version] = true
+ hm2[histories[1].Version] = true
+ }
+ if len(hm1) != 1 || len(hm2) != 1 {
+ t.Fatalf("migration history should have only one version in tenant mode")
+ }
}
func TestTenantVCS(t *testing.T) {
@@ -481,6 +506,31 @@ func TestTenantVCS(t *testing.T) {
t.Fatalf("SQL result want %q, got %q, diff %q", bookSchemaSQLResult, result, pretty.Diff(bookSchemaSQLResult, result))
}
}
+
+ // Query migration history
+ instances := append(stagingInstances, prodInstances...)
+ var hm1 map[string]bool = map[string]bool{}
+ var hm2 map[string]bool = map[string]bool{}
+ for _, instance := range instances {
+ histories, err := ctl.getInstanceMigrationHistory(db.MigrationHistoryFind{ID: &instance.ID, Database: &databaseName})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(histories) != 2 {
+ t.Fatalf("invalid number of migration histories, want 2, got %v", len(histories))
+ }
+ if histories[0].Version != "ver1" {
+ t.Fatalf("invalid migration history(0) version, want ver1, got %q", histories[0].Version)
+ }
+ if histories[1].Version == "" {
+ t.Fatalf("empty migration history(1) version")
+ }
+ hm1[histories[0].Version] = true
+ hm2[histories[1].Version] = true
+ }
+ if len(hm1) != 1 || len(hm2) != 1 {
+ t.Fatalf("migration history should have only one version in tenant mode")
+ }
}
func TestTenantDatabaseNameTemplate(t *testing.T) {