Skip to content

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

chore(sql-editor): ensure default datasource selection according to policy in SQL Editor (#14688)

Liu Ji084b7c566b85
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.ts
index db188028a8..8c1e11a133 100644
--- a/frontend/src/composables/useExecuteSQL.ts
+++ b/frontend/src/composables/useExecuteSQL.ts
@@ -30,6 +30,7 @@ import {
} from "@/types/proto/v1/sql_service";
import {
emptySQLEditorTabQueryContext,
+ ensureDataSourceSelection,
hasPermissionToCreateChangeDatabaseIssue,
} from "@/utils";
import { extractGrpcErrorMessage } from "@/utils/grpcweb";
@@ -310,9 +311,12 @@ const useExecuteSQL = () => {
? database.instance
: params.connection.instance;
const dataSourceId =
- instance === params.connection.instance
- ? (params.connection.dataSourceId ?? "")
- : "";
+ ensureDataSourceSelection(
+ instance === params.connection.instance
+ ? params.connection.dataSourceId
+ : undefined,
+ database
+ ) ?? "";
const resultSet = await sqlStore.query(
{
name: database.name,
diff --git a/frontend/src/utils/sqlEditor.ts b/frontend/src/utils/sqlEditor.ts
index 3c8929e517..4630cda00a 100644
--- a/frontend/src/utils/sqlEditor.ts
+++ b/frontend/src/utils/sqlEditor.ts
@@ -1,4 +1,5 @@
import dayjs from "dayjs";
+import { head } from "lodash-es";
import { v1 as uuidv1 } from "uuid";
import {
useDatabaseV1Store,
@@ -20,7 +21,10 @@ import {
UNKNOWN_DATABASE_NAME,
} from "@/types";
import { Engine } from "@/types/proto/v1/common";
-import type { InstanceResource } from "@/types/proto/v1/instance_service";
+import {
+ DataSourceType,
+ type InstanceResource,
+} from "@/types/proto/v1/instance_service";
import {
DataSourceQueryPolicy_Restriction,
PolicyType,
@@ -205,3 +209,62 @@ export const getAdminDataSourceRestrictionOfDatabase = (
DataSourceQueryPolicy_Restriction.RESTRICTION_UNSPECIFIED,
};
};
+
+export const ensureDataSourceSelection = (
+ current: string | undefined,
+ database: ComposedDatabase
+) => {
+ const restriction = getAdminDataSourceRestrictionOfDatabase(database);
+ const adminDataSource = database.instanceResource.dataSources.find(
+ (ds) => ds.type === DataSourceType.ADMIN
+ )!;
+ const readonlyDataSources = database.instanceResource.dataSources.filter(
+ (ds) => ds.type === DataSourceType.READ_ONLY
+ );
+
+ let behavior: "RO" | "FALLBACK" | "ALLOW_ADMIN";
+ if (
+ restriction.environmentPolicy ===
+ DataSourceQueryPolicy_Restriction.DISALLOW ||
+ restriction.projectPolicy === DataSourceQueryPolicy_Restriction.DISALLOW
+ ) {
+ behavior = "RO";
+ } else if (
+ restriction.environmentPolicy ===
+ DataSourceQueryPolicy_Restriction.FALLBACK ||
+ restriction.projectPolicy === DataSourceQueryPolicy_Restriction.FALLBACK
+ ) {
+ behavior = "FALLBACK";
+ } else {
+ behavior = "ALLOW_ADMIN";
+ }
+ if (behavior === "ALLOW_ADMIN") {
+ if (current) {
+ return current;
+ }
+ return adminDataSource.id;
+ }
+ if (behavior === "FALLBACK") {
+ if (current) {
+ return current;
+ }
+ return head(readonlyDataSources)?.id ?? adminDataSource.id;
+ }
+ if (behavior === "RO") {
+ if (
+ current &&
+ readonlyDataSources.findIndex((ds) => ds.id === current) >= 0
+ ) {
+ return current;
+ }
+ return head(readonlyDataSources)?.id;
+ }
+ console.warn(
+ "[SQL Editor] failed to ensureDataSourceSelection",
+ current,
+ behavior,
+ database,
+ restriction
+ );
+ return undefined;
+};
diff --git a/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue b/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue
index 4e834a27cb..b526a78de5 100644
--- a/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue
@@ -107,7 +107,7 @@
</template>
<script lang="ts" setup>
-import { head, orderBy } from "lodash-es";
+import { orderBy } from "lodash-es";
import { ChevronDown } from "lucide-vue-next";
import { NButton, NPopover, NRadioGroup, NRadio, NTooltip } from "naive-ui";
import { storeToRefs } from "pinia";
@@ -131,7 +131,7 @@ import {
PolicyType,
} from "@/types/proto/v1/org_policy_service";
import { QueryOption_RedisRunCommandsOn } from "@/types/proto/v1/sql_service";
-import { readableDataSourceType } from "@/utils";
+import { ensureDataSourceSelection, readableDataSourceType } from "@/utils";
import { getAdminDataSourceRestrictionOfDatabase } from "@/utils";
import ResultLimitSelect from "./ResultLimitSelect.vue";
@@ -227,21 +227,12 @@ const onDataSourceSelected = (dataSourceId?: string) => {
};
watch(
- () => selectedDataSourceId.value,
- () => {
- // If current connection has data source, skip initial selection.
- if (selectedDataSourceId.value) {
- return;
- }
-
- const readOnlyDataSources = dataSources.value.filter(
- (dataSource) => dataSource.type === DataSourceType.READ_ONLY
- );
- // Default set the first read only data source as selected.
- if (readOnlyDataSources.length > 0) {
- onDataSourceSelected(readOnlyDataSources[0].id);
- } else {
- onDataSourceSelected(head(dataSources.value)?.id);
+ [selectedDataSourceId, database],
+ ([current, database]) => {
+ if (!isValidDatabaseName(database.name)) return;
+ const fixed = ensureDataSourceSelection(current, database);
+ if (fixed !== current) {
+ onDataSourceSelected(fixed);
}
},
{