Skip to content

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

fix: create a new config instead of in-place changes to avoid shared-map (#16974)

rebelicea4c8c8e4e622
diff --git a/backend/api/v1/query_result_masker.go b/backend/api/v1/query_result_masker.go
index bd02d0b09f..feada08b72 100644
--- a/backend/api/v1/query_result_masker.go
+++ b/backend/api/v1/query_result_masker.go
@@ -356,9 +356,9 @@ func (s *QueryResultMasker) getColumnForColumnResource(ctx context.Context, inst
if config == nil {
return columnMetadata, nil, nil
}
- schemaConfig := config.CreateOrGetSchemaConfig(sourceColumn.Schema)
- tableConfig := schemaConfig.CreateOrGetTableConfig(sourceColumn.Table)
- columnConfig = tableConfig.CreateOrGetColumnConfig(sourceColumn.Column)
+ schemaConfig := config.GetSchemaConfig(sourceColumn.Schema)
+ tableConfig := schemaConfig.GetTableConfig(sourceColumn.Table)
+ columnConfig = tableConfig.GetColumnConfig(sourceColumn.Column)
return columnMetadata, columnConfig, nil
}
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index db3de31d96..0d9bacd0d6 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -1789,16 +1789,16 @@ func (*SQLService) DiffMetadata(_ context.Context, req *connect.Request[v1pb.Dif
func sanitizeCommentForSchemaMetadata(dbSchema *storepb.DatabaseSchemaMetadata, dbModelConfig *model.DatabaseConfig, classificationFromConfig bool) {
for _, schema := range dbSchema.Schemas {
- schemaConfig := dbModelConfig.CreateOrGetSchemaConfig(schema.Name)
+ schemaConfig := dbModelConfig.GetSchemaConfig(schema.Name)
for _, table := range schema.Tables {
- tableConfig := schemaConfig.CreateOrGetTableConfig(table.Name)
+ tableConfig := schemaConfig.GetTableConfig(table.Name)
classificationID := ""
if !classificationFromConfig {
classificationID = tableConfig.Classification
}
table.Comment = common.GetCommentFromClassificationAndUserComment(classificationID, table.UserComment)
for _, col := range table.Columns {
- columnConfig := tableConfig.CreateOrGetColumnConfig(col.Name)
+ columnConfig := tableConfig.GetColumnConfig(col.Name)
classificationID := ""
if !classificationFromConfig {
classificationID = columnConfig.Classification
diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.go
index 751a02401e..faf5adadf2 100644
--- a/backend/runner/schemasync/syncer.go
+++ b/backend/runner/schemasync/syncer.go
@@ -402,13 +402,17 @@ func (s *Syncer) SyncDatabaseSchemaToHistory(ctx context.Context, database *stor
// Force to disable classification from comment if the engine is not MYSQL or PG.
classificationConfig.ClassificationFromConfig = true
}
+ var dbConfig *storepb.DatabaseConfig
if classificationConfig.ClassificationFromConfig {
// Only set the user comment.
setUserCommentFromComment(databaseMetadata)
+ dbConfig = dbModelConfig.BuildDatabaseConfig()
} else {
// Get classification from the comment.
if err := s.licenseService.IsFeatureEnabledForInstance(v1pb.PlanFeature_FEATURE_DATA_CLASSIFICATION, instance); err == nil {
- setClassificationAndUserCommentFromComment(databaseMetadata, dbModelConfig, classificationConfig)
+ dbConfig = buildDatabaseConfigWithClassificationFromComment(databaseMetadata, dbModelConfig, classificationConfig)
+ } else {
+ dbConfig = dbModelConfig.BuildDatabaseConfig()
}
}
@@ -437,7 +441,7 @@ func (s *Syncer) SyncDatabaseSchemaToHistory(ctx context.Context, database *stor
todo := !common.EngineDBSchemaReadyToMigrate(instance.Metadata.GetEngine())
if err := s.store.UpsertDBSchema(ctx,
database.InstanceID, database.DatabaseName,
- databaseMetadata, dbModelConfig.BuildDatabaseConfig(), rawDump, todo,
+ databaseMetadata, dbConfig, rawDump, todo,
); err != nil {
if strings.Contains(err.Error(), "escape sequence") {
if metadataBytes, err := protojson.Marshal(databaseMetadata); err == nil {
@@ -512,12 +516,14 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, database *store.Databas
// Force to disable classification from comment if the engine is not MYSQL or PG.
classificationConfig.ClassificationFromConfig = true
}
+ var dbConfig *storepb.DatabaseConfig
if classificationConfig.ClassificationFromConfig {
// Only set the user comment.
setUserCommentFromComment(databaseMetadata)
+ dbConfig = dbModelConfig.BuildDatabaseConfig()
} else {
// Get classification from the comment.
- setClassificationAndUserCommentFromComment(databaseMetadata, dbModelConfig, classificationConfig)
+ dbConfig = buildDatabaseConfigWithClassificationFromComment(databaseMetadata, dbModelConfig, classificationConfig)
}
d := false
@@ -547,7 +553,7 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, database *store.Databas
todo := !common.EngineDBSchemaReadyToMigrate(instance.Metadata.GetEngine())
if err := s.store.UpsertDBSchema(ctx,
database.InstanceID, database.DatabaseName,
- databaseMetadata, dbModelConfig.BuildDatabaseConfig(), rawDump, todo,
+ databaseMetadata, dbConfig, rawDump, todo,
); err != nil {
if strings.Contains(err.Error(), "escape sequence") {
if metadataBytes, err := protojson.Marshal(databaseMetadata); err == nil {
@@ -636,43 +642,78 @@ func (s *Syncer) databaseBackupAvailable(ctx context.Context, instance *store.In
return false
}
-func setClassificationAndUserCommentFromComment(dbSchema *storepb.DatabaseSchemaMetadata, databaseConfig *model.DatabaseConfig, classificationConfig *storepb.DataClassificationSetting_DataClassificationConfig) {
+func buildDatabaseConfigWithClassificationFromComment(dbSchema *storepb.DatabaseSchemaMetadata, databaseConfig *model.DatabaseConfig, classificationConfig *storepb.DataClassificationSetting_DataClassificationConfig) *storepb.DatabaseConfig {
+ // Create a new DatabaseConfig to build from scratch
+ newConfig := &storepb.DatabaseConfig{
+ Name: dbSchema.Name,
+ }
+
for _, schema := range dbSchema.Schemas {
- schemaConfig := databaseConfig.CreateOrGetSchemaConfig(schema.Name)
+ var tables []*storepb.TableCatalog
+ hasSchemaContent := false
+
+ // Get existing schema config to preserve SemanticType and Labels
+ schemaConfig := databaseConfig.GetSchemaConfig(schema.Name)
for _, table := range schema.Tables {
- tableConfig := schemaConfig.CreateOrGetTableConfig(table.Name)
classification, userComment := common.GetClassificationAndUserComment(table.Comment, classificationConfig)
table.UserComment = userComment
- tableConfig.Classification = classification
+
+ // Get existing table config
+ tableConfig := schemaConfig.GetTableConfig(table.Name)
+
+ var columns []*storepb.ColumnCatalog
+ hasTableContent := false
+
+ // Check if table has classification
+ if classification != "" {
+ hasTableContent = true
+ }
for _, col := range table.Columns {
- columnConfig := tableConfig.CreateOrGetColumnConfig(col.Name)
colClassification, colUserComment := common.GetClassificationAndUserComment(col.Comment, classificationConfig)
col.UserComment = colUserComment
- columnConfig.Classification = colClassification
- if isEmptyColumnConfig(columnConfig) {
- tableConfig.RemoveColumnConfig(col.Name)
+ // Get existing column config to preserve SemanticType and Labels
+ existingColumnConfig := tableConfig.GetColumnConfig(col.Name)
+
+ // Add column config if it has any meaningful data
+ if colClassification != "" || existingColumnConfig.SemanticType != "" || len(existingColumnConfig.Labels) > 0 {
+ columns = append(columns, &storepb.ColumnCatalog{
+ Name: col.Name,
+ Classification: colClassification,
+ SemanticType: existingColumnConfig.SemanticType,
+ Labels: existingColumnConfig.Labels,
+ })
+ hasTableContent = true
}
}
- if tableConfig.IsEmpty() {
- schemaConfig.RemoveTableConfig(table.Name)
+ // Only add table catalog if it has content (either table classification or column configurations)
+ if hasTableContent {
+ tableCatalog := &storepb.TableCatalog{
+ Name: table.Name,
+ Classification: classification,
+ Columns: columns,
+ }
+ tables = append(tables, tableCatalog)
+ hasSchemaContent = true
}
}
- if schemaConfig.IsEmpty() {
- databaseConfig.RemoveSchemaConfig(schema.Name)
+
+ // Only add schema catalog if it has content
+ if hasSchemaContent {
+ schemaCatalog := &storepb.SchemaCatalog{
+ Name: schema.Name,
+ Tables: tables,
+ }
+ newConfig.Schemas = append(newConfig.Schemas, schemaCatalog)
}
}
-}
-func isEmptyColumnConfig(config *storepb.ColumnCatalog) bool {
- return config == nil || (len(config.Labels) == 0 &&
- config.Classification == "" &&
- config.SemanticType == "")
+ return newConfig
}
func setUserCommentFromComment(dbSchema *storepb.DatabaseSchemaMetadata) {
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index 22a6104784..5516c55252 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -140,15 +140,15 @@ func NewDatabaseConfig(config *storepb.DatabaseConfig) *DatabaseConfig {
return databaseConfig
}
-// CreateOrGetSchemaConfig creates or gets a new schema config by name.
-func (d *DatabaseConfig) CreateOrGetSchemaConfig(name string) *SchemaConfig {
+// GetSchemaConfig gets the schema config by name.
+// If not found, returns a new empty schema config.
+func (d *DatabaseConfig) GetSchemaConfig(name string) *SchemaConfig {
if config := d.internal[name]; config != nil {
return config
}
- d.internal[name] = &SchemaConfig{
+ return &SchemaConfig{
internal: make(map[string]*TableConfig),
}
- return d.internal[name]
}
// RemoveSchemaConfig delete the schema config by name.
@@ -191,20 +191,15 @@ func (s *SchemaConfig) IsEmpty() bool {
return len(s.internal) == 0
}
-// CreateOrGetTableConfig creates or gets the table config by name.
-func (s *SchemaConfig) CreateOrGetTableConfig(name string) *TableConfig {
+// GetTableConfig gets the table config by name.
+// If not found, returns a new empty table config.
+func (s *SchemaConfig) GetTableConfig(name string) *TableConfig {
if config := s.internal[name]; config != nil {
return config
}
- s.internal[name] = &TableConfig{
+ return &TableConfig{
internal: make(map[string]*storepb.ColumnCatalog),
}
- return s.internal[name]
-}
-
-// RemoveTableConfig delete the table config by name.
-func (s *SchemaConfig) RemoveTableConfig(name string) {
- delete(s.internal, name)
}
// TableConfig is the config for a table.
@@ -213,15 +208,15 @@ type TableConfig struct {
internal map[string]*storepb.ColumnCatalog
}
-// CreateOrGetColumnConfig creates or gets the column config by name.
-func (t *TableConfig) CreateOrGetColumnConfig(name string) *storepb.ColumnCatalog {
+// GetColumnConfig gets the column config by name.
+// If not found, returns a new empty column config.
+func (t *TableConfig) GetColumnConfig(name string) *storepb.ColumnCatalog {
if config := t.internal[name]; config != nil {
return config
}
- t.internal[name] = &storepb.ColumnCatalog{
+ return &storepb.ColumnCatalog{
Name: name,
}
- return t.internal[name]
}
// RemoveColumnConfig delete the column config by name.