Skip to content

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

chore: ensure instance name is unique (#3780)

ecmadao652829ba6e86
diff --git a/frontend/src/components/CreateInstanceForm.vue b/frontend/src/components/CreateInstanceForm.vue
index 5369dcefea..d559b0ead9 100644
--- a/frontend/src/components/CreateInstanceForm.vue
+++ b/frontend/src/components/CreateInstanceForm.vue
@@ -31,6 +31,9 @@
 
<span style="color: red">*</span>
</label>
+ <p class="text-sm text-gray-500 mt-1 mb-2">
+ {{ $t("instance.instance-name-unique") }}
+ </p>
<input
id="name"
required
diff --git a/frontend/src/components/InstanceForm.vue b/frontend/src/components/InstanceForm.vue
index ee32700ebf..3ddcae93e1 100644
--- a/frontend/src/components/InstanceForm.vue
+++ b/frontend/src/components/InstanceForm.vue
@@ -10,6 +10,9 @@
<InstanceEngineIcon :instance="state.instance" />
<span class="ml-1">{{ state.instance.engineVersion }}</span>
</label>
+ <p class="text-sm text-gray-500 mt-1 mb-2">
+ {{ $t("instance.instance-name-unique") }}
+ </p>
<input
id="name"
required
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 9d2018ff8c..d30d434bf3 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -667,6 +667,7 @@
"your-snowflake-account-name": "your Snowflake account name",
"port": "Port",
"instance-name": "Instance Name",
+ "instance-name-unique": "The instance name should be unique.",
"snowflake-web-console": "Snowflake Web Console",
"external-link": "External Link",
"web-console": "Web Console",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index a89209e2c9..d8b021bae5 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -667,6 +667,7 @@
"your-snowflake-account-name": "您的 Snowflake @:{'instance.account'}@:{'instance.name'}",
"port": "端口",
"instance-name": "@:common.instance@:instance.name",
+ "instance-name-unique": "@:{'common.instance'}@:{'instance.name'}需要保证唯一性",
"snowflake-web-console": "Snowflake @:{'instance.web-console'}",
"external-link": "外部链接",
"web-console": "Web 控制台",
diff --git a/server/instance.go b/server/instance.go
index 9fd40975e5..964ae83636 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -463,6 +463,12 @@ func (s *Server) createInstance(ctx context.Context, create *store.InstanceCreat
if err := s.instanceCountGuard(ctx); err != nil {
return nil, err
}
+ if err := s.validateInstanceName(ctx, create.Name); err != nil {
+ return nil, err
+ }
+ if err := s.validateDataSourceList(create.DataSourceList); err != nil {
+ return nil, err
+ }
if err := s.disallowBytebaseStore(create.Engine, create.Host, create.Port); err != nil {
return nil, echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
@@ -504,6 +510,17 @@ func (s *Server) createInstance(ctx context.Context, create *store.InstanceCreat
}
func (s *Server) updateInstance(ctx context.Context, patch *store.InstancePatch) (*api.Instance, error) {
+ if v := patch.Name; v != nil {
+ if err := s.validateInstanceName(ctx, *v); err != nil {
+ return nil, err
+ }
+ }
+ if v := patch.DataSourceList; v != nil {
+ if err := s.validateDataSourceList(v); err != nil {
+ return nil, err
+ }
+ }
+
instance, err := s.store.GetInstanceByID(ctx, patch.ID)
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get instance ID: %v", patch.ID)).SetInternal(err)
@@ -588,3 +605,33 @@ func (s *Server) updateInstance(ctx context.Context, patch *store.InstancePatch)
return instancePatched, nil
}
+
+func (s *Server) validateInstanceName(ctx context.Context, instanceName string) error {
+ count, err := s.store.CountInstance(ctx, &api.InstanceFind{
+ Name: &instanceName,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to count the instance by name").SetInternal(err)
+ }
+
+ if count > 0 {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Duplicate instance name %s", instanceName))
+ }
+
+ return nil
+}
+
+func (*Server) validateDataSourceList(dataSourceList []*api.DataSourceCreate) error {
+ dataSourceMap := map[api.DataSourceType]bool{}
+ for _, dataSource := range dataSourceList {
+ if dataSourceMap[dataSource.Type] {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Duplicate data source type %s", dataSource.Type))
+ }
+ dataSourceMap[dataSource.Type] = true
+ }
+ if !dataSourceMap[api.Admin] {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Missing required data source type %s", api.Admin))
+ }
+
+ return nil
+}