Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: stuff config to GetClassificationAndUserComment (#13586)
Danny Xu7a8b6a00f8df
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.goindex 639a496198..f298ba7638 100644--- a/backend/api/v1/branch_service.go+++ b/backend/api/v1/branch_service.go@@ -594,7 +594,7 @@ func (s *BranchService) RebaseBranch(ctx context.Context, request *v1pb.RebaseBrreturn nil, status.Errorf(codes.Internal, `failed to get classification config by id "%s" with error: %v`, baseProject.DataClassificationConfigID, err)}- trimClassificationIDFromCommentIfNeeded(newHeadMetadata, classificationConfig.ClassificationFromConfig)+ trimClassificationIDFromCommentIfNeeded(newHeadMetadata, classificationConfig)sanitizeCommentForSchemaMetadata(newHeadMetadata, modelNewHeadConfig, classificationConfig.ClassificationFromConfig)} else {newHeadMetadata, err = tryMerge(baseBranch.Base.Metadata, baseBranch.Head.Metadata, filteredNewBaseMetadata, baseBranch.Engine)@@ -1529,18 +1529,18 @@ func initBranchLastUpdateInfoConfig(metadata *storepb.DatabaseSchemaMetadata, co}}-func trimClassificationIDFromCommentIfNeeded(dbSchema *storepb.DatabaseSchemaMetadata, classificationFromConfig bool) {- if classificationFromConfig {+func trimClassificationIDFromCommentIfNeeded(dbSchema *storepb.DatabaseSchemaMetadata, classificationConfig *storepb.DataClassificationSetting_DataClassificationConfig) {+ if classificationConfig.ClassificationFromConfig {return}for _, schema := range dbSchema.Schemas {for _, table := range schema.Tables {- if !classificationFromConfig {- _, table.UserComment = common.GetClassificationAndUserComment(table.Comment)+ if !classificationConfig.ClassificationFromConfig {+ _, table.UserComment = common.GetClassificationAndUserComment(table.Comment, classificationConfig)}for _, col := range table.Columns {- if !classificationFromConfig {- _, col.UserComment = common.GetClassificationAndUserComment(col.Comment)+ if !classificationConfig.ClassificationFromConfig {+ _, col.UserComment = common.GetClassificationAndUserComment(col.Comment, classificationConfig)}}}diff --git a/backend/common/db_schema.go b/backend/common/db_schema.goindex 0c835392d8..494c8bd5df 100644--- a/backend/common/db_schema.go+++ b/backend/common/db_schema.go@@ -1,29 +1,28 @@package commonimport (- "regexp""slices"- "strings"storepb "github.com/bytebase/bytebase/proto/generated-go/store")-var numberReg = regexp.MustCompile("^[0-9]+$")-// GetClassificationAndUserComment parses classification and user comment from the given comment.-func GetClassificationAndUserComment(comment string) (string, string) {- sections := strings.Split(comment, "-")- classification := []string{}- userComment := ""- for i, section := range sections {- if numberReg.MatchString(section) {- classification = append(classification, section)- } else {- userComment = strings.Join(sections[i:], "-")- break- }- }- return strings.Join(classification, "-"), userComment+func GetClassificationAndUserComment(comment string, classificationConfig *storepb.DataClassificationSetting_DataClassificationConfig) (string, string) {+ if classificationConfig == nil {+ return "", comment+ }+ if _, ok := classificationConfig.Classification[comment]; ok {+ return comment, ""+ }+ for i := len(comment) - 1; i >= 0; i-- {+ if comment[i] != '-' {+ continue+ }+ if _, ok := classificationConfig.Classification[comment[:i]]; ok {+ return comment[:i], comment[i+1:]+ }+ }+ return "", comment}// GetCommentFromClassificationAndUserComment returns the comment from the given classification and user comment.diff --git a/backend/common/db_schema_test.go b/backend/common/db_schema_test.goindex 43273dcac8..8c223ce831 100644--- a/backend/common/db_schema_test.go+++ b/backend/common/db_schema_test.go@@ -10,7 +10,14 @@ import (storepb "github.com/bytebase/bytebase/proto/generated-go/store")-func TestClassification(t *testing.T) {+func TestGetClassificationAndUserComment(t *testing.T) {+ classificationConfig := &storepb.DataClassificationSetting_DataClassificationConfig{+ Classification: map[string]*storepb.DataClassificationSetting_DataClassificationConfig_DataClassification{+ "0": {},+ "1": {},+ "1-2": {},+ },+ }tests := []struct {rawComment stringclassification string@@ -76,11 +83,16 @@ func TestClassification(t *testing.T) {classification: "",userComment: "1 2",},+ {+ rawComment: "7-8-9-stuff-not-exists",+ classification: "",+ userComment: "7-8-9-stuff-not-exists",+ },}for _, test := range tests {t.Run(fmt.Sprintf("test classification for comment: %v", test.rawComment), func(t *testing.T) {- classification, userComment := GetClassificationAndUserComment(test.rawComment)+ classification, userComment := GetClassificationAndUserComment(test.rawComment, classificationConfig)assert.Equal(t, test.classification, classification)assert.Equal(t, test.userComment, userComment)rebuildComment := GetCommentFromClassificationAndUserComment(classification, userComment)diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.goindex ff070d9bf3..6c40dde190 100644--- a/backend/runner/schemasync/syncer.go+++ b/backend/runner/schemasync/syncer.go@@ -384,7 +384,7 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, database *store.DatabassetUserCommentFromComment(databaseMetadata)} else {// Get classification from the comment.- setClassificationAndUserCommentFromComment(databaseMetadata, dbModelConfig)+ setClassificationAndUserCommentFromComment(databaseMetadata, dbModelConfig, classificationConfig)}syncStatus := api.OK@@ -579,20 +579,20 @@ func (s *Syncer) upsertDatabaseConnectionAnomaly(ctx context.Context, instance *}}-func setClassificationAndUserCommentFromComment(dbSchema *storepb.DatabaseSchemaMetadata, databaseConfig *model.DatabaseConfig) {+func setClassificationAndUserCommentFromComment(dbSchema *storepb.DatabaseSchemaMetadata, databaseConfig *model.DatabaseConfig, classificationConfig *storepb.DataClassificationSetting_DataClassificationConfig) {for _, schema := range dbSchema.Schemas {schemaConfig := databaseConfig.CreateOrGetSchemaConfig(schema.Name)for _, table := range schema.Tables {tableConfig := schemaConfig.CreateOrGetTableConfig(table.Name)- classification, userComment := common.GetClassificationAndUserComment(table.Comment)+ classification, userComment := common.GetClassificationAndUserComment(table.Comment, classificationConfig)table.UserComment = userCommenttableConfig.ClassificationID = classificationfor _, col := range table.Columns {columnConfig := tableConfig.CreateOrGetColumnConfig(col.Name)- colClassification, colUserComment := common.GetClassificationAndUserComment(col.Comment)+ colClassification, colUserComment := common.GetClassificationAndUserComment(col.Comment, classificationConfig)col.UserComment = colUserCommentcolumnConfig.ClassificationId = colClassification