Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: refine last updater (#13669)
h3n4l4c580d659980
diff --git a/backend/api/v1/branch_merge.go b/backend/api/v1/branch_merge.goindex 32b1ff1a19..359a53d26f 100644--- a/backend/api/v1/branch_merge.go+++ b/backend/api/v1/branch_merge.go@@ -12,6 +12,7 @@ import ("github.com/pkg/errors""google.golang.org/protobuf/proto""google.golang.org/protobuf/testing/protocmp"+ "google.golang.org/protobuf/types/known/timestamppb""github.com/bytebase/bytebase/backend/plugin/db/mysql""github.com/bytebase/bytebase/backend/plugin/db/tidb"@@ -152,7 +153,7 @@ const ()// tryMerge merges other metadata to current metadata, always returns a non-nil metadata if no error occurs.-func tryMerge(ancestor, head, base *storepb.DatabaseSchemaMetadata, engine storepb.Engine) (*storepb.DatabaseSchemaMetadata, error) {+func tryMerge(ancestor, head, base *storepb.DatabaseSchemaMetadata, ancestorConfig *storepb.DatabaseConfig, headConfig, baseConfig *storepb.DatabaseConfig, engine storepb.Engine) (*storepb.DatabaseSchemaMetadata, *storepb.DatabaseConfig, error) {ancestor, head, base = proto.Clone(ancestor).(*storepb.DatabaseSchemaMetadata), proto.Clone(head).(*storepb.DatabaseSchemaMetadata), proto.Clone(base).(*storepb.DatabaseSchemaMetadata)if ancestor == nil {@@ -161,26 +162,33 @@ func tryMerge(ancestor, head, base *storepb.DatabaseSchemaMetadata, engine storediffBetweenAncestorAndHead, err := diffMetadata(ancestor, head)if err != nil {- return nil, errors.Wrap(err, "failed to diff between ancestor and head")+ return nil, nil, errors.Wrap(err, "failed to diff between ancestor and head")}+ configDiffBetweenAncestorAndHead := deriveUpdateInfoFromMetadataDiff(diffBetweenAncestorAndHead, headConfig)diffBetweenAncestorAndBase, err := diffMetadata(ancestor, base)if err != nil {- return nil, errors.Wrap(err, "failed to diff between ancestor and base")+ return nil, nil, errors.Wrap(err, "failed to diff between ancestor and base")}+ configDiffBetweenAncestorAndBase := deriveUpdateInfoFromMetadataDiff(diffBetweenAncestorAndBase, baseConfig)if conflict, msg := diffBetweenAncestorAndBase.tryMerge(diffBetweenAncestorAndHead, engine); conflict {- return nil, errors.Errorf("merge conflict: %s", msg)+ return nil, nil, errors.Errorf("merge conflict: %s", msg)+ }+ mergedUpdateInfoDiff, err := mergeUpdateInfoDiffRooNode(configDiffBetweenAncestorAndBase, configDiffBetweenAncestorAndHead)+ if err != nil {+ return nil, nil, errors.Wrap(err, "failed to merge update info diff")}if err := diffBetweenAncestorAndBase.applyDiffTo(ancestor); err != nil {- return nil, errors.Wrap(err, "failed to apply diff to target")+ return nil, nil, errors.Wrap(err, "failed to apply diff to target")}- return ancestor, nil+ config := applyUpdateInfoDiffRootNode(mergedUpdateInfoDiff, ancestorConfig)+ return ancestor, config, nil}-type metadataDiffBaseNode struct {+type diffBaseNode struct {action diffAction}@@ -225,7 +233,7 @@ func (mr *metadataDiffRootNode) applyDiffTo(target *storepb.DatabaseSchemaMetada// Schema related.type metadataDiffSchemaNode struct {- metadataDiffBaseNode+ diffBaseNodename string//nolintbase *storepb.SchemaMetadata@@ -434,7 +442,7 @@ func (n *metadataDiffSchemaNode) applyDiffTo(target *storepb.DatabaseSchemaMetad// Table related.type metadataDiffTableNode struct {- metadataDiffBaseNode+ diffBaseNodename string//nolintbase *storepb.TableMetadata@@ -701,7 +709,7 @@ func (n *metadataDiffTableNode) applyDiffTo(target *storepb.SchemaMetadata) erro// Column related.type metadataDiffColumnNode struct {- metadataDiffBaseNode+ diffBaseNodename string//nolintbase *storepb.ColumnMetadata@@ -871,7 +879,7 @@ func (n *metadataDiffColumnNode) applyDiffTo(target *storepb.TableMetadata) erro// Index related.type metadataDiffIndexNode struct {- metadataDiffBaseNode+ diffBaseNodename string//nolintbase *storepb.IndexMetadata@@ -975,7 +983,7 @@ func (n *metadataDiffIndexNode) applyDiffTo(target *storepb.TableMetadata) error}type metadataDiffPartitionNode struct {- metadataDiffBaseNode+ diffBaseNodename string// nolintbase *storepb.TablePartitionMetadata@@ -1098,7 +1106,7 @@ func (n *metadataDiffPartitionNode) applyDiffTo(target *storepb.TableMetadata) e}type metadataDiffFunctioNnode struct {- metadataDiffBaseNode+ diffBaseNodename string//nolintbase *storepb.FunctionMetadata@@ -1178,7 +1186,7 @@ func (n *metadataDiffFunctioNnode) applyDiffTo(target *storepb.SchemaMetadata) e}type metadataDiffProcedureNode struct {- metadataDiffBaseNode+ diffBaseNodename string//nolintbase *storepb.ProcedureMetadata@@ -1258,7 +1266,7 @@ func (n *metadataDiffProcedureNode) applyDiffTo(target *storepb.SchemaMetadata)}type metadataDiffViewNode struct {- metadataDiffBaseNode+ diffBaseNodename string//nolintbase *storepb.ViewMetadata@@ -1391,7 +1399,7 @@ func (n *metadataDiffViewNode) applyDiffTo(target *storepb.SchemaMetadata) error// Foreign Key related.type metadataDiffForeignKeyNode struct {- metadataDiffBaseNode+ diffBaseNodename string//nolintbase *storepb.ForeignKeyMetadata@@ -1567,7 +1575,7 @@ func diffSchemaMetadata(base, head *storepb.SchemaMetadata) (*metadataDiffSchema}schemaNode := &metadataDiffSchemaNode{- metadataDiffBaseNode: metadataDiffBaseNode{+ diffBaseNode: diffBaseNode{action: action,},name: name,@@ -1719,7 +1727,7 @@ func diffViewMetadata(base, head *storepb.ViewMetadata) (*metadataDiffViewNode,}viewNode := &metadataDiffViewNode{- metadataDiffBaseNode: metadataDiffBaseNode{+ diffBaseNode: diffBaseNode{action: action,},name: name,@@ -1754,7 +1762,7 @@ func diffProcedureMetadata(base, head *storepb.ProcedureMetadata) (*metadataDiff}procedureNode := &metadataDiffProcedureNode{- metadataDiffBaseNode: metadataDiffBaseNode{+ diffBaseNode: diffBaseNode{action: action,},name: name,@@ -1789,7 +1797,7 @@ func diffFunctionMetadata(base, head *storepb.FunctionMetadata) (*metadataDiffFu}functionNode := &metadataDiffFunctioNnode{- metadataDiffBaseNode: metadataDiffBaseNode{+ diffBaseNode: diffBaseNode{action: action,},name: name,@@ -1824,7 +1832,7 @@ func diffTableMetadata(base, head *storepb.TableMetadata) (*metadataDiffTableNod}tableNode := &metadataDiffTableNode{- metadataDiffBaseNode: metadataDiffBaseNode{+ diffBaseNode: diffBaseNode{action: action,},name: name,@@ -2022,7 +2030,7 @@ func diffColumnMetadata(base, head *storepb.ColumnMetadata) (*metadataDiffColumn}columnNode := &metadataDiffColumnNode{- metadataDiffBaseNode: metadataDiffBaseNode{+ diffBaseNode: diffBaseNode{action: action,},name: name,@@ -2057,7 +2065,7 @@ func diffForeignKeyMetadata(base, head *storepb.ForeignKeyMetadata) (*metadataDi}fkNode := &metadataDiffForeignKeyNode{- metadataDiffBaseNode: metadataDiffBaseNode{+ diffBaseNode: diffBaseNode{action: action,},name: name,@@ -2092,7 +2100,7 @@ func diffIndexMetadata(base, head *storepb.IndexMetadata) (*metadataDiffIndexNod}indexNode := &metadataDiffIndexNode{- metadataDiffBaseNode: metadataDiffBaseNode{+ diffBaseNode: diffBaseNode{action: action,},name: name,@@ -2127,7 +2135,7 @@ func diffPartitionMetadata(base, head *storepb.TablePartitionMetadata) (*metadat}partitionNode := &metadataDiffPartitionNode{- metadataDiffBaseNode: metadataDiffBaseNode{+ diffBaseNode: diffBaseNode{action: action,},name: name,@@ -2201,3 +2209,621 @@ func isColumnTypeEqual(a, b string, engine storepb.Engine) bool {return strings.EqualFold(a, b)}}++type updateInfo struct {+ lastUpdatedTime *timestamppb.Timestamp+ lastUpdater string+ sourceBranch string+}++func (u *updateInfo) clone() *updateInfo {+ if u == nil {+ return nil+ }+ return &updateInfo{+ //nolint+ lastUpdatedTime: proto.Clone(u.lastUpdatedTime).(*timestamppb.Timestamp),+ lastUpdater: u.lastUpdater,+ sourceBranch: u.sourceBranch,+ }+}++type updateInfoDiffRootNode struct {+ schemas map[string]*updateInfoDiffSchemaNode+}++type updateInfoDiffSchemaNode struct {+ diffBaseNode+ name string+ tables map[string]*updateInfoDiffTableNode+ views map[string]*updateInfoDiffViewNode+ functions map[string]*updateInfoDiffFunctionNode+ procedures map[string]*updateInfoDiffProcedureNode+}++type updateInfoDiffTableNode struct {+ diffBaseNode+ name string+ // If the action is diffActionDelete, the updateInfo should be nil.+ updateInfo *updateInfo+}++type updateInfoDiffViewNode struct {+ diffBaseNode+ name string+ // If the action is diffActionDelete, the updateInfo should be nil.+ updateInfo *updateInfo+}++type updateInfoDiffFunctionNode struct {+ diffBaseNode+ name string+ // If the action is diffActionDelete, the updateInfo should be nil.+ updateInfo *updateInfo+}++type updateInfoDiffProcedureNode struct {+ diffBaseNode+ name string+ // If the action is diffActionDelete, the updateInfo should be nil.+ updateInfo *updateInfo+}++// deriveUpdateInfoFromMetadataDiff derives the update info diff from the metadata diff.+func deriveUpdateInfoFromMetadataDiff(metadataDiff *metadataDiffRootNode, headDatabaseConfig *storepb.DatabaseConfig) *updateInfoDiffRootNode {+ if metadataDiff == nil {+ return nil+ }+ rootNode := &updateInfoDiffRootNode{+ schemas: make(map[string]*updateInfoDiffSchemaNode),+ }++ schemaConfigMap := buildMap(headDatabaseConfig.GetSchemaConfigs(), func(schemaConfig *storepb.SchemaConfig) string {+ return schemaConfig.GetName()+ })+ for _, metadataSchemaDiff := range metadataDiff.schemas {+ action := metadataSchemaDiff.action+ updateInfoSchemaDiff := &updateInfoDiffSchemaNode{+ diffBaseNode: diffBaseNode{+ action: action,+ },+ name: metadataSchemaDiff.name,+ tables: make(map[string]*updateInfoDiffTableNode),+ views: make(map[string]*updateInfoDiffViewNode),+ functions: make(map[string]*updateInfoDiffFunctionNode),+ procedures: make(map[string]*updateInfoDiffProcedureNode),+ }+ schemaConfig := schemaConfigMap[metadataSchemaDiff.name]+ tableConfigMap := buildMap(schemaConfig.GetTableConfigs(), func(tableConfig *storepb.TableConfig) string {+ return tableConfig.GetName()+ })+ for tableName, table := range metadataSchemaDiff.tables {+ action := table.action+ updateInfoSchemaDiff.tables[tableName] = &updateInfoDiffTableNode{+ diffBaseNode: diffBaseNode{+ action: action,+ },+ name: tableName,+ }+ if action != diffActionDrop {+ tableConfig := tableConfigMap[tableName]+ updateInfoSchemaDiff.tables[tableName].updateInfo = &updateInfo{+ lastUpdatedTime: tableConfig.GetUpdateTime(),+ lastUpdater: tableConfig.GetUpdater(),+ sourceBranch: tableConfig.GetSourceBranch(),+ }+ }+ }++ // Views+ viewConfigMap := buildMap(schemaConfig.GetViewConfigs(), func(viewConfig *storepb.ViewConfig) string {+ return viewConfig.GetName()+ })+ for viewName, view := range metadataSchemaDiff.views {+ action := view.action+ updateInfoSchemaDiff.views[viewName] = &updateInfoDiffViewNode{+ diffBaseNode: diffBaseNode{+ action: action,+ },+ name: viewName,+ }+ if action != diffActionDrop {+ viewConfig := viewConfigMap[viewName]+ updateInfoSchemaDiff.views[viewName].updateInfo = &updateInfo{+ lastUpdatedTime: viewConfig.GetUpdateTime(),+ lastUpdater: viewConfig.GetUpdater(),+ sourceBranch: viewConfig.GetSourceBranch(),+ }+ }+ }++ // Functions+ functionConfigMap := buildMap(schemaConfig.GetFunctionConfigs(), func(functionConfig *storepb.FunctionConfig) string {+ return functionConfig.GetName()+ })+ for functionName, function := range metadataSchemaDiff.functions {+ action := function.action+ updateInfoSchemaDiff.functions[functionName] = &updateInfoDiffFunctionNode{+ diffBaseNode: diffBaseNode{+ action: action,+ },+ name: functionName,+ }+ if action != diffActionDrop {+ functionConfig := functionConfigMap[functionName]+ updateInfoSchemaDiff.functions[functionName].updateInfo = &updateInfo{+ lastUpdatedTime: functionConfig.GetUpdateTime(),+ lastUpdater: functionConfig.GetUpdater(),+ sourceBranch: functionConfig.GetSourceBranch(),+ }+ }+ }++ // Procedures+ procedureConfigMap := buildMap(schemaConfig.GetProcedureConfigs(), func(procedureConfig *storepb.ProcedureConfig) string {+ return procedureConfig.GetName()+ })+ for procedureName, procedure := range metadataSchemaDiff.procedures {+ action := procedure.action+ updateInfoSchemaDiff.procedures[procedureName] = &updateInfoDiffProcedureNode{+ diffBaseNode: diffBaseNode{+ action: action,+ },+ name: procedureName,+ }+ if action != diffActionDrop {+ procedureConfig := procedureConfigMap[procedureName]+ updateInfoSchemaDiff.procedures[procedureName].updateInfo = &updateInfo{+ lastUpdatedTime: procedureConfig.GetUpdateTime(),+ lastUpdater: procedureConfig.GetUpdater(),+ sourceBranch: procedureConfig.GetSourceBranch(),+ }+ }+ }+ rootNode.schemas[metadataSchemaDiff.name] = updateInfoSchemaDiff+ }++ return rootNode+}++func mergeUpdateInfoDiffRooNode(a, b *updateInfoDiffRootNode) (*updateInfoDiffRootNode, error) {+ if a == nil && b == nil {+ return nil, nil+ }+ if a == nil {+ return b, nil+ }+ if b == nil {+ return a, nil+ }+ rootNode := &updateInfoDiffRootNode{+ schemas: make(map[string]*updateInfoDiffSchemaNode),+ }++ schemaNamesMap := make(map[string]bool)+ for schemaName := range a.schemas {+ schemaNamesMap[schemaName] = true+ }+ for schemaName := range b.schemas {+ schemaNamesMap[schemaName] = true+ }++ for schemaName := range schemaNamesMap {+ aSchema, aOk := a.schemas[schemaName]+ bSchema, bOk := b.schemas[schemaName]+ if aOk && bOk {+ mergedSchema, err := mergeUpdateInfoDiffSchemaNode(aSchema, bSchema)+ if err != nil {+ return nil, err+ }+ rootNode.schemas[schemaName] = mergedSchema+ continue+ }+ if aOk {+ rootNode.schemas[schemaName] = aSchema+ continue+ }+ if bOk {+ rootNode.schemas[schemaName] = bSchema+ continue+ }+ }++ return rootNode, nil+}++func mergeUpdateInfoDiffSchemaNode(a, b *updateInfoDiffSchemaNode) (*updateInfoDiffSchemaNode, error) {+ if a == nil && b == nil {+ return nil, nil+ }+ if a == nil {+ return b, nil+ }+ if b == nil {+ return a, nil+ }++ if a.action != b.action {+ return nil, errors.Errorf("conflict schema config action, one is %s, the other is %s", a.action, b.action)+ }+ schemaNode := &updateInfoDiffSchemaNode{+ diffBaseNode: diffBaseNode{+ action: b.action,+ },+ name: b.name,+ tables: make(map[string]*updateInfoDiffTableNode),+ views: make(map[string]*updateInfoDiffViewNode),+ functions: make(map[string]*updateInfoDiffFunctionNode),+ procedures: make(map[string]*updateInfoDiffProcedureNode),+ }++ tableNameMap := make(map[string]bool)+ for tableName := range a.tables {+ tableNameMap[tableName] = true+ }+ for tableName := range b.tables {+ tableNameMap[tableName] = true+ }+ for tableName := range tableNameMap {+ aTable, aOk := a.tables[tableName]+ bTable, bOk := b.tables[tableName]+ if aOk && bOk {+ mergedTable, err := mergeUpdateInfoDiffTableNode(aTable, bTable)+ if err != nil {+ return nil, err+ }+ schemaNode.tables[tableName] = mergedTable+ continue+ }+ if aOk {+ schemaNode.tables[tableName] = aTable+ continue+ }+ if bOk {+ schemaNode.tables[tableName] = bTable+ continue+ }+ }++ viewNameMap := make(map[string]bool)+ for viewName := range a.views {+ viewNameMap[viewName] = true+ }+ for viewName := range b.views {+ viewNameMap[viewName] = true+ }+ for viewName := range viewNameMap {+ aView, aOk := a.views[viewName]+ bView, bOk := b.views[viewName]+ if aOk && bOk {+ mergedView, err := mergeUpdateInfoDiffViewNode(aView, bView)+ if err != nil {+ return nil, err+ }+ schemaNode.views[viewName] = mergedView+ continue+ }+ if aOk {+ schemaNode.views[viewName] = aView+ continue+ }+ if bOk {+ schemaNode.views[viewName] = bView+ continue+ }+ }++ procedureNameMap := make(map[string]bool)+ for procedureName := range a.procedures {+ procedureNameMap[procedureName] = true+ }+ for procedureName := range b.procedures {+ procedureNameMap[procedureName] = true+ }+ for procedureName := range procedureNameMap {+ aProcedure, aOk := a.procedures[procedureName]+ bProcedure, bOk := b.procedures[procedureName]+ if aOk && bOk {+ mergedProcedure, err := mergeUpdateInfoDiffProcedureNode(aProcedure, bProcedure)+ if err != nil {+ return nil, err+ }+ schemaNode.procedures[procedureName] = mergedProcedure+ continue+ }+ if aOk {+ schemaNode.procedures[procedureName] = aProcedure+ continue+ }+ if bOk {+ schemaNode.procedures[procedureName] = bProcedure+ continue+ }+ }++ functionNameMap := make(map[string]bool)+ for functionName := range a.functions {+ functionNameMap[functionName] = true+ }+ for functionName := range b.functions {+ functionNameMap[functionName] = true+ }+ for functionName := range functionNameMap {+ aFunction, aOk := a.functions[functionName]+ bFunction, bOk := b.functions[functionName]+ if aOk && bOk {+ mergedFunction, err := mergeUpdateInfoDiffFunctionNode(aFunction, bFunction)+ if err != nil {+ return nil, err+ }+ schemaNode.functions[functionName] = mergedFunction+ continue+ }+ if aOk {+ schemaNode.functions[functionName] = aFunction+ continue+ }+ if bOk {+ schemaNode.functions[functionName] = bFunction+ continue+ }+ }++ return schemaNode, nil+}++func mergeUpdateInfoDiffTableNode(a, b *updateInfoDiffTableNode) (*updateInfoDiffTableNode, error) {+ if a == nil && b == nil {+ return nil, nil+ }+ if a == nil {+ return b, nil+ }+ if b == nil {+ return a, nil+ }++ if a.action != b.action {+ return nil, errors.Errorf("conflict table config action, one is %s, the other is %s", a.action, b.action)+ }++ return &updateInfoDiffTableNode{+ diffBaseNode: diffBaseNode{+ action: b.action,+ },+ name: b.name,+ updateInfo: b.updateInfo.clone(),+ }, nil+}++func mergeUpdateInfoDiffViewNode(a, b *updateInfoDiffViewNode) (*updateInfoDiffViewNode, error) {+ if a == nil && b == nil {+ return nil, nil+ }+ if a == nil {+ return b, nil+ }+ if b == nil {+ return a, nil+ }++ if a.action != b.action {+ return nil, errors.Errorf("conflict view config action, one is %s, the other is %s", a.action, b.action)+ }++ return &updateInfoDiffViewNode{+ diffBaseNode: diffBaseNode{+ action: b.action,+ },+ name: b.name,+ updateInfo: b.updateInfo.clone(),+ }, nil+}++func mergeUpdateInfoDiffProcedureNode(a, b *updateInfoDiffProcedureNode) (*updateInfoDiffProcedureNode, error) {+ if a == nil && b == nil {+ return nil, nil+ }+ if a == nil {+ return b, nil+ }+ if b == nil {+ return a, nil+ }++ if a.action != b.action {+ return nil, errors.Errorf("conflict procedure config action, one is %s, the other is %s", a.action, b.action)+ }++ return &updateInfoDiffProcedureNode{+ diffBaseNode: diffBaseNode{+ action: b.action,+ },+ name: b.name,+ updateInfo: b.updateInfo.clone(),+ }, nil+}++func mergeUpdateInfoDiffFunctionNode(a, b *updateInfoDiffFunctionNode) (*updateInfoDiffFunctionNode, error) {+ if a == nil && b == nil {+ return nil, nil+ }+ if a == nil {+ return b, nil+ }+ if b == nil {+ return a, nil+ }++ if a.action != b.action {+ return nil, errors.Errorf("conflict function config action, one is %s, the other is %s", a.action, b.action)+ }++ return &updateInfoDiffFunctionNode{+ diffBaseNode: diffBaseNode{+ action: b.action,+ },+ name: b.name,+ updateInfo: b.updateInfo.clone(),+ }, nil+}++func applyUpdateInfoDiffRootNode(a *updateInfoDiffRootNode, target *storepb.DatabaseConfig) *storepb.DatabaseConfig {+ if a == nil {+ //nolint+ return proto.Clone(target).(*storepb.DatabaseConfig)+ }++ schemaConfigMap := buildMap(target.GetSchemaConfigs(), func(schemaConfig *storepb.SchemaConfig) string {+ return schemaConfig.GetName()+ })++ var schemaConfigs []*storepb.SchemaConfig+ for schemaName, schema := range a.schemas {+ switch schema.action {+ case diffActionDrop:+ continue+ default:+ schemaConfig := schemaConfigMap[schemaName]+ if schemaConfig == nil {+ schemaConfig = &storepb.SchemaConfig{+ Name: schema.name,+ }+ }+ appliedSchema := applyUpdateInfoDiffSchemaNode(schema, schemaConfig)+ schemaConfigs = append(schemaConfigs, appliedSchema)+ }+ }++ return &storepb.DatabaseConfig{+ Name: target.GetName(),+ SchemaConfigs: schemaConfigs,+ }+}++func applyUpdateInfoDiffSchemaNode(a *updateInfoDiffSchemaNode, target *storepb.SchemaConfig) *storepb.SchemaConfig {+ if a == nil {+ //nolint+ return proto.Clone(target).(*storepb.SchemaConfig)+ }++ tableConfigMap := buildMap(target.GetTableConfigs(), func(tableConfig *storepb.TableConfig) string {+ return tableConfig.GetName()+ })+ var tableConfigs []*storepb.TableConfig+ for tableName, table := range a.tables {+ switch table.action {+ case diffActionDrop:+ delete(tableConfigMap, tableName)+ continue+ default:+ tableConfig := tableConfigMap[tableName]+ if tableConfig == nil {+ tableConfig = &storepb.TableConfig{+ Name: tableName,+ }+ }+ tableConfig.UpdateTime = table.updateInfo.lastUpdatedTime+ tableConfig.Updater = table.updateInfo.lastUpdater+ tableConfig.SourceBranch = table.updateInfo.sourceBranch+ tableConfigs = append(tableConfigs, tableConfig)+ delete(tableConfigMap, tableName)+ }+ }+ // Add the remaining table configs in the target schema config.+ for _, tableConfig := range tableConfigMap {+ tableConfigs = append(tableConfigs, tableConfig)+ }++ viewConfigMap := buildMap(target.GetViewConfigs(), func(viewConfig *storepb.ViewConfig) string {+ return viewConfig.GetName()+ })+ var viewConfigs []*storepb.ViewConfig+ for viewName, view := range a.views {+ switch view.action {+ case diffActionDrop:+ delete(viewConfigMap, viewName)+ continue+ default:+ viewConfig := viewConfigMap[viewName]+ if viewConfig == nil {+ viewConfig = &storepb.ViewConfig{+ Name: viewName,+ }+ }+ viewConfig.UpdateTime = view.updateInfo.lastUpdatedTime+ viewConfig.Updater = view.updateInfo.lastUpdater+ viewConfig.SourceBranch = view.updateInfo.sourceBranch+ viewConfigs = append(viewConfigs, viewConfig)+ delete(viewConfigMap, viewName)+ }+ }+ // Add the remaining view configs in the target schema config.+ for _, viewConfig := range viewConfigMap {+ viewConfigs = append(viewConfigs, viewConfig)+ }++ procedureConfigMap := buildMap(target.GetProcedureConfigs(), func(procedureConfig *storepb.ProcedureConfig) string {+ return procedureConfig.GetName()+ })+ var procedureConfigs []*storepb.ProcedureConfig+ for procedureName, procedure := range a.procedures {+ switch procedure.action {+ case diffActionDrop:+ delete(procedureConfigMap, procedureName)+ continue+ default:+ procedureConfig := procedureConfigMap[procedureName]+ if procedureConfig == nil {+ procedureConfig = &storepb.ProcedureConfig{+ Name: procedureName,+ }+ }+ procedureConfig.UpdateTime = procedure.updateInfo.lastUpdatedTime+ procedureConfig.Updater = procedure.updateInfo.lastUpdater+ procedureConfig.SourceBranch = procedure.updateInfo.sourceBranch+ procedureConfigs = append(procedureConfigs, procedureConfig)+ delete(procedureConfigMap, procedureName)+ }+ }+ // Add the remaining procedure configs in the target schema config.+ for _, procedureConfig := range procedureConfigMap {+ procedureConfigs = append(procedureConfigs, procedureConfig)+ }++ functionConfigMap := buildMap(target.GetFunctionConfigs(), func(functionConfig *storepb.FunctionConfig) string {+ return functionConfig.GetName()+ })+ var functionConfigs []*storepb.FunctionConfig+ for functionName, function := range a.functions {+ switch function.action {+ case diffActionDrop:+ delete(functionConfigMap, functionName)+ continue+ default:+ functionConfig := functionConfigMap[functionName]+ if functionConfig == nil {+ functionConfig = &storepb.FunctionConfig{+ Name: functionName,+ }+ }+ functionConfig.UpdateTime = function.updateInfo.lastUpdatedTime+ functionConfig.Updater = function.updateInfo.lastUpdater+ functionConfig.SourceBranch = function.updateInfo.sourceBranch+ functionConfigs = append(functionConfigs, functionConfig)+ delete(functionConfigMap, functionName)+ }+ }+ // Add the remaining function configs in the target schema config.+ for _, functionConfig := range functionConfigMap {+ functionConfigs = append(functionConfigs, functionConfig)+ }++ return &storepb.SchemaConfig{+ Name: target.GetName(),+ TableConfigs: tableConfigs,+ ViewConfigs: viewConfigs,+ FunctionConfigs: functionConfigs,+ ProcedureConfigs: procedureConfigs,+ }+}diff --git a/backend/api/v1/branch_merge_test.go b/backend/api/v1/branch_merge_test.goindex bafa556123..0da9c8eceb 100644--- a/backend/api/v1/branch_merge_test.go+++ b/backend/api/v1/branch_merge_test.go@@ -4,11 +4,13 @@ import ("os""strings""testing"+ "time""github.com/google/go-cmp/cmp""github.com/stretchr/testify/require""google.golang.org/protobuf/encoding/protojson""google.golang.org/protobuf/testing/protocmp"+ "google.golang.org/protobuf/types/known/timestamppb""gopkg.in/yaml.v3""github.com/bytebase/bytebase/backend/common"@@ -50,7 +52,7 @@ func TestTryMerge(t *testing.T) {baseSchemaMetadata := new(storepb.DatabaseSchemaMetadata)err := common.ProtojsonUnmarshaler.Unmarshal([]byte(tc.Base), baseSchemaMetadata)a.NoErrorf(err, "test case %d: %s", idx, tc.Description)- mergedSchemaMetadata, err := tryMerge(ancestorSchemaMetadata, headSchemaMetadata, baseSchemaMetadata, storepb.Engine_MYSQL)+ mergedSchemaMetadata, _, err := tryMerge(ancestorSchemaMetadata, headSchemaMetadata, baseSchemaMetadata, nil, nil, nil /* TODO(zp): fix test */, storepb.Engine_MYSQL)a.NoErrorf(err, "test case %d: %s", idx, tc.Description)a.NotNil(mergedSchemaMetadata, "test case %d: %s, mergedSchemaMetadata should not be nil if there is no error", idx, tc.Description)@@ -137,3 +139,198 @@ func TestAutoRandomEqual(t *testing.T) {require.Equal(t, tc.equal, got)}}++func TestDeriveUpdateInfoFromMetadataDiff(t *testing.T) {+ time1 := timestamppb.Now()+ time2 := timestamppb.New(time1.AsTime().Add(1 * time.Second))+ testCases := []struct {+ metadataDiff *metadataDiffRootNode+ config *storepb.DatabaseConfig+ want *updateInfoDiffRootNode+ }{+ {+ metadataDiff: &metadataDiffRootNode{+ schemas: map[string]*metadataDiffSchemaNode{+ "": {+ tables: map[string]*metadataDiffTableNode{+ "t1": {+ diffBaseNode: diffBaseNode{+ action: diffActionCreate,+ },+ },+ "t2": {+ diffBaseNode: diffBaseNode{+ action: diffActionUpdate,+ },+ },+ "t3": {+ diffBaseNode: diffBaseNode{+ action: diffActionDrop,+ },+ },+ },+ },+ },+ },+ config: &storepb.DatabaseConfig{+ SchemaConfigs: []*storepb.SchemaConfig{+ {+ Name: "",+ TableConfigs: []*storepb.TableConfig{+ {+ Name: "t1",+ Updater: "[email protected]",+ UpdateTime: time1,+ SourceBranch: "feat/01",+ },+ {+ Name: "t2",+ Updater: "[email protected]",+ UpdateTime: time2,+ SourceBranch: "feat/02",+ },+ },+ },+ },+ },+ want: &updateInfoDiffRootNode{+ schemas: map[string]*updateInfoDiffSchemaNode{+ "": {+ tables: map[string]*updateInfoDiffTableNode{+ "t1": {+ name: "t1",+ diffBaseNode: diffBaseNode{+ action: diffActionCreate,+ },+ updateInfo: &updateInfo{+ lastUpdatedTime: time1,+ lastUpdater: "[email protected]",+ sourceBranch: "feat/01",+ },+ },+ "t2": {+ name: "t2",+ diffBaseNode: diffBaseNode{+ action: diffActionUpdate,+ },+ updateInfo: &updateInfo{+ lastUpdatedTime: time2,+ lastUpdater: "[email protected]",+ sourceBranch: "feat/02",+ },+ },+ "t3": {+ name: "t3",+ diffBaseNode: diffBaseNode{+ action: diffActionDrop,+ },+ updateInfo: nil,+ },+ },+ views: map[string]*updateInfoDiffViewNode{},+ functions: map[string]*updateInfoDiffFunctionNode{},+ procedures: map[string]*updateInfoDiffProcedureNode{},+ },+ },+ },+ },+ }++ for _, tc := range testCases {+ got := deriveUpdateInfoFromMetadataDiff(tc.metadataDiff, tc.config)+ require.Equal(t, tc.want, got)+ }+}++func TestApplyUpdateInfoDiffRootNode(t *testing.T) {+ time1 := timestamppb.Now()+ time2 := timestamppb.New(time1.AsTime().Add(1 * time.Second))+ testCases := []struct {+ updateInfoDiffRootNode *updateInfoDiffRootNode+ config *storepb.DatabaseConfig+ want *storepb.DatabaseConfig+ }{+ {+ updateInfoDiffRootNode: &updateInfoDiffRootNode{+ schemas: map[string]*updateInfoDiffSchemaNode{+ "": {+ tables: map[string]*updateInfoDiffTableNode{+ "t1": {+ name: "t1",+ diffBaseNode: diffBaseNode{+ action: diffActionCreate,+ },+ updateInfo: &updateInfo{+ lastUpdatedTime: time2,+ lastUpdater: "[email protected]",+ sourceBranch: "feat/01",+ },+ },+ "t2": {+ name: "t2",+ diffBaseNode: diffBaseNode{+ action: diffActionDrop,+ },+ updateInfo: nil,+ },+ },+ views: map[string]*updateInfoDiffViewNode{},+ functions: map[string]*updateInfoDiffFunctionNode{},+ procedures: map[string]*updateInfoDiffProcedureNode{},+ },+ },+ },+ config: &storepb.DatabaseConfig{+ SchemaConfigs: []*storepb.SchemaConfig{+ {+ Name: "",+ TableConfigs: []*storepb.TableConfig{+ {+ Name: "t1",+ Updater: "my",+ UpdateTime: time1,+ ColumnConfigs: []*storepb.ColumnConfig{+ {+ Name: "t",+ ClassificationId: "1-1-1",+ },+ },+ },+ {+ Name: "t2",+ Updater: "my",+ UpdateTime: time1,+ },+ },+ },+ },+ },+ want: &storepb.DatabaseConfig{+ SchemaConfigs: []*storepb.SchemaConfig{+ {+ Name: "",+ TableConfigs: []*storepb.TableConfig{+ {+ Name: "t1",+ UpdateTime: time2,+ Updater: "[email protected]",+ SourceBranch: "feat/01",+ ColumnConfigs: []*storepb.ColumnConfig{+ {+ Name: "t",+ ClassificationId: "1-1-1",+ },+ },+ },+ },+ },+ },+ },+ },+ }++ for _, tc := range testCases {+ got := applyUpdateInfoDiffRootNode(tc.updateInfoDiffRootNode, tc.config)+ require.Equal(t, tc.want, got)+ }+}diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.goindex c286dea085..289b7a2133 100644--- a/backend/api/v1/branch_service.go+++ b/backend/api/v1/branch_service.go@@ -12,7 +12,6 @@ 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"@@ -236,6 +235,7 @@ func (s *BranchService) CreateBranch(ctx context.Context, request *v1pb.CreateBrconfig := databaseSchema.GetConfig()sanitizeCommentForSchemaMetadata(filteredBaseSchemaMetadata, model.NewDatabaseConfig(config), classificationConfig.ClassificationFromConfig)initBranchLastUpdateInfoConfig(filteredBaseSchemaMetadata, config)+ alignDatabaseConfig(filteredBaseSchemaMetadata, config)created, err := s.store.CreateBranch(ctx, &store.BranchMessage{ProjectID: project.ResourceID,ResourceID: branchID,@@ -462,12 +462,12 @@ func (s *BranchService) MergeBranch(ctx context.Context, request *v1pb.MergeBransanitizeCommentForSchemaMetadata(headBranch.Head.Metadata, model.NewDatabaseConfig(headBranch.Head.DatabaseConfig), classificationConfig.ClassificationFromConfig)sanitizeCommentForSchemaMetadata(baseBranch.Head.Metadata, model.NewDatabaseConfig(baseBranch.Head.DatabaseConfig), classificationConfig.ClassificationFromConfig)- adHead, err := tryMerge(headBranch.Base.Metadata, headBranch.Head.Metadata, baseBranch.Head.Metadata, baseBranch.Engine)+ adHead, adConfig, err := tryMerge(headBranch.Base.Metadata, headBranch.Head.Metadata, baseBranch.Head.Metadata, headBranch.Base.DatabaseConfig, headBranch.Head.DatabaseConfig, baseBranch.Head.DatabaseConfig, baseBranch.Engine)if err != nil {slog.Info("cannot merge branches", log.BBError(err))return nil, status.Errorf(codes.Aborted, "cannot merge branches without conflict, error: %v", err)}- mergedMetadata, err := tryMerge(baseBranch.Head.Metadata, adHead, baseBranch.Head.Metadata, baseBranch.Engine)+ mergedMetadata, mergedConfig, err := tryMerge(baseBranch.Head.Metadata, adHead, baseBranch.Head.Metadata, baseBranch.Head.DatabaseConfig, adConfig, baseBranch.Head.DatabaseConfig, baseBranch.Engine)if err != nil {slog.Info("cannot merge branches", log.BBError(err))return nil, status.Errorf(codes.Aborted, "cannot merge branches without conflict, error: %v", err)@@ -480,9 +480,11 @@ func (s *BranchService) MergeBranch(ctx context.Context, request *v1pb.MergeBranif err != nil {return nil, status.Errorf(codes.Internal, "failed to convert merged metadata to schema string, %v", err)}+ alignDatabaseConfig(mergedMetadata, mergedConfig)// 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(baseBranch.Head.GetDatabaseConfig(), headBranch.Base.GetDatabaseConfig(), headBranch.Head.GetDatabaseConfig())+ classificationIDSource := utils.MergeDatabaseConfig(baseBranch.Head.GetDatabaseConfig(), headBranch.Base.GetDatabaseConfig(), headBranch.Head.GetDatabaseConfig())+ setClassificationIDToConfig(classificationIDSource, mergedConfig)mergedSchemaBytes := []byte(mergedSchema)@@ -582,21 +584,23 @@ func (s *BranchService) RebaseBranch(ctx context.Context, request *v1pb.RebaseBr}newHeadMetadata = filterDatabaseMetadataByEngine(newHeadMetadata, baseBranch.Engine)newHeadConfig = baseBranch.Head.GetDatabaseConfig()- // If users solve the conflict manually, it is equivalent to them updating HEAD on the branch first.- updateConfigBranchUpdateInfoForUpdate(baseBranch.Head.Metadata, newHeadMetadata, newHeadConfig, common.FormatUserUID(user.ID), common.FormatBranchResourceID(baseProjectID, baseBranchID))- // 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)-+ // While users manually merge the metadata in the frontend, the config is stale.+ // We should align the config with the merged metadata.classificationConfig, err := s.store.GetDataClassificationConfigByID(ctx, baseProject.DataClassificationConfigID)if err != nil {return nil, status.Errorf(codes.Internal, `failed to get classification config by id "%s" with error: %v`, baseProject.DataClassificationConfigID, err)}-+ // 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.trimClassificationIDFromCommentIfNeeded(newHeadMetadata, classificationConfig)+ modelNewHeadConfig := model.NewDatabaseConfig(newHeadConfig)sanitizeCommentForSchemaMetadata(newHeadMetadata, modelNewHeadConfig, classificationConfig.ClassificationFromConfig)+ alignDatabaseConfig(newHeadMetadata, newHeadConfig)++ // If users solve the conflict manually, it is equivalent to them updating HEAD on the branch first.+ updateConfigBranchUpdateInfoForUpdate(baseBranch.Head.Metadata, newHeadMetadata, newHeadConfig, common.FormatUserUID(user.ID), common.FormatBranchResourceID(baseProjectID, baseBranchID))} else {- newHeadMetadata, err = tryMerge(baseBranch.Base.Metadata, baseBranch.Head.Metadata, filteredNewBaseMetadata, baseBranch.Engine)+ newHeadMetadata, newHeadConfig, err = tryMerge(baseBranch.Base.Metadata, baseBranch.Head.Metadata, filteredNewBaseMetadata, baseBranch.Base.DatabaseConfig, baseBranch.Head.DatabaseConfig, newBaseConfig, baseBranch.Engine)if err != nil {slog.Info("cannot rebase branches", log.BBError(err))conflictSchema, err := diff3.Merge(@@ -623,9 +627,11 @@ func (s *BranchService) RebaseBranch(ctx context.Context, request *v1pb.RebaseBrif newHeadMetadata == nil {return nil, status.Errorf(codes.Internal, "merged metadata should not be nil if there is no error while merging (%+v, %+v, %+v)", baseBranch.Base.Metadata, baseBranch.Head.Metadata, filteredNewBaseMetadata)}+ alignDatabaseConfig(newHeadMetadata, newHeadConfig)// 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(newBaseConfig, baseBranch.Base.GetDatabaseConfig(), baseBranch.Head.GetDatabaseConfig())+ classificationIDSource := utils.MergeDatabaseConfig(newBaseConfig, baseBranch.Base.GetDatabaseConfig(), baseBranch.Head.GetDatabaseConfig())+ setClassificationIDToConfig(classificationIDSource, newHeadConfig)}reconcileMetadata(newHeadMetadata, baseBranch.Engine)@@ -635,10 +641,8 @@ func (s *BranchService) RebaseBranch(ctx context.Context, request *v1pb.RebaseBrif err != nil {return nil, status.Errorf(codes.Internal, "failed to convert new head metadata to schema string, %v", err)}-newBaseSchemaBytes := []byte(newBaseSchema)newHeadSchemaBytes := []byte(newHeadSchema)- updateDatabaseConfigLastModifierForMerge(filteredNewHeadMetadata, filteredNewBaseMetadata, newHeadConfig, newBaseConfig)if request.ValidateOnly {baseBranch.Base = &storepb.BranchSnapshot{Metadata: filteredNewBaseMetadata,@@ -1420,11 +1424,18 @@ func initSchemaConfig(schema *storepb.SchemaMetadata, formattedUserUID string, b}func initTableConfig(table *storepb.TableMetadata, formattedUserEmail string, branchResourceID string, time *timestamppb.Timestamp) *storepb.TableConfig {+ var columnConfigs []*storepb.ColumnConfig+ for _, column := range table.Columns {+ columnConfigs = append(columnConfigs, &storepb.ColumnConfig{+ Name: column.Name,+ })+ }return &storepb.TableConfig{- Name: table.Name,- Updater: formattedUserEmail,- SourceBranch: branchResourceID,- UpdateTime: time,+ Name: table.Name,+ Updater: formattedUserEmail,+ SourceBranch: branchResourceID,+ UpdateTime: time,+ ColumnConfigs: columnConfigs,}}@@ -1546,189 +1557,6 @@ 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 = headTableConfig.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 diff := cmp.Diff(headTable, baseTable.GetProto(), protocmp.Transform()); diff != "" {- baseTableConfig.SourceBranch = headTableConfig.SourceBranch- baseTableConfig.Updater = headTableConfig.Updater- baseTableConfig.UpdateTime = now- }- }- baseSchemaConfig.TableConfigs = append(baseSchemaConfig.TableConfigs, newBaseTableConfigs...)-- // 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 = headViewConfig.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 !equalView(headView, baseView.GetProto()) {- baseViewConfig.SourceBranch = headViewConfig.SourceBranch- baseViewConfig.Updater = headViewConfig.Updater- baseViewConfig.UpdateTime = now- }- }- 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()- })- 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 = headProcedureConfig.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 !equalProcedure(headProcedure, baseProcedure.GetProto()) {- baseProcedureConfig.SourceBranch = headProcedureConfig.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{@@ -1759,6 +1587,19 @@ func alignDatabaseConfig(metadata *storepb.DatabaseSchemaMetadata, config *storenewTableConfigs = append(newTableConfigs, initTableConfig(tableModel.GetProto(), "", "", now))continue}+ // Align column configs+ tableConfig := tableConfigMap[tableName]+ columnConfigMap := buildMap(tableConfig.ColumnConfigs, func(s *storepb.ColumnConfig) string {+ return s.Name+ })+ for _, column := range tableModel.GetColumns() {+ if _, ok := columnConfigMap[column.GetName()]; !ok {+ tableConfig.ColumnConfigs = append(tableConfig.ColumnConfigs, &storepb.ColumnConfig{+ Name: column.GetName(),+ })+ continue+ }+ }}schemaConfig.TableConfigs = append(schemaConfig.TableConfigs, newTableConfigs...)@@ -1808,28 +1649,37 @@ 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())-}+// setClassificationIDToConfig inplace set the classification ID from the a config to the b config.+func setClassificationIDToConfig(a, b *storepb.DatabaseConfig) {+ aSchemaConfigMap := buildMap(a.SchemaConfigs, func(s *storepb.SchemaConfig) string {+ return s.Name+ })-func equalProcedure(a, b *storepb.ProcedureMetadata) bool {- if a.GetName() != b.GetName() {- return false+ for _, schemaConfig := range b.SchemaConfigs {+ aSchemaConfig, ok := aSchemaConfigMap[schemaConfig.Name]+ if !ok {+ continue+ }+ aTableConfigMap := buildMap(aSchemaConfig.TableConfigs, func(t *storepb.TableConfig) string {+ return t.Name+ })+ for _, tableConfig := range schemaConfig.TableConfigs {+ aTableConfig, ok := aTableConfigMap[tableConfig.Name]+ if !ok {+ continue+ }+ tableConfig.ClassificationId = aTableConfig.ClassificationId+ aColumnConfigMap := buildMap(aTableConfig.ColumnConfigs, func(c *storepb.ColumnConfig) string {+ return c.Name+ })+ for _, columnConfig := range tableConfig.ColumnConfigs {+ aColumnConfig, ok := aColumnConfigMap[columnConfig.Name]+ if !ok {+ continue+ }+ columnConfig.ClassificationId = aColumnConfig.ClassificationId+ delete(aColumnConfigMap, columnConfig.Name)+ }+ }}-- return equalRoutineDefinition(a.GetDefinition(), b.GetDefinition())}