Skip to content

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

fix(sql-editor): default data source (#13228)

boojackb0981f61fef4
diff --git a/frontend/src/utils/sqlEditor.ts b/frontend/src/utils/sqlEditor.ts
index 3b08bb65ad..2cf35e5bd3 100644
--- a/frontend/src/utils/sqlEditor.ts
+++ b/frontend/src/utils/sqlEditor.ts
@@ -3,6 +3,7 @@ import { v1 as uuidv1 } from "uuid";
import {
useDatabaseV1Store,
useInstanceResourceByName,
+ usePolicyV1Store,
useSQLEditorTabStore,
} from "@/store";
import type {
@@ -19,6 +20,10 @@ import {
} from "@/types";
import { Engine } from "@/types/proto/v1/common";
import type { InstanceResource } from "@/types/proto/v1/instance_service";
+import {
+ DataSourceQueryPolicy_Restriction,
+ PolicyType,
+} from "@/types/proto/v1/org_policy_service";
import { instanceV1AllowsCrossDatabaseQuery } from "./v1/instance";
export const defaultSQLEditorTab = (): SQLEditorTab => {
@@ -165,3 +170,29 @@ export const emptySQLEditorTabQueryContext = (): SQLEditorTabQueryContext => ({
statement: "",
},
});
+
+export const getAdminDataSourceRestrictionOfDatabase = (
+ database: ComposedDatabase
+) => {
+ const policyStore = usePolicyV1Store();
+ const projectLevelPolicy = policyStore.getPolicyByParentAndType({
+ parentPath: database.project,
+ policyType: PolicyType.DATA_SOURCE_QUERY,
+ });
+ const projectLevelAdminDSRestriction =
+ projectLevelPolicy?.dataSourceQueryPolicy?.adminDataSourceRestriction;
+ const envLevelPolicy = policyStore.getPolicyByParentAndType({
+ parentPath: database.effectiveEnvironment,
+ policyType: PolicyType.DATA_SOURCE_QUERY,
+ });
+ const envLevelAdminDSRestriction =
+ envLevelPolicy?.dataSourceQueryPolicy?.adminDataSourceRestriction;
+ return {
+ environmentPolicy:
+ envLevelAdminDSRestriction ??
+ DataSourceQueryPolicy_Restriction.RESTRICTION_UNSPECIFIED,
+ projectPolicy:
+ projectLevelAdminDSRestriction ??
+ DataSourceQueryPolicy_Restriction.RESTRICTION_UNSPECIFIED,
+ };
+};
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx
index 7fc113f72a..de72cc38ab 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx
@@ -1,3 +1,4 @@
+import { head } from "lodash-es";
import {
CodeIcon,
CopyIcon,
@@ -13,11 +14,7 @@ import { useExecuteSQL } from "@/composables/useExecuteSQL";
import { t } from "@/plugins/i18n";
import { PROJECT_V1_ROUTE_DATABASE_DETAIL } from "@/router/dashboard/projectV1";
import { SQL_EDITOR_DATABASE_MODULE } from "@/router/sqlEditor";
-import {
- pushNotification,
- useAppFeature,
- useSQLEditorTabStore,
-} from "@/store";
+import { pushNotification, useAppFeature, useSQLEditorTabStore } from "@/store";
import {
DEFAULT_SQL_EDITOR_TAB_MODE,
type ComposedDatabase,
@@ -25,6 +22,7 @@ import {
type Position,
} from "@/types";
import { Engine } from "@/types/proto/v1/common";
+import { DataSource, DataSourceType } from "@/types/proto/v1/instance_service";
import {
defer,
extractInstanceResourceName,
@@ -325,6 +323,7 @@ const runQuery = async (
database: database.name,
schema,
table: tableOrViewName,
+ dataSourceId: getDefaultQueriableDataSourceOfDatabase(database).id,
},
mode: DEFAULT_SQL_EDITOR_TAB_MODE,
worksheet: "",
@@ -391,3 +390,14 @@ export const selectAllFromTableOrView = async (
}
runQuery(db, schema, tableOrViewName, query);
};
+
+const getDefaultQueriableDataSourceOfDatabase = (
+ database: ComposedDatabase
+) => {
+ const dataSources = database.instanceResource.dataSources;
+ const readonlyDataSources = dataSources.filter(
+ (ds) => ds.type === DataSourceType.READ_ONLY
+ );
+ // First try to use readonly data source if available.
+ return (head(readonlyDataSources) || head(dataSources)) as DataSource;
+};
diff --git a/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue b/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue
index 8a13332ff6..8a84954672 100644
--- a/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue
@@ -61,6 +61,7 @@ import {
DataSourceQueryPolicy_Restriction,
PolicyType,
} from "@/types/proto/v1/org_policy_service";
+import { getAdminDataSourceRestrictionOfDatabase } from "@/utils";
const { t } = useI18n();
const tabStore = useSQLEditorTabStore();
@@ -75,27 +76,7 @@ const adminDataSourceRestriction = computed(() => {
projectPolicy: DataSourceQueryPolicy_Restriction.RESTRICTION_UNSPECIFIED,
};
}
-
- const projectLevelPolicy = policyStore.getPolicyByParentAndType({
- parentPath: database.value.project,
- policyType: PolicyType.DATA_SOURCE_QUERY,
- });
- const projectLevelAdminDSRestriction =
- projectLevelPolicy?.dataSourceQueryPolicy?.adminDataSourceRestriction;
- const envLevelPolicy = policyStore.getPolicyByParentAndType({
- parentPath: database.value.effectiveEnvironment,
- policyType: PolicyType.DATA_SOURCE_QUERY,
- });
- const envLevelAdminDSRestriction =
- envLevelPolicy?.dataSourceQueryPolicy?.adminDataSourceRestriction;
- return {
- environmentPolicy:
- envLevelAdminDSRestriction ??
- DataSourceQueryPolicy_Restriction.RESTRICTION_UNSPECIFIED,
- projectPolicy:
- projectLevelAdminDSRestriction ??
- DataSourceQueryPolicy_Restriction.RESTRICTION_UNSPECIFIED,
- };
+ return getAdminDataSourceRestrictionOfDatabase(database.value);
});
const selectedDataSourceId = computed(() => {