Skip to content

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

feat: handle latest updater in create/update/merge (#12008)

h3n4lc5e6b2e2d2e6
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.go
index bbe56da9ed..17cc976a17 100644
--- a/backend/api/v1/branch_service.go
+++ b/backend/api/v1/branch_service.go
@@ -163,7 +163,6 @@ func (s *BranchService) CreateBranch(ctx context.Context, request *v1pb.CreateBr
return nil, status.Errorf(codes.NotFound, "parent branch %q not found", parentBranchID)
}
parentBranchHeadConfig := parentBranch.Head.GetDatabaseConfig()
- initializeBranchUpdaterInfoConfig(parentBranch.Head.Metadata, parentBranchHeadConfig, common.FormatUserEmail(user.Email))
created, err := s.store.CreateBranch(ctx, &store.BranchMessage{
ProjectID: project.ResourceID,
ResourceID: branchID,
@@ -222,6 +221,7 @@ func (s *BranchService) CreateBranch(ctx context.Context, request *v1pb.CreateBr
return nil, status.Errorf(codes.Internal, "failed to create branch: %v", err)
}
config := databaseSchema.GetConfig()
+ sanitizeCommentForSchemaMetadata(filteredBaseSchemaMetadata, model.NewDatabaseConfig(config))
initializeBranchUpdaterInfoConfig(filteredBaseSchemaMetadata, config, common.FormatUserEmail(user.Email))
created, err := s.store.CreateBranch(ctx, &store.BranchMessage{
ProjectID: project.ResourceID,
@@ -318,9 +318,9 @@ func (s *BranchService) UpdateBranch(ctx context.Context, request *v1pb.UpdateBr
reconcileMetadata(metadata, branch.Engine)
filteredMetadata := filterDatabaseMetadataByEngine(metadata, branch.Engine)
- updateConfigBranchUpdateInfo(branch.Head.Metadata, metadata, config, common.FormatUserEmail(user.Email))
- defaultSchema := extractDefaultSchemaForOracleBranch(storepb.Engine(branch.Engine), metadata)
- schema, err := schema.GetDesignSchema(branch.Engine, defaultSchema, "", metadata)
+ updateConfigBranchUpdateInfo(branch.Head.Metadata, filteredMetadata, config, common.FormatUserEmail(user.Email))
+ defaultSchema := extractDefaultSchemaForOracleBranch(storepb.Engine(branch.Engine), filteredMetadata)
+ schema, err := schema.GetDesignSchema(branch.Engine, defaultSchema, "", filteredMetadata)
if err != nil {
return nil, err
}
@@ -458,7 +458,7 @@ func (s *BranchService) MergeBranch(ctx context.Context, request *v1pb.MergeBran
filteredMergedMetadata := filterDatabaseMetadataByEngine(mergedMetadata, baseBranch.Engine)
reconcileMetadata(filteredMergedMetadata, baseBranch.Engine)
- updateConfigBranchUpdateInfo(baseBranch.Head.Metadata, mergedMetadata, mergedConfig, common.FormatUserEmail(user.Email))
+ updateConfigBranchUpdateInfo(baseBranch.Base.Metadata, filteredMergedMetadata, mergedConfig, common.FormatUserEmail(user.Email))
baseBranchNewHead := &storepb.BranchSnapshot{
Metadata: filteredMergedMetadata,
DatabaseConfig: mergedConfig,
diff --git a/backend/utils/database_config_merge.go b/backend/utils/database_config_merge.go
index 335a0e2bd2..b789ab1941 100644
--- a/backend/utils/database_config_merge.go
+++ b/backend/utils/database_config_merge.go
@@ -4,6 +4,8 @@ import (
"reflect"
"sort"
+ "google.golang.org/protobuf/types/known/timestamppb"
+
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -81,16 +83,85 @@ func mergeSchemaConfig(target, baseline, current *storepb.SchemaConfig) *storepb
delete(currentMap, tableName)
}
+ targetProcedureMap, baselineProcedureMap, currentProcedureMap := buildProcedureMap(target), buildProcedureMap(baseline), buildProcedureMap(current)
+ for procedureName, targetProcedure := range targetProcedureMap {
+ currentProcedure, hasCurrent := currentProcedureMap[procedureName]
+ baselineProcedure := baselineProcedureMap[procedureName]
+ if hasCurrent {
+ currentProcedureMap[procedureName] = mergeProcedureConfig(targetProcedure, baselineProcedure, currentProcedure)
+ } else {
+ currentProcedureMap[procedureName] = targetProcedure
+ }
+ }
+
+ targetFunctionMap, baselineFunctionMap, currentFunctionMap := buildFunctionMap(target), buildFunctionMap(baseline), buildFunctionMap(current)
+ for functionName, targetFunction := range targetFunctionMap {
+ currentFunction, hasCurrent := currentFunctionMap[functionName]
+ baselineFunction := baselineFunctionMap[functionName]
+ if hasCurrent {
+ currentFunctionMap[functionName] = mergeFunctionConfig(targetFunction, baselineFunction, currentFunction)
+ } else {
+ currentFunctionMap[functionName] = targetFunction
+ }
+ }
+
+ targetViewMap, baselineViewMap, currentViewMap := buildViewMap(target), buildViewMap(baseline), buildViewMap(current)
+ for viewName, targetView := range targetViewMap {
+ currentView, hasCurrent := currentViewMap[viewName]
+ baselineView := baselineViewMap[viewName]
+ if hasCurrent {
+ currentViewMap[viewName] = mergeViewConfig(targetView, baselineView, currentView)
+ } else {
+ currentViewMap[viewName] = targetView
+ }
+ }
+
result := &storepb.SchemaConfig{Name: current.Name}
for _, v := range currentMap {
result.TableConfigs = append(result.TableConfigs, v)
}
+ for _, v := range currentProcedureMap {
+ result.ProcedureConfigs = append(result.ProcedureConfigs, v)
+ }
+ for _, v := range currentFunctionMap {
+ result.FunctionConfigs = append(result.FunctionConfigs, v)
+ }
+ for _, v := range currentViewMap {
+ result.ViewConfigs = append(result.ViewConfigs, v)
+ }
sort.Slice(result.TableConfigs, func(i, j int) bool {
return result.TableConfigs[i].Name < result.TableConfigs[j].Name
})
return result
}
+func mergeFunctionConfig(target, baseline, current *storepb.FunctionConfig) *storepb.FunctionConfig {
+ lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, baseline.UpdateTime, current.Updater, current.UpdateTime)
+ return &storepb.FunctionConfig{
+ Name: current.Name,
+ Updater: lastUpdater,
+ UpdateTime: lastUpdateTime,
+ }
+}
+
+func mergeProcedureConfig(target, baseline, current *storepb.ProcedureConfig) *storepb.ProcedureConfig {
+ lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, baseline.UpdateTime, current.Updater, current.UpdateTime)
+ return &storepb.ProcedureConfig{
+ Name: current.Name,
+ Updater: lastUpdater,
+ UpdateTime: lastUpdateTime,
+ }
+}
+
+func mergeViewConfig(target, baseline, current *storepb.ViewConfig) *storepb.ViewConfig {
+ lastUpdater, lastUpdateTime := getLastUpdater(target.Updater, target.UpdateTime, baseline.Updater, baseline.UpdateTime, current.Updater, current.UpdateTime)
+ return &storepb.ViewConfig{
+ Name: current.Name,
+ Updater: lastUpdater,
+ UpdateTime: lastUpdateTime,
+ }
+}
+
func mergeTableConfig(target, baseline, current *storepb.TableConfig) *storepb.TableConfig {
// Avoid nil values.
if target == nil {
@@ -123,6 +194,9 @@ 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)
+ result.Updater = lastUpdater
+ result.UpdateTime = lastUpdateTime
for _, v := range currentMap {
result.ColumnConfigs = append(result.ColumnConfigs, v)
}
@@ -170,6 +244,30 @@ func buildTableMap(config *storepb.SchemaConfig) map[string]*storepb.TableConfig
return m
}
+func buildProcedureMap(config *storepb.SchemaConfig) map[string]*storepb.ProcedureConfig {
+ m := make(map[string]*storepb.ProcedureConfig)
+ for _, v := range config.ProcedureConfigs {
+ m[v.Name] = v
+ }
+ return m
+}
+
+func buildFunctionMap(config *storepb.SchemaConfig) map[string]*storepb.FunctionConfig {
+ m := make(map[string]*storepb.FunctionConfig)
+ for _, v := range config.FunctionConfigs {
+ m[v.Name] = v
+ }
+ return m
+}
+
+func buildViewMap(config *storepb.SchemaConfig) map[string]*storepb.ViewConfig {
+ m := make(map[string]*storepb.ViewConfig)
+ for _, v := range config.ViewConfigs {
+ m[v.Name] = v
+ }
+ return m
+}
+
func buildColumnMap(config *storepb.TableConfig) map[string]*storepb.ColumnConfig {
m := make(map[string]*storepb.ColumnConfig)
for _, v := range config.ColumnConfigs {
@@ -177,3 +275,14 @@ 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
+ }
+ return lastUpdater, lastUpdateTime
+}
diff --git a/frontend/src/types/proto/v1/instance_service.ts b/frontend/src/types/proto/v1/instance_service.ts
index 5dc0718480..7a3df4ea74 100644
--- a/frontend/src/types/proto/v1/instance_service.ts
+++ b/frontend/src/types/proto/v1/instance_service.ts
@@ -508,7 +508,7 @@ export interface DataSource {
host: string;
port: string;
database: string;
- /** srv and authentication_database are used for MongoDB. */
+ /** srv, authentication_database and replica_set are used for MongoDB. */
srv: boolean;
authenticationDatabase: string;
/** sid and service_name are used for Oracle. */
diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yaml
index 79f94386e2..02d8d02e4a 100644
--- a/proto/gen/grpc-doc/openapi.yaml
+++ b/proto/gen/grpc-doc/openapi.yaml
@@ -9232,7 +9232,7 @@ components:
type: string
srv:
type: boolean
- description: srv and authentication_database are used for MongoDB.
+ description: srv, authentication_database and replica_set are used for MongoDB.
authenticationDatabase:
type: string
sid:
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index f40759d40c..946c3f1454 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -1614,7 +1614,7 @@ This value should be 4-63 characters, and valid characters are /[a-z][0-9]-/. |
| host | [string](#string) | | |
| port | [string](#string) | | |
| database | [string](#string) | | |
-| srv | [bool](#bool) | | srv and authentication_database are used for MongoDB. |
+| srv | [bool](#bool) | | srv, authentication_database and replica_set are used for MongoDB. |
| authentication_database | [string](#string) | | |
| sid | [string](#string) | | sid and service_name are used for Oracle. |
| service_name | [string](#string) | | |
diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.html
index 19adfe0c07..83f292caa9 100644
--- a/proto/gen/grpc-doc/v1/index.html
+++ b/proto/gen/grpc-doc/v1/index.html
@@ -5199,7 +5199,7 @@ are /[a-z][0-9]-/. </p></td>
<td>srv</td>
<td><a href="#bool">bool</a></td>
<td></td>
- <td><p>srv and authentication_database are used for MongoDB. </p></td>
+ <td><p>srv, authentication_database and replica_set are used for MongoDB. </p></td>
</tr>
<tr>
diff --git a/proto/generated-go/v1/instance_service.pb.go b/proto/generated-go/v1/instance_service.pb.go
index bb25eeb5e8..c7a4bdfde3 100644
--- a/proto/generated-go/v1/instance_service.pb.go
+++ b/proto/generated-go/v1/instance_service.pb.go
@@ -1581,7 +1581,7 @@ type DataSource struct {
Host string `protobuf:"bytes,8,opt,name=host,proto3" json:"host,omitempty"`
Port string `protobuf:"bytes,9,opt,name=port,proto3" json:"port,omitempty"`
Database string `protobuf:"bytes,10,opt,name=database,proto3" json:"database,omitempty"`
- // srv and authentication_database are used for MongoDB.
+ // srv, authentication_database and replica_set are used for MongoDB.
Srv bool `protobuf:"varint,11,opt,name=srv,proto3" json:"srv,omitempty"`
AuthenticationDatabase string `protobuf:"bytes,12,opt,name=authentication_database,json=authenticationDatabase,proto3" json:"authentication_database,omitempty"`
// sid and service_name are used for Oracle.
diff --git a/proto/v1/instance_service.proto b/proto/v1/instance_service.proto
index 5e67377274..a412f5b4a9 100644
--- a/proto/v1/instance_service.proto
+++ b/proto/v1/instance_service.proto
@@ -369,7 +369,7 @@ message DataSource {
string host = 8;
string port = 9;
string database = 10;
- // srv and authentication_database are used for MongoDB.
+ // srv, authentication_database and replica_set are used for MongoDB.
bool srv = 11;
string authentication_database = 12;
// sid and service_name are used for Oracle.