Skip to content

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

chore(sql-editor): update initial connection from query (#10854)

boojack3f1fe20a279d
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index 8f1386e465..9573e6a8d5 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -31,6 +31,7 @@ import {
useCurrentUserV1,
useWorkSheetStore,
useDatabaseV1Store,
+ useFilterStore,
} from "@/store";
import { useSQLEditorTreeStore } from "@/store/modules/sqlEditorTree";
import { projectNamePrefix } from "@/store/modules/v1/common";
@@ -229,31 +230,6 @@ const prepareConnectionSlug = async () => {
return false;
}
- const connect = (connection: Connection) => {
- const tab = tabStore.currentTab;
- if (tab.sheetName) {
- // Don't touch a saved sheet.
- tabStore.addTab();
- return;
- }
- const target: CoreTabInfo = {
- connection,
- mode: TabMode.ReadOnly,
- };
-
- if (isSimilarTab(target, tabStore.currentTab)) {
- // Don't go further if the connection doesn't change.
- return;
- }
- const name = getSuggestedTabNameFromConnection(target.connection);
- tabStore.selectOrAddSimilarTab(
- target,
- /* beside */ false,
- /* defaultTabName */ name
- );
- tabStore.updateCurrentTab(target);
- };
-
if (Number.isNaN(databaseId)) {
// connected to instance
const connection = await treeStore.fetchConnectionByInstanceId(
@@ -271,12 +247,12 @@ const prepareConnectionSlug = async () => {
return true;
};
-// Get sheetId from query.
const setConnectionFromQuery = async () => {
// Priority:
// 1. idFromSlug in sheetSlug
// 2. instanceId and databaseId in connectionSlug
- // 3. disconnected
+ // 3. datanase in global filter
+ // 4. disconnected
if (await prepareSheet()) {
return;
@@ -286,6 +262,19 @@ const setConnectionFromQuery = async () => {
return;
}
+ const { filter } = useFilterStore();
+ if (filter.database) {
+ const database = await databaseStore.getOrFetchDatabaseByName(
+ filter.database,
+ true
+ );
+ connect({
+ instanceId: database.instanceEntity.uid,
+ databaseId: database.uid,
+ });
+ return;
+ }
+
// Keep disconnected otherwise
// We don't need to `selectOrAddTempTab` here since we already have the
// default tab.
@@ -352,6 +341,31 @@ const syncURLWithConnection = () => {
);
};
+const connect = (connection: Connection) => {
+ const tab = tabStore.currentTab;
+ if (tab.sheetName) {
+ // Don't touch a saved sheet.
+ tabStore.addTab();
+ return;
+ }
+ const target: CoreTabInfo = {
+ connection,
+ mode: TabMode.ReadOnly,
+ };
+
+ if (isSimilarTab(target, tabStore.currentTab)) {
+ // Don't go further if the connection doesn't change.
+ return;
+ }
+ const name = getSuggestedTabNameFromConnection(target.connection);
+ tabStore.selectOrAddSimilarTab(
+ target,
+ /* beside */ false,
+ /* defaultTabName */ name
+ );
+ tabStore.updateCurrentTab(target);
+};
+
onMounted(async () => {
await router.isReady();
diff --git a/frontend/src/store/modules/tab.ts b/frontend/src/store/modules/tab.ts
index 37894af264..ee3cc8d371 100644
--- a/frontend/src/store/modules/tab.ts
+++ b/frontend/src/store/modules/tab.ts
@@ -11,7 +11,6 @@ import {
WebStorageHelper,
isDisconnectedTab,
} from "@/utils";
-import { useFilterStore } from "./filter";
import { useWebTerminalV1Store } from "./v1";
import { useWorkSheetStore } from "./v1/worksheet";
@@ -39,7 +38,6 @@ type PersistentTabInfo = Pick<TabInfo, typeof PERSISTENT_TAB_FIELDS[number]>;
export const useTabStore = defineStore("tab", () => {
const storage = new WebStorageHelper("bb.sql-editor.tab-list", localStorage);
- const { filter } = useFilterStore();
// states
// We store the tabIdList and the tabs separately.
@@ -178,17 +176,13 @@ export const useTabStore = defineStore("tab", () => {
// Load session from local storage.
// Reset if failed.
const init = () => {
- if (filter.project || filter.database) {
- // Don't load tabs from local storage if the filter is specified.
- } else {
- // Load tabIdList and currentTabId
- tabIdList.value = storage.load(KEYS.tabIdList, []);
- currentTabId.value = storage.load(KEYS.currentTabId, INITIAL_TAB.id);
- if (tabIdList.value.indexOf(currentTabId.value) < 0) {
- // currentTabId is not in tabIdList accidentally
- // fallback to the first tab
- currentTabId.value = tabIdList.value[0];
- }
+ // Load tabIdList and currentTabId
+ tabIdList.value = storage.load(KEYS.tabIdList, []);
+ currentTabId.value = storage.load(KEYS.currentTabId, INITIAL_TAB.id);
+ if (tabIdList.value.indexOf(currentTabId.value) < 0) {
+ // currentTabId is not in tabIdList accidentally
+ // fallback to the first tab
+ currentTabId.value = tabIdList.value[0];
}
// Fallback to the initial tab if tabIdList is empty.
diff --git a/frontend/src/utils/tab.ts b/frontend/src/utils/tab.ts
index b2a127005e..7127af14fe 100644
--- a/frontend/src/utils/tab.ts
+++ b/frontend/src/utils/tab.ts
@@ -130,7 +130,7 @@ export const tryConnectToCoreTab = (tab: CoreTabInfo) => {
// If the current tab is "fresh new", update its connection directly.
tabStore.updateCurrentTab(tab);
} else {
- // Otherwise select or add a new tab and set its connection
+ // Otherwise select or add a new tab and set its connection.
const name = getSuggestedTabNameFromConnection(tab.connection);
tabStore.selectOrAddSimilarTab(
tab,
diff --git a/frontend/src/views/sql-editor/TabList/TabList.vue b/frontend/src/views/sql-editor/TabList/TabList.vue
index 6403f91b2e..0841ebc6e4 100644
--- a/frontend/src/views/sql-editor/TabList/TabList.vue
+++ b/frontend/src/views/sql-editor/TabList/TabList.vue
@@ -6,7 +6,7 @@
<Draggable
id="tab-list"
ref="tabListRef"
- v-model="tabStore.tabIdList"
+ v-model="filteredTabIdList"
item-key="id"
animation="300"
class="tab-list hide-scrollbar"
@@ -60,9 +60,14 @@ import { useI18n } from "vue-i18n";
import Draggable from "vuedraggable";
import ProfileDropdown from "@/components/ProfileDropdown.vue";
import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
-import { useTabStore, useActuatorV1Store } from "@/store";
+import {
+ useTabStore,
+ useActuatorV1Store,
+ useFilterStore,
+ useDatabaseV1Store,
+} from "@/store";
import type { TabInfo } from "@/types";
-import { TabMode } from "@/types";
+import { TabMode, UNKNOWN_ID } from "@/types";
import {
defer,
getSuggestedTabNameFromConnection,
@@ -81,6 +86,8 @@ type LocalState = {
const tabStore = useTabStore();
const { t } = useI18n();
+const { filter } = useFilterStore();
+const databaseStore = useDatabaseV1Store();
const dialog = useDialog();
const state = reactive<LocalState>({
@@ -102,6 +109,26 @@ const showProfileDropdown = computed(() => {
return pageMode.value === "BUNDLED";
});
+const filteredTabIdList = computed(() => {
+ // If a database is selected, only show tabs that are associated with the database.
+ if (filter.database) {
+ const database = databaseStore.getDatabaseByName(filter.database);
+ return tabStore.tabIdList.filter((id) => {
+ const tab = tabStore.getTabById(id);
+ if (
+ tab.connection.databaseId === String(UNKNOWN_ID) ||
+ tab.connection.databaseId === database.uid
+ ) {
+ return true;
+ } else {
+ return false;
+ }
+ });
+ }
+
+ return tabStore.tabIdList;
+});
+
const handleSelectTab = async (tab: TabInfo) => {
tabStore.setCurrentTabId(tab.id);
};