Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: optimize semantic types (#21399)
ecmadao7cbdf0330bd6
diff --git a/backend/api/v1/catalog_masking_cosmosdb_test.go b/backend/api/v1/catalog_masking_cosmosdb_test.goindex b728b18cf6..94fdea946f 100644--- a/backend/api/v1/catalog_masking_cosmosdb_test.go+++ b/backend/api/v1/catalog_masking_cosmosdb_test.go@@ -26,7 +26,7 @@ func cosmosDBTestSchema() *storepb.ObjectSchema {StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{"name": {Type: storepb.ObjectSchema_STRING},- "email": {Type: storepb.ObjectSchema_STRING, SemanticType: "bb.default"},+ "email": {Type: storepb.ObjectSchema_STRING, SemanticType: defaultSemanticTypeID},"country": {Type: storepb.ObjectSchema_STRING},"population": {Type: storepb.ObjectSchema_NUMBER},"status": {Type: storepb.ObjectSchema_STRING},@@ -36,7 +36,7 @@ func cosmosDBTestSchema() *storepb.ObjectSchema {"_etag": {Type: storepb.ObjectSchema_STRING},"location": {Type: storepb.ObjectSchema_OBJECT,- SemanticType: "bb.default",+ SemanticType: defaultSemanticTypeID,Kind: &storepb.ObjectSchema_StructKind_{StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{@@ -51,7 +51,7 @@ func cosmosDBTestSchema() *storepb.ObjectSchema {Kind: &storepb.ObjectSchema_StructKind_{StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{- "phone": {Type: storepb.ObjectSchema_STRING, SemanticType: "bb.default"},+ "phone": {Type: storepb.ObjectSchema_STRING, SemanticType: defaultSemanticTypeID},"city": {Type: storepb.ObjectSchema_STRING},},},@@ -65,7 +65,7 @@ func cosmosDBTestSchema() *storepb.ObjectSchema {func cosmosDBMaskers() map[string]masker.Masker {return map[string]masker.Masker{- "bb.default": masker.NewDefaultFullMasker(),+ defaultSemanticTypeID: masker.NewDefaultFullMasker(),}}@@ -585,7 +585,7 @@ func TestCosmosDBPredicateNotEqual(t *testing.T) {require.True(t, ok, "expected c.email in predicate paths")semanticType := getFirstSemanticTypeInPath(span.PredicatePaths["c.email"], schema)- require.Equal(t, "bb.default", semanticType, "email should be sensitive in predicate")+ require.Equal(t, defaultSemanticTypeID, semanticType, "email should be sensitive in predicate")}func TestCosmosDBPredicateIn(t *testing.T) {@@ -599,7 +599,7 @@ func TestCosmosDBPredicateIn(t *testing.T) {require.True(t, ok, "expected c.email in predicate paths")semanticType := getFirstSemanticTypeInPath(span.PredicatePaths["c.email"], schema)- require.Equal(t, "bb.default", semanticType, "email should be sensitive in predicate")+ require.Equal(t, defaultSemanticTypeID, semanticType, "email should be sensitive in predicate")}func TestCosmosDBPredicateBetween(t *testing.T) {@@ -655,7 +655,7 @@ func TestCosmosDBPredicateNestedSensitive(t *testing.T) {require.True(t, ok, "expected c.contact.phone in predicate paths")semanticType := getFirstSemanticTypeInPath(span.PredicatePaths["c.contact.phone"], schema)- require.Equal(t, "bb.default", semanticType, "contact.phone should be sensitive")+ require.Equal(t, defaultSemanticTypeID, semanticType, "contact.phone should be sensitive")}func TestCosmosDBPredicateFunctionInWhere(t *testing.T) {@@ -669,7 +669,7 @@ func TestCosmosDBPredicateFunctionInWhere(t *testing.T) {require.True(t, ok, "expected c.email in predicate paths from function argument")semanticType := getFirstSemanticTypeInPath(span.PredicatePaths["c.email"], schema)- require.Equal(t, "bb.default", semanticType, "email should be sensitive even in function")+ require.Equal(t, defaultSemanticTypeID, semanticType, "email should be sensitive even in function")}func TestCosmosDBPredicateCompoundWhere(t *testing.T) {diff --git a/backend/api/v1/catalog_masking_elasticsearch_test.go b/backend/api/v1/catalog_masking_elasticsearch_test.goindex 472b0ac217..5dcdf212d0 100644--- a/backend/api/v1/catalog_masking_elasticsearch_test.go+++ b/backend/api/v1/catalog_masking_elasticsearch_test.go@@ -21,14 +21,14 @@ func testElasticsearchObjectSchema() *storepb.ObjectSchema {StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{"name": {Type: storepb.ObjectSchema_STRING},- "email": {Type: storepb.ObjectSchema_STRING, SemanticType: "bb.default"},+ "email": {Type: storepb.ObjectSchema_STRING, SemanticType: defaultSemanticTypeID},"age": {Type: storepb.ObjectSchema_NUMBER},"contact": {Type: storepb.ObjectSchema_OBJECT,Kind: &storepb.ObjectSchema_StructKind_{StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{- "phone": {Type: storepb.ObjectSchema_STRING, SemanticType: "bb.default"},+ "phone": {Type: storepb.ObjectSchema_STRING, SemanticType: defaultSemanticTypeID},"city": {Type: storepb.ObjectSchema_STRING},},},@@ -42,7 +42,7 @@ func testElasticsearchObjectSchema() *storepb.ObjectSchema {func testElasticsearchMaskerMap() map[string]masker.Masker {return map[string]masker.Masker{- "bb.default": masker.NewDefaultFullMasker(),+ defaultSemanticTypeID: masker.NewDefaultFullMasker(),}}@@ -224,7 +224,7 @@ func TestMaskElasticsearchMSearchResponses(t *testing.T) {func TestMaskElasticsearchSourceObjectDirectReplacement(t *testing.T) {t.Parallel()maskers := map[string]masker.Masker{- "bb.default": masker.NewDefaultFullMasker(),+ defaultSemanticTypeID: masker.NewDefaultFullMasker(),}tests := []struct {@@ -249,7 +249,7 @@ func TestMaskElasticsearchSourceObjectDirectReplacement(t *testing.T) {"name": {Type: storepb.ObjectSchema_STRING},"contact": {Type: storepb.ObjectSchema_OBJECT,- SemanticType: "bb.default",+ SemanticType: defaultSemanticTypeID,Kind: &storepb.ObjectSchema_StructKind_{StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{@@ -281,7 +281,7 @@ func TestMaskElasticsearchSourceObjectDirectReplacement(t *testing.T) {"name": {Type: storepb.ObjectSchema_STRING},"tags": {Type: storepb.ObjectSchema_ARRAY,- SemanticType: "bb.default",+ SemanticType: defaultSemanticTypeID,},},},diff --git a/backend/api/v1/catalog_masking_mongodb_test.go b/backend/api/v1/catalog_masking_mongodb_test.goindex a018bd4c5d..76e8e3214c 100644--- a/backend/api/v1/catalog_masking_mongodb_test.go+++ b/backend/api/v1/catalog_masking_mongodb_test.go@@ -64,7 +64,7 @@ func mustMongoSchema(t *testing.T, schemaName string) *storepb.ObjectSchema {Kind: &storepb.ObjectSchema_StructKind_{StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{- "phone": {Type: storepb.ObjectSchema_STRING, SemanticType: "bb.default"},+ "phone": {Type: storepb.ObjectSchema_STRING, SemanticType: defaultSemanticTypeID},"city": {Type: storepb.ObjectSchema_STRING},},},@@ -84,7 +84,7 @@ func mustMongoSchema(t *testing.T, schemaName string) *storepb.ObjectSchema {Type: storepb.ObjectSchema_ARRAY,Kind: &storepb.ObjectSchema_ArrayKind_{ArrayKind: &storepb.ObjectSchema_ArrayKind{- Kind: &storepb.ObjectSchema{Type: storepb.ObjectSchema_STRING, SemanticType: "bb.default"},+ Kind: &storepb.ObjectSchema{Type: storepb.ObjectSchema_STRING, SemanticType: defaultSemanticTypeID},},},},@@ -101,7 +101,7 @@ func mustMongoSchema(t *testing.T, schemaName string) *storepb.ObjectSchema {Properties: map[string]*storepb.ObjectSchema{"profile": {Type: storepb.ObjectSchema_OBJECT,- SemanticType: "bb.default",+ SemanticType: defaultSemanticTypeID,Kind: &storepb.ObjectSchema_StructKind_{StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{@@ -132,7 +132,7 @@ func mustMongoSchema(t *testing.T, schemaName string) *storepb.ObjectSchema {},},},- "email": {Type: storepb.ObjectSchema_STRING, SemanticType: "bb.default"},+ "email": {Type: storepb.ObjectSchema_STRING, SemanticType: defaultSemanticTypeID},"createdAt": {Type: storepb.ObjectSchema_OBJECT,Kind: &storepb.ObjectSchema_StructKind_{@@ -156,7 +156,7 @@ func mustMongoSchema(t *testing.T, schemaName string) *storepb.ObjectSchema {Properties: map[string]*storepb.ObjectSchema{"tags": {Type: storepb.ObjectSchema_ARRAY,- SemanticType: "bb.default",+ SemanticType: defaultSemanticTypeID,Kind: &storepb.ObjectSchema_ArrayKind_{ArrayKind: &storepb.ObjectSchema_ArrayKind{Kind: &storepb.ObjectSchema{Type: storepb.ObjectSchema_STRING},@@ -185,7 +185,7 @@ func mustMongoSchema(t *testing.T, schemaName string) *storepb.ObjectSchema {StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{"name": {Type: storepb.ObjectSchema_STRING},- "phone": {Type: storepb.ObjectSchema_STRING, SemanticType: "bb.default"},+ "phone": {Type: storepb.ObjectSchema_STRING, SemanticType: defaultSemanticTypeID},},},},@@ -214,7 +214,7 @@ func mustMongoSchema(t *testing.T, schemaName string) *storepb.ObjectSchema {Kind: &storepb.ObjectSchema_StructKind_{StructKind: &storepb.ObjectSchema_StructKind{Properties: map[string]*storepb.ObjectSchema{- "total": {Type: storepb.ObjectSchema_NUMBER, SemanticType: "bb.default"},+ "total": {Type: storepb.ObjectSchema_NUMBER, SemanticType: defaultSemanticTypeID},"status": {Type: storepb.ObjectSchema_STRING},},},@@ -263,7 +263,7 @@ func TestMaskMongoDBDocumentString(t *testing.T) {t.Parallel()td := loadMongoDBMaskingTestData(t)maskers := map[string]masker.Masker{- "bb.default": masker.NewDefaultFullMasker(),+ defaultSemanticTypeID: masker.NewDefaultFullMasker(),}for _, tc := range td.MaskDocument {diff --git a/backend/api/v1/database_catalog_service.go b/backend/api/v1/database_catalog_service.goindex 2f50ec0191..57f0820326 100644--- a/backend/api/v1/database_catalog_service.go+++ b/backend/api/v1/database_catalog_service.go@@ -120,7 +120,7 @@ func (s *DatabaseCatalogService) UpdateDatabaseCatalog(ctx context.Context, reqdatabaseConfig := convertDatabaseCatalog(req.Msg.GetCatalog())- semanticTypesSetting, err := s.store.GetSemanticTypesSetting(ctx, common.GetWorkspaceIDFromContext(ctx))+ semanticTypesSetting, err := getSemanticTypesSettingWithBuiltins(ctx, s.store)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get semantic types setting"))}diff --git a/backend/api/v1/database_catalog_service_test.go b/backend/api/v1/database_catalog_service_test.goindex b83a73f37a..4e4651bed6 100644--- a/backend/api/v1/database_catalog_service_test.go+++ b/backend/api/v1/database_catalog_service_test.go@@ -109,6 +109,22 @@ func TestValidateCatalogSemanticTypeIDs(t *testing.T) {Types: []*storepb.SemanticTypeSetting_SemanticType{{Id: "email"}},},},+ {+ name: "built-in semantic types pass without workspace configuration",+ config: &storepb.DatabaseConfig{+ Schemas: []*storepb.SchemaCatalog{{+ Name: "public",+ Tables: []*storepb.TableCatalog{{+ Name: "users",+ Columns: []*storepb.ColumnCatalog{+ {Name: "email", SemanticType: defaultSemanticTypeID},+ {Name: "phone", SemanticType: defaultPartialSemanticTypeID},+ },+ }},+ }},+ },+ setting: &storepb.SemanticTypeSetting{Types: getBuiltinSemanticTypes()},+ },{name: "unknown column semantic type rejected",config: &storepb.DatabaseConfig{diff --git a/backend/api/v1/masking_evaluator.go b/backend/api/v1/masking_evaluator.goindex 35d8e3f721..3c4f529c97 100644--- a/backend/api/v1/masking_evaluator.go+++ b/backend/api/v1/masking_evaluator.go@@ -93,8 +93,8 @@ func (m *maskingLevelEvaluator) evaluateSemanticTypeOfColumn(// Check column-level semantic typesemanticTypeID := columnConfig.GetSemanticType()if semanticTypeID != "" {- semanticType, ok := m.semanticTypesMap[semanticTypeID]- if ok {+ semanticType := m.semanticTypesMap[semanticTypeID]+ if semanticType != nil {context := ""if schemaName != "" {context = fmt.Sprintf("Column-level semantic type: %s.%s.%s.%s.%s", databaseMessage.InstanceID, databaseMessage.DatabaseName, schemaName, tableName, columnName)@@ -104,8 +104,8 @@ func (m *maskingLevelEvaluator) evaluateSemanticTypeOfColumn(algorithmName := getAlgorithmNameFromSemanticType(semanticType)eval = &MaskingEvaluation{SemanticTypeID: semanticTypeID,- SemanticTypeTitle: semanticType.Title,- SemanticTypeIcon: semanticType.Icon,+ SemanticTypeTitle: semanticType.GetTitle(),+ SemanticTypeIcon: semanticType.GetIcon(),Algorithm: algorithmName,Context: context,}diff --git a/backend/api/v1/masking_evaluator_test.go b/backend/api/v1/masking_evaluator_test.goindex 72a2a732c3..853ab438c3 100644--- a/backend/api/v1/masking_evaluator_test.go+++ b/backend/api/v1/masking_evaluator_test.go@@ -55,6 +55,24 @@ func TestEvalMaskingLevelOfColumn(t *testing.T) {}defaultSemanticType := &storepb.SemanticTypeSetting{Types: []*storepb.SemanticTypeSetting_SemanticType{+ {+ Id: defaultSemanticTypeID,+ Title: "Default",+ Algorithm: &storepb.Algorithm{+ Mask: &storepb.Algorithm_FullMask_{+ FullMask: &storepb.Algorithm_FullMask{},+ },+ },+ },+ {+ Id: defaultPartialSemanticTypeID,+ Title: "Default Partial",+ Algorithm: &storepb.Algorithm{+ Mask: &storepb.Algorithm_RangeMask_{+ RangeMask: &storepb.Algorithm_RangeMask{},+ },+ },+ },{Id: "default",Algorithm: fullAlgorithm,@@ -79,6 +97,7 @@ func TestEvalMaskingLevelOfColumn(t *testing.T) {dataClassification *storepb.DataClassificationSettingwant string+ wantTitle stringwantAlgorithm stringwantContext stringwantClassLevel int32@@ -109,6 +128,29 @@ func TestEvalMaskingLevelOfColumn(t *testing.T) {wantAlgorithm: "Full mask",wantClassLevel: 2,},+ {+ description: "Follow The Global Masking Rule With Built-in Semantic Type",+ databaseMessage: defaultDatabaseMessage,+ schemaName: "hiring",+ tableName: "employees",+ columnName: "salary",+ columnCatalog: &storepb.ColumnCatalog{},+ maskingRulePolicy: &storepb.MaskingRulePolicy{+ Rules: []*storepb.MaskingRulePolicy_MaskingRule{+ {+ Condition: &expr.Expr{Expression: "true"},+ SemanticType: defaultSemanticTypeID,+ },+ },+ },+ filteredMaskingExemptions: []*storepb.MaskingExemptionPolicy_Exemption{},+ dataClassification: defaultClassification,+ databaseProjectDatabaseClassificationID: defaultProjectDatabaseDataClassificationID,++ want: defaultSemanticTypeID,+ wantTitle: "Default",+ wantAlgorithm: "Full mask",+ },{description: "Respect The Exception",databaseMessage: defaultDatabaseMessage,@@ -218,6 +260,40 @@ func TestEvalMaskingLevelOfColumn(t *testing.T) {want: "salary-amount",wantAlgorithm: "Hash (MD5)",},+ {+ description: "Built-in Full Mask Column Catalog",+ databaseMessage: defaultDatabaseMessage,+ schemaName: "hiring",+ tableName: "employees",+ columnName: "salary",+ columnCatalog: &storepb.ColumnCatalog{+ SemanticType: defaultSemanticTypeID,+ },+ maskingRulePolicy: &storepb.MaskingRulePolicy{},+ dataClassification: defaultClassification,+ databaseProjectDatabaseClassificationID: defaultProjectDatabaseDataClassificationID,++ want: defaultSemanticTypeID,+ wantTitle: "Default",+ wantAlgorithm: "Full mask",+ },+ {+ description: "Built-in Partial Mask Column Catalog",+ databaseMessage: defaultDatabaseMessage,+ schemaName: "hiring",+ tableName: "employees",+ columnName: "salary",+ columnCatalog: &storepb.ColumnCatalog{+ SemanticType: defaultPartialSemanticTypeID,+ },+ maskingRulePolicy: &storepb.MaskingRulePolicy{},+ dataClassification: defaultClassification,+ databaseProjectDatabaseClassificationID: defaultProjectDatabaseDataClassificationID,++ want: defaultPartialSemanticTypeID,+ wantTitle: "Default Partial",+ wantAlgorithm: "Partial mask",+ },}a := require.New(t)@@ -231,6 +307,9 @@ func TestEvalMaskingLevelOfColumn(t *testing.T) {} else {a.NotNil(result, tc.description)a.Equal(tc.want, result.SemanticTypeID, tc.description)+ if tc.wantTitle != "" {+ a.Equal(tc.wantTitle, result.SemanticTypeTitle, tc.description)+ }if tc.wantAlgorithm != "" {a.Equal(tc.wantAlgorithm, result.Algorithm, tc.description)}diff --git a/backend/api/v1/predicate_columns_check.go b/backend/api/v1/predicate_columns_check.goindex 44a765104b..27672dc6f1 100644--- a/backend/api/v1/predicate_columns_check.go+++ b/backend/api/v1/predicate_columns_check.go@@ -24,7 +24,7 @@ func (s *QueryResultMasker) ExtractSensitivePredicateColumns(ctx context.Contextreturn nil, errors.Wrapf(err, "failed to find masking rule policy")}- semanticTypesSetting, err := s.store.GetSemanticTypesSetting(ctx, common.GetWorkspaceIDFromContext(ctx))+ semanticTypesSetting, err := getSemanticTypesSettingWithBuiltins(ctx, s.store)if err != nil {return nil, errors.Wrapf(err, "failed to find semantic types setting")}diff --git a/backend/api/v1/query_result_masker.go b/backend/api/v1/query_result_masker.goindex f59f2b91a5..a9718c21ea 100644--- a/backend/api/v1/query_result_masker.go+++ b/backend/api/v1/query_result_masker.go@@ -39,7 +39,7 @@ func (s *QueryResultMasker) MaskResults(ctx context.Context, spans []*parserbasereturn errors.Wrapf(err, "failed to find masking rule policy")}- semanticTypesSetting, err := s.store.GetSemanticTypesSetting(ctx, maskerWorkspaceID)+ semanticTypesSetting, err := getSemanticTypesSettingWithBuiltins(ctx, s.store)if err != nil {return errors.Wrapf(err, "failed to find semantic types setting")}@@ -149,22 +149,23 @@ func getAlgorithmName(m masker.Masker) string {}func buildSemanticTypeToMaskerMap(ctx context.Context, stores *store.Store) (map[string]masker.Masker, error) {- semanticTypeToMasker := map[string]masker.Masker{- "bb.default": masker.NewDefaultFullMasker(),- "bb.default-partial": masker.NewDefaultRangeMasker(),- }- semanticTypesSetting, err := stores.GetSemanticTypesSetting(ctx, common.GetWorkspaceIDFromContext(ctx))+ semanticTypesSetting, err := getSemanticTypesSettingWithBuiltins(ctx, stores)if err != nil {return nil, errors.Wrap(err, "failed to get semantic types setting")}+ semanticTypeToMasker := make(map[string]masker.Masker)for _, semanticType := range semanticTypesSetting.GetTypes() {- if semanticType.GetId() == "bb.default" || semanticType.GetId() == "bb.default-partial" {- // Skip the built-in default semantic types.- continue- }- m, err := getMaskerByMaskingAlgorithmAndLevel(semanticType.GetAlgorithm())- if err != nil {- return nil, err+ var m masker.Masker+ switch semanticType.GetId() {+ case defaultSemanticTypeID:+ m = masker.NewDefaultFullMasker()+ case defaultPartialSemanticTypeID:+ m = masker.NewDefaultRangeMasker()+ default:+ m, err = getMaskerByMaskingAlgorithmAndLevel(semanticType.GetAlgorithm())+ if err != nil {+ return nil, err+ }}// Only add semantic types that have actual masking configured (not NoneMasker)if _, isNoneMasker := m.(*masker.NoneMasker); !isNoneMasker {diff --git a/backend/api/v1/semantic_type.go b/backend/api/v1/semantic_type.gonew file mode 100644index 0000000000..d707dc26c3--- /dev/null+++ b/backend/api/v1/semantic_type.go@@ -0,0 +1,60 @@+package v1++import (+ "context"++ "github.com/bytebase/bytebase/backend/common"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store"+)++const (+ defaultSemanticTypeID = "bb.default"+ defaultPartialSemanticTypeID = "bb.default-partial"+)++func getBuiltinSemanticTypes() []*storepb.SemanticTypeSetting_SemanticType {+ return []*storepb.SemanticTypeSetting_SemanticType{+ {+ Id: defaultSemanticTypeID,+ Title: "Default",+ Algorithm: &storepb.Algorithm{+ Mask: &storepb.Algorithm_FullMask_{+ FullMask: &storepb.Algorithm_FullMask{},+ },+ },+ },+ {+ Id: defaultPartialSemanticTypeID,+ Title: "Default Partial",+ Algorithm: &storepb.Algorithm{+ Mask: &storepb.Algorithm_RangeMask_{+ RangeMask: &storepb.Algorithm_RangeMask{},+ },+ },+ },+ }+}++func getSemanticTypesSettingWithBuiltins(ctx context.Context, stores *store.Store) (*storepb.SemanticTypeSetting, error) {+ setting, err := stores.GetSemanticTypesSetting(ctx, common.GetWorkspaceIDFromContext(ctx))+ if err != nil {+ return nil, err+ }+ return appendBuiltinSemanticTypes(setting), nil+}++func appendBuiltinSemanticTypes(setting *storepb.SemanticTypeSetting) *storepb.SemanticTypeSetting {+ types := make([]*storepb.SemanticTypeSetting_SemanticType, 0, len(setting.GetTypes())+2)+ for _, semanticType := range setting.GetTypes() {+ if !isBuiltinSemanticTypeID(semanticType.GetId()) {+ types = append(types, semanticType)+ }+ }+ types = append(types, getBuiltinSemanticTypes()...)+ return &storepb.SemanticTypeSetting{Types: types}+}++func isBuiltinSemanticTypeID(id string) bool {+ return id == defaultSemanticTypeID || id == defaultPartialSemanticTypeID+}diff --git a/backend/api/v1/semantic_type_test.go b/backend/api/v1/semantic_type_test.gonew file mode 100644index 0000000000..6893305bcd--- /dev/null+++ b/backend/api/v1/semantic_type_test.go@@ -0,0 +1,52 @@+package v1++import (+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+)++func TestIsBuiltinSemanticTypeID(t *testing.T) {+ tests := []struct {+ id string+ want bool+ }{+ {id: defaultSemanticTypeID, want: true},+ {id: defaultPartialSemanticTypeID, want: true},+ {id: "email", want: false},+ {id: "", want: false},+ }++ for _, test := range tests {+ t.Run(test.id, func(t *testing.T) {+ if got := isBuiltinSemanticTypeID(test.id); got != test.want {+ t.Fatalf("isBuiltinSemanticTypeID(%q) = %v, want %v", test.id, got, test.want)+ }+ })+ }+}++func TestAppendBuiltinSemanticTypes(t *testing.T) {+ configured := &storepb.SemanticTypeSetting{+ Types: []*storepb.SemanticTypeSetting_SemanticType{+ {Id: "email", Title: "Email"},+ {Id: defaultSemanticTypeID, Title: "Stored default"},+ },+ }++ got := appendBuiltinSemanticTypes(configured)++ require.Equal(t, []string{"email", defaultSemanticTypeID, defaultPartialSemanticTypeID}, semanticTypeIDs(got))+ require.Equal(t, "Default", got.Types[1].Title)+ require.Equal(t, []string{"email", defaultSemanticTypeID}, semanticTypeIDs(configured))+}++func semanticTypeIDs(setting *storepb.SemanticTypeSetting) []string {+ ids := make([]string, 0, len(setting.GetTypes()))+ for _, semanticType := range setting.GetTypes() {+ ids = append(ids, semanticType.GetId())+ }+ return ids+}diff --git a/frontend/scripts/ui-guideline-legacy-debt.json b/frontend/scripts/ui-guideline-legacy-debt.jsonindex 44f71d317d..979574abe7 100644--- a/frontend/scripts/ui-guideline-legacy-debt.json+++ b/frontend/scripts/ui-guideline-legacy-debt.json@@ -1651,53 +1651,41 @@"count": 1},{- "path": "src/routes/project/database-detail/overview/TableDetailDialog.tsx",- "rule": "no-native-control",- "token": "button",- "count": 3- },- {- "path": "src/routes/project/database-detail/overview/TableDetailDialog.tsx",+ "path": "src/routes/project/database-detail/overview/TableDetailSheet.tsx","rule": "no-raw-color","token": "bg-amber-500","count": 1},{- "path": "src/routes/project/database-detail/overview/TableDetailDialog.tsx",+ "path": "src/routes/project/database-detail/overview/TableDetailSheet.tsx","rule": "no-raw-color","token": "bg-green-200","count": 1},{- "path": "src/routes/project/database-detail/overview/TableDetailDialog.tsx",+ "path": "src/routes/project/database-detail/overview/TableDetailSheet.tsx","rule": "no-raw-color","token": "bg-orange-300","count": 1},{- "path": "src/routes/project/database-detail/overview/TableDetailDialog.tsx",+ "path": "src/routes/project/database-detail/overview/TableDetailSheet.tsx","rule": "no-raw-color","token": "bg-red-500","count": 1},{- "path": "src/routes/project/database-detail/overview/TableDetailDialog.tsx",+ "path": "src/routes/project/database-detail/overview/TableDetailSheet.tsx","rule": "no-raw-color","token": "bg-yellow-200","count": 1},{- "path": "src/routes/project/database-detail/overview/TableDetailDialog.tsx",+ "path": "src/routes/project/database-detail/overview/TableDetailSheet.tsx","rule": "no-raw-table","token": "table","count": 2},- {- "path": "src/routes/project/database-detail/panels/DatabaseCatalogPanel.tsx",- "rule": "no-raw-color",- "token": "text-white",- "count": 1- },{"path": "src/routes/project/database-detail/revision/ImportRevisionSheet.tsx","rule": "no-ad-hoc-sheet-width",diff --git a/frontend/src/hooks/useSemanticTypes.test.tsx b/frontend/src/hooks/useSemanticTypes.test.tsxnew file mode 100644index 0000000000..0603b2327a--- /dev/null+++ b/frontend/src/hooks/useSemanticTypes.test.tsx@@ -0,0 +1,109 @@+import { act } from "react";+import { createRoot } from "react-dom/client";+import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";+import i18n from "@/lib/i18n";+import { useSemanticTypes } from "./useSemanticTypes";++(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT =+ true;++const mocks = vi.hoisted(() => ({+ setting: undefined as unknown,+}));++vi.mock("@/stores/app", () => ({+ useAppStore: (+ selector: (state: { getSettingByName: () => unknown }) => unknown+ ) => selector({ getSettingByName: () => mocks.setting }),+}));++function SemanticTypeList() {+ const { configuredSemanticTypes, semanticTypes } = useSemanticTypes();+ return (+ <div+ data-configured={configuredSemanticTypes.map(({ id }) => id).join(",")}+ data-expanded={semanticTypes.map(({ id }) => id).join(",")}+ data-first-title={semanticTypes[0]?.title}+ />+ );+}++let container: HTMLDivElement;+let root: ReturnType<typeof createRoot>;++beforeEach(() => {+ container = document.createElement("div");+ root = createRoot(container);+});++afterEach(async () => {+ act(() => root.unmount());+ await i18n.changeLanguage("en-US");+});++describe("useSemanticTypes", () => {+ test("puts built-ins before configured types without duplicates", () => {+ mocks.setting = {+ value: {+ value: {+ case: "semanticType",+ value: {+ types: [+ { id: "email", title: "Email" },+ { id: "bb.default", title: "Stored default" },+ ],+ },+ },+ },+ };++ act(() => root.render(<SemanticTypeList />));++ const result = container.firstElementChild;+ expect(result?.getAttribute("data-configured")).toBe(+ "email,bb.default"+ );+ expect(result?.getAttribute("data-expanded")).toBe(+ "bb.default,bb.default-partial,email"+ );+ });++ test("updates when the cached setting changes", () => {+ mocks.setting = undefined;+ act(() => root.render(<SemanticTypeList />));+ expect(container.firstElementChild?.getAttribute("data-expanded")).toBe(+ "bb.default,bb.default-partial"+ );++ mocks.setting = {+ value: {+ value: {+ case: "semanticType",+ value: { types: [{ id: "phone", title: "Phone" }] },+ },+ },+ };+ act(() => root.render(<SemanticTypeList />));++ expect(container.firstElementChild?.getAttribute("data-expanded")).toBe(+ "bb.default,bb.default-partial,phone"+ );+ });++ test("updates built-in labels when the locale changes", async () => {+ await act(async () => {+ await i18n.changeLanguage("en-US");+ root.render(<SemanticTypeList />);+ });+ expect(+ container.firstElementChild?.getAttribute("data-first-title")+ ).toBe("Default");++ await act(async () => {+ await i18n.changeLanguage("zh-CN");+ });+ expect(+ container.firstElementChild?.getAttribute("data-first-title")+ ).toBe("全遮掩");+ });+});diff --git a/frontend/src/hooks/useSemanticTypes.ts b/frontend/src/hooks/useSemanticTypes.tsnew file mode 100644index 0000000000..f0857a5c0b--- /dev/null+++ b/frontend/src/hooks/useSemanticTypes.ts@@ -0,0 +1,30 @@+import { useMemo } from "react";+import { useTranslation } from "react-i18next";+import { useAppStore } from "@/stores/app";+import type { SemanticTypeSetting_SemanticType } from "@/types/proto-es/v1/setting_service_pb";+import { Setting_SettingName } from "@/types/proto-es/v1/setting_service_pb";+import { getSemanticTypeListWithBuiltins } from "@/types/semanticTypes";++export const useSemanticTypes = (): {+ configuredSemanticTypes: SemanticTypeSetting_SemanticType[];+ semanticTypes: SemanticTypeSetting_SemanticType[];+} => {+ const { t } = useTranslation();+ const setting = useAppStore((state) =>+ state.getSettingByName(Setting_SettingName.SEMANTIC_TYPES)+ );++ return useMemo(() => {+ const configuredSemanticTypes =+ setting?.value?.value.case === "semanticType"+ ? setting.value.value.value.types+ : [];+ return {+ configuredSemanticTypes,+ semanticTypes: getSemanticTypeListWithBuiltins(+ configuredSemanticTypes,+ t+ ),+ };+ }, [setting, t]);+};diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 7979dfccaa..0533c77b38 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -2369,8 +2369,7 @@},"schema-template": {"classification": {- "search": "Search classification",- "select": "Select classification"+ "search": "Search classification"}},"sensitive-data": {@@ -2935,9 +2934,14 @@"re-order": "Re-order"},"grant-access": "Grant Access",+ "mark-sensitive-data": "Mark sensitive data","never-expires": "Never expires","remove-sensitive-column-tips": "Expose this column?","semantic-types": {+ "error": {+ "no-changes-to-save": "No changes to save",+ "title-required": "Title is required"+ },"label": "Columns having the same semantic type will use the same masking algorithms.","select": "Select semantic type","self": "Semantic Types",@@ -2946,13 +2950,9 @@"description": "Description","icon": "Icon","masking-algorithm": "Masking Algorithm",- "semantic-type": "Semantic Type"- },- "template": {- "description": "You can apply built-in semantic types from the template. Built-in template cannot be changed.",- "duplicate-warning": "Semantic type \"{{title}}\" already exists"- },- "use-predefined-type": "Use Predefined Type"+ "semantic-type": "Semantic Type",+ "title": "Title"+ }}},"sidebar": {diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex 3a3cbb6493..cc258c405e 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -2369,8 +2369,7 @@},"schema-template": {"classification": {- "search": "Clasificación de búsqueda",- "select": "Seleccionar clasificación"+ "search": "Clasificación de búsqueda"}},"sensitive-data": {@@ -2935,9 +2934,14 @@"re-order": "Reordenar"},"grant-access": "Conceder acceso",+ "mark-sensitive-data": "Marcar datos confidenciales","never-expires": "Nunca expira","remove-sensitive-column-tips": "¿Exponer esta columna?","semantic-types": {+ "error": {+ "no-changes-to-save": "No hay cambios para guardar",+ "title-required": "El título es obligatorio"+ },"label": "Las columnas que tengan el mismo tipo semántico utilizarán los mismos algoritmos de enmascaramiento.","select": "Seleccionar tipo semántico","self": "Tipos Semánticos",@@ -2946,13 +2950,9 @@"description": "Descripción","icon": "Icono","masking-algorithm": "Algoritmo de enmascaramiento",- "semantic-type": "Tipo Semántico"- },- "template": {- "description": "Puede aplicar tipos semánticos integrados desde la plantilla. La plantilla integrada no se puede modificar.",- "duplicate-warning": "El tipo semántico \"{{title}}\" ya existe"- },- "use-predefined-type": "Utiliza el Tipo Predefinido"+ "semantic-type": "Tipo Semántico",+ "title": "Título"+ }}},"sidebar": {diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex a707c84659..418fee1f31 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -2369,8 +2369,7 @@},"schema-template": {"classification": {- "search": "カテゴリを検索する",- "select": "分類を選択"+ "search": "カテゴリを検索する"}},"sensitive-data": {@@ -2935,9 +2934,14 @@"re-order": "並び替え"},"grant-access": "アクセスを許可する",+ "mark-sensitive-data": "機密データをマークする","never-expires": "有効期限なし","remove-sensitive-column-tips": "このデータ列を表示しますか?","semantic-types": {+ "error": {+ "no-changes-to-save": "保存する変更はありません",+ "title-required": "タイトルは必須です"+ },"label": "同じセマンティック タイプの列は、同じマスキング アルゴリズムを使用します。","select": "セマンティックタイプの選択","self": "セマンティックタイプ",@@ -2946,13 +2950,9 @@"description": "説明","icon": "アイコン","masking-algorithm": "マスキングアルゴリズム",- "semantic-type": "セマンティックタイプ"- },- "template": {- "description": "テンプレートから組み込みのセマンティック タイプを適用できます。組み込みテンプレートは変更できません。",- "duplicate-warning": "セマンティック タイプ \"{{title}}\" は既に存在します"- },- "use-predefined-type": "事前定義されたタイプを使用する"+ "semantic-type": "セマンティックタイプ",+ "title": "タイトル"+ }}},"sidebar": {diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.jsonindex d929a33f27..d914adbaa8 100644--- a/frontend/src/locales/vi-VN.json+++ b/frontend/src/locales/vi-VN.json@@ -2369,8 +2369,7 @@},"schema-template": {"classification": {- "search": "Tìm kiếm phân loại",- "select": "Chọn phân loại"+ "search": "Tìm kiếm phân loại"}},"sensitive-data": {@@ -2935,9 +2934,14 @@"re-order": "Sắp xếp lại"},"grant-access": "Cấp Quyền truy cập",+ "mark-sensitive-data": "Đánh dấu dữ liệu nhạy cảm","never-expires": "Không bao giờ hết hạn","remove-sensitive-column-tips": "Hiển thị cột này?","semantic-types": {+ "error": {+ "no-changes-to-save": "Không có thay đổi để lưu",+ "title-required": "Tiêu đề là bắt buộc"+ },"label": "Các cột có cùng loại ngữ nghĩa sẽ sử dụng cùng một thuật toán che.","select": "Chọn loại ngữ nghĩa","self": "Loại ngữ nghĩa",@@ -2946,13 +2950,9 @@"description": "Mô tả","icon": "Biểu tượng","masking-algorithm": "Thuật toán che",- "semantic-type": "Loại ngữ nghĩa"- },- "template": {- "description": "Bạn có thể áp dụng các loại ngữ nghĩa tích hợp từ mẫu. Mẫu tích hợp không thể thay đổi.",- "duplicate-warning": "Loại ngữ nghĩa \"{{title}}\" đã tồn tại"- },- "use-predefined-type": "Sử dụng Loại định nghĩa trước"+ "semantic-type": "Loại ngữ nghĩa",+ "title": "Tiêu đề"+ }}},"sidebar": {diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex 02c4f24e58..d5b7a73965 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -2369,8 +2369,7 @@},"schema-template": {"classification": {- "search": "搜索分类",- "select": "选择分类"+ "search": "搜索分类"}},"sensitive-data": {@@ -2935,9 +2934,14 @@"re-order": "排序"},"grant-access": "授予访问权限",+ "mark-sensitive-data": "标记敏感数据","never-expires": "无过期时间","remove-sensitive-column-tips": "不再对此列数据脱敏?","semantic-types": {+ "error": {+ "no-changes-to-save": "没有需要保存的更改",+ "title-required": "标题为必填项"+ },"label": "具有相同语义类型的列会使用相同的脱敏算法。","select": "选择语义类型","self": "语义类型",@@ -2946,13 +2950,9 @@"description": "描述","icon": "图标","masking-algorithm": "脱敏算法",- "semantic-type": "语义类型"- },- "template": {- "description": "您可以从模板中应用内置语义类型。内置模板无法更改。",- "duplicate-warning": "语义类型“{{title}}”已存在"- },- "use-predefined-type": "使用预定义类型"+ "semantic-type": "语义类型",+ "title": "标题"+ }}},"sidebar": {diff --git a/frontend/src/routes/project/database-detail/catalog/MarkSensitiveDataSheet.tsx b/frontend/src/routes/project/database-detail/catalog/MarkSensitiveDataSheet.tsxnew file mode 100644index 0000000000..9d8e23fff3--- /dev/null+++ b/frontend/src/routes/project/database-detail/catalog/MarkSensitiveDataSheet.tsx@@ -0,0 +1,244 @@+import { useEffect, useState } from "react";+import { useTranslation } from "react-i18next";+import { Button } from "@/components/ui/button";+import { FormField, FormFieldGroup, FormLabel } from "@/components/ui/form";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";+import {+ Sheet,+ SheetBody,+ SheetContent,+ SheetDescription,+ SheetFooter,+ SheetHeader,+ SheetTitle,+} from "@/components/ui/sheet";+import { useAppDatabaseMetadata } from "@/hooks/useAppDatabaseMetadata";+import { updateColumnCatalog } from "@/lib/column-data-table/utils";+import type { Database } from "@/types/proto-es/v1/database_service_pb";+import type { SemanticTypeSetting_SemanticType } from "@/types/proto-es/v1/setting_service_pb";++interface MarkSensitiveDataSheetProps {+ database: Database;+ open: boolean;+ semanticTypeList: SemanticTypeSetting_SemanticType[];+ onOpenChange: (open: boolean) => void;+}++export function MarkSensitiveDataSheet({+ database,+ open,+ semanticTypeList,+ onOpenChange,+}: MarkSensitiveDataSheetProps) {+ const { t } = useTranslation();+ const metadata = useAppDatabaseMetadata(database.name, { autoFetch: open });+ const [schemaIndex, setSchemaIndex] = useState("");+ const [tableIndex, setTableIndex] = useState("");+ const [columnName, setColumnName] = useState("");+ const [semanticTypeId, setSemanticTypeId] = useState("bb.default");+ const [saving, setSaving] = useState(false);++ const selectedSchema =+ schemaIndex === "" ? undefined : metadata.schemas[Number(schemaIndex)];+ const selectedTable =+ tableIndex === "" ? undefined : selectedSchema?.tables[Number(tableIndex)];+ const canSave =+ !!selectedSchema &&+ !!selectedTable &&+ !!columnName &&+ !!semanticTypeId &&+ !saving;++ useEffect(() => {+ if (open) {+ return;+ }+ setSchemaIndex("");+ setTableIndex("");+ setColumnName("");+ setSemanticTypeId("bb.default");+ setSaving(false);+ }, [database.name, open]);++ const handleSave = async () => {+ if (!canSave || !selectedSchema || !selectedTable) {+ return;+ }++ setSaving(true);+ try {+ await updateColumnCatalog({+ database: database.name,+ schema: selectedSchema.name,+ table: selectedTable.name,+ column: columnName,+ columnCatalog: {+ semanticType: semanticTypeId,+ },+ notification: "common.updated",+ });+ onOpenChange(false);+ } finally {+ setSaving(false);+ }+ };++ return (+ <Sheet open={open} onOpenChange={onOpenChange}>+ <SheetContent width="standard">+ <SheetHeader>+ <SheetTitle>+ {t("settings.sensitive-data.mark-sensitive-data")}+ </SheetTitle>+ <SheetDescription>+ {t("settings.sensitive-data.semantic-types.label")}+ </SheetDescription>+ </SheetHeader>+ <SheetBody>+ <FormFieldGroup>+ <FormField>+ <FormLabel htmlFor="mark-sensitive-data-schema">+ {t("common.schema")}+ </FormLabel>+ <Select+ value={schemaIndex}+ onValueChange={(value) => {+ setSchemaIndex(value ?? "");+ setTableIndex("");+ setColumnName("");+ }}+ >+ <SelectTrigger+ id="mark-sensitive-data-schema"+ className="w-full"+ >+ <SelectValue>+ {selectedSchema+ ? selectedSchema.name || t("database.schema.unspecified")+ : t("common.select")}+ </SelectValue>+ </SelectTrigger>+ <SelectContent>+ {metadata.schemas.map((schema, index) => (+ <SelectItem+ key={`${schema.name}-${index}`}+ value={`${index}`}+ >+ {schema.name || t("database.schema.unspecified")}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </FormField>++ <FormField>+ <FormLabel htmlFor="mark-sensitive-data-table">+ {t("common.table")}+ </FormLabel>+ <Select+ value={tableIndex}+ disabled={!selectedSchema}+ onValueChange={(value) => {+ setTableIndex(value ?? "");+ setColumnName("");+ }}+ >+ <SelectTrigger+ id="mark-sensitive-data-table"+ className="w-full"+ >+ <SelectValue>+ {selectedTable?.name || t("common.select")}+ </SelectValue>+ </SelectTrigger>+ <SelectContent>+ {selectedSchema?.tables.map((table, index) => (+ <SelectItem+ key={`${table.name}-${index}`}+ value={`${index}`}+ >+ {table.name}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </FormField>++ <FormField>+ <FormLabel htmlFor="mark-sensitive-data-column">+ {t("common.column")}+ </FormLabel>+ <Select+ value={columnName}+ disabled={!selectedTable}+ onValueChange={(value) => setColumnName(value ?? "")}+ >+ <SelectTrigger+ id="mark-sensitive-data-column"+ className="w-full"+ >+ <SelectValue placeholder={t("common.select")} />+ </SelectTrigger>+ <SelectContent>+ {selectedTable?.columns.map((column) => (+ <SelectItem key={column.name} value={column.name}>+ {column.name}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </FormField>++ <FormField>+ <FormLabel htmlFor="mark-sensitive-data-semantic-type">+ {t(+ "settings.sensitive-data.semantic-types.table.semantic-type"+ )}+ </FormLabel>+ <Select+ value={semanticTypeId}+ onValueChange={(value) => setSemanticTypeId(value ?? "")}+ >+ <SelectTrigger+ id="mark-sensitive-data-semantic-type"+ className="w-full"+ >+ <SelectValue+ placeholder={t(+ "settings.sensitive-data.semantic-types.select"+ )}+ />+ </SelectTrigger>+ <SelectContent>+ {semanticTypeList.map((semanticType) => (+ <SelectItem key={semanticType.id} value={semanticType.id}>+ {semanticType.title || semanticType.id}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </FormField>+ </FormFieldGroup>+ </SheetBody>+ <SheetFooter>+ <Button+ type="button"+ appearance="outline"+ onClick={() => onOpenChange(false)}+ >+ {t("common.cancel")}+ </Button>+ <Button type="button" disabled={!canSave} onClick={handleSave}>+ {t("common.save")}+ </Button>+ </SheetFooter>+ </SheetContent>+ </Sheet>+ );+}diff --git a/frontend/src/routes/project/database-detail/overview/DatabaseObjectExplorer.test.tsx b/frontend/src/routes/project/database-detail/overview/DatabaseObjectExplorer.test.tsxindex d9688aabf0..8fbc7082e7 100644--- a/frontend/src/routes/project/database-detail/overview/DatabaseObjectExplorer.test.tsx+++ b/frontend/src/routes/project/database-detail/overview/DatabaseObjectExplorer.test.tsx@@ -44,7 +44,7 @@ const mocks = vi.hoisted(() => {instanceV1SupportsTrigger: vi.fn(() => false),bytesToString: vi.fn((size: number) => `${size} B`),hasProjectPermissionV2: vi.fn(() => true),- dialogProps: [] as unknown[],+ sheetProps: [] as unknown[],useTranslation: vi.fn(() => ({t: (key: string) => key,})),@@ -138,10 +138,10 @@ vi.mock("@/components/ui/input", () => ({),}));-vi.mock("./TableDetailDialog", () => ({+vi.mock("./TableDetailSheet", () => ({EditableClassificationCell: () => null,- TableDetailDialog: (props: unknown) => {- mocks.dialogProps.push(props);+ TableDetailSheet: (props: unknown) => {+ mocks.sheetProps.push(props);return null;},}));@@ -288,7 +288,7 @@ beforeEach(async () => {mocks.instanceV1SupportsColumn.mockReturnValue(true);mocks.instanceV1SupportsIndex.mockReset();mocks.instanceV1SupportsIndex.mockReturnValue(true);- mocks.dialogProps.length = 0;+ mocks.sheetProps.length = 0;vi.resetModules();({ DatabaseObjectExplorer } = await import("./DatabaseObjectExplorer"));@@ -356,9 +356,9 @@ describe("DatabaseObjectExplorer", () => {clickElement(ordersRow as HTMLTableRowElement);await flush();- const latestDialogProps = mocks.dialogProps.at(-1);- expect(() => JSON.stringify(latestDialogProps)).not.toThrow();- expect(latestDialogProps).toEqual(+ const latestSheetProps = mocks.sheetProps.at(-1);+ expect(() => JSON.stringify(latestSheetProps)).not.toThrow();+ expect(latestSheetProps).toEqual(expect.objectContaining({table: expect.objectContaining({partitions: [diff --git a/frontend/src/routes/project/database-detail/overview/DatabaseObjectExplorer.tsx b/frontend/src/routes/project/database-detail/overview/DatabaseObjectExplorer.tsxindex fc559c4ff0..f0ad32263f 100644--- a/frontend/src/routes/project/database-detail/overview/DatabaseObjectExplorer.tsx+++ b/frontend/src/routes/project/database-detail/overview/DatabaseObjectExplorer.tsx@@ -48,9 +48,9 @@ import {ObjectSectionTable,} from "./ObjectSectionTable";import {- TableDetailDialog,- type TableDetailDialogData,-} from "./TableDetailDialog";+ TableDetailSheet,+ type TableDetailSheetData,+} from "./TableDetailSheet";import { TableMetadataTable } from "./TableMetadataTable";function filterByKeyword(name: string, keyword: string) {@@ -159,7 +159,7 @@ export function DatabaseObjectExplorer({Engine.CASSANDRA,Engine.TRINO,].includes(databaseEngine);- const selectedTableDetail: TableDetailDialogData | undefined = selectedTable+ const selectedTableDetail: TableDetailSheetData | undefined = selectedTable? {database,editable: canUpdateCatalog,@@ -198,7 +198,7 @@ export function DatabaseObjectExplorer({partitions: (selectedTable.partitions ?? []).map(function mapPartition(partition- ): NonNullable<TableDetailDialogData["partitions"]>[number] {+ ): NonNullable<TableDetailSheetData["partitions"]>[number] {return {name: partition.name,type:@@ -491,7 +491,7 @@ export function DatabaseObjectExplorer({</>)}- <TableDetailDialog+ <TableDetailSheetopen={!!selectedTableName}table={selectedTableDetail}onOpenChange={(open) => {diff --git a/frontend/src/routes/project/database-detail/overview/TableDetailDialog.test.tsx b/frontend/src/routes/project/database-detail/overview/TableDetailSheet.test.tsxsimilarity index 79%rename from frontend/src/routes/project/database-detail/overview/TableDetailDialog.test.tsxrename to frontend/src/routes/project/database-detail/overview/TableDetailSheet.test.tsxindex 3d5dd8d7b9..c78b53362a 100644--- a/frontend/src/routes/project/database-detail/overview/TableDetailDialog.test.tsx+++ b/frontend/src/routes/project/database-detail/overview/TableDetailSheet.test.tsx@@ -5,7 +5,7 @@ import type {InputHTMLAttributes,ReactNode,} from "react";-import { act, createElement } from "react";+import { act, createElement, useState } from "react";import { createRoot } from "react-dom/client";import { beforeEach, describe, expect, test, vi } from "vitest";import { Engine } from "@/types/proto-es/v1/common_pb";@@ -14,6 +14,7 @@ import type { Database } from "@/types/proto-es/v1/database_service_pb";import type { DataClassificationSetting_DataClassificationConfig } from "@/types/proto-es/v1/setting_service_pb";import { Setting_SettingName } from "@/types/proto-es/v1/setting_service_pb";import { PlanFeature } from "@/types/proto-es/v1/subscription_service_pb";+import type { TableDetailSheetData } from "./TableDetailSheet";(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }@@ -41,7 +42,7 @@ const mocks = vi.hoisted(() => ({hasWorkspacePermissionV2: vi.fn(),}));-let TableDetailDialog: typeof import("./TableDetailDialog").TableDetailDialog;+let TableDetailSheet: typeof import("./TableDetailSheet").TableDetailSheet;vi.mock("react-i18next", () => ({useTranslation: mocks.useTranslation,@@ -56,6 +57,56 @@ vi.mock("@/components/ui/dialog", () => ({DialogTitle: ({ children }: { children: ReactNode }) => <h1>{children}</h1>,}));+vi.mock("@/components/ui/sheet", () => ({+ Sheet: ({+ open,+ children,+ onOpenChange,+ }: {+ open: boolean;+ children: ReactNode;+ onOpenChange: (open: boolean) => void;+ }) => (+ <div data-testid="sheet-root" data-open={open}>+ <button+ type="button"+ data-testid="sheet-close"+ onClick={() => onOpenChange(false)}+ >+ Close+ </button>+ {children}+ </div>+ ),+ SheetBody: ({ children }: { children: ReactNode }) => <div>{children}</div>,+ SheetContent: ({+ children,+ width,+ }: {+ children: ReactNode;+ width: string;+ }) => <div data-sheet-width={width}>{children}</div>,+ SheetHeader: ({ children }: { children: ReactNode }) => <div>{children}</div>,+ SheetTitle: ({ children }: { children: ReactNode }) => <h1>{children}</h1>,+}));++vi.mock("@/components/ui/popover", () => ({+ Popover: ({ children }: { children: ReactNode }) => (+ <div data-testid="inline-picker">{children}</div>+ ),+ PopoverContent: ({ children }: { children: ReactNode }) => (+ <div>{children}</div>+ ),+ PopoverTrigger: ({+ children,+ ...props+ }: ButtonHTMLAttributes<HTMLButtonElement>) => (+ <button type="button" {...props}>+ {children}+ </button>+ ),+}));+vi.mock("@/components/ui/input", () => ({Input: (props: InputHTMLAttributes<HTMLInputElement>) => <input {...props} />,}));@@ -119,6 +170,18 @@ vi.mock("@/components/FeatureAttention", () => ({),}));+vi.mock("@/types/semanticTypes", () => ({+ getSemanticTypeListWithBuiltins: (+ semanticTypeList: Array<{ id: string; title: string }>+ ) => [+ { id: "bb.default", title: "Default" },+ { id: "bb.default-partial", title: "Default Partial" },+ ...semanticTypeList.filter(+ ({ id }) => id !== "bb.default" && id !== "bb.default-partial"+ ),+ ],+}));+vi.mock("@/stores", () => ({pushNotification: mocks.pushNotification,}));@@ -332,10 +395,91 @@ beforeEach(async () => {mocks.hasWorkspacePermissionV2.mockReturnValue(true);vi.resetModules();- ({ TableDetailDialog } = await import("./TableDetailDialog"));+ ({ TableDetailSheet } = await import("./TableDetailSheet"));});-describe("TableDetailDialog", () => {+describe("TableDetailSheet", () => {+ test("renders table details in a huge sheet instead of a dialog", async () => {+ const { container, render, unmount } = renderIntoContainer(+ createElement(TableDetailSheet, {+ open: true,+ onOpenChange: vi.fn(),+ table: {+ database: makeDatabase(),+ name: '"public"."audit"',+ schema: "public",+ tableName: "audit",+ columns: [],+ rowCount: "0",+ dataSize: "8 KB",+ indexSize: "32 KB",+ indexes: [],+ showIndexes: false,+ },+ })+ );++ render();+ await flush();++ expect(container.querySelector('[data-testid="sheet-root"]')).not.toBeNull();+ expect(container.querySelector('[data-sheet-width="huge"]')).not.toBeNull();+ expect(container.querySelector('[data-testid="dialog-root"]')).toBeNull();++ unmount();+ });++ test("preserves table content while the sheet closes", async () => {+ const table: TableDetailSheetData = {+ database: makeDatabase(),+ name: '"public"."audit"',+ schema: "public",+ tableName: "audit",+ columns: [],+ rowCount: "0",+ dataSize: "8 KB",+ indexSize: "32 KB",+ indexes: [],+ showIndexes: false,+ };++ function Harness() {+ const [open, setOpen] = useState(true);+ const [selectedTable, setSelectedTable] = useState<+ TableDetailSheetData | undefined+ >(table);++ return createElement(TableDetailSheet, {+ open,+ table: selectedTable,+ onOpenChange: (nextOpen) => {+ setOpen(nextOpen);+ if (!nextOpen) {+ setSelectedTable(undefined);+ }+ },+ });+ }++ const { container, render, unmount } = renderIntoContainer(+ createElement(Harness)+ );++ render();+ await flush();+ click(container.querySelector('[data-testid="sheet-close"]') as HTMLElement);+ await flush();++ expect(+ container.querySelector('[data-testid="sheet-root"]')?.getAttribute(+ "data-open"+ )+ ).toBe("false");+ expect(container.textContent).toContain('"public"."audit"');++ unmount();+ });+test("restores the legacy table detail sections for columns and indexes", async () => {const classificationConfig = {id: "classification-config",@@ -350,7 +494,7 @@ describe("TableDetailDialog", () => {} as unknown as DataClassificationSetting_DataClassificationConfig;const { container, render, unmount } = renderIntoContainer(- createElement(TableDetailDialog, {+ createElement(TableDetailSheet, {open: true,onOpenChange: vi.fn(),table: {@@ -453,7 +597,7 @@ describe("TableDetailDialog", () => {} as unknown as DataClassificationSetting_DataClassificationConfig;const { container, render, unmount } = renderIntoContainer(- createElement(TableDetailDialog as unknown as ElementType, {+ createElement(TableDetailSheet as unknown as ElementType, {open: true,onOpenChange: vi.fn(),table: {@@ -563,9 +707,70 @@ describe("TableDetailDialog", () => {unmount();});+ test("offers built-in semantic types when the workspace setting is empty", async () => {+ mocks.getSettingByName.mockReturnValue({+ value: {+ value: {+ case: "semanticType",+ value: { types: [] },+ },+ },+ });++ const { container, render, unmount } = renderIntoContainer(+ createElement(TableDetailSheet as unknown as ElementType, {+ open: true,+ onOpenChange: vi.fn(),+ table: {+ database: makeDatabase(),+ editable: true,+ name: '"public"."audit"',+ schema: "public",+ tableName: "audit",+ columns: [+ {+ name: "email",+ semanticType: "",+ type: "text",+ defaultValue: "No default",+ nullable: true,+ },+ ],+ rowCount: "0",+ dataSize: "8 KB",+ indexSize: "32 KB",+ indexes: [],+ showIndexes: false,+ showSemanticType: true,+ },+ })+ );++ render();+ await flush();++ press(+ container.querySelector(+ '[data-testid="column-semantic-type-email-edit"]'+ ) as HTMLElement+ );+ await flush();++ expect(+ container.querySelector('[data-testid="semantic-type-option-bb-default"]')+ ).not.toBeNull();+ expect(+ container.querySelector(+ '[data-testid="semantic-type-option-bb-default-partial"]'+ )+ ).not.toBeNull();++ unmount();+ });+test("loads classification config from settings when the dialog prop is unavailable", async () => {const { container, render, unmount } = renderIntoContainer(- createElement(TableDetailDialog as unknown as ElementType, {+ createElement(TableDetailSheet as unknown as ElementType, {open: true,onOpenChange: vi.fn(),table: {@@ -631,7 +836,7 @@ describe("TableDetailDialog", () => {test("shows unknown classification ids instead of replacing them with a placeholder", async () => {const { container, render, unmount } = renderIntoContainer(- createElement(TableDetailDialog as unknown as ElementType, {+ createElement(TableDetailSheet as unknown as ElementType, {open: true,onOpenChange: vi.fn(),table: {@@ -659,9 +864,9 @@ describe("TableDetailDialog", () => {unmount();});- test("restores partition and trigger sections in the React table detail dialog", async () => {+ test("restores partition and trigger sections in the React table detail sheet", async () => {const { container, render, unmount } = renderIntoContainer(- createElement(TableDetailDialog as unknown as ElementType, {+ createElement(TableDetailSheet as unknown as ElementType, {open: true,onOpenChange: vi.fn(),table: {@@ -728,7 +933,7 @@ describe("TableDetailDialog", () => {});const { container, render, unmount } = renderIntoContainer(- createElement(TableDetailDialog as unknown as ElementType, {+ createElement(TableDetailSheet as unknown as ElementType, {open: true,onOpenChange: vi.fn(),table: {diff --git a/frontend/src/routes/project/database-detail/overview/TableDetailDialog.tsx b/frontend/src/routes/project/database-detail/overview/TableDetailSheet.tsxsimilarity index 61%rename from frontend/src/routes/project/database-detail/overview/TableDetailDialog.tsxrename to frontend/src/routes/project/database-detail/overview/TableDetailSheet.tsxindex 10714f6684..bdbeed1bd9 100644--- a/frontend/src/routes/project/database-detail/overview/TableDetailDialog.tsx+++ b/frontend/src/routes/project/database-detail/overview/TableDetailSheet.tsx@@ -2,14 +2,26 @@ import { create, fromJsonString, toJsonString } from "@bufbuild/protobuf";import { cloneDeep } from "lodash-es";import { Pencil, X } from "lucide-react";import type { MouseEvent, ReactNode } from "react";-import { useEffect, useMemo, useState } from "react";+import { useEffect, useMemo, useRef, useState } from "react";import { useTranslation } from "react-i18next";import { FeatureAttention } from "@/components/FeatureAttention";import { Button } from "@/components/ui/button";import { Checkbox } from "@/components/ui/checkbox";import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";import { Input } from "@/components/ui/input";+import {+ Popover,+ PopoverContent,+ PopoverTrigger,+} from "@/components/ui/popover";import { SearchInput } from "@/components/ui/search-input";+import {+ Sheet,+ SheetBody,+ SheetContent,+ SheetHeader,+ SheetTitle,+} from "@/components/ui/sheet";import {Table,TableBody,@@ -20,10 +32,12 @@ import {} from "@/components/ui/table";import { Textarea } from "@/components/ui/textarea";import { useDatabaseCatalog } from "@/hooks/useDatabaseCatalog";+import { useSemanticTypes } from "@/hooks/useSemanticTypes";import {updateColumnCatalog,updateTableCatalog,} from "@/lib/column-data-table/utils";+import { cn } from "@/lib/utils";import { pushNotification } from "@/stores";import { useAppStore } from "@/stores/app";import { getTableCatalog } from "@/stores/app/databaseCatalog";@@ -72,7 +86,7 @@ interface TriggerDetail {timing: string;}-export interface TableDetailDialogData {+export interface TableDetailSheetData {classification?: string;classificationConfig?: DataClassificationSetting_DataClassificationConfig;columns: TableColumnDetail[];@@ -292,19 +306,25 @@ function MiniActionButton({onClick: (event: MouseEvent<HTMLButtonElement>) => void;}) {return (- <button+ <Buttontype="button"aria-label={ariaLabel}data-testid={dataTestId}- className={`inline-flex size-5 items-center justify-center rounded-xs text-control transition-colors hover:bg-control-bg hover:text-main disabled:cursor-not-allowed disabled:opacity-50 [&_svg]:pointer-events-none ${className ?? ""}`}+ appearance="secondary"+ size="xs"+ className={cn("w-6 px-0", className)}disabled={disabled}- onKeyDown={(event) => event.stopPropagation()}+ onKeyDown={(event) => {+ if (event.key === "Enter" || event.key === " ") {+ event.stopPropagation();+ }+ }}onMouseDown={(event) => event.stopPropagation()}onPointerDown={(event) => event.stopPropagation()}onClick={onClick}>{children}- </button>+ </Button>);}@@ -489,18 +509,17 @@ function flattenPartitionRows(]);}-function ClassificationPickerDialog({+function ClassificationPickerPopover({classificationConfig,- onOpenChange,onSelect,- open,+ testId,}: {classificationConfig: DataClassificationSetting_DataClassificationConfig;- onOpenChange: (open: boolean) => void;onSelect: (classificationId: string) => void;- open: boolean;+ testId: string;}) {const { t } = useTranslation();+ const [open, setOpen] = useState(false);const [searchText, setSearchText] = useState("");useEffect(() => {@@ -517,16 +536,30 @@ function ClassificationPickerDialog({}, [classificationConfig, searchText]);return (- <Dialog open={open} onOpenChange={onOpenChange}>- <DialogContent className="max-w-2xl p-6">- <DialogTitle>{t("schema-template.classification.select")}</DialogTitle>- <div className="mt-4 flex flex-col gap-y-4">+ <Popover open={open} onOpenChange={setOpen}>+ <PopoverTrigger+ aria-label={t("common.edit")}+ data-testid={testId}+ className="inline-flex size-5 items-center justify-center rounded-xs text-control transition-colors hover:bg-control-bg hover:text-main focus:outline-hidden focus-visible:ring-2 focus-visible:ring-accent [&_svg]:pointer-events-none"+ onKeyDown={(event) => {+ if (event.key === "Enter" || event.key === " ") {+ event.stopPropagation();+ }+ }}+ onMouseDown={(event) => event.stopPropagation()}+ onPointerDown={(event) => event.stopPropagation()}+ onClick={(event) => event.stopPropagation()}+ >+ <Pencil className="size-3" />+ </PopoverTrigger>+ <PopoverContent align="start" className="w-96 p-0">+ <div className="flex flex-col gap-y-2 p-3"><SearchInputplaceholder={t("schema-template.classification.search")}value={searchText}onChange={(event) => setSearchText(event.target.value)}/>- <div className="max-h-[28rem] overflow-y-auto rounded-sm border border-block-border">+ <div className="max-h-80 overflow-y-auto rounded-sm border border-block-border">{rows.length === 0 ? (<div className="px-4 py-6 text-sm text-control-light">{t("common.no-data")}@@ -562,47 +595,43 @@ function ClassificationPickerDialog({}return (- <button+ <Buttonkey={node.id}type="button"+ appearance="secondary"+ size="sm"data-testid={`classification-option-${toTestId(node.id)}`}- className="flex w-full items-center justify-between gap-x-2 px-4 py-2 text-left text-sm text-control hover:bg-control-bg"+ className="h-auto w-full justify-between rounded-none px-4 py-2 text-left text-sm font-normal whitespace-normal"style={{ paddingLeft: `${depth * 16 + 16}px` }}onClick={() => {onSelect(node.id);- onOpenChange(false);+ setOpen(false);}}>{content}- </button>+ </Button>);})}</div>)}</div>- <div className="flex justify-end gap-x-2">- <Button appearance="outline" onClick={() => onOpenChange(false)}>- {t("common.cancel")}- </Button>- </div></div>- </DialogContent>- </Dialog>+ </PopoverContent>+ </Popover>);}-function SemanticTypePickerDialog({- onOpenChange,+function SemanticTypePickerPopover({onSelect,- open,semanticTypeList,+ testId,}: {- onOpenChange: (open: boolean) => void;onSelect: (semanticTypeId: string) => void;- open: boolean;semanticTypeList: SemanticTypeSetting_SemanticType[];+ testId: string;}) {const { t } = useTranslation();+ const [open, setOpen] = useState(false);const [searchText, setSearchText] = useState("");useEffect(() => {@@ -628,18 +657,30 @@ function SemanticTypePickerDialog({}, [searchText, semanticTypeList]);return (- <Dialog open={open} onOpenChange={onOpenChange}>- <DialogContent className="max-w-3xl p-6">- <DialogTitle>- {t("settings.sensitive-data.semantic-types.self")}- </DialogTitle>- <div className="mt-4 flex flex-col gap-y-4">+ <Popover open={open} onOpenChange={setOpen}>+ <PopoverTrigger+ aria-label={t("common.edit")}+ data-testid={testId}+ className="inline-flex size-5 items-center justify-center rounded-xs text-control transition-colors hover:bg-control-bg hover:text-main focus:outline-hidden focus-visible:ring-2 focus-visible:ring-accent [&_svg]:pointer-events-none"+ onKeyDown={(event) => {+ if (event.key === "Enter" || event.key === " ") {+ event.stopPropagation();+ }+ }}+ onMouseDown={(event) => event.stopPropagation()}+ onPointerDown={(event) => event.stopPropagation()}+ onClick={(event) => event.stopPropagation()}+ >+ <Pencil className="size-3" />+ </PopoverTrigger>+ <PopoverContent align="start" className="w-96 p-0">+ <div className="flex flex-col gap-y-2 p-3"><SearchInputplaceholder={t("common.filter-by-name")}value={searchText}onChange={(event) => setSearchText(event.target.value)}/>- <div className="max-h-[28rem] overflow-y-auto rounded-sm border border-block-border">+ <div className="max-h-80 overflow-y-auto rounded-sm border border-block-border">{filteredSemanticTypeList.length === 0 ? (<div className="px-4 py-6 text-sm text-control-light">{t("common.no-data")}@@ -647,14 +688,16 @@ function SemanticTypePickerDialog({) : (<div className="divide-y divide-block-border">{filteredSemanticTypeList.map((semanticType) => (- <button+ <Buttonkey={semanticType.id}type="button"+ appearance="secondary"+ size="sm"data-testid={`semantic-type-option-${toTestId(semanticType.id)}`}- className="flex w-full flex-col items-start gap-y-1 px-4 py-3 text-left hover:bg-control-bg"+ className="h-auto w-full flex-col items-start rounded-none px-4 py-3 text-left font-normal whitespace-normal"onClick={() => {onSelect(semanticType.id);- onOpenChange(false);+ setOpen(false);}}><span className="text-sm font-medium text-main">@@ -668,19 +711,14 @@ function SemanticTypePickerDialog({{semanticType.description}</span>)}- </button>+ </Button>))}</div>)}</div>- <div className="flex justify-end gap-x-2">- <Button appearance="outline" onClick={() => onOpenChange(false)}>- {t("common.cancel")}- </Button>- </div></div>- </DialogContent>- </Dialog>+ </PopoverContent>+ </Popover>);}@@ -698,51 +736,35 @@ export function EditableClassificationCell({testIdPrefix: string;}) {const { t } = useTranslation();- const [showPicker, setShowPicker] = useState(false);return (- <>- <div className="flex min-w-0 items-center gap-x-1">- <ClassificationLevelBadge- classification={classification}- classificationConfig={classificationConfig}- />- {!readonly && classification && (- <MiniActionButton- ariaLabel={t("common.remove")}- dataTestId={`${testIdPrefix}-remove`}- onClick={(event) => {- event.stopPropagation();- void onApply("");- }}- >- <X className="size-3" />- </MiniActionButton>- )}- {!readonly && classificationConfig && (- <MiniActionButton- ariaLabel={t("common.edit")}- dataTestId={`${testIdPrefix}-edit`}- onClick={(event) => {- event.stopPropagation();- setShowPicker(true);- }}- >- <Pencil className="size-3" />- </MiniActionButton>- )}- </div>- {classificationConfig && (- <ClassificationPickerDialog+ <div className="flex min-w-0 items-center gap-x-1">+ <ClassificationLevelBadge+ classification={classification}+ classificationConfig={classificationConfig}+ />+ {!readonly && classification && (+ <MiniActionButton+ ariaLabel={t("common.remove")}+ dataTestId={`${testIdPrefix}-remove`}+ onClick={(event) => {+ event.stopPropagation();+ void onApply("");+ }}+ >+ <X className="size-3" />+ </MiniActionButton>+ )}+ {!readonly && classificationConfig && (+ <ClassificationPickerPopoverclassificationConfig={classificationConfig}- open={showPicker}- onOpenChange={setShowPicker}+ testId={`${testIdPrefix}-edit`}onSelect={(classificationId) => {void onApply(classificationId);}}/>)}- </>+ </div>);}@@ -761,17 +783,8 @@ function EditableSemanticTypeCell({}) {const { t } = useTranslation();const [showFeatureDialog, setShowFeatureDialog] = useState(false);- const [showPicker, setShowPicker] = useState(false);- const semanticTypeSetting = useAppStore((s) =>- s.getSettingByName(Setting_SettingName.SEMANTIC_TYPES)- );- const semanticTypeList = useMemo<SemanticTypeSetting_SemanticType[]>(() => {- return semanticTypeSetting?.value?.value.case === "semanticType"- ? ((semanticTypeSetting.value.value.value.types ??- []) as SemanticTypeSetting_SemanticType[])- : [];- }, [semanticTypeSetting]);+ const { semanticTypes: semanticTypeList } = useSemanticTypes();const hasSensitiveDataFeature = useAppStore((s) =>s.hasFeature(PlanFeature.FEATURE_DATA_MASKING));@@ -813,33 +826,29 @@ function EditableSemanticTypeCell({<X className="size-3" /></MiniActionButton>)}- {!readonly && (+ {!readonly && (!hasSensitiveDataFeature || instanceMissingLicense) && (<MiniActionButtonariaLabel={t("common.edit")}dataTestId={`${testIdPrefix}-edit`}onClick={(event) => {event.stopPropagation();- if (!hasSensitiveDataFeature || instanceMissingLicense) {- setShowFeatureDialog(true);- return;- }- setShowPicker(true);+ setShowFeatureDialog(true);}}><Pencil className="size-3" /></MiniActionButton>)}+ {!readonly && hasSensitiveDataFeature && !instanceMissingLicense && (+ <SemanticTypePickerPopover+ semanticTypeList={semanticTypeList}+ testId={`${testIdPrefix}-edit`}+ onSelect={(nextSemanticTypeId) => {+ void onApply(nextSemanticTypeId);+ }}+ />+ )}</div>- <SemanticTypePickerDialog- open={showPicker}- semanticTypeList={semanticTypeList}- onOpenChange={setShowPicker}- onSelect={(nextSemanticTypeId) => {- void onApply(nextSemanticTypeId);- }}- />-<Dialog open={showFeatureDialog} onOpenChange={setShowFeatureDialog}><DialogContent className="p-6"><DialogTitle>{t("common.warning")}</DialogTitle>@@ -861,12 +870,41 @@ function EditableSemanticTypeCell({);}-export function TableDetailDialog({+export function TableDetailSheet({table,open,onOpenChange,}: {- table?: TableDetailDialogData;+ table?: TableDetailSheetData;+ open: boolean;+ onOpenChange: (open: boolean) => void;+}) {+ const stableTableRef = useRef(table);+ if (table) {+ stableTableRef.current = table;+ }++ const stableTable = stableTableRef.current;+ if (!stableTable) {+ return null;+ }++ return (+ <TableDetailSheetContent+ key={`${stableTable.database?.name ?? ""}.${stableTable.schema ?? ""}.${stableTable.tableName ?? stableTable.name}`}+ table={stableTable}+ open={open}+ onOpenChange={onOpenChange}+ />+ );+}++function TableDetailSheetContent({+ table,+ open,+ onOpenChange,+}: {+ table: TableDetailSheetData;open: boolean;onOpenChange: (open: boolean) => void;}) {@@ -908,10 +946,6 @@ export function TableDetailDialog({.getOrFetchSettingByName(Setting_SettingName.DATA_CLASSIFICATION, true);}, []);- if (!table) {- return null;- }-const showCharacterSetColumn = table.showCharacterSet;const showColumnClassification = table.showColumnClassification;const showColumnCollation = table.showColumnCollation;@@ -987,333 +1021,339 @@ export function TableDetailDialog({};return (- <Dialog open={open} onOpenChange={onOpenChange}>- <DialogContent className="max-w-6xl p-6">- <DialogTitle>{table.name}</DialogTitle>- <div className="mt-4 grid gap-4 sm:grid-cols-3">- <div>- <div className="text-sm font-medium text-control-light">- {t("database.classification.self")}- </div>- <div className="mt-1 text-sm text-main">- <EditableClassificationCell- classification={table.classification}- classificationConfig={classificationConfig}- readonly={readonly}- testIdPrefix="table-classification"- onApply={handleTableClassificationApply}- />- </div>- </div>- {table.showEngine && (+ <Sheet open={open} onOpenChange={onOpenChange}>+ <SheetContent width="huge">+ <SheetHeader>+ <SheetTitle>{table.name}</SheetTitle>+ </SheetHeader>+ <SheetBody>+ <div className="grid gap-4 sm:grid-cols-3"><div><div className="text-sm font-medium text-control-light">- {t("database.engine")}+ {t("database.classification.self")}</div><div className="mt-1 text-sm text-main">- {table.engine || "-"}+ <EditableClassificationCell+ classification={table.classification}+ classificationConfig={classificationConfig}+ readonly={readonly}+ testIdPrefix="table-classification"+ onApply={handleTableClassificationApply}+ /></div></div>- )}- <div>- <div className="text-sm font-medium text-control-light">- {t("database.row-count-estimate")}- </div>- <div className="mt-1 text-sm text-main">{table.rowCount}</div>- </div>- <div>- <div className="text-sm font-medium text-control-light">- {t("database.data-size")}- </div>- <div className="mt-1 text-sm text-main">{table.dataSize}</div>- </div>- {table.showIndexSize && (+ {table.showEngine && (+ <div>+ <div className="text-sm font-medium text-control-light">+ {t("database.engine")}+ </div>+ <div className="mt-1 text-sm text-main">+ {table.engine || "-"}+ </div>+ </div>+ )}<div><div className="text-sm font-medium text-control-light">- {t("database.index-size")}+ {t("database.row-count-estimate")}</div>- <div className="mt-1 text-sm text-main">{table.indexSize}</div>+ <div className="mt-1 text-sm text-main">{table.rowCount}</div></div>- )}- {showSummaryCollation && (<div><div className="text-sm font-medium text-control-light">- {t("db.collation")}- </div>- <div className="mt-1 text-sm text-main">- {table.collation || "-"}+ {t("database.data-size")}</div>+ <div className="mt-1 text-sm text-main">{table.dataSize}</div></div>- )}- </div>+ {table.showIndexSize && (+ <div>+ <div className="text-sm font-medium text-control-light">+ {t("database.index-size")}+ </div>+ <div className="mt-1 text-sm text-main">{table.indexSize}</div>+ </div>+ )}+ {showSummaryCollation && (+ <div>+ <div className="text-sm font-medium text-control-light">+ {t("db.collation")}+ </div>+ <div className="mt-1 text-sm text-main">+ {table.collation || "-"}+ </div>+ </div>+ )}+ </div>- {showPartitionTables && (- <DetailSection title={t("database.partition-tables")}>- <div className="rounded-sm border border-block-border">- <Table className="min-w-full">- <TableHeader className="bg-control-bg">- <TableRow className="hover:bg-control-bg">- <TableHead>{t("common.name")}</TableHead>- <TableHead>{t("common.type")}</TableHead>- <TableHead>{t("database.expression")}</TableHead>- </TableRow>- </TableHeader>- <TableBody className="bg-background">- {partitionRows.map(({ depth, partition }) => (- <TableRow key={`${partition.name}-${depth}`}>- <TableCell className="text-main">- <span style={{ paddingLeft: `${depth * 16}px` }}>- {partition.name}- </span>- </TableCell>- <TableCell>{partition.type}</TableCell>- <TableCell>{partition.expression || "-"}</TableCell>+ {showPartitionTables && (+ <DetailSection title={t("database.partition-tables")}>+ <div className="rounded-sm border border-block-border">+ <Table className="min-w-full">+ <TableHeader className="bg-control-bg">+ <TableRow className="hover:bg-control-bg">+ <TableHead>{t("common.name")}</TableHead>+ <TableHead>{t("common.type")}</TableHead>+ <TableHead>{t("database.expression")}</TableHead></TableRow>- ))}- </TableBody>- </Table>- </div>- </DetailSection>- )}+ </TableHeader>+ <TableBody className="bg-background">+ {partitionRows.map(({ depth, partition }) => (+ <TableRow key={`${partition.name}-${depth}`}>+ <TableCell className="text-main">+ <span style={{ paddingLeft: `${depth * 16}px` }}>+ {partition.name}+ </span>+ </TableCell>+ <TableCell>{partition.type}</TableCell>+ <TableCell>{partition.expression || "-"}</TableCell>+ </TableRow>+ ))}+ </TableBody>+ </Table>+ </div>+ </DetailSection>+ )}- {showColumns && (- <DetailSection title={t("database.columns")}>- <div className="flex items-center justify-between gap-3">- <Input- className="w-full max-w-sm"- placeholder={t("common.filter-by-name")}- value={columnSearchKeyword}- onChange={(event) => setColumnSearchKeyword(event.target.value)}- />- </div>- <div className="rounded-sm border border-block-border">- <table className="min-w-full divide-y divide-block-border text-sm">- <thead className="bg-control-bg">- <tr className="text-left text-sm text-control-light">- <th className="px-4 py-2 font-medium">- {t("common.name")}- </th>- {showSemanticTypeColumn && (+ {showColumns && (+ <DetailSection title={t("database.columns")}>+ <div className="flex items-center justify-between gap-3">+ <Input+ className="w-full max-w-sm"+ placeholder={t("common.filter-by-name")}+ value={columnSearchKeyword}+ onChange={(event) =>+ setColumnSearchKeyword(event.target.value)+ }+ />+ </div>+ <div className="rounded-sm border border-block-border">+ <table className="min-w-full divide-y divide-block-border text-sm">+ <thead className="bg-control-bg">+ <tr className="text-left text-sm text-control-light"><th className="px-4 py-2 font-medium">- {t(- "settings.sensitive-data.semantic-types.table.semantic-type"- )}+ {t("common.name")}</th>- )}- {showColumnClassification && (+ {showSemanticTypeColumn && (+ <th className="px-4 py-2 font-medium">+ {t(+ "settings.sensitive-data.semantic-types.table.semantic-type"+ )}+ </th>+ )}+ {showColumnClassification && (+ <th className="px-4 py-2 font-medium">+ {t("database.classification.self")}+ </th>+ )}<th className="px-4 py-2 font-medium">- {t("database.classification.self")}+ {t("common.type")}</th>- )}- <th className="px-4 py-2 font-medium">- {t("common.type")}- </th>- <th className="px-4 py-2 font-medium">- {t("common.default")}- </th>- <th className="px-4 py-2 font-medium">- {t("database.nullable")}- </th>- {showCharacterSetColumn && (<th className="px-4 py-2 font-medium">- {t("db.character-set")}+ {t("common.default")}</th>- )}- {showColumnCollation && (<th className="px-4 py-2 font-medium">- {t("db.collation")}+ {t("database.nullable")}</th>- )}- <th className="px-4 py-2 font-medium">- {t("common.comment")}- </th>- </tr>- </thead>- <tbody className="divide-y divide-block-border bg-background">- {filteredColumns.map((column) => (- <tr key={column.name}>- <td className="px-4 py-3 text-sm text-main">- {column.name}- </td>- {showSemanticTypeColumn && (- <td className="px-4 py-3 text-sm text-control">- {table.database ? (- <EditableSemanticTypeCell- database={table.database}+ {showCharacterSetColumn && (+ <th className="px-4 py-2 font-medium">+ {t("db.character-set")}+ </th>+ )}+ {showColumnCollation && (+ <th className="px-4 py-2 font-medium">+ {t("db.collation")}+ </th>+ )}+ <th className="px-4 py-2 font-medium">+ {t("common.comment")}+ </th>+ </tr>+ </thead>+ <tbody className="divide-y divide-block-border bg-background">+ {filteredColumns.map((column) => (+ <tr key={column.name}>+ <td className="px-4 py-3 text-sm text-main">+ {column.name}+ </td>+ {showSemanticTypeColumn && (+ <td className="px-4 py-3 text-sm text-control">+ {table.database ? (+ <EditableSemanticTypeCell+ database={table.database}+ readonly={readonly}+ semanticTypeId={column.semanticType}+ testIdPrefix={`column-semantic-type-${toTestId(column.name)}`}+ onApply={(semanticTypeId) =>+ handleColumnSemanticTypeApply(+ column.name,+ semanticTypeId+ )+ }+ />+ ) : (+ column.semanticType || "-"+ )}+ </td>+ )}+ {showColumnClassification && (+ <td className="px-4 py-3 text-sm text-control">+ <EditableClassificationCell+ classification={column.classification}+ classificationConfig={classificationConfig}readonly={readonly}- semanticTypeId={column.semanticType}- testIdPrefix={`column-semantic-type-${toTestId(column.name)}`}- onApply={(semanticTypeId) =>- handleColumnSemanticTypeApply(+ testIdPrefix={`column-classification-${toTestId(column.name)}`}+ onApply={(classificationId) =>+ handleColumnClassificationApply(column.name,- semanticTypeId+ classificationId)}/>- ) : (- column.semanticType || "-"- )}+ </td>+ )}+ <td className="px-4 py-3 text-sm text-control">+ {column.type}</td>- )}- {showColumnClassification && (<td className="px-4 py-3 text-sm text-control">- <EditableClassificationCell- classification={column.classification}- classificationConfig={classificationConfig}- readonly={readonly}- testIdPrefix={`column-classification-${toTestId(column.name)}`}- onApply={(classificationId) =>- handleColumnClassificationApply(- column.name,- classificationId- )- }- />+ {column.defaultValue}</td>- )}- <td className="px-4 py-3 text-sm text-control">- {column.type}- </td>- <td className="px-4 py-3 text-sm text-control">- {column.defaultValue}- </td>- <td className="px-4 py-3 text-sm text-control">- <Checkbox checked={column.nullable} disabled />- </td>- {showCharacterSetColumn && (<td className="px-4 py-3 text-sm text-control">- {column.characterSet || "-"}+ <Checkbox checked={column.nullable} disabled /></td>- )}- {showColumnCollation && (+ {showCharacterSetColumn && (+ <td className="px-4 py-3 text-sm text-control">+ {column.characterSet || "-"}+ </td>+ )}+ {showColumnCollation && (+ <td className="px-4 py-3 text-sm text-control">+ {column.collation || "-"}+ </td>+ )}<td className="px-4 py-3 text-sm text-control">- {column.collation || "-"}+ {column.comment || "-"}</td>- )}- <td className="px-4 py-3 text-sm text-control">- {column.comment || "-"}- </td>- </tr>- ))}- {filteredColumns.length === 0 && (- <tr>- <td- className="px-4 py-6 text-center text-sm text-control-light"- colSpan={- 5 +- (showSemanticTypeColumn ? 1 : 0) +- (showColumnClassification ? 1 : 0) +- (showCharacterSetColumn ? 1 : 0) +- (showColumnCollation ? 1 : 0)- }- >- {t("common.no-data")}- </td>- </tr>- )}- </tbody>- </table>- </div>- </DetailSection>- )}+ </tr>+ ))}+ {filteredColumns.length === 0 && (+ <tr>+ <td+ className="px-4 py-6 text-center text-sm text-control-light"+ colSpan={+ 5 ++ (showSemanticTypeColumn ? 1 : 0) ++ (showColumnClassification ? 1 : 0) ++ (showCharacterSetColumn ? 1 : 0) ++ (showColumnCollation ? 1 : 0)+ }+ >+ {t("common.no-data")}+ </td>+ </tr>+ )}+ </tbody>+ </table>+ </div>+ </DetailSection>+ )}- {table.showIndexes && table.indexes.length > 0 && (- <DetailSection title={t("database.indexes")}>- {table.indexes.map((index) => (- <div- key={index.name}- className="rounded-sm border border-block-border"- >- <div className="border-b border-block-border px-4 py-3 text-base font-medium text-main">- {index.name}- </div>- <table className="min-w-full divide-y divide-block-border text-sm">- <thead className="bg-control-bg">- <tr className="text-left text-sm text-control-light">- <th className="px-4 py-2 font-medium">- {t("database.expression")}- </th>- <th className="px-4 py-2 font-medium">- {t("database.unique")}- </th>- {showIndexVisibleColumn && (+ {table.showIndexes && table.indexes.length > 0 && (+ <DetailSection title={t("database.indexes")}>+ {table.indexes.map((index) => (+ <div+ key={index.name}+ className="rounded-sm border border-block-border"+ >+ <div className="border-b border-block-border px-4 py-3 text-base font-medium text-main">+ {index.name}+ </div>+ <table className="min-w-full divide-y divide-block-border text-sm">+ <thead className="bg-control-bg">+ <tr className="text-left text-sm text-control-light"><th className="px-4 py-2 font-medium">- {t("database.visible")}+ {t("database.expression")}</th>- )}- {showIndexCommentColumn && (<th className="px-4 py-2 font-medium">- {t("common.comment")}+ {t("database.unique")}</th>- )}- </tr>- </thead>- <tbody className="divide-y divide-block-border bg-background">- <tr>- <td className="px-4 py-3 text-sm text-control">- {index.expressions.join(", ") || "-"}- </td>- <td className="px-4 py-3 text-sm text-control">- {String(index.unique)}- </td>- {showIndexVisibleColumn && (+ {showIndexVisibleColumn && (+ <th className="px-4 py-2 font-medium">+ {t("database.visible")}+ </th>+ )}+ {showIndexCommentColumn && (+ <th className="px-4 py-2 font-medium">+ {t("common.comment")}+ </th>+ )}+ </tr>+ </thead>+ <tbody className="divide-y divide-block-border bg-background">+ <tr><td className="px-4 py-3 text-sm text-control">- {String(index.visible)}+ {index.expressions.join(", ") || "-"}</td>- )}- {showIndexCommentColumn && (<td className="px-4 py-3 text-sm text-control">- {index.comment || "-"}+ {String(index.unique)}</td>- )}- </tr>- </tbody>- </table>- </div>- ))}- </DetailSection>- )}+ {showIndexVisibleColumn && (+ <td className="px-4 py-3 text-sm text-control">+ {String(index.visible)}+ </td>+ )}+ {showIndexCommentColumn && (+ <td className="px-4 py-3 text-sm text-control">+ {index.comment || "-"}+ </td>+ )}+ </tr>+ </tbody>+ </table>+ </div>+ ))}+ </DetailSection>+ )}- {showTriggers && (- <DetailSection title={t("db.triggers")}>- <div className="rounded-sm border border-block-border">- <Table className="min-w-full">- <TableHeader className="bg-control-bg">- <TableRow className="hover:bg-control-bg">- <TableHead>{t("common.name")}</TableHead>- <TableHead>{t("db.trigger.event")}</TableHead>- <TableHead>{t("db.trigger.timing")}</TableHead>- <TableHead>{t("db.trigger.body")}</TableHead>- </TableRow>- </TableHeader>- <TableBody className="bg-background">- {table.triggers?.map((trigger) => (- <TableRow key={trigger.name}>- <TableCell className="text-main">- {trigger.name}- </TableCell>- <TableCell>{trigger.event || "-"}</TableCell>- <TableCell>{trigger.timing || "-"}</TableCell>- <TableCell className="max-w-xl whitespace-pre-wrap break-all">- {trigger.body || trigger.sqlMode || "-"}- </TableCell>+ {showTriggers && (+ <DetailSection title={t("db.triggers")}>+ <div className="rounded-sm border border-block-border">+ <Table className="min-w-full">+ <TableHeader className="bg-control-bg">+ <TableRow className="hover:bg-control-bg">+ <TableHead>{t("common.name")}</TableHead>+ <TableHead>{t("db.trigger.event")}</TableHead>+ <TableHead>{t("db.trigger.timing")}</TableHead>+ <TableHead>{t("db.trigger.body")}</TableHead></TableRow>- ))}- </TableBody>- </Table>- </div>- </DetailSection>- )}+ </TableHeader>+ <TableBody className="bg-background">+ {table.triggers?.map((trigger) => (+ <TableRow key={trigger.name}>+ <TableCell className="text-main">+ {trigger.name}+ </TableCell>+ <TableCell>{trigger.event || "-"}</TableCell>+ <TableCell>{trigger.timing || "-"}</TableCell>+ <TableCell className="max-w-xl whitespace-pre-wrap break-all">+ {trigger.body || trigger.sqlMode || "-"}+ </TableCell>+ </TableRow>+ ))}+ </TableBody>+ </Table>+ </div>+ </DetailSection>+ )}- {showNoSQLCatalog && table.database && table.tableName && (- <NoSQLCatalogEditor- database={table.database}- readonly={readonly}- schema={table.schema ?? ""}- tableName={table.tableName}- />- )}- </DialogContent>- </Dialog>+ {showNoSQLCatalog && table.database && table.tableName && (+ <NoSQLCatalogEditor+ database={table.database}+ readonly={readonly}+ schema={table.schema ?? ""}+ tableName={table.tableName}+ />+ )}+ </SheetBody>+ </SheetContent>+ </Sheet>);}diff --git a/frontend/src/routes/project/database-detail/overview/TableMetadataTable.test.tsx b/frontend/src/routes/project/database-detail/overview/TableMetadataTable.test.tsxindex bbccaa17dc..b28e31f465 100644--- a/frontend/src/routes/project/database-detail/overview/TableMetadataTable.test.tsx+++ b/frontend/src/routes/project/database-detail/overview/TableMetadataTable.test.tsx@@ -122,6 +122,21 @@ vi.mock("@/components/ui/dialog", () => ({DialogTitle: ({ children }: { children: ReactNode }) => <h1>{children}</h1>,}));+vi.mock("@/components/ui/popover", () => ({+ Popover: ({ children }: { children: ReactNode }) => <div>{children}</div>,+ PopoverContent: ({ children }: { children: ReactNode }) => (+ <div>{children}</div>+ ),+ PopoverTrigger: ({+ children,+ ...props+ }: ButtonHTMLAttributes<HTMLButtonElement>) => (+ <button type="button" {...props}>+ {children}+ </button>+ ),+}));+vi.mock("@/components/ui/search-input", () => ({SearchInput: (props: InputHTMLAttributes<HTMLInputElement>) => (<input {...props} />diff --git a/frontend/src/routes/project/database-detail/overview/TableMetadataTable.tsx b/frontend/src/routes/project/database-detail/overview/TableMetadataTable.tsxindex 9fd9b01edc..3e2b099e24 100644--- a/frontend/src/routes/project/database-detail/overview/TableMetadataTable.tsx+++ b/frontend/src/routes/project/database-detail/overview/TableMetadataTable.tsx@@ -26,7 +26,7 @@ import {hasProjectPermissionV2,hasSchemaProperty,} from "@/utils";-import { EditableClassificationCell } from "./TableDetailDialog";+import { EditableClassificationCell } from "./TableDetailSheet";export function TableMetadataTable({database,diff --git a/frontend/src/routes/project/database-detail/panels/DatabaseCatalogPanel.test.tsx b/frontend/src/routes/project/database-detail/panels/DatabaseCatalogPanel.test.tsxindex 4d8796b31a..a5f028cd90 100644--- a/frontend/src/routes/project/database-detail/panels/DatabaseCatalogPanel.test.tsx+++ b/frontend/src/routes/project/database-detail/panels/DatabaseCatalogPanel.test.tsx@@ -3,7 +3,7 @@ import type {InputHTMLAttributes,ReactNode,} from "react";-import { act, createElement } from "react";+import { act, createContext, createElement, useContext } from "react";import { createRoot } from "react-dom/client";import { beforeEach, describe, expect, test, vi } from "vitest";import type { DatabaseCatalog } from "@/types/proto-es/v1/database_catalog_service_pb";@@ -179,6 +179,7 @@ const mocks = vi.hoisted(() => {useDatabaseCatalog: vi.fn(() => makeDatabaseCatalog()),getOrFetchPolicyByParentAndType: vi.fn(),upsertPolicy: vi.fn(),+ updateColumnCatalog: vi.fn(),updateDatabaseCatalog: vi.fn(),getOrFetchSettingByName: vi.fn(),getProjectClassification: vi.fn(() => ({@@ -223,6 +224,19 @@ const mocks = vi.hoisted(() => {engine: 1,})),instanceV1MaskingForNoSQL: vi.fn(() => false),+ useAppDatabaseMetadata: vi.fn(() => ({+ schemas: [+ {+ name: "public",+ tables: [+ {+ name: "book",+ columns: [{ name: "email" }, { name: "phone" }],+ },+ ],+ },+ ],+ })),hasProjectPermissionV2,autoDatabaseRoute: vi.fn(() => ({ name: "database" })),routerResolve: vi.fn(() => ({ fullPath: "/database/route" })),@@ -258,6 +272,22 @@ const mocks = vi.hoisted(() => {<div>{children}</div>),DialogTitle: ({ children }: { children: ReactNode }) => <h1>{children}</h1>,+ Sheet: ({ open, children }: { open: boolean; children: ReactNode }) =>+ open ? <div data-testid="sheet-root">{children}</div> : null,+ SheetBody: ({ children }: { children: ReactNode }) => <div>{children}</div>,+ SheetContent: ({ children }: { children: ReactNode }) => (+ <div>{children}</div>+ ),+ SheetDescription: ({ children }: { children: ReactNode }) => (+ <p>{children}</p>+ ),+ SheetFooter: ({ children }: { children: ReactNode }) => (+ <div>{children}</div>+ ),+ SheetHeader: ({ children }: { children: ReactNode }) => (+ <div>{children}</div>+ ),+ SheetTitle: ({ children }: { children: ReactNode }) => <h1>{children}</h1>,AlertDialog: ({open,children,@@ -356,7 +386,8 @@ let DatabaseCatalogPanel: typeof import("./DatabaseCatalogPanel").DatabaseCatalovi.stubGlobal("localStorage", mocks.localStorage);-vi.mock("react-i18next", () => ({+vi.mock("react-i18next", async (importOriginal) => ({+ ...(await importOriginal<typeof import("react-i18next")>()),useTranslation: mocks.useTranslation,}));@@ -369,6 +400,14 @@ vi.mock("@/hooks/useDatabaseCatalog", () => ({useDatabaseCatalog: () => mocks.useDatabaseCatalog(),}));+vi.mock("@/hooks/useAppDatabaseMetadata", () => ({+ useAppDatabaseMetadata: () => mocks.useAppDatabaseMetadata(),+}));++vi.mock("@/lib/column-data-table/utils", () => ({+ updateColumnCatalog: mocks.updateColumnCatalog,+}));+vi.mock("@/stores/app", () => {const getState = () => ({updateDatabaseCatalog: mocks.updateDatabaseCatalog,@@ -438,14 +477,77 @@ vi.mock("@/components/ui/input", () => ({Input: mocks.Input,}));-vi.mock("@/components/ui/select", () => ({- Select: mocks.Select,- SelectContent: mocks.SelectContent,- SelectItem: mocks.SelectItem,- SelectTrigger: mocks.SelectTrigger,- SelectValue: mocks.SelectValue,+vi.mock("@/components/ui/sheet", () => ({+ Sheet: mocks.Sheet,+ SheetBody: mocks.SheetBody,+ SheetContent: mocks.SheetContent,+ SheetDescription: mocks.SheetDescription,+ SheetFooter: mocks.SheetFooter,+ SheetHeader: mocks.SheetHeader,+ SheetTitle: mocks.SheetTitle,}));+vi.mock("@/components/ui/select", () => {+ const SelectContext = createContext<{+ value?: string;+ disabled?: boolean;+ onValueChange?: (value: string) => void;+ }>({});+ return {+ Select: ({+ value,+ disabled,+ onValueChange,+ children,+ }: {+ value?: string;+ disabled?: boolean;+ onValueChange?: (value: string) => void;+ children: ReactNode;+ }) => (+ <SelectContext.Provider value={{ value, disabled, onValueChange }}>+ {children}+ </SelectContext.Provider>+ ),+ SelectTrigger: ({ children }: { children: ReactNode }) => (+ <div data-testid="select-trigger">{children}</div>+ ),+ SelectValue: ({+ children,+ }: {+ children?: ReactNode | ((value: string | undefined) => ReactNode);+ }) => {+ const { value } = useContext(SelectContext);+ return (+ <span data-testid="select-value">+ {typeof children === "function"+ ? children(value)+ : (children ?? value)}+ </span>+ );+ },+ SelectContent: ({ children }: { children: ReactNode }) => {+ const { value, disabled, onValueChange } = useContext(SelectContext);+ return (+ <select+ value={value}+ disabled={disabled}+ onChange={(event) => onValueChange?.(event.target.value)}+ >+ {children}+ </select>+ );+ },+ SelectItem: ({+ value,+ children,+ }: {+ value: string;+ children: ReactNode;+ }) => <option value={value}>{children}</option>,+ };+});+vi.mock("@/components/ui/tooltip", () => ({Tooltip: mocks.Tooltip,}));@@ -476,6 +578,8 @@ beforeEach(async () => {mocks.upsertPolicy.mockReset();mocks.updateDatabaseCatalog.mockReset();mocks.updateDatabaseCatalog.mockResolvedValue(undefined);+ mocks.updateColumnCatalog.mockReset();+ mocks.updateColumnCatalog.mockResolvedValue(undefined);mocks.getOrFetchSettingByName.mockReset();mocks.getProjectClassification.mockReset();mocks.getProjectClassification.mockReturnValue({@@ -526,6 +630,20 @@ beforeEach(async () => {});mocks.instanceV1MaskingForNoSQL.mockReset();mocks.instanceV1MaskingForNoSQL.mockReturnValue(false);+ mocks.useAppDatabaseMetadata.mockReset();+ mocks.useAppDatabaseMetadata.mockReturnValue({+ schemas: [+ {+ name: "public",+ tables: [+ {+ name: "book",+ columns: [{ name: "email" }, { name: "phone" }],+ },+ ],+ },+ ],+ });mocks.featureToRef.mockReset();mocks.featureToRef.mockReturnValue({ value: true });mocks.hasProjectPermissionV2.mockReset();@@ -563,6 +681,210 @@ describe("DatabaseCatalogPanel", () => {unmount();});+ test("shows mark sensitive data when catalog update is permitted", async () => {+ mocks.featureToRef.mockReturnValue({ value: false });++ const { container, render, unmount } = renderIntoContainer(+ createElement(DatabaseCatalogPanel, {+ database: makeDatabase(),+ })+ );++ render();+ await flush();++ expect(+ getButton(+ container,+ "settings.sensitive-data.mark-sensitive-data"+ )+ ).toBeDefined();++ unmount();+ });++ test("hides mark sensitive data for NoSQL databases", async () => {+ mocks.instanceV1MaskingForNoSQL.mockReturnValue(true);++ const { container, render, unmount } = renderIntoContainer(+ createElement(DatabaseCatalogPanel, {+ database: makeDatabase(),+ })+ );++ render();+ await flush();++ expect(+ getButton(+ container,+ "settings.sensitive-data.mark-sensitive-data"+ )+ ).toBeUndefined();++ unmount();+ });++ test("hides mark sensitive data without catalog update permission", async () => {+ mocks.hasProjectPermissionV2.mockImplementation(+ (_project, permission) => permission !== "bb.databaseCatalogs.update"+ );++ const { container, render, unmount } = renderIntoContainer(+ createElement(DatabaseCatalogPanel, {+ database: makeDatabase(),+ })+ );++ render();+ await flush();++ expect(+ getButton(+ container,+ "settings.sensitive-data.mark-sensitive-data"+ )+ ).toBeUndefined();++ unmount();+ });++ test("opens the feature dialog when marking requires data masking", async () => {+ mocks.featureToRef.mockReturnValue({ value: false });++ const { container, render, unmount } = renderIntoContainer(+ createElement(DatabaseCatalogPanel, {+ database: makeDatabase(),+ })+ );++ render();+ await flush();++ click(+ getButton(+ container,+ "settings.sensitive-data.mark-sensitive-data"+ ) as HTMLElement+ );+ await flush();++ expect(+ container.querySelector('[data-testid="dialog-root"]')+ ).not.toBeNull();+ expect(container.querySelector('[data-testid="sheet-root"]')).toBeNull();++ unmount();+ });++ test("marks a selected database column with a semantic type", async () => {+ mocks.getSettingByName.mockReturnValue({+ value: {+ value: {+ case: "semanticType",+ value: { types: [] },+ },+ },+ });++ const { container, render, unmount } = renderIntoContainer(+ createElement(DatabaseCatalogPanel, {+ database: makeDatabase(),+ })+ );++ render();+ await flush();++ click(+ getButton(+ container,+ "settings.sensitive-data.mark-sensitive-data"+ ) as HTMLElement+ );+ await flush();++ const sheet = container.querySelector('[data-testid="sheet-root"]');+ expect(sheet).not.toBeNull();++ let selects = Array.from(sheet?.querySelectorAll("select") ?? []);+ expect(selects).toHaveLength(4);+ await changeSelect(selects[0] as HTMLSelectElement, "0");+ let renderedValues = Array.from(+ sheet?.querySelectorAll('[data-testid="select-value"]') ?? []+ );+ expect(renderedValues[0]?.textContent).toBe("public");++ selects = Array.from(sheet?.querySelectorAll("select") ?? []);+ await changeSelect(selects[1] as HTMLSelectElement, "0");+ renderedValues = Array.from(+ sheet?.querySelectorAll('[data-testid="select-value"]') ?? []+ );+ expect(renderedValues[1]?.textContent).toBe("book");++ selects = Array.from(sheet?.querySelectorAll("select") ?? []);+ await changeSelect(selects[2] as HTMLSelectElement, "email");+ expect(+ Array.from(selects[3]?.options ?? []).map((option) => option.value)+ ).toEqual(expect.arrayContaining(["bb.default", "bb.default-partial"]));+ await changeSelect(selects[3] as HTMLSelectElement, "bb.default");++ click(getButton(sheet as HTMLElement, "common.save") as HTMLElement);+ await flush();++ expect(mocks.updateColumnCatalog).toHaveBeenCalledWith({+ database: "instances/inst1/databases/db1",+ schema: "public",+ table: "book",+ column: "email",+ columnCatalog: {+ semanticType: "bb.default",+ },+ notification: "common.updated",+ });++ unmount();+ });++ test("offers built-in semantic types in the sensitive-column table", async () => {+ const catalog = makeSimpleCatalog();+ const table = catalog.schemas[0]?.tables[0];+ if (!table || table.kind.case !== "columns") {+ throw new Error("expected a column-backed table catalog");+ }+ table.kind.value.columns[0]!.semanticType = "bb.default";+ mocks.useDatabaseCatalog.mockReturnValue(catalog);+ mocks.getSettingByName.mockReturnValue({+ value: {+ value: {+ case: "semanticType",+ value: { types: [] },+ },+ },+ });++ const { container, render, unmount } = renderIntoContainer(+ createElement(DatabaseCatalogPanel, {+ database: makeDatabase(),+ })+ );++ render();+ await flush();++ const semanticTypeSelect = container.querySelector("select");+ expect(+ Array.from(semanticTypeSelect?.options ?? []).map(+ (option) => option.value+ )+ ).toEqual(["__EMPTY__", "bb.default", "bb.default-partial"]);+ expect(+ container.querySelector('[data-testid="select-value"]')?.textContent+ ).not.toBe("common.empty");++ unmount();+ });+test("enables grant access after selecting a row", async () => {const { container, render, unmount } = renderIntoContainer(createElement(DatabaseCatalogPanel, {diff --git a/frontend/src/routes/project/database-detail/panels/DatabaseCatalogPanel.tsx b/frontend/src/routes/project/database-detail/panels/DatabaseCatalogPanel.tsxindex 18daa980bb..2fb4131482 100644--- a/frontend/src/routes/project/database-detail/panels/DatabaseCatalogPanel.tsx+++ b/frontend/src/routes/project/database-detail/panels/DatabaseCatalogPanel.tsx@@ -1,5 +1,5 @@import { create } from "@bufbuild/protobuf";-import { ShieldCheck } from "lucide-react";+import { Plus, ShieldCheck } from "lucide-react";import { useEffect, useMemo, useState } from "react";import { useTranslation } from "react-i18next";import { FeatureAttention } from "@/components/FeatureAttention";@@ -16,6 +16,7 @@ import { Button } from "@/components/ui/button";import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";import { Input } from "@/components/ui/input";import { useDatabaseCatalog } from "@/hooks/useDatabaseCatalog";+import { useSemanticTypes } from "@/hooks/useSemanticTypes";import type { MaskData, MaskDataTarget } from "@/lib/sensitive-data/types";import {getMaskDataIdentifier,@@ -36,10 +37,7 @@ import {MaskingExemptionPolicySchema,PolicyType,} from "@/types/proto-es/v1/org_policy_service_pb";-import {- type SemanticTypeSetting_SemanticType,- Setting_SettingName,-} from "@/types/proto-es/v1/setting_service_pb";+import { Setting_SettingName } from "@/types/proto-es/v1/setting_service_pb";import { PlanFeature } from "@/types/proto-es/v1/subscription_service_pb";import {getDatabaseProject,@@ -48,6 +46,7 @@ import {instanceV1MaskingForNoSQL,} from "@/utils";import { GrantAccessDialog } from "../catalog/GrantAccessDialog";+import { MarkSensitiveDataSheet } from "../catalog/MarkSensitiveDataSheet";import { SensitiveColumnTable } from "../catalog/SensitiveColumnTable";const GRANT_ACCESS_PERMISSIONS: Permission[] = [@@ -190,15 +189,7 @@ export function DatabaseCatalogPanel({ database }: { database: Database }) {(permission) => hasProjectPermissionV2(project, permission));- const semanticTypeSetting = useAppStore((s) =>- s.getSettingByName(Setting_SettingName.SEMANTIC_TYPES)- );- const semanticTypeList = useMemo<SemanticTypeSetting_SemanticType[]>(() => {- return semanticTypeSetting?.value?.value.case === "semanticType"- ? ((semanticTypeSetting.value.value.value.types ??- []) as SemanticTypeSetting_SemanticType[])- : [];- }, [semanticTypeSetting]);+ const { semanticTypes } = useSemanticTypes();const classificationConfig = useAppStore((s) =>s.getProjectClassification(project.dataClassificationConfigId ?? ""));@@ -209,6 +200,8 @@ export function DatabaseCatalogPanel({ database }: { database: Database }) {const [searchText, setSearchText] = useState("");const [checkedColumnList, setCheckedColumnList] = useState<MaskData[]>([]);const [showFeatureDialog, setShowFeatureDialog] = useState(false);+ const [showMarkSensitiveDataSheet, setShowMarkSensitiveDataSheet] =+ useState(false);const [showGrantAccessDialog, setShowGrantAccessDialog] = useState(false);const [pendingDeleteItem, setPendingDeleteItem] = useState<MaskData | null>(null@@ -230,17 +223,18 @@ export function DatabaseCatalogPanel({ database }: { database: Database }) {setSearchText("");setCheckedColumnList([]);setShowFeatureDialog(false);+ setShowMarkSensitiveDataSheet(false);setShowGrantAccessDialog(false);setPendingDeleteItem(null);}, [database.name]);const semanticTypeOptions = useMemo(() =>- semanticTypeList.map((semanticType) => ({+ semanticTypes.map((semanticType) => ({label: semanticType.title || semanticType.id,value: semanticType.id,})),- [semanticTypeList]+ [semanticTypes]);const classificationOptions = useMemo(() =>@@ -381,6 +375,14 @@ export function DatabaseCatalogPanel({ database }: { database: Database }) {setShowGrantAccessDialog(true);};+ const handleMarkSensitiveDataClick = () => {+ if (!hasSensitiveDataFeature) {+ setShowFeatureDialog(true);+ return;+ }+ setShowMarkSensitiveDataSheet(true);+ };+const closeGrantAccessDialog = () => {setShowGrantAccessDialog(false);setCheckedColumnList([]);@@ -410,37 +412,59 @@ export function DatabaseCatalogPanel({ database }: { database: Database }) {className="w-full max-w-sm"/>- {!isMaskingForNoSQL && (- <PermissionGuard- permissions={GRANT_ACCESS_PERMISSIONS}- project={project}- >- {({ disabled }) => (- <Button- type="button"- className="w-full sm:w-auto"- disabled={- disabled ||- !hasGrantAccessPermission ||- checkedColumnList.length === 0- }- onClick={handleGrantAccessClick}- >- {hasSensitiveDataFeature ? (- <ShieldCheck className="w-4 h-4" />- ) : (- <FeatureBadge- feature={PlanFeature.FEATURE_DATA_MASKING}- instance={instance}- clickable={false}- className="text-white"- />- )}- {t("settings.sensitive-data.grant-access")}- </Button>- )}- </PermissionGuard>- )}+ <div className="flex w-full flex-col items-stretch gap-2 sm:w-auto sm:flex-row">+ {hasUpdateCatalogPermission && !isMaskingForNoSQL && (+ <Button+ type="button"+ className="w-full sm:w-auto"+ onClick={handleMarkSensitiveDataClick}+ >+ {hasSensitiveDataFeature ? (+ <Plus className="size-4" />+ ) : (+ <FeatureBadge+ feature={PlanFeature.FEATURE_DATA_MASKING}+ instance={instance}+ clickable={false}+ className="inline-flex text-accent-text"+ />+ )}+ {t("settings.sensitive-data.mark-sensitive-data")}+ </Button>+ )}++ {!isMaskingForNoSQL && (+ <PermissionGuard+ permissions={GRANT_ACCESS_PERMISSIONS}+ project={project}+ >+ {({ disabled }) => (+ <Button+ type="button"+ appearance="outline"+ className="w-full sm:w-auto"+ disabled={+ disabled ||+ !hasGrantAccessPermission ||+ checkedColumnList.length === 0+ }+ onClick={handleGrantAccessClick}+ >+ {hasSensitiveDataFeature ? (+ <ShieldCheck className="size-4" />+ ) : (+ <FeatureBadge+ feature={PlanFeature.FEATURE_DATA_MASKING}+ instance={instance}+ clickable={false}+ />+ )}+ {t("settings.sensitive-data.grant-access")}+ </Button>+ )}+ </PermissionGuard>+ )}+ </div></div><SensitiveColumnTable@@ -481,6 +505,13 @@ export function DatabaseCatalogPanel({ database }: { database: Database }) {</DialogContent></Dialog>+ <MarkSensitiveDataSheet+ database={database}+ open={showMarkSensitiveDataSheet}+ semanticTypeList={semanticTypes}+ onOpenChange={setShowMarkSensitiveDataSheet}+ />+<GrantAccessDialogopen={openGrantAccessDialog}projectName={database.project}diff --git a/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsx b/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsxindex ecc38362d1..dfea49493d 100644--- a/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsx+++ b/frontend/src/routes/project/database-detail/panels/DatabaseOverviewPanel.test.tsx@@ -100,7 +100,7 @@ vi.mock("@/app/router", async (importOriginal) => ({}),}));-// The component (and its children `DatabaseObjectExplorer`, `TableDetailDialog`,+// The component (and its children `DatabaseObjectExplorer`, `TableDetailSheet`,// `TableMetadataTable`) now read dbSchema getters plus the former// setting/subscription store methods via the app store. Merge the// setting/subscription mocks into the `mocks.dbSchemaStore` shape so the@@ -231,6 +231,38 @@ vi.mock("@/components/ui/dialog", () => ({),}));+vi.mock("@/components/ui/sheet", () => ({+ Sheet: ({+ open,+ onOpenChange,+ children,+ }: {+ open: boolean;+ onOpenChange?: (open: boolean) => void;+ children: React.ReactNode;+ }) =>+ open ? (+ <div data-testid="sheet-root">+ <button type="button" onClick={() => onOpenChange?.(false)}>+ close-sheet+ </button>+ {children}+ </div>+ ) : null,+ SheetBody: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+ SheetContent: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+ SheetHeader: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+ SheetTitle: ({ children }: { children: React.ReactNode }) => (+ <h1>{children}</h1>+ ),+}));+const renderIntoContainer = (element: ReturnType<typeof createElement>) => {const container = document.createElement("div");const root = createRoot(container);@@ -612,11 +644,11 @@ describe("DatabaseOverviewPanel", () => {expect(container.textContent).toContain("orders");expect(- container.querySelector('[data-testid="dialog-root"]')+ container.querySelector('[data-testid="sheet-root"]')).not.toBeNull();const closeButton = Array.from(container.querySelectorAll("button")).find(- (button) => button.textContent === "close-dialog"+ (button) => button.textContent === "close-sheet");expect(closeButton).toBeTruthy();clickElement(closeButton as HTMLButtonElement);diff --git a/frontend/src/routes/workspace/GlobalMaskingPage.test.tsx b/frontend/src/routes/workspace/GlobalMaskingPage.test.tsxindex 9888f49f07..71ab904c21 100644--- a/frontend/src/routes/workspace/GlobalMaskingPage.test.tsx+++ b/frontend/src/routes/workspace/GlobalMaskingPage.test.tsx@@ -55,8 +55,13 @@ vi.mock("@/components/ui/select", () => ({createElement("div", {}, children),SelectContent: ({ children }: { children: ReactNode }) =>createElement("div", {}, children),- SelectItem: ({ children }: { children: ReactNode; value: string }) =>- createElement("div", {}, children),+ SelectItem: ({+ children,+ value,+ }: {+ children: ReactNode;+ value: string;+ }) => createElement("div", { "data-semantic-type-id": value }, children),SelectTrigger: ({children,className,@@ -143,6 +148,16 @@ vi.mock("@/types/proto-es/v1/subscription_service_pb", () => ({PlanFeature: { FEATURE_DATA_MASKING: 1 },}));+vi.mock("@/types/semanticTypes", () => ({+ getSemanticTypeListWithBuiltins: (+ semanticTypes: Array<{ id: string; title: string }>+ ) => [+ { id: "bb.default", title: "Default" },+ { id: "bb.default-partial", title: "Default Partial" },+ ...semanticTypes,+ ],+}));+vi.mock("@bufbuild/protobuf", () => ({create: (_schema: unknown, init?: Record<string, unknown>) => ({ ...init }),}));@@ -228,4 +243,18 @@ describe("GlobalMaskingPage", () => {container.querySelector("[data-testid='semantic-type-trigger']")).not.toBeNull();});++ it("lists built-in semantic types before configured types", async () => {+ await renderPage();++ const semanticTypeIds = Array.from(+ container.querySelectorAll("[data-semantic-type-id]")+ ).map((item) => item.getAttribute("data-semantic-type-id"));++ expect(semanticTypeIds).toEqual([+ "bb.default",+ "bb.default-partial",+ "DEFAULT",+ ]);+ });});diff --git a/frontend/src/routes/workspace/GlobalMaskingPage.tsx b/frontend/src/routes/workspace/GlobalMaskingPage.tsxindex 13cb9775a2..58b9a5db67 100644--- a/frontend/src/routes/workspace/GlobalMaskingPage.tsx+++ b/frontend/src/routes/workspace/GlobalMaskingPage.tsx@@ -27,6 +27,7 @@ import {WorkspacePageLayout,WorkspacePageToolbar,} from "@/components/WorkspacePageLayout";+import { useSemanticTypes } from "@/hooks/useSemanticTypes";import {factorOperatorOverrideMap,getClassificationLevelOptions,@@ -123,16 +124,7 @@ function MaskingRuleConfig({const [dirty, setDirty] = useState(false);const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);- const settingsByName = useAppStore((s) => s.settingsByName);- const semanticTypeOptions = useMemo(() => {- const setting = useAppStore- .getState()- .getSettingByName(Setting_SettingName.SEMANTIC_TYPES);- if (setting?.value?.value?.case === "semanticType") {- return setting.value.value.value.types ?? [];- }- return [];- }, [settingsByName]);+ const { semanticTypes: semanticTypeOptions } = useSemanticTypes();const resetIdRef = useRef(0);const resetToRule = useCallback(diff --git a/frontend/src/routes/workspace/SemanticTypesPage.test.tsx b/frontend/src/routes/workspace/SemanticTypesPage.test.tsxnew file mode 100644index 0000000000..6af23c2aa0--- /dev/null+++ b/frontend/src/routes/workspace/SemanticTypesPage.test.tsx@@ -0,0 +1,261 @@+import type { ReactNode } from "react";+import { act } from "react";+import { createRoot } from "react-dom/client";+import { afterEach, describe, expect, test, vi } from "vitest";+import { SemanticTypesPage } from "./SemanticTypesPage";++(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT =+ true;++const mocks = vi.hoisted(() => ({+ getOrFetchSettingByName: vi.fn(async () => undefined),+ getSettingByName: vi.fn<() => unknown>(() => undefined),+ upsertSetting: vi.fn(async (_request: unknown) => undefined),+}));++vi.mock("react-i18next", async (importOriginal) => ({+ ...(await importOriginal<typeof import("react-i18next")>()),+ useTranslation: () => ({ t: (key: string) => key }),+}));++vi.mock("@/components/FeatureAttention", () => ({+ FeatureAttention: () => null,+}));++vi.mock("@/components/ui/tooltip", () => ({+ Tooltip: ({+ children,+ content,+ }: {+ children: ReactNode;+ content: ReactNode;+ }) => (+ <span+ data-tooltip-content={typeof content === "string" ? content : undefined}+ >+ {children}+ </span>+ ),+}));++vi.mock("@/components/WorkspacePageLayout", () => ({+ WorkspacePageInfo: ({ description }: { description: ReactNode }) => (+ <div>{description}</div>+ ),+ WorkspacePageLayout: ({ children }: { children: ReactNode }) => (+ <main>{children}</main>+ ),+ WorkspacePageToolbar: ({ children }: { children: ReactNode }) => (+ <div>{children}</div>+ ),+}));++vi.mock("@/stores/app", () => {+ type MockAppState = {+ getSettingByName: () => unknown;+ hasInstanceFeature: () => boolean;+ settingsByName: Map<unknown, unknown>;+ };+ const state: MockAppState = {+ getSettingByName: mocks.getSettingByName,+ hasInstanceFeature: () => true,+ settingsByName: new Map(),+ };+ const useAppStore = Object.assign(+ (selector: (state: MockAppState) => unknown) => selector(state),+ {+ getState: () => ({+ getOrFetchSettingByName: mocks.getOrFetchSettingByName,+ getSettingByName: mocks.getSettingByName,+ upsertSetting: mocks.upsertSetting,+ }),+ }+ );+ return { useAppStore };+});++vi.mock("@/utils", async (importOriginal) => ({+ ...(await importOriginal<typeof import("@/utils")>()),+ hasWorkspacePermissionV2: () => true,+}));++const roots: Array<ReturnType<typeof createRoot>> = [];++afterEach(() => {+ for (const root of roots.splice(0)) {+ act(() => root.unmount());+ }+ mocks.getOrFetchSettingByName.mockClear();+ mocks.getSettingByName.mockClear();+ mocks.upsertSetting.mockClear();+});++describe("SemanticTypesPage", () => {+ test("keeps the semantic type table horizontally scrollable on narrow screens", async () => {+ mocks.getOrFetchSettingByName.mockReturnValueOnce(new Promise(() => {}));+ const container = document.createElement("div");+ const root = createRoot(container);+ roots.push(root);++ await act(async () => {+ root.render(<SemanticTypesPage />);+ await Promise.resolve();+ });++ const table = container.querySelector("table");+ expect(table?.classList.contains("min-w-5xl")).toBe(true);+ expect(table?.parentElement?.classList.contains("overflow-x-auto")).toBe(+ true+ );+ });++ test("shows built-in semantic types without installing templates", async () => {+ mocks.getOrFetchSettingByName.mockReturnValueOnce(new Promise(() => {}));+ const container = document.createElement("div");+ const root = createRoot(container);+ roots.push(root);++ await act(async () => {+ root.render(<SemanticTypesPage />);+ await Promise.resolve();+ });++ const text = container.textContent ?? "";+ expect(text).toContain("bb.default");+ expect(text).toContain("bb.default-partial");+ expect(text).not.toContain(+ "settings.sensitive-data.semantic-types.use-predefined-type"+ );+ });++ test("does not duplicate built-in semantic types already stored in the workspace setting", async () => {+ mocks.getSettingByName.mockReturnValue({+ value: {+ value: {+ case: "semanticType",+ value: {+ types: [+ { id: "bb.default", title: "Stored default" },+ { id: "bb.default-partial", title: "Stored partial default" },+ { id: "email", title: "Email" },+ ],+ },+ },+ },+ });+ const container = document.createElement("div");+ const root = createRoot(container);+ roots.push(root);++ await act(async () => {+ root.render(<SemanticTypesPage />);+ await Promise.resolve();+ });++ const renderedIds = Array.from(+ container.querySelectorAll("tbody tr td:nth-child(2)")+ ).map((cell) => cell.textContent);+ expect(renderedIds).toEqual(["bb.default", "bb.default-partial", "email"]);+ });++ test("explains why a new semantic type cannot be submitted", async () => {+ mocks.getSettingByName.mockReturnValue(undefined);+ const container = document.createElement("div");+ const root = createRoot(container);+ roots.push(root);++ await act(async () => {+ root.render(<SemanticTypesPage />);+ await Promise.resolve();+ });++ const createButton = Array.from(container.querySelectorAll("button")).find(+ (button) => button.textContent === "common.create"+ );+ expect(createButton).toBeDefined();+ act(() => createButton?.click());++ const confirmButton = container.querySelector<HTMLButtonElement>(+ 'button[aria-label="common.confirm"]'+ );+ expect(confirmButton?.disabled).toBe(true);+ expect(+ confirmButton+ ?.closest("[data-tooltip-content]")+ ?.getAttribute("data-tooltip-content")+ ).toBe("settings.sensitive-data.semantic-types.error.title-required");+ expect(container.textContent).toContain(+ "settings.sensitive-data.semantic-types.table.title"+ );+ });++ test("preserves non-built-in bb-prefixed semantic types when saving", async () => {+ mocks.getSettingByName.mockReturnValue({+ value: {+ value: {+ case: "semanticType",+ value: {+ types: [+ { id: "bb.customer", title: "Customer" },+ { id: "email", title: "Email" },+ ],+ },+ },+ },+ });+ const container = document.createElement("div");+ const root = createRoot(container);+ roots.push(root);++ await act(async () => {+ root.render(<SemanticTypesPage />);+ await Promise.resolve();+ });++ const emailRow = Array.from(container.querySelectorAll("tbody tr")).find(+ (row) => row.querySelector("td:nth-child(2)")?.textContent === "email"+ );+ expect(emailRow).toBeDefined();+ const editButton = Array.from(+ emailRow?.querySelectorAll<HTMLButtonElement>("button") ?? []+ ).at(-1);+ expect(editButton).toBeDefined();+ act(() => editButton?.click());++ const titleInput = emailRow?.querySelector<HTMLInputElement>(+ 'input[placeholder="settings.sensitive-data.semantic-types.table.title"]'+ );+ expect(titleInput).toBeDefined();+ act(() => {+ const descriptor = Object.getOwnPropertyDescriptor(+ HTMLInputElement.prototype,+ "value"+ );+ descriptor?.set?.call(titleInput, "Email address");+ titleInput?.dispatchEvent(new Event("input", { bubbles: true }));+ });++ const confirmButton = emailRow?.querySelector<HTMLButtonElement>(+ 'button[aria-label="common.confirm"]'+ );+ expect(confirmButton?.disabled).toBe(false);+ act(() => confirmButton?.click());+ await act(async () => {+ await Promise.resolve();+ });++ const request = mocks.upsertSetting.mock.calls.at(-1)?.[0] as+ | {+ value?: {+ value?: {+ value?: { types?: Array<{ id: string }> };+ };+ };+ }+ | undefined;+ expect(request?.value?.value?.value?.types?.map(({ id }) => id)).toEqual([+ "bb.customer",+ "email",+ ]);+ });+});diff --git a/frontend/src/routes/workspace/SemanticTypesPage.tsx b/frontend/src/routes/workspace/SemanticTypesPage.tsxindex 7ffce3b7a5..ef76876063 100644--- a/frontend/src/routes/workspace/SemanticTypesPage.tsx+++ b/frontend/src/routes/workspace/SemanticTypesPage.tsx@@ -35,9 +35,9 @@ import {WorkspacePageLayout,WorkspacePageToolbar,} from "@/components/WorkspacePageLayout";+import { useSemanticTypes } from "@/hooks/useSemanticTypes";import { pushNotification } from "@/stores";import { useAppStore } from "@/stores/app";-import { getSemanticTemplateList } from "@/types";import type {Algorithm,SemanticTypeSetting_SemanticType,@@ -55,6 +55,7 @@ import {SettingValueSchema as SettingSettingValueSchema,} from "@/types/proto-es/v1/setting_service_pb";import { PlanFeature } from "@/types/proto-es/v1/subscription_service_pb";+import { isBuiltinSemanticTypeId } from "@/types/semanticTypes";import { hasWorkspacePermissionV2 } from "@/utils";type SemanticItemMode = "NORMAL" | "CREATE" | "EDIT";@@ -86,7 +87,25 @@ function getMaskingType(}function isBuiltinSemanticType(item: SemanticTypeSetting_SemanticType) {- return item.id.startsWith("bb.");+ return isBuiltinSemanticTypeId(item.id);+}++function getPersistedSemanticTypes(items: SemanticItem[]) {+ return items+ .filter(+ ({ item, mode }) => mode === "NORMAL" && !isBuiltinSemanticType(item)+ )+ .map(({ item }) => item);+}++function toSemanticItems(+ semanticTypeList: SemanticTypeSetting_SemanticType[]+): SemanticItem[] {+ return semanticTypeList.map((item) => ({+ dirty: false,+ item,+ mode: "NORMAL",+ }));}function useEscapeKey(onEscape: () => void) {@@ -107,25 +126,17 @@ export function SemanticTypesPage() {s.hasInstanceFeature(PlanFeature.FEATURE_DATA_MASKING));const isReadonly = !hasPermission || !hasSensitiveDataFeature;+ const { configuredSemanticTypes, semanticTypes } = useSemanticTypes();- const [items, setItems] = useState<SemanticItem[]>([]);+ const [items, setItems] = useState<SemanticItem[]>(() =>+ toSemanticItems(semanticTypes)+ );const [loaded, setLoaded] = useState(false);- const [showTemplateDrawer, setShowTemplateDrawer] = useState(false);const [algorithmDrawer, setAlgorithmDrawer] = useState<{index: number;algorithm?: Algorithm;} | null>(null);- const settingsByName = useAppStore((s) => s.settingsByName);- const semanticTypeSettingValue = useMemo(() => {- const setting = useAppStore- .getState()- .getSettingByName(Setting_SettingName.SEMANTIC_TYPES);- return setting?.value?.value?.case === "semanticType"- ? (setting.value.value.value.types ?? [])- : [];- }, [settingsByName]);-useEffect(() => {useAppStore.getState()@@ -135,13 +146,7 @@ export function SemanticTypesPage() {useEffect(() => {if (!loaded) return;- setItems(- semanticTypeSettingValue.map((st) => ({- dirty: false,- item: st,- mode: "NORMAL" as const,- }))- );+ setItems(toSemanticItems(semanticTypes));}, [loaded]);const upsertSetting = useCallback(@@ -193,9 +198,7 @@ export function SemanticTypesPage() {next.splice(index, 1);setItems(next);if (current.mode !== "CREATE") {- const types = next- .filter((d) => d.mode === "NORMAL")- .map((d) => d.item);+ const types = getPersistedSemanticTypes(next);void upsertSetting(types, t("common.deleted"));}},@@ -212,7 +215,7 @@ export function SemanticTypesPage() {const next = [...items];next[index] = { ...current, dirty: false, mode: "NORMAL" };setItems(next);- const types = next.filter((d) => d.mode === "NORMAL").map((d) => d.item);+ const types = getPersistedSemanticTypes(next);void upsertSetting(types, msg);},[upsertSetting, t, items]@@ -226,7 +229,7 @@ export function SemanticTypesPage() {if (item.mode === "CREATE") {next.splice(index, 1);} else {- const origin = semanticTypeSettingValue.find(+ const origin = configuredSemanticTypes.find((s) => s.id === item.item.id);if (origin) {@@ -236,7 +239,7 @@ export function SemanticTypesPage() {return next;});},- [semanticTypeSettingValue]+ [configuredSemanticTypes]);const onInput = useCallback(@@ -282,9 +285,7 @@ export function SemanticTypesPage() {const next = [...items];next[index] = updated;setItems(next);- const types = next- .filter((d) => d.mode === "NORMAL")- .map((d) => d.item);+ const types = getPersistedSemanticTypes(next);const msg = t(current.mode === "CREATE" ? "common.created" : "common.updated");@@ -306,40 +307,16 @@ export function SemanticTypesPage() {[]);- const onTemplateApply = useCallback(- (template: SemanticTypeSetting_SemanticType) => {- if (items.find((item) => item.item.id === template.id)) {- pushNotification({- module: "bytebase",- style: "INFO",- title: t(- "settings.sensitive-data.semantic-types.template.duplicate-warning",- { title: template.title }- ),- });- setShowTemplateDrawer(false);- return;- }- const newItem: SemanticItem = {- dirty: false,- mode: "NORMAL",- item: create(SemanticTypeSetting_SemanticTypeSchema, {- ...template,- }),- };- const next = [...items, newItem];- setItems(next);- const types = next.filter((d) => d.mode === "NORMAL").map((d) => d.item);- void upsertSetting(types, t("common.created"));- setShowTemplateDrawer(false);- },- [upsertSetting, t, items]- );-- const isConfirmDisabled = (data: SemanticItem): boolean => {- if (!data.item.title) return true;- if (data.mode === "EDIT" && !data.dirty) return true;- return false;+ const getConfirmDisabledReason = (data: SemanticItem): string | undefined => {+ if (!data.item.title.trim()) {+ return t("settings.sensitive-data.semantic-types.error.title-required");+ }+ if (data.mode === "EDIT" && !data.dirty) {+ return t(+ "settings.sensitive-data.semantic-types.error.no-changes-to-save"+ );+ }+ return undefined;};return (@@ -350,23 +327,14 @@ export function SemanticTypesPage() {/><WorkspacePageToolbar align="end">- <div className="flex items-center gap-x-2">- <Button- appearance="outline"- disabled={isReadonly}- onClick={() => setShowTemplateDrawer(true)}- >- {t("settings.sensitive-data.semantic-types.use-predefined-type")}- </Button>- <Button disabled={isReadonly} onClick={onAdd}>- <Plus className="h-4 w-4" />- {t("common.create")}- </Button>- </div>+ <Button disabled={isReadonly} onClick={onAdd}>+ <Plus />+ {t("common.create")}+ </Button></WorkspacePageToolbar>- <div className="border rounded-sm overflow-hidden">- <Table>+ <div className="overflow-x-auto rounded-sm border">+ <Table className="min-w-5xl"><TableHeader><TableRow className="bg-control-bg"><TableHead className="w-20 text-center">@@ -374,9 +342,7 @@ export function SemanticTypesPage() {</TableHead><TableHead className="w-36">ID</TableHead><TableHead>- {t(- "settings.sensitive-data.semantic-types.table.semantic-type"- )}+ {t("settings.sensitive-data.semantic-types.table.title")}</TableHead><TableHead className="w-48">{t("settings.sensitive-data.semantic-types.table.description")}@@ -400,7 +366,7 @@ export function SemanticTypesPage() {row={row}index={index}readonly={isReadonly}- isConfirmDisabled={isConfirmDisabled}+ getConfirmDisabledReason={getConfirmDisabledReason}onInput={onInput}onRemove={onRemove}onConfirm={onConfirm}@@ -423,13 +389,6 @@ export function SemanticTypesPage() {</Table></div>- {showTemplateDrawer && (- <SemanticTemplateDrawer- onApply={onTemplateApply}- onDismiss={() => setShowTemplateDrawer(false)}- />- )}-{algorithmDrawer !== null && (<MaskingAlgorithmDraweralgorithm={algorithmDrawer.algorithm}@@ -948,108 +907,13 @@ function MaskingAlgorithmDrawer({);}-// --- SemanticTemplateDrawer (React) -----interface SemanticTemplateDrawerProps {- onApply: (template: SemanticTypeSetting_SemanticType) => void;- onDismiss: () => void;-}--function SemanticTemplateDrawer({- onApply,- onDismiss,-}: SemanticTemplateDrawerProps) {- const { t } = useTranslation();- useEscapeKey(onDismiss);- const templates = useMemo(() => getSemanticTemplateList(), []);-- return (- <Sheet open onOpenChange={(nextOpen) => !nextOpen && onDismiss()}>- <SheetContent width="standard">- <SheetHeader>- <SheetTitle>- {t("settings.sensitive-data.semantic-types.table.semantic-type")}- </SheetTitle>- </SheetHeader>- <SheetBody className="p-6">- <p className="text-sm text-control-placeholder mb-4">- {t("settings.sensitive-data.semantic-types.template.description")}- </p>- <div className="border rounded-sm overflow-hidden">- <Table>- <TableHeader>- <TableRow className="bg-control-bg">- <TableHead>ID</TableHead>- <TableHead>- {t(- "settings.sensitive-data.semantic-types.table.semantic-type"- )}- </TableHead>- <TableHead>- {t(- "settings.sensitive-data.semantic-types.table.description"- )}- </TableHead>- <TableHead>- {t(- "settings.sensitive-data.semantic-types.table.masking-algorithm"- )}- </TableHead>- </TableRow>- </TableHeader>- <TableBody>- {templates.map((template) => {- const key = template.id.split(".").join("-");- return (- <TableRow- key={template.id}- className="cursor-pointer"- onClick={() => onApply(template)}- >- <TableCell>{template.id}</TableCell>- <TableCell>{template.title}</TableCell>- <TableCell>{template.description}</TableCell>- <TableCell>- <div className="flex items-center gap-x-1">- <span>- {t(- `dynamic.settings.sensitive-data.semantic-types.template.${key}.title`- )}- </span>- <span- className="text-control-placeholder cursor-help"- title={t(- `dynamic.settings.sensitive-data.semantic-types.template.${key}.algorithm.description`- )}- >- <Info className="w-4 h-4" />- </span>- </div>- </TableCell>- </TableRow>- );- })}- </TableBody>- </Table>- </div>- </SheetBody>- <SheetFooter>- <Button appearance="outline" onClick={onDismiss}>- {t("common.cancel")}- </Button>- </SheetFooter>- </SheetContent>- </Sheet>- );-}-// --- SemanticTypeRow ---interface SemanticTypeRowProps {row: SemanticItem;index: number;readonly: boolean;- isConfirmDisabled: (data: SemanticItem) => boolean;+ getConfirmDisabledReason: (data: SemanticItem) => string | undefined;onInput: (index: number,updater: (@@ -1067,7 +931,7 @@ function SemanticTypeRow({row,index,readonly,- isConfirmDisabled,+ getConfirmDisabledReason,onInput,onRemove,onConfirm,@@ -1080,6 +944,7 @@ function SemanticTypeRow({const isBuiltin = isBuiltinSemanticType(row.item);const isItemReadonly = readonly || isBuiltin;const isEditing = row.mode !== "NORMAL";+ const confirmDisabledReason = getConfirmDisabledReason(row);return (<TableRow>@@ -1110,7 +975,7 @@ function SemanticTypeRow({value={row.item.title}size="sm"placeholder={t(- "settings.sensitive-data.semantic-types.table.semantic-type"+ "settings.sensitive-data.semantic-types.table.title")}onChange={(e) =>onInput(index, (item) => ({ ...item, title: e.target.value }))@@ -1189,16 +1054,7 @@ function SemanticTypeRow({{!readonly && (<TableCell><div className="flex items-center justify-end gap-x-1">- {isBuiltin ? (- <DeleteConfirmButton- show={showDeleteConfirm}- onShowChange={setShowDeleteConfirm}- message={t(- "settings.sensitive-data.semantic-types.table.delete"- )}- onConfirm={() => onRemove(index)}- />- ) : (+ {!isBuiltin && (<>{isEditing && (<button@@ -1219,13 +1075,19 @@ function SemanticTypeRow({/>)}{isEditing && (- <button- className="p-1 rounded-xs hover:bg-accent/10 text-accent disabled:opacity-50 disabled:cursor-not-allowed"- disabled={isConfirmDisabled(row)}- onClick={() => onConfirm(index)}- >- <Check className="w-4 h-4" />- </button>+ <Tooltip content={confirmDisabledReason}>+ <span className="inline-flex">+ <button+ type="button"+ aria-label={t("common.confirm")}+ className="p-1 rounded-xs hover:bg-accent/10 text-accent disabled:opacity-50 disabled:cursor-not-allowed"+ disabled={!!confirmDisabledReason}+ onClick={() => onConfirm(index)}+ >+ <Check className="w-4 h-4" />+ </button>+ </span>+ </Tooltip>)}{row.mode === "NORMAL" && (<buttondiff --git a/frontend/src/types/semanticTypes.ts b/frontend/src/types/semanticTypes.tsindex d4aa1a530f..06d23d9606 100644--- a/frontend/src/types/semanticTypes.ts+++ b/frontend/src/types/semanticTypes.ts@@ -1,4 +1,5 @@import { create } from "@bufbuild/protobuf";+import type { TFunction } from "i18next";import i18n from "@/lib/i18n";import type { Algorithm } from "@/types/proto-es/v1/setting_service_pb";import {@@ -12,21 +13,42 @@ interface BuildInSemantic {algorithm: Algorithm;}-export const getSemanticTemplateList =- (): SemanticTypeSetting_SemanticType[] => {- return (buildInSemanticTypes as unknown as BuildInSemantic[]).map(- (buildInSemantic) => {- const key = buildInSemantic.id.split(".").join("-");- return create(SemanticTypeSetting_SemanticTypeSchema, {- id: buildInSemantic.id,- title: i18n.t(- `dynamic.settings.sensitive-data.semantic-types.template.${key}.title`- ),- description: i18n.t(- `dynamic.settings.sensitive-data.semantic-types.template.${key}.description`- ),- algorithm: buildInSemantic.algorithm,- });- }- );- };+const builtinSemanticTypeIds = new Set(+ (buildInSemanticTypes as unknown as BuildInSemantic[]).map(({ id }) => id)+);++export const isBuiltinSemanticTypeId = (id: string): boolean =>+ builtinSemanticTypeIds.has(id);++export const getSemanticTemplateList = (+ t: TFunction = i18n.t+): SemanticTypeSetting_SemanticType[] => {+ return (buildInSemanticTypes as unknown as BuildInSemantic[]).map(+ (buildInSemantic) => {+ const key = buildInSemantic.id.split(".").join("-");+ return create(SemanticTypeSetting_SemanticTypeSchema, {+ id: buildInSemantic.id,+ title: t(+ `dynamic.settings.sensitive-data.semantic-types.template.${key}.title`+ ),+ description: t(+ `dynamic.settings.sensitive-data.semantic-types.template.${key}.description`+ ),+ algorithm: buildInSemantic.algorithm,+ });+ }+ );+};++export const getSemanticTypeListWithBuiltins = (+ semanticTypeList: SemanticTypeSetting_SemanticType[],+ t: TFunction = i18n.t+): SemanticTypeSetting_SemanticType[] => {+ const builtins = getSemanticTemplateList(t);+ return [+ ...builtins,+ ...semanticTypeList.filter(+ (semanticType) => !isBuiltinSemanticTypeId(semanticType.id)+ ),+ ];+};