Skip to content

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

feat(sql-editor): change connected database on the worksheet (#11558)

Liu Ji03b338e3fcc2
diff --git a/frontend/src/utils/sqlEditor.ts b/frontend/src/utils/sqlEditor.ts
index 573d881cf5..66dfc649e3 100644
--- a/frontend/src/utils/sqlEditor.ts
+++ b/frontend/src/utils/sqlEditor.ts
@@ -130,20 +130,17 @@ export const tryConnectToCoreSQLEditorTab = (
// Don't go further if the connection doesn't change.
return;
}
- if (currentTab.status === "NEW" || !currentTab.sheet) {
- // If the current tab is "fresh new", update its connection directly.
- tabStore.updateCurrentTab(tab);
- if (
- overrideTitle &&
- isSimilarToDefaultSQLEditorTabTitle(currentTab.title)
- ) {
- const title = suggestedTabTitleForSQLEditorConnection(tab.connection);
- tabStore.updateCurrentTab({
- title,
- });
- }
- return;
+ tabStore.updateCurrentTab(tab);
+ if (
+ overrideTitle &&
+ isSimilarToDefaultSQLEditorTabTitle(currentTab.title)
+ ) {
+ const title = suggestedTabTitleForSQLEditorConnection(tab.connection);
+ tabStore.updateCurrentTab({
+ title,
+ });
}
+ return;
}
// Otherwise select or add a new tab and set its connection.
diff --git a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetList.vue b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetList.vue
index 0e10484ae1..721ab6b4f2 100644
--- a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetList.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetList.vue
@@ -160,10 +160,24 @@ const mergedItemList = computed(() => {
// Untitled sheets go behind
// They are probably dirty data
(item) => (item.type === "SHEET" && !item.target.title ? 1 : 0),
+ // Unconnected sheets go behind then
+ (item) => {
+ if (item.type === "TAB") {
+ return 0;
+ }
+ if (!item.target.database) {
+ return Number.MAX_VALUE;
+ }
+ const db = useDatabaseV1Store().getDatabaseByName(item.target.database);
+ if (db.uid === String(UNKNOWN_ID)) {
+ return Number.MAX_VALUE;
+ }
+ return 1;
+ },
// Alphabetically otherwise
(item) => item.target.title,
],
- ["asc", "asc"]
+ ["asc", "asc", "asc"]
);
return sortedList;
});
@@ -218,6 +232,10 @@ const renderPrefix = ({ option }: { option: TreeOption }) => {
}
return <ProjectV1Name project={treeNode.project} link={true} />;
} else if (treeNode.database) {
+ const db = treeNode.database;
+ if (db.uid === String(UNKNOWN_ID)) {
+ return null;
+ }
return (
<span class="flex items-center gap-x-1">
<InstanceV1EngineIcon instance={treeNode.database.instanceEntity} />
@@ -269,7 +287,13 @@ const renderLabel = ({ option }: { option: TreeOption }) => {
return null;
}
if (treeNode.database) {
- return <DatabaseV1Name database={treeNode.database} link={false} />;
+ const db = treeNode.database;
+ if (db.uid === String(UNKNOWN_ID)) {
+ return (
+ <span class="text-control-placeholder">{t("sheet.unconnected")}</span>
+ );
+ }
+ return <DatabaseV1Name database={db} link={false} />;
}
return (
diff --git a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue
index f1cb46faa1..d1a75bac36 100644
--- a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue
+++ b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue
@@ -73,7 +73,11 @@ import type {
SQLEditorTreeNodeTarget,
SQLEditorTreeNodeType,
} from "@/types";
-import { ExpandableTreeNodeTypes, UNKNOWN_ID } from "@/types";
+import {
+ DEFAULT_SQL_EDITOR_TAB_MODE,
+ ExpandableTreeNodeTypes,
+ UNKNOWN_ID,
+} from "@/types";
import { findAncestor, isDescendantOf } from "@/utils";
import { useSQLEditorContext } from "../../context";
import {
@@ -207,7 +211,10 @@ const nodeProps = ({ option }: { option: TreeOption }) => {
// Check if clicked on the content part.
// And ignore the fold/unfold arrow.
if (type === "database") {
- setConnection(node);
+ setConnection(node, {
+ sheet: tabStore.currentTab?.sheet ?? "",
+ mode: DEFAULT_SQL_EDITOR_TAB_MODE,
+ });
showConnectionPanel.value = false;
}
diff --git a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
index 3e6a150469..55baa9f259 100644
--- a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
@@ -116,6 +116,7 @@ import {
useActuatorV1Store,
useSQLEditorTabStore,
useConnectionOfCurrentSQLEditorTab,
+ useWorkSheetStore,
} from "@/store";
import type { FeatureType, SQLEditorQueryParams } from "@/types";
import { keyboardShortcutStr } from "@/utils";
@@ -193,6 +194,13 @@ const allowSave = computed(() => {
if (!tab) {
return false;
}
+
+ if (tab.sheet) {
+ const sheet = useWorkSheetStore().getSheetByName(tab.sheet);
+ if (sheet && sheet.database !== tab.connection.database) {
+ return true;
+ }
+ }
if (tab.status === "NEW" || tab.status === "CLEAN") {
return false;
}
diff --git a/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue b/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
index deb5de904e..3bb148c8ea 100644
--- a/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
@@ -57,9 +57,10 @@ const doSaveSheet = async (tab: SQLEditorTab) => {
{
name: sheet,
title: title,
+ database: tab.connection.database,
content: new TextEncoder().encode(statement),
},
- ["title", "content"]
+ ["title", "content", "database"]
);
if (updatedSheet) {
const tab = tabStore.tabList.find((t) => t.sheet === updatedSheet.name);