Skip to content

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

fix: prevent empty schema names in database catalog config (#19385)

Vincent Huang6c649c4acbbb
diff --git a/backend/api/v1/database_catalog_service.go b/backend/api/v1/database_catalog_service.go
index 900a210b40..a60587d323 100644
--- a/backend/api/v1/database_catalog_service.go
+++ b/backend/api/v1/database_catalog_service.go
@@ -99,11 +99,16 @@ func (s *DatabaseCatalogService) UpdateDatabaseCatalog(ctx context.Context, req
}
databaseConfig := convertDatabaseCatalog(req.Msg.GetCatalog())
+
+ if err := validateCatalogSchemaNames(databaseConfig, dbMetadata.GetProto()); err != nil {
+ return nil, connect.NewError(connect.CodeInvalidArgument, err)
+ }
+
if err := s.store.UpdateDBSchema(ctx, database.InstanceID, database.DatabaseName, &store.UpdateDBSchemaMessage{Config: databaseConfig}); err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
- return connect.NewResponse(req.Msg.GetCatalog()), nil
+ return connect.NewResponse(convertDatabaseConfig(database, databaseConfig)), nil
}
func convertDatabaseConfig(database *store.DatabaseMessage, config *storepb.DatabaseConfig) *v1pb.DatabaseCatalog {
@@ -200,6 +205,29 @@ func convertDatabaseCatalog(catalog *v1pb.DatabaseCatalog) *storepb.DatabaseConf
return c
}
+// validateCatalogSchemaNames rejects empty schema names for engines that use
+// named schemas. Engines like Cassandra/MySQL where metadata has empty schema
+// names are allowed through.
+func validateCatalogSchemaNames(config *storepb.DatabaseConfig, metadata *storepb.DatabaseSchemaMetadata) error {
+ if !hasEmptySchemaName(config.Schemas) {
+ return nil
+ }
+ // Engines like Cassandra/MySQL legitimately use empty schema names.
+ if metadata != nil && hasEmptySchemaName(metadata.Schemas) {
+ return nil
+ }
+ return errors.New("schema name must not be empty for this database engine")
+}
+
+func hasEmptySchemaName[T interface{ GetName() string }](schemas []T) bool {
+ for _, s := range schemas {
+ if s.GetName() == "" {
+ return true
+ }
+ }
+ return false
+}
+
func convertV1TableCatalog(t *v1pb.TableCatalog) *storepb.TableCatalog {
tc := &storepb.TableCatalog{
Name: t.Name,
diff --git a/backend/api/v1/database_catalog_service_test.go b/backend/api/v1/database_catalog_service_test.go
new file mode 100644
index 0000000000..29a14be59d
--- /dev/null
+++ b/backend/api/v1/database_catalog_service_test.go
@@ -0,0 +1,72 @@
+package v1
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+)
+
+func TestValidateCatalogSchemaNames(t *testing.T) {
+ tests := []struct {
+ name string
+ config *storepb.DatabaseConfig
+ metadata *storepb.DatabaseSchemaMetadata
+ wantErr bool
+ }{
+ {
+ name: "named schemas pass",
+ config: &storepb.DatabaseConfig{
+ Schemas: []*storepb.SchemaCatalog{
+ {Name: "public", Tables: []*storepb.TableCatalog{{Name: "users"}}},
+ },
+ },
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public", Tables: []*storepb.TableMetadata{{Name: "users"}}},
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "empty schema rejected for named-schema engine",
+ config: &storepb.DatabaseConfig{
+ Schemas: []*storepb.SchemaCatalog{
+ {Name: "", Tables: []*storepb.TableCatalog{{Name: "users"}}},
+ },
+ },
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public", Tables: []*storepb.TableMetadata{{Name: "users"}}},
+ },
+ },
+ wantErr: true,
+ },
+ {
+ name: "empty schema allowed when metadata has empty schema",
+ config: &storepb.DatabaseConfig{
+ Schemas: []*storepb.SchemaCatalog{
+ {Name: "", Tables: []*storepb.TableCatalog{{Name: "users"}}},
+ },
+ },
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "", Tables: []*storepb.TableMetadata{{Name: "users"}}},
+ },
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ err := validateCatalogSchemaNames(tc.config, tc.metadata)
+ if tc.wantErr {
+ require.Error(t, err)
+ } else {
+ require.NoError(t, err)
+ }
+ })
+ }
+}
diff --git a/frontend/src/components/Database/DatabaseOverviewPanel.vue b/frontend/src/components/Database/DatabaseOverviewPanel.vue
index a9edfe12ee..1fd3bc5f46 100644
--- a/frontend/src/components/Database/DatabaseOverviewPanel.vue
+++ b/frontend/src/components/Database/DatabaseOverviewPanel.vue
@@ -224,7 +224,7 @@ const schemaList = computed(() => {
});
watch(
- () => props.database.name,
+ [() => props.database.name, schemaList],
async () => {
state.loading = true;
if (schemaList.value.length > 0) {
diff --git a/frontend/src/store/modules/v1/databaseCatalog.ts b/frontend/src/store/modules/v1/databaseCatalog.ts
index 5938738749..befdff9905 100644
--- a/frontend/src/store/modules/v1/databaseCatalog.ts
+++ b/frontend/src/store/modules/v1/databaseCatalog.ts
@@ -69,9 +69,6 @@ export const useDatabaseCatalogV1Store = defineStore(
}
}
- console.debug("[getOrFetchDatabaseCatalog]", {
- name: catalogResourceName,
- });
const request = create(GetDatabaseCatalogRequestSchema, {
name: catalogResourceName,
});
@@ -111,13 +108,14 @@ export const useDatabaseCatalogV1Store = defineStore(
});
}
- return (
- getCache(database) ??
- create(DatabaseCatalogSchema, {
+ const cached = getCache(database);
+ if (!cached) {
+ return create(DatabaseCatalogSchema, {
name: ensureDatabaseCatalogResourceName(database),
schemas: [],
- })
- );
+ });
+ }
+ return cached;
};
const removeCache = (name: string) => {