Skip to content

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

fix: filter in search database (#11130)

ecmadao602d8fb21ad7
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index 9624dd5849..af2ed726a3 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -126,18 +126,11 @@ func (s *DatabaseService) GetDatabase(ctx context.Context, request *v1pb.GetData
}
func (s *DatabaseService) SearchDatabases(ctx context.Context, request *v1pb.SearchDatabasesRequest) (*v1pb.SearchDatabasesResponse, error) {
- find := &store.FindDatabaseMessage{}
- if request.Filter != "" {
- projectFilter, err := getProjectFilter(request.Filter)
- if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, err.Error())
- }
- projectID, err := common.GetProjectID(projectFilter)
- if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, "invalid project %q in the filter", projectFilter)
- }
- find.ProjectID = &projectID
+ find, err := getDatabaseFind(request.Filter)
+ if err != nil {
+ return nil, err
}
+
permission := iam.PermissionDatabasesGet
if request.GetPermission() == iam.PermissionDatabasesQuery.String() {
permission = iam.PermissionDatabasesQuery
@@ -175,6 +168,7 @@ func (s *DatabaseService) ListDatabases(ctx context.Context, request *v1pb.ListD
if instanceID != "-" {
find.InstanceID = &instanceID
}
+
if request.Filter != "" {
projectFilter, err := getProjectFilter(request.Filter)
if err != nil {
@@ -205,6 +199,45 @@ func (s *DatabaseService) ListDatabases(ctx context.Context, request *v1pb.ListD
return response, nil
}
+func getDatabaseFind(filter string) (*store.FindDatabaseMessage, error) {
+ filters, err := parseFilter(filter)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+
+ find := &store.FindDatabaseMessage{}
+
+ for _, spec := range filters {
+ switch spec.key {
+ case "instance":
+ if spec.operator != comparatorTypeEqual {
+ return nil, status.Errorf(codes.InvalidArgument, `only support "=" operation for "instance" filter`)
+ }
+ instanceID, err := common.GetInstanceID(spec.value)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ if instanceID != "-" {
+ find.InstanceID = &instanceID
+ }
+ case "project":
+ if spec.operator != comparatorTypeEqual {
+ return nil, status.Errorf(codes.InvalidArgument, `only support "=" operation for "project" filter`)
+ }
+
+ projectID, err := common.GetProjectID(spec.value)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ if projectID != "-" {
+ find.ProjectID = &projectID
+ }
+ }
+ }
+
+ return find, nil
+}
+
func filterDatabasesV2(ctx context.Context, s *store.Store, iamManager *iam.Manager, databases []*store.DatabaseMessage, needPermission iam.Permission) ([]*store.DatabaseMessage, error) {
user, ok := ctx.Value(common.UserContextKey).(*store.UserMessage)
if !ok {
diff --git a/frontend/src/components/AlterSchemaPrepForm/AlterSchemaPrepForm.vue b/frontend/src/components/AlterSchemaPrepForm/AlterSchemaPrepForm.vue
index f0578dc9bc..7496c63a42 100644
--- a/frontend/src/components/AlterSchemaPrepForm/AlterSchemaPrepForm.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/AlterSchemaPrepForm.vue
@@ -477,7 +477,7 @@ watch(
const environmentList = useEnvironmentV1List(false /* !showDeleted */);
const { ready } = useSearchDatabaseV1List({
- parent: "instances/-",
+ filter: "instances/-",
});
const prepareDatabaseGroupList = async () => {
diff --git a/frontend/src/components/AnomalyCenter/AnomalyCenterDashboard.vue b/frontend/src/components/AnomalyCenter/AnomalyCenterDashboard.vue
index 83a8d4da8c..e1c2c71efc 100644
--- a/frontend/src/components/AnomalyCenter/AnomalyCenterDashboard.vue
+++ b/frontend/src/components/AnomalyCenter/AnomalyCenterDashboard.vue
@@ -175,7 +175,7 @@ const environmentList = useEnvironmentV1List(false /* !showDeleted */);
const prepareDatabaseList = () => {
// It will also be called when user logout
if (currentUserV1.value.name !== UNKNOWN_USER_NAME) {
- const filter = `instance == "instances/-"`;
+ const filter = `instance = "instances/-"`;
databaseStore.searchDatabases({
filter,
});
diff --git a/frontend/src/components/Changelist/ChangelistDetail/ApplyToDatabasePanel/ApplyToDatabasePanel.vue b/frontend/src/components/Changelist/ChangelistDetail/ApplyToDatabasePanel/ApplyToDatabasePanel.vue
index 429e63e42b..dbf0daebef 100644
--- a/frontend/src/components/Changelist/ChangelistDetail/ApplyToDatabasePanel/ApplyToDatabasePanel.vue
+++ b/frontend/src/components/Changelist/ChangelistDetail/ApplyToDatabasePanel/ApplyToDatabasePanel.vue
@@ -128,8 +128,7 @@ const state = reactive<LocalState>({
const { ready } = useSearchDatabaseV1List(
computed(() => ({
- parent: "instances/-",
- filter: `project == "${project.value.name}"`,
+ filter: `project = "${project.value.name}" && instances/-`,
}))
);
diff --git a/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/DatabaseResourceSelector.vue b/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/DatabaseResourceSelector.vue
index 2023e5446d..afc9f729d2 100644
--- a/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/DatabaseResourceSelector.vue
+++ b/frontend/src/components/Issue/panel/RequestExportPanel/ExportResourceForm/DatabaseResourceSelector.vue
@@ -77,10 +77,7 @@ watch(
loading.value = true;
const project = await useProjectV1Store().getOrFetchProjectByUID(projectId);
- const filters = [
- `instance == "instances/-"`,
- `project == "${project.name}"`,
- ];
+ const filters = [`instance = "instances/-"`, `project = "${project.name}"`];
const databaseList = await databaseStore.searchDatabases({
filter: filters.join(" && "),
});
diff --git a/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/DatabaseResourceSelector.vue b/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/DatabaseResourceSelector.vue
index 273357d2bd..20246647bd 100644
--- a/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/DatabaseResourceSelector.vue
+++ b/frontend/src/components/Issue/panel/RequestQueryPanel/DatabaseResourceForm/DatabaseResourceSelector.vue
@@ -80,10 +80,7 @@ watch(
const project = await useProjectV1Store().getOrFetchProjectByUID(
props.projectId
);
- const filters = [
- `instance == "instances/-"`,
- `project == "${project.name}"`,
- ];
+ const filters = [`instance = "instances/-"`, `project = "${project.name}"`];
const databaseList = await databaseStore.searchDatabases({
filter: filters.join(" && "),
});
diff --git a/frontend/src/components/IssueV1/components/IssueSearch/useSearchScopeOptions.ts b/frontend/src/components/IssueV1/components/IssueSearch/useSearchScopeOptions.ts
index 7b0a1a6b06..395f907382 100644
--- a/frontend/src/components/IssueV1/components/IssueSearch/useSearchScopeOptions.ts
+++ b/frontend/src/components/IssueV1/components/IssueSearch/useSearchScopeOptions.ts
@@ -58,7 +58,7 @@ export const useSearchScopeOptions = (
const { projectList } = useProjectV1List();
const { instanceList } = useInstanceV1List(false /* !showDeleted */);
const { databaseList } = useSearchDatabaseV1List({
- parent: "instances/-",
+ filter: "instances/-",
});
const principalSearchValueOptions = computed(() => {
diff --git a/frontend/src/components/IssueV1/logic/initialize/create.ts b/frontend/src/components/IssueV1/logic/initialize/create.ts
index 987578d80b..39a64a4a34 100644
--- a/frontend/src/components/IssueV1/logic/initialize/create.ts
+++ b/frontend/src/components/IssueV1/logic/initialize/create.ts
@@ -547,7 +547,7 @@ export const prepareDatabaseList = async (
};
const prepareDatabaseListByProject = async (project: string) => {
- const filters = [`instance == "instances/-"`, `project == "${project}"`];
+ const filters = [`instance = "instances/-"`, `project = "${project}"`];
await useDatabaseV1Store().searchDatabases({
filter: filters.join(" && "),
});
diff --git a/frontend/src/components/ProvideDashboardContext.vue b/frontend/src/components/ProvideDashboardContext.vue
index 01619342b9..a989fa0f84 100644
--- a/frontend/src/components/ProvideDashboardContext.vue
+++ b/frontend/src/components/ProvideDashboardContext.vue
@@ -27,10 +27,10 @@ const policyStore = usePolicyV1Store();
const databaseStore = useDatabaseV1Store();
const prepareDatabases = async () => {
- const filters = [`instance == "instances/-"`];
+ const filters = [`instance = "instances/-"`];
// If `projectId` is provided in the route, filter the database list by the project.
if (route.params.projectId) {
- filters.push(`project == "${projectNamePrefix}${route.params.projectId}"`);
+ filters.push(`project = "${projectNamePrefix}${route.params.projectId}"`);
}
await databaseStore.searchDatabases({
filter: filters.join(" && "),
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index c8b3e5da60..0b7bc4774b 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -138,9 +138,9 @@ const prepareDatabases = async () => {
return;
}
const { project } = editorStore;
- const filters = [`instance == "instances/-"`];
+ const filters = [`instance = "instances/-"`];
if (project) {
- filters.push(`project == "${project}"`);
+ filters.push(`project = "${project}"`);
}
// `databaseList` is the database list accessible by current user.
// Only accessible instances and databases will be listed in the tree.
diff --git a/frontend/src/components/TransferDatabaseForm.vue b/frontend/src/components/TransferDatabaseForm.vue
index d4a6f7ca10..0dabf4785d 100644
--- a/frontend/src/components/TransferDatabaseForm.vue
+++ b/frontend/src/components/TransferDatabaseForm.vue
@@ -142,7 +142,7 @@ const state = reactive<LocalState>({
const { project } = useProjectV1ByUID(toRef(props, "projectId"));
const prepare = async () => {
- const filter = `instance == "instances/-"`;
+ const filter = `instance = "instances/-"`;
databaseStore.searchDatabases({
filter,
});
diff --git a/frontend/src/components/v2/Select/DatabaseSelect.vue b/frontend/src/components/v2/Select/DatabaseSelect.vue
index 271ae74815..359187f2b8 100644
--- a/frontend/src/components/v2/Select/DatabaseSelect.vue
+++ b/frontend/src/components/v2/Select/DatabaseSelect.vue
@@ -67,7 +67,7 @@ const emit = defineEmits<{
const { t } = useI18n();
const currentUserV1 = useCurrentUserV1();
const { ready } = useSearchDatabaseV1List({
- parent: "instances/-",
+ filter: "instances/-",
});
const rawDatabaseList = computed(() => {
diff --git a/frontend/src/types/proto/v1/database_service.ts b/frontend/src/types/proto/v1/database_service.ts
index 9c14095a47..63adc1625f 100644
--- a/frontend/src/types/proto/v1/database_service.ts
+++ b/frontend/src/types/proto/v1/database_service.ts
@@ -165,7 +165,15 @@ export interface SearchDatabasesRequest {
pageToken: string;
/**
* Filter is used to filter databases returned in the list.
- * For example, "project = projects/{project}" can be used to list databases in a project.
+ * follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax.
+ * The field only support in filter:
+ * - project with "=" operator, for example:
+ * - project = "projects/sample-project"
+ * - project = "projects/-"
+ * - instance with "=" operator, for example:
+ * - instance = "instances/mysql"
+ * - instance = "instances/-"
+ * for example, we can use project = "projects/sample" && instance = "instances/-" to list all databases in the sample project.
*/
filter: string;
/**
diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vue
index 8d17d15b1c..7d646a7879 100644
--- a/frontend/src/views/InstanceDetail.vue
+++ b/frontend/src/views/InstanceDetail.vue
@@ -123,7 +123,7 @@ const environment = computed(() => {
});
watchEffect(() => {
- const filter = `instance == "${instance.value.name}"`;
+ const filter = `instance = "${instance.value.name}"`;
databaseStore.searchDatabases({
filter,
});
@@ -159,7 +159,7 @@ const syncSchema = async () => {
state.syncingSchema = true;
try {
await instanceV1Store.syncInstance(instance.value).then(() => {
- const filter = `instance == "${instance.value.name}"`;
+ const filter = `instance = "${instance.value.name}"`;
databaseStore.searchDatabases({
filter,
});
diff --git a/frontend/src/views/project/ProjectDatabaseDashboard.vue b/frontend/src/views/project/ProjectDatabaseDashboard.vue
index 7f6ea3400f..4439ad3a60 100644
--- a/frontend/src/views/project/ProjectDatabaseDashboard.vue
+++ b/frontend/src/views/project/ProjectDatabaseDashboard.vue
@@ -31,8 +31,7 @@ const project = computed(() => {
useSearchDatabaseV1List(
computed(() => ({
- parent: "instances/-",
- filter: `project == "${project.value.name}"`,
+ filter: `project = "${project.value.name}" && instances/-`,
}))
);
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index 1b5fae7e6a..ceafd010ae 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -3278,7 +3278,7 @@ This is the concept of schema in Postgres, but it&#39;s a no-op for MySQL.
| page_token | [string](#string) | | A page token, received from a previous `ListDatabases` call. Provide this to retrieve the subsequent page.
When paginating, all other parameters provided to `ListDatabases` must match the call that provided the page token. |
-| filter | [string](#string) | | Filter is used to filter databases returned in the list. For example, &#34;project = projects/{project}&#34; can be used to list databases in a project. |
+| filter | [string](#string) | | Filter is used to filter databases returned in the list. follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax. The field only support in filter: - project with &#34;=&#34; operator, for example: - project = &#34;projects/sample-project&#34; - project = &#34;projects/-&#34; - instance with &#34;=&#34; operator, for example: - instance = &#34;instances/mysql&#34; - instance = &#34;instances/-&#34; for example, we can use project = &#34;projects/sample&#34; &amp;&amp; instance = &#34;instances/-&#34; to list all databases in the sample project. |
| permission | [string](#string) | | By default, the permission &#34;bb.databases.get&#34; is used. Alternatively, &#34;bb.databases.query&#34; can be used to retrieve databases with query permissions to. |
diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.html
index 0745543996..65dc3a9341 100644
--- a/proto/gen/grpc-doc/v1/index.html
+++ b/proto/gen/grpc-doc/v1/index.html
@@ -9630,7 +9630,15 @@ the call that provided the page token. </p></td>
<td><a href="#string">string</a></td>
<td></td>
<td><p>Filter is used to filter databases returned in the list.
-For example, &#34;project = projects/{project}&#34; can be used to list databases in a project. </p></td>
+follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax.
+The field only support in filter:
+- project with &#34;=&#34; operator, for example:
+ - project = &#34;projects/sample-project&#34;
+ - project = &#34;projects/-&#34;
+- instance with &#34;=&#34; operator, for example:
+ - instance = &#34;instances/mysql&#34;
+ - instance = &#34;instances/-&#34;
+for example, we can use project = &#34;projects/sample&#34; &amp;&amp; instance = &#34;instances/-&#34; to list all databases in the sample project. </p></td>
</tr>
<tr>
diff --git a/proto/generated-go/v1/database_service.pb.go b/proto/generated-go/v1/database_service.pb.go
index 011eae6238..c1b4364929 100644
--- a/proto/generated-go/v1/database_service.pb.go
+++ b/proto/generated-go/v1/database_service.pb.go
@@ -837,7 +837,17 @@ type SearchDatabasesRequest struct {
// the call that provided the page token.
PageToken string `protobuf:"bytes,2,opt,name=page_token,json=pageToken,proto3" json:"page_token,omitempty"`
// Filter is used to filter databases returned in the list.
- // For example, "project = projects/{project}" can be used to list databases in a project.
+ // follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax.
+ // The field only support in filter:
+ // - project with "=" operator, for example:
+ // - project = "projects/sample-project"
+ // - project = "projects/-"
+ //
+ // - instance with "=" operator, for example:
+ // - instance = "instances/mysql"
+ // - instance = "instances/-"
+ //
+ // for example, we can use project = "projects/sample" && instance = "instances/-" to list all databases in the sample project.
Filter string `protobuf:"bytes,3,opt,name=filter,proto3" json:"filter,omitempty"`
// By default, the permission "bb.databases.get" is used.
// Alternatively, "bb.databases.query" can be used to retrieve databases with query permissions to.
diff --git a/proto/v1/database_service.proto b/proto/v1/database_service.proto
index dc5c497db6..76397e2554 100644
--- a/proto/v1/database_service.proto
+++ b/proto/v1/database_service.proto
@@ -194,7 +194,15 @@ message SearchDatabasesRequest {
string page_token = 2;
// Filter is used to filter databases returned in the list.
- // For example, "project = projects/{project}" can be used to list databases in a project.
+ // follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax.
+ // The field only support in filter:
+ // - project with "=" operator, for example:
+ // - project = "projects/sample-project"
+ // - project = "projects/-"
+ // - instance with "=" operator, for example:
+ // - instance = "instances/mysql"
+ // - instance = "instances/-"
+ // for example, we can use project = "projects/sample" && instance = "instances/-" to list all databases in the sample project.
string filter = 3;
// By default, the permission "bb.databases.get" is used.