Skip to content

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

chore: do not lose last updater on view/routines in rebase database (#13320)

h3n4lfdfb0a966344
diff --git a/backend/api/v1/schema_design_merge.go b/backend/api/v1/branch_merge.go
similarity index 99%
rename from backend/api/v1/schema_design_merge.go
rename to backend/api/v1/branch_merge.go
index f6c5bb4574..f1b1cc0e3d 100644
--- a/backend/api/v1/schema_design_merge.go
+++ b/backend/api/v1/branch_merge.go
@@ -1260,10 +1260,7 @@ var qualifiedRe = regexp.MustCompile("`" + `[^` + "`" + `]` + "`" + `\.` + "`")
func normalizeMySQLViewDefinition(query string) string {
query = strings.TrimSpace(query)
- if query == "" {
- return ""
- }
- if query[len(query)-1] != ';' {
+ if !strings.HasSuffix(query, ";") {
query += ";"
}
trailSymbols := []string{"` ", "`,", "`;"}
diff --git a/backend/api/v1/schema_design_merge_test.go b/backend/api/v1/branch_merge_test.go
similarity index 100%
rename from backend/api/v1/schema_design_merge_test.go
rename to backend/api/v1/branch_merge_test.go
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.go
index 5ac97e356e..1f01cb5ade 100644
--- a/backend/api/v1/branch_service.go
+++ b/backend/api/v1/branch_service.go
@@ -1604,44 +1604,6 @@ func updateDatabaseConfigLastModifierForMerge(baseMetadata *storepb.DatabaseSche
}
baseSchemaConfig.TableConfigs = append(baseSchemaConfig.TableConfigs, newBaseTableConfigs...)
- // Functions
- baseFunctionConfigMap := buildMap(baseSchemaConfig.FunctionConfigs, func(s *storepb.FunctionConfig) string {
- return s.GetName()
- })
- headFunctionConfigMap := buildMap(headSchemaConfig.FunctionConfigs, func(s *storepb.FunctionConfig) string {
- return s.GetName()
- })
- var newBaseFunctionConfigs []*storepb.FunctionConfig
- for _, headFunction := range headSchema.Functions {
- headFunctionConfig := headFunctionConfigMap[headFunction.Name]
- if headFunctionConfig == nil {
- headFunctionConfig = initFunctionConfig(headFunction, "", "", now)
- }
- baseFunction := baseSchema.GetFunction(headFunction.Name)
- // New function, reset the source branch and last modifier because we do not remove the config while deleting the object.
- if baseFunction == nil {
- //nolint
- newBaseFunctionConfig := proto.Clone(headFunctionConfig).(*storepb.FunctionConfig)
- newBaseFunctionConfig.SourceBranch = headFunctionConfig.SourceBranch
- newBaseFunctionConfig.Updater = headFunctionConfig.Updater
- newBaseFunctionConfig.UpdateTime = now
- newBaseFunctionConfigs = append(newBaseFunctionConfigs, newBaseFunctionConfig)
- continue
- }
- // Modified function, set the last updater as head in base.
- baseFunctionConfig := baseFunctionConfigMap[headFunction.Name]
- // Rebase database, no schema config.
- if baseFunctionConfig == nil {
- baseFunctionConfig = initFunctionConfig(baseFunction.GetProto(), "", "", now)
- }
- if !cmp.Equal(headFunction, baseFunction.GetProto(), protocmp.Transform()) {
- baseFunctionConfig.SourceBranch = headFunctionConfig.SourceBranch
- baseFunctionConfig.Updater = headFunctionConfig.Updater
- baseFunctionConfig.UpdateTime = now
- }
- }
- baseSchemaConfig.FunctionConfigs = append(baseSchemaConfig.FunctionConfigs, newBaseFunctionConfigs...)
-
// Views
baseViewConfigMap := buildMap(baseSchemaConfig.ViewConfigs, func(s *storepb.ViewConfig) string {
return s.GetName()
@@ -1672,7 +1634,7 @@ func updateDatabaseConfigLastModifierForMerge(baseMetadata *storepb.DatabaseSche
if baseViewConfig == nil {
baseViewConfig = initViewConfig(baseView.GetProto(), "", "", now)
}
- if !cmp.Equal(headView, baseView.GetProto(), protocmp.Transform()) {
+ if !equalView(headView, baseView.GetProto()) {
baseViewConfig.SourceBranch = headViewConfig.SourceBranch
baseViewConfig.Updater = headViewConfig.Updater
baseViewConfig.UpdateTime = now
@@ -1680,6 +1642,44 @@ func updateDatabaseConfigLastModifierForMerge(baseMetadata *storepb.DatabaseSche
}
baseSchemaConfig.ViewConfigs = append(baseSchemaConfig.ViewConfigs, newBaseViewConfigs...)
+ // Functions
+ baseFunctionConfigMap := buildMap(baseSchemaConfig.FunctionConfigs, func(s *storepb.FunctionConfig) string {
+ return s.GetName()
+ })
+ headFunctionConfigMap := buildMap(headSchemaConfig.FunctionConfigs, func(s *storepb.FunctionConfig) string {
+ return s.GetName()
+ })
+ var newBaseFunctionConfigs []*storepb.FunctionConfig
+ for _, headFunction := range headSchema.Functions {
+ headFunctionConfig := headFunctionConfigMap[headFunction.Name]
+ if headFunctionConfig == nil {
+ headFunctionConfig = initFunctionConfig(headFunction, "", "", now)
+ }
+ baseFunction := baseSchema.GetFunction(headFunction.Name)
+ // New function, reset the source branch and last modifier because we do not remove the config while deleting the object.
+ if baseFunction == nil {
+ //nolint
+ newBaseFunctionConfig := proto.Clone(headFunctionConfig).(*storepb.FunctionConfig)
+ newBaseFunctionConfig.SourceBranch = headFunctionConfig.SourceBranch
+ newBaseFunctionConfig.Updater = headFunctionConfig.Updater
+ newBaseFunctionConfig.UpdateTime = now
+ newBaseFunctionConfigs = append(newBaseFunctionConfigs, newBaseFunctionConfig)
+ continue
+ }
+ // Modified function, set the last updater as head in base.
+ baseFunctionConfig := baseFunctionConfigMap[headFunction.Name]
+ // Rebase database, no schema config.
+ if baseFunctionConfig == nil {
+ baseFunctionConfig = initFunctionConfig(baseFunction.GetProto(), "", "", now)
+ }
+ if !equalFunction(headFunction, baseFunction.GetProto()) {
+ baseFunctionConfig.SourceBranch = headFunctionConfig.SourceBranch
+ baseFunctionConfig.Updater = headFunctionConfig.Updater
+ baseFunctionConfig.UpdateTime = now
+ }
+ }
+ baseSchemaConfig.FunctionConfigs = append(baseSchemaConfig.FunctionConfigs, newBaseFunctionConfigs...)
+
// Procedures
baseProcedureConfigMap := buildMap(baseSchemaConfig.ProcedureConfigs, func(s *storepb.ProcedureConfig) string {
return s.GetName()
@@ -1706,7 +1706,7 @@ func updateDatabaseConfigLastModifierForMerge(baseMetadata *storepb.DatabaseSche
}
// Modified procedure, set the last updater as head in base.
baseProcedureConfig := baseProcedureConfigMap[headProcedure.Name]
- if !cmp.Equal(headProcedure, baseProcedure.GetProto(), protocmp.Transform()) {
+ if !equalProcedure(headProcedure, baseProcedure.GetProto()) {
baseProcedureConfig.SourceBranch = headProcedureConfig.SourceBranch
baseProcedureConfig.Updater = headProcedureConfig.Updater
baseProcedureConfig.UpdateTime = now
@@ -1796,3 +1796,29 @@ func alignDatabaseConfig(metadata *storepb.DatabaseSchemaMetadata, config *store
func formatViewDef(def string) string {
return strings.TrimRight(def, "; \n\r\t")
}
+
+func equalView(a, b *storepb.ViewMetadata) bool {
+ if a.GetName() != b.GetName() {
+ return false
+ }
+ if a.GetComment() != b.GetComment() {
+ return false
+ }
+ return equalViewDefinition(a.GetDefinition(), b.GetDefinition())
+}
+
+func equalFunction(a, b *storepb.FunctionMetadata) bool {
+ if a.GetName() != b.GetName() {
+ return false
+ }
+
+ return equalRoutineDefinition(a.GetDefinition(), b.GetDefinition())
+}
+
+func equalProcedure(a, b *storepb.ProcedureMetadata) bool {
+ if a.GetName() != b.GetName() {
+ return false
+ }
+
+ return equalRoutineDefinition(a.GetDefinition(), b.GetDefinition())
+}
diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.go
index d77143b36d..b922c62c0c 100644
--- a/backend/plugin/db/mysql/sync.go
+++ b/backend/plugin/db/mysql/sync.go
@@ -533,7 +533,6 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema
schemaMetadata.Tables = append(schemaMetadata.Tables, tableMetadata)
case viewTableType:
if view, ok := viewMap[key]; ok {
- view.Comment = comment
schemaMetadata.Views = append(schemaMetadata.Views, view)
}
}
diff --git a/backend/plugin/db/tidb/sync.go b/backend/plugin/db/tidb/sync.go
index de52f6a44f..cfd2875018 100644
--- a/backend/plugin/db/tidb/sync.go
+++ b/backend/plugin/db/tidb/sync.go
@@ -459,7 +459,6 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema
schemaMetadata.Tables = append(schemaMetadata.Tables, tableMetadata)
case viewTableType:
if view, ok := viewMap[key]; ok {
- view.Comment = comment
schemaMetadata.Views = append(schemaMetadata.Views, view)
}
}