Skip to content

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

feat: handle last updater in merge/rebase branch (#13250)

h3n4l24e7762a5ee3
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.go
index 115e4b3feb..75f090f96f 100644
--- a/backend/api/v1/branch_service.go
+++ b/backend/api/v1/branch_service.go
@@ -12,6 +12,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
+ "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
@@ -336,7 +337,7 @@ func (s *BranchService) UpdateBranch(ctx context.Context, request *v1pb.UpdateBr
reconcileMetadata(metadata, branch.Engine)
filteredMetadata := filterDatabaseMetadataByEngine(metadata, branch.Engine)
- updateConfigBranchUpdateInfo(branch.Head.Metadata, filteredMetadata, config, common.FormatUserUID(user.ID), common.FormatBranchResourceID(projectID, branchID))
+ updateConfigBranchUpdateInfoForUpdate(branch.Head.Metadata, filteredMetadata, config, common.FormatUserUID(user.ID), common.FormatBranchResourceID(projectID, branchID))
defaultSchema := extractDefaultSchemaForOracleBranch(storepb.Engine(branch.Engine), filteredMetadata)
schema, err := schema.GetDesignSchema(branch.Engine, defaultSchema, "", filteredMetadata)
if err != nil {
@@ -479,16 +480,16 @@ func (s *BranchService) MergeBranch(ctx context.Context, request *v1pb.MergeBran
}
// XXX(zp): We only try to merge the schema config while the schema could be merged successfully. Otherwise, users manually merge the
// metadata in the frontend, and config would be ignored.
- mergedConfig := utils.MergeDatabaseConfig(headBranch.Base.GetDatabaseConfig(), headBranch.Head.GetDatabaseConfig(), baseBranch.Head.GetDatabaseConfig())
+ mergedConfig := utils.MergeDatabaseConfig(baseBranch.Head.GetDatabaseConfig(), headBranch.Base.GetDatabaseConfig(), headBranch.Head.GetDatabaseConfig())
mergedSchemaBytes := []byte(mergedSchema)
reconcileMetadata(mergedMetadata, baseBranch.Engine)
filteredMergedMetadata := filterDatabaseMetadataByEngine(mergedMetadata, baseBranch.Engine)
- updateConfigBranchUpdateInfo(baseBranch.Base.Metadata, filteredMergedMetadata, mergedConfig, "", "" /* TODO(zp): fix me later */)
+ updateDatabaseConfigLastModifierForMerge(baseBranch.Head.Metadata, filteredMergedMetadata, baseBranch.Head.DatabaseConfig, mergedConfig)
baseBranchNewHead := &storepb.BranchSnapshot{
Metadata: filteredMergedMetadata,
- DatabaseConfig: mergedConfig,
+ DatabaseConfig: baseBranch.Head.DatabaseConfig,
}
baseBranchNewHeadSchema := mergedSchemaBytes
@@ -579,6 +580,8 @@ func (s *BranchService) RebaseBranch(ctx context.Context, request *v1pb.RebaseBr
return nil, status.Errorf(codes.InvalidArgument, "failed to convert merged schema to metadata, %v", err)
}
newHeadConfig = baseBranch.Head.GetDatabaseConfig()
+ updateDatabaseConfigLastModifierForMerge(baseBranch.Head.Metadata, newHeadMetadata, baseBranch.Head.DatabaseConfig, newHeadConfig)
+ newHeadConfig = baseBranch.Head.GetDatabaseConfig()
// String-based rebase operation do not include the structural information, such as classification, so we need to sanitize the user comment,
// trim the classification in user comment if the classification is not from the config.
modelNewHeadConfig := model.NewDatabaseConfig(newHeadConfig)
@@ -620,7 +623,7 @@ func (s *BranchService) RebaseBranch(ctx context.Context, request *v1pb.RebaseBr
}
// XXX(zp): We only try to merge the schema config while the schema could be merged successfully. Otherwise, users manually merge the
// metadata in the frontend, and config would be ignored.
- newHeadConfig = utils.MergeDatabaseConfig(baseBranch.Base.GetDatabaseConfig(), baseBranch.Head.GetDatabaseConfig(), newBaseConfig)
+ newHeadConfig = utils.MergeDatabaseConfig(newBaseConfig, baseBranch.Base.GetDatabaseConfig(), baseBranch.Head.GetDatabaseConfig())
}
reconcileMetadata(newHeadMetadata, baseBranch.Engine)
@@ -633,7 +636,7 @@ func (s *BranchService) RebaseBranch(ctx context.Context, request *v1pb.RebaseBr
newBaseSchemaBytes := []byte(newBaseSchema)
newHeadSchemaBytes := []byte(newHeadSchema)
- updateConfigBranchUpdateInfo(filteredNewBaseMetadata, filteredNewHeadMetadata, newHeadConfig, "", "" /* TODO(zp): fix me later */)
+ updateDatabaseConfigLastModifierForMerge(filteredNewHeadMetadata, filteredNewBaseMetadata, newHeadConfig, newBaseConfig)
if request.ValidateOnly {
baseBranch.Base = &storepb.BranchSnapshot{
Metadata: filteredNewBaseMetadata,
@@ -715,7 +718,9 @@ func (s *BranchService) getFilteredNewBaseFromRebaseRequest(ctx context.Context,
return nil, "", nil, status.Errorf(codes.Internal, err.Error())
}
- return filteredNewBaseMetadata, sourceSchema, databaseSchema.GetConfig(), nil
+ alignedDatabaseConfig := databaseSchema.GetConfig()
+ alignDatabaseConfig(filteredNewBaseMetadata, alignedDatabaseConfig)
+ return filteredNewBaseMetadata, sourceSchema, alignedDatabaseConfig, nil
}
if request.SourceBranch != "" {
@@ -1197,10 +1202,10 @@ func reconcileMySQLPartitionMetadata(partitions []*storepb.TablePartitionMetadat
}
}
-// updateConfigBranchUpdateInfo compare the proto of old and new metadata, and update the config branch update info.
+// updateConfigBranchUpdateInfoForUpdate compare the proto of old and new metadata, and update the config branch update info.
// NOTE: this function would not delete the config of deleted objects, and it's safe because the next time adding the object
// back will trigger the update of the config branch update info.
-func updateConfigBranchUpdateInfo(old *storepb.DatabaseSchemaMetadata, new *storepb.DatabaseSchemaMetadata, config *storepb.DatabaseConfig, formattedUserUID string, formattedBranchResourceID string) {
+func updateConfigBranchUpdateInfoForUpdate(old *storepb.DatabaseSchemaMetadata, new *storepb.DatabaseSchemaMetadata, config *storepb.DatabaseConfig, formattedUserUID string, formattedBranchResourceID string) {
time := timestamppb.Now()
oldModel := model.NewDatabaseMetadata(old)
@@ -1499,3 +1504,261 @@ func trimClassificationIDFromCommentIfNeeded(dbSchema *storepb.DatabaseSchemaMet
}
}
}
+
+func updateDatabaseConfigLastModifierForMerge(baseMetadata *storepb.DatabaseSchemaMetadata, headMetadata *storepb.DatabaseSchemaMetadata, baseConfig *storepb.DatabaseConfig, headConfig *storepb.DatabaseConfig) {
+ now := timestamppb.Now()
+ baseModel := model.NewDatabaseMetadata(baseMetadata)
+ baseSchemaConfigMap := buildMap(baseConfig.SchemaConfigs, func(s *storepb.SchemaConfig) string {
+ return s.GetName()
+ })
+ headSchemaConfigMap := buildMap(headConfig.SchemaConfigs, func(s *storepb.SchemaConfig) string {
+ return s.GetName()
+ })
+
+ var newBaseSchemaConfigs []*storepb.SchemaConfig
+ for _, headSchema := range headMetadata.Schemas {
+ headSchemaConfig := headSchemaConfigMap[headSchema.Name]
+ if headSchemaConfig == nil {
+ headSchemaConfig = initSchemaConfig(headSchema, "", "", now)
+ }
+ baseSchema := baseModel.GetSchema(headSchema.Name)
+ if baseSchema == nil {
+ //nolint
+ newBaseSchemaConfig := proto.Clone(headSchemaConfig).(*storepb.SchemaConfig)
+ newBaseSchemaConfigs = append(newBaseSchemaConfigs, newBaseSchemaConfig)
+ continue
+ }
+
+ // Modified schema, set the last updater as head in base.
+ baseSchemaConfig := baseSchemaConfigMap[headSchema.Name]
+ // Rebase database, no schema config.
+ if baseSchemaConfig == nil {
+ baseSchemaConfig = initSchemaConfig(baseSchema.GetProto(), "", "", now)
+ }
+
+ // Tables
+ baseTableConfigMap := buildMap(baseSchemaConfig.TableConfigs, func(s *storepb.TableConfig) string {
+ return s.GetName()
+ })
+ headTableConfigMap := buildMap(headSchemaConfig.TableConfigs, func(s *storepb.TableConfig) string {
+ return s.GetName()
+ })
+ var newBaseTableConfigs []*storepb.TableConfig
+ for _, headTable := range headSchema.Tables {
+ headTableConfig := headTableConfigMap[headTable.Name]
+ if headTableConfig == nil {
+ headTableConfig = initTableConfig(headTable, "", "", now)
+ }
+ baseTable := baseSchema.GetTable(headTable.Name)
+ // New table, reset the source branch and last modifier because we do not remove the config while deleting the object.
+ if baseTable == nil {
+ //nolint
+ newBaseTableConfig := proto.Clone(headTableConfig).(*storepb.TableConfig)
+ newBaseTableConfig.SourceBranch = ""
+ newBaseTableConfig.Updater = headTableConfig.Updater
+ newBaseTableConfig.UpdateTime = now
+ newBaseTableConfigs = append(newBaseTableConfigs, newBaseTableConfig)
+ continue
+ }
+ // Modified table, set the last updater as head in base.
+ baseTableConfig := baseTableConfigMap[headTable.Name]
+ // Rebase database, no schema config.
+ if baseTableConfig == nil {
+ baseTableConfig = initTableConfig(baseTable.GetProto(), "", "", now)
+ }
+ if !cmp.Equal(headTable, baseTable.GetProto(), protocmp.Transform()) {
+ baseTableConfig.SourceBranch = ""
+ baseTableConfig.Updater = headTableConfig.Updater
+ baseTableConfig.UpdateTime = now
+ }
+ }
+ 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 = ""
+ 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 = ""
+ 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()
+ })
+ headViewConfigMap := buildMap(headSchemaConfig.ViewConfigs, func(s *storepb.ViewConfig) string {
+ return s.GetName()
+ })
+ var newBaseViewConfigs []*storepb.ViewConfig
+ for _, headView := range headSchema.Views {
+ headViewConfig := headViewConfigMap[headView.Name]
+ if headViewConfig == nil {
+ headViewConfig = initViewConfig(headView, "", "", now)
+ }
+ baseView := baseSchema.GetView(headView.Name)
+ // New view, reset the source branch and last modifier because we do not remove the config while deleting the object.
+ if baseView == nil {
+ //nolint
+ newBaseViewConfig := proto.Clone(headViewConfig).(*storepb.ViewConfig)
+ newBaseViewConfig.SourceBranch = ""
+ newBaseViewConfig.Updater = headViewConfig.Updater
+ newBaseViewConfig.UpdateTime = now
+ newBaseViewConfigs = append(newBaseViewConfigs, newBaseViewConfig)
+ continue
+ }
+ // Modified view, set the last updater as head in base.
+ baseViewConfig := baseViewConfigMap[headView.Name]
+ // Rebase database, no schema config.
+ if baseViewConfig == nil {
+ baseViewConfig = initViewConfig(baseView.GetProto(), "", "", now)
+ }
+ if !cmp.Equal(headView, baseView.GetProto(), protocmp.Transform()) {
+ baseViewConfig.SourceBranch = ""
+ baseViewConfig.Updater = headViewConfig.Updater
+ baseViewConfig.UpdateTime = now
+ }
+ }
+ baseSchemaConfig.ViewConfigs = append(baseSchemaConfig.ViewConfigs, newBaseViewConfigs...)
+
+ // Procedures
+ baseProcedureConfigMap := buildMap(baseSchemaConfig.ProcedureConfigs, func(s *storepb.ProcedureConfig) string {
+ return s.GetName()
+ })
+ headProcedureConfigMap := buildMap(headSchemaConfig.ProcedureConfigs, func(s *storepb.ProcedureConfig) string {
+ return s.GetName()
+ })
+ var newBaseProcedureConfigs []*storepb.ProcedureConfig
+ for _, headProcedure := range headSchema.Procedures {
+ headProcedureConfig := headProcedureConfigMap[headProcedure.Name]
+ if headProcedureConfig == nil {
+ headProcedureConfig = initProcedureConfig(headProcedure, "", "", now)
+ }
+ baseProcedure := baseSchema.GetProcedure(headProcedure.Name)
+ // New procedure, reset the source branch and last modifier because we do not remove the config while deleting the object.
+ if baseProcedure == nil {
+ //nolint
+ newBaseProcedureConfig := proto.Clone(headProcedureConfig).(*storepb.ProcedureConfig)
+ newBaseProcedureConfig.SourceBranch = ""
+ newBaseProcedureConfig.Updater = headProcedureConfig.Updater
+ newBaseProcedureConfig.UpdateTime = now
+ newBaseProcedureConfigs = append(newBaseProcedureConfigs, newBaseProcedureConfig)
+ continue
+ }
+ // Modified procedure, set the last updater as head in base.
+ baseProcedureConfig := baseProcedureConfigMap[headProcedure.Name]
+ if !cmp.Equal(headProcedure, baseProcedure.GetProto(), protocmp.Transform()) {
+ baseProcedureConfig.SourceBranch = ""
+ baseProcedureConfig.Updater = headProcedureConfig.Updater
+ baseProcedureConfig.UpdateTime = now
+ }
+ }
+ baseSchemaConfig.ProcedureConfigs = append(baseSchemaConfig.ProcedureConfigs, newBaseProcedureConfigs...)
+ }
+
+ baseConfig.SchemaConfigs = append(baseConfig.SchemaConfigs, newBaseSchemaConfigs...)
+}
+
+func alignDatabaseConfig(metadata *storepb.DatabaseSchemaMetadata, config *storepb.DatabaseConfig) {
+ if config == nil {
+ config = &storepb.DatabaseConfig{
+ Name: metadata.Name,
+ }
+ }
+ now := timestamppb.Now()
+ dbModel := model.NewDatabaseMetadata(metadata)
+ schemaConfigMap := buildMap(config.SchemaConfigs, func(s *storepb.SchemaConfig) string {
+ return s.Name
+ })
+ var newSchemaConfigs []*storepb.SchemaConfig
+ for _, schemaName := range dbModel.ListSchemaNames() {
+ schemaModel := dbModel.GetSchema(schemaName)
+ schemaConfig, ok := schemaConfigMap[schemaName]
+ if !ok {
+ newSchemaConfigs = append(newSchemaConfigs, initSchemaConfig(schemaModel.GetProto(), "", "", now))
+ continue
+ }
+
+ var newTableConfigs []*storepb.TableConfig
+ tableConfigMap := buildMap(schemaConfig.TableConfigs, func(s *storepb.TableConfig) string {
+ return s.Name
+ })
+ for _, tableName := range schemaModel.ListTableNames() {
+ tableModel := schemaModel.GetTable(tableName)
+ if _, ok := tableConfigMap[tableName]; !ok {
+ newTableConfigs = append(newTableConfigs, initTableConfig(tableModel.GetProto(), "", "", now))
+ continue
+ }
+ }
+ schemaConfig.TableConfigs = append(schemaConfig.TableConfigs, newTableConfigs...)
+
+ var newViewConfigs []*storepb.ViewConfig
+ viewConfigMap := buildMap(schemaConfig.ViewConfigs, func(s *storepb.ViewConfig) string {
+ return s.Name
+ })
+ for _, viewName := range schemaModel.ListViewNames() {
+ viewModel := schemaModel.GetView(viewName)
+ if _, ok := viewConfigMap[viewName]; !ok {
+ newViewConfigs = append(newViewConfigs, initViewConfig(viewModel.GetProto(), "", "", now))
+ continue
+ }
+ }
+ schemaConfig.ViewConfigs = append(schemaConfig.ViewConfigs, newViewConfigs...)
+
+ var newProcedureConfigs []*storepb.ProcedureConfig
+ procedureConfigMap := buildMap(schemaConfig.ProcedureConfigs, func(s *storepb.ProcedureConfig) string {
+ return s.Name
+ })
+ for _, procedureName := range schemaModel.ListProcedureNames() {
+ procedureModel := schemaModel.GetProcedure(procedureName)
+ if _, ok := procedureConfigMap[procedureName]; !ok {
+ newProcedureConfigs = append(newProcedureConfigs, initProcedureConfig(procedureModel.GetProto(), "", "", now))
+ continue
+ }
+ }
+ schemaConfig.ProcedureConfigs = append(schemaConfig.ProcedureConfigs, newProcedureConfigs...)
+
+ var newFunctionConfigs []*storepb.FunctionConfig
+ functionConfigMap := buildMap(schemaConfig.FunctionConfigs, func(s *storepb.FunctionConfig) string {
+ return s.Name
+ })
+ for _, functionName := range schemaModel.ListFunctionNames() {
+ functionModel := schemaModel.GetFunction(functionName)
+ if _, ok := functionConfigMap[functionName]; !ok {
+ newFunctionConfigs = append(newFunctionConfigs, initFunctionConfig(functionModel.GetProto(), "", "", now))
+ continue
+ }
+ }
+ schemaConfig.FunctionConfigs = append(schemaConfig.FunctionConfigs, newFunctionConfigs...)
+ }
+ config.SchemaConfigs = append(config.SchemaConfigs, newSchemaConfigs...)
+}
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index a68ad54d76..d93e99901d 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -294,7 +294,7 @@ func postMigration(ctx context.Context, stores *store.Store, task *store.TaskMes
if err != nil {
slog.Error("Failed to get database config from store", slog.Int("sheetID", *sheetID), slog.Int("databaseUID", *task.DatabaseID), log.BBError(err))
} else {
- updatedDatabaseConfig := utils.MergeDatabaseConfig(sheet.Payload.BaselineDatabaseConfig, databaseSchema.GetConfig(), sheet.Payload.DatabaseConfig)
+ updatedDatabaseConfig := utils.MergeDatabaseConfig(sheet.Payload.DatabaseConfig, sheet.Payload.BaselineDatabaseConfig, databaseSchema.GetConfig())
err = stores.UpdateDBSchema(ctx, *task.DatabaseID, &store.UpdateDBSchemaMessage{
Config: updatedDatabaseConfig,
}, api.SystemBotID)
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index 97b7d89b33..feb4de70b9 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -321,6 +321,7 @@ func NewDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata) *DatabaseMeta
internalFunctions: make(map[string]*FunctionMetadata),
internalProcedures: make(map[string]*ProcedureMetadata),
internalSequences: make(map[string]*SequenceMetadata),
+ proto: schema,
}
for _, table := range schema.Tables {
tables, names := buildTablesMetadata(table)
@@ -428,6 +429,8 @@ type SchemaMetadata struct {
internalFunctions map[string]*FunctionMetadata
internalProcedures map[string]*ProcedureMetadata
internalSequences map[string]*SequenceMetadata
+
+ proto *storepb.SchemaMetadata
}
// GetTable gets the schema by name.
@@ -464,6 +467,11 @@ func (s *SchemaMetadata) GetSequence(name string) *SequenceMetadata {
return s.internalSequences[name]
}
+// GetProto gets the proto of SchemaMetadata.
+func (s *SchemaMetadata) GetProto() *storepb.SchemaMetadata {
+ return s.proto
+}
+
// ListTableNames lists the table names.
func (s *SchemaMetadata) ListTableNames() []string {
var result []string
@@ -475,6 +483,28 @@ func (s *SchemaMetadata) ListTableNames() []string {
return result
}
+// ListProcedureNames lists the procedure names.
+func (s *SchemaMetadata) ListProcedureNames() []string {
+ var result []string
+ for procedureName := range s.internalProcedures {
+ result = append(result, procedureName)
+ }
+
+ sort.Strings(result)
+ return result
+}
+
+// ListFunctionNames lists the function names.
+func (s *SchemaMetadata) ListFunctionNames() []string {
+ var result []string
+ for functionName := range s.internalFunctions {
+ result = append(result, functionName)
+ }
+
+ sort.Strings(result)
+ return result
+}
+
// ListViewNames lists the view names.
func (s *SchemaMetadata) ListViewNames() []string {
var result []string
diff --git a/backend/utils/database_config_merge.go b/backend/utils/database_config_merge.go
index 7fa1a408f5..93f20eb24b 100644
--- a/backend/utils/database_config_merge.go
+++ b/backend/utils/database_config_merge.go
@@ -11,7 +11,7 @@ import (
// MergeDatabaseConfig computes the migration from target and baseline, and applies the migration to current databaseConfig.
// Return the merged databaseConfig.
-func MergeDatabaseConfig(baseline, head, target *storepb.DatabaseConfig) *storepb.DatabaseConfig {
+func MergeDatabaseConfig(target, baseline, current *storepb.DatabaseConfig) *storepb.DatabaseConfig {
// Avoid nil values.
if target == nil {
target = &storepb.DatabaseConfig{}
@@ -19,11 +19,11 @@ func MergeDatabaseConfig(baseline, head, target *storepb.DatabaseConfig) *storep
if baseline == nil {
baseline = &storepb.DatabaseConfig{}
}
- if head == nil {
- head = &storepb.DatabaseConfig{}
+ if current == nil {
+ current = &storepb.DatabaseConfig{}
}
- targetMap, baselineMap, currentMap := buildSchemaMap(target), buildSchemaMap(baseline), buildSchemaMap(head)
+ targetMap, baselineMap, currentMap := buildSchemaMap(target), buildSchemaMap(baseline), buildSchemaMap(current)
for schemaName, targetSchema := range targetMap {
currentSchema, hasCurrent := currentMap[schemaName]
baselineSchema := baselineMap[schemaName]
@@ -38,11 +38,11 @@ func MergeDatabaseConfig(baseline, head, target *storepb.DatabaseConfig) *storep
// Already checked above.
continue
}
- // Remove from the current since the change is to delete the schema..
+ // Remove from the current since the change is to delete the schema.
delete(currentMap, schemaName)
}
- result := &storepb.DatabaseConfig{Name: head.Name}
+ result := &storepb.DatabaseConfig{Name: current.Name}
for _, v := range currentMap {
result.SchemaConfigs = append(result.SchemaConfigs, v)
}
@@ -136,7 +136,7 @@ func mergeSchemaConfig(target, baseline, current *storepb.SchemaConfig) *storepb
}
func mergeFunctionConfig(target, baseline, current *storepb.FunctionConfig) *storepb.FunctionConfig {
- lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, baseline.UpdateTime, current.Updater, current.UpdateTime)
+ lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, current.Updater, current.UpdateTime)
return &storepb.FunctionConfig{
Name: current.Name,
Updater: lastUpdater,
@@ -145,7 +145,7 @@ func mergeFunctionConfig(target, baseline, current *storepb.FunctionConfig) *sto
}
func mergeProcedureConfig(target, baseline, current *storepb.ProcedureConfig) *storepb.ProcedureConfig {
- lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, baseline.UpdateTime, current.Updater, current.UpdateTime)
+ lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, current.Updater, current.UpdateTime)
return &storepb.ProcedureConfig{
Name: current.Name,
Updater: lastUpdater,
@@ -154,7 +154,7 @@ func mergeProcedureConfig(target, baseline, current *storepb.ProcedureConfig) *s
}
func mergeViewConfig(target, baseline, current *storepb.ViewConfig) *storepb.ViewConfig {
- lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, baseline.UpdateTime, current.Updater, current.UpdateTime)
+ lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, current.Updater, current.UpdateTime)
return &storepb.ViewConfig{
Name: current.Name,
Updater: lastUpdater,
@@ -194,7 +194,7 @@ func mergeTableConfig(target, baseline, current *storepb.TableConfig) *storepb.T
}
result := &storepb.TableConfig{Name: current.Name, ClassificationId: current.ClassificationId}
- lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, baseline.UpdateTime, current.Updater, current.UpdateTime)
+ lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, current.Updater, current.UpdateTime)
result.Updater = lastUpdater
result.UpdateTime = lastUpdateTime
for _, v := range currentMap {
@@ -209,6 +209,9 @@ func mergeTableConfig(target, baseline, current *storepb.TableConfig) *storepb.T
func mergeColumnConfig(target, baseline, current *storepb.ColumnConfig) *storepb.ColumnConfig {
if baseline == nil {
// Baseline could be nil. When it's nil, we should set the current stale value to target value.
+ if target == nil {
+ return current
+ }
return target
}
// Current is never nil.
@@ -276,13 +279,9 @@ func buildColumnMap(config *storepb.TableConfig) map[string]*storepb.ColumnConfi
return m
}
-func getLastUpdater(updaterA string, updateTimeA *timestamppb.Timestamp, updaterB string, updateTimeB *timestamppb.Timestamp, updaterC string, updateTimeC *timestamppb.Timestamp) (string, *timestamppb.Timestamp) {
- lastUpdater, lastUpdateTime := updaterA, updateTimeA
- if lastUpdateTime == nil || (updateTimeB != nil && updateTimeB.AsTime().After(lastUpdateTime.AsTime())) {
- lastUpdater, lastUpdateTime = updaterB, updateTimeB
- }
- if lastUpdateTime == nil || (updateTimeC != nil && updateTimeC.AsTime().After(lastUpdateTime.AsTime())) {
- lastUpdater, lastUpdateTime = updaterC, updateTimeC
+func getLastUpdater(target string, targetTime *timestamppb.Timestamp, baseline string, current string, currentTime *timestamppb.Timestamp) (string, *timestamppb.Timestamp) {
+ if target == baseline {
+ return current, currentTime
}
- return lastUpdater, lastUpdateTime
+ return target, targetTime
}
diff --git a/backend/utils/database_config_merge_test.go b/backend/utils/database_config_merge_test.go
index 27391176d6..0a47f515f1 100644
--- a/backend/utils/database_config_merge_test.go
+++ b/backend/utils/database_config_merge_test.go
@@ -356,7 +356,7 @@ func TestUpdateDatabaseConfig(t *testing.T) {
a := require.New(t)
for _, tc := range testCases {
- got := MergeDatabaseConfig(tc.baseline, tc.head, tc.target)
+ got := MergeDatabaseConfig(tc.target, tc.baseline, tc.head)
equal := proto.Equal(got, tc.want)
a.True(equal, fmt.Sprintf("%s: \ngot:\t%%+v, \nexpected:\t%%+v", tc.description), got, tc.want)
}
diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vue
index 83b827453a..1853b81df7 100644
--- a/frontend/src/components/Branch/BranchDetailView.vue
+++ b/frontend/src/components/Branch/BranchDetailView.vue
@@ -236,7 +236,13 @@ const checkPermission = (permission: Permission): boolean => {
};
const allowEdit = computed(() => {
- return checkPermission("bb.branches.update");
+ if (!checkPermission("bb.branches.update")) {
+ return false;
+ }
+ if (props.dirtyBranch.parentBranch === "") {
+ return checkPermission("bb.branches.admin");
+ }
+ return true;
});
const allowDelete = computed(() => {
@@ -280,7 +286,7 @@ const showRebaseBranchButton = computed(() => {
props.project,
useCurrentUserV1().value,
"bb.branches.admin"
- )
+ );
}
// For feature branches: project owners and branch creator