Skip to content

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

feat: improve admin-mode entrances (#3766)

Liu Jibbcf74d1f156
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 86ac17a1b3..28e03c65c1 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1390,7 +1390,6 @@
"format": "Format",
"sql-review-result": "SQL review result",
"alter-table": "Alter table",
- "open-connection": "Open connection",
"visualize-explain": "Visualize Explain",
"sql-execute-in-protected-environment": "The SQL statement below will be executed in a protected environment.",
"rows-upper-limit": "reached the limit of query results",
@@ -1398,7 +1397,12 @@
"readonly": "Read-only",
"admin": "Admin"
},
- "close-sheet": "Close sheet"
+ "close-sheet": "Close sheet",
+ "connect": "Connect",
+ "connect-in-admin-mode": "Connect in admin mode",
+ "admin-mode": {
+ "self": "Admin mode"
+ }
},
"ui-editor": {
"self": "UI Editor",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index e2c2ca0103..89d063d3eb 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1390,7 +1390,6 @@
"format": "格式化",
"sql-review-result": "SQL 检查结果",
"alter-table": "修改表结构",
- "open-connection": "打开连接",
"visualize-explain": "可视化 Explain",
"sql-execute-in-protected-environment": "下面的 SQL 语句将会在受保护的环境中执行。",
"rows-upper-limit": "到达查询结果条数上限",
@@ -1398,7 +1397,12 @@
"readonly": "只读",
"admin": "管理员"
},
- "close-sheet": "关闭工作表"
+ "close-sheet": "关闭工作表",
+ "connect": "连接",
+ "connect-in-admin-mode": "以管理员模式连接",
+ "admin-mode": {
+ "self": "管理员模式"
+ }
},
"ui-editor": {
"self": "UI 编辑器",
diff --git a/frontend/src/store/modules/tab.ts b/frontend/src/store/modules/tab.ts
index a5d3df15ff..fffd17334c 100644
--- a/frontend/src/store/modules/tab.ts
+++ b/frontend/src/store/modules/tab.ts
@@ -2,12 +2,13 @@ import { defineStore } from "pinia";
import { computed, reactive, ref, watch } from "vue";
import { pick } from "lodash-es";
import { watchThrottled } from "@vueuse/core";
-import type { TabInfo, AnyTabInfo } from "@/types";
+import type { TabInfo, CoreTabInfo, AnyTabInfo } from "@/types";
import { UNKNOWN_ID, TabMode } from "@/types";
import {
getDefaultTab,
INITIAL_TAB,
isTempTab,
+ isSimilarTab,
WebStorageHelper,
} from "@/utils";
import { useInstanceStore } from "./instance";
@@ -70,14 +71,19 @@ export const useTabStore = defineStore("tab", () => {
});
// actions
- const addTab = (payload?: AnyTabInfo) => {
+ const addTab = (payload?: AnyTabInfo, beside = false) => {
const newTab = reactive<TabInfo>({
...getDefaultTab(),
...payload,
});
const { id } = newTab;
- tabIdList.value.push(id);
+ const index = tabIdList.value.indexOf(currentTabId.value ?? "");
+ if (beside && index >= 0) {
+ tabIdList.value.splice(index + 1, 0, id);
+ } else {
+ tabIdList.value.push(id);
+ }
currentTabId.value = id;
tabs.value.set(id, newTab);
@@ -98,7 +104,24 @@ export const useTabStore = defineStore("tab", () => {
const setCurrentTabId = (id: string) => {
currentTabId.value = id;
};
+ const selectOrAddSimilarTab = (tab: CoreTabInfo, beside = false) => {
+ if (isDisconnected.value) {
+ return;
+ }
+ if (isSimilarTab(tab, currentTab.value)) {
+ return;
+ }
+ const similarTab = tabList.value.find((tmp) => isSimilarTab(tmp, tab));
+ if (similarTab) {
+ setCurrentTabId(similarTab.id);
+ } else {
+ addTab(tab, beside);
+ }
+ };
const selectOrAddTempTab = () => {
+ if (isDisconnected.value) {
+ return;
+ }
if (isTempTab(currentTab.value)) {
return;
}
@@ -228,6 +251,7 @@ export const useTabStore = defineStore("tab", () => {
updateCurrentTab,
setCurrentTabId,
selectOrAddTempTab,
+ selectOrAddSimilarTab,
reset,
};
});
diff --git a/frontend/src/types/tab.ts b/frontend/src/types/tab.ts
index 3d4666e9d8..7aa9e7cca5 100644
--- a/frontend/src/types/tab.ts
+++ b/frontend/src/types/tab.ts
@@ -39,4 +39,5 @@ export interface TabInfo {
adviceList?: Advice[];
}
+export type CoreTabInfo = Pick<TabInfo, "connection" | "sheetId" | "mode">;
export type AnyTabInfo = Partial<TabInfo>;
diff --git a/frontend/src/utils/tab.ts b/frontend/src/utils/tab.ts
index af9156c51e..31fb8174fb 100644
--- a/frontend/src/utils/tab.ts
+++ b/frontend/src/utils/tab.ts
@@ -2,7 +2,7 @@ import dayjs from "dayjs";
import { computed } from "vue";
import { v1 as uuidv1 } from "uuid";
import { t } from "../plugins/i18n";
-import type { Connection, TabInfo } from "@/types";
+import type { Connection, CoreTabInfo, TabInfo } from "@/types";
import { UNKNOWN_ID, TabMode } from "@/types";
export const defaultTabName = computed(() => t("sql-editor.untitled-sheet"));
@@ -40,3 +40,11 @@ export const isTempTab = (tab: TabInfo): boolean => {
export const isSameConnection = (a: Connection, b: Connection): boolean => {
return a.instanceId === b.instanceId && a.databaseId === b.databaseId;
};
+
+export const isSimilarTab = (a: CoreTabInfo, b: CoreTabInfo): boolean => {
+ return (
+ isSameConnection(a.connection, b.connection) &&
+ a.sheetId === b.sheetId &&
+ a.mode === b.mode
+ );
+};
diff --git a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
index f4461e42f7..101d1a8e1d 100644
--- a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
@@ -52,9 +52,16 @@ import { useI18n } from "vue-i18n";
import { stringify } from "qs";
import ConnectedIcon from "~icons/heroicons-outline/lightning-bolt";
-import type { ConnectionAtom, DatabaseId, InstanceId } from "@/types";
-import { ConnectionTreeState, UNKNOWN_ID } from "@/types";
+import type {
+ ConnectionAtom,
+ CoreTabInfo,
+ DatabaseId,
+ InstanceId,
+ SheetId,
+} from "@/types";
+import { ConnectionTreeState, TabMode, UNKNOWN_ID } from "@/types";
import {
+ useCurrentUser,
useDatabaseStore,
useInstanceStore,
useIsLoggedIn,
@@ -65,8 +72,9 @@ import {
import {
emptyConnection,
getHighlightHTMLByKeyWords,
+ hasWorkspacePermission,
isDescendantOf,
- isSameConnection,
+ isSimilarTab,
mapConnectionAtom,
} from "@/utils";
import { generateTableItem } from "./utils";
@@ -93,6 +101,7 @@ const sqlEditorStore = useSQLEditorStore();
const tableStore = useTableStore();
const tabStore = useTabStore();
const isLoggedIn = useIsLoggedIn();
+const currentUser = useCurrentUser();
const searchPattern = ref();
const showDropdown = ref(false);
@@ -118,13 +127,21 @@ const dropdownOptions = computed((): DropdownOptionWithConnectionAtom[] => {
if (dropdownContext.value.disabled) {
return [];
}
- return [
+ const items = [
{
- key: "open-connection",
- label: t("sql-editor.open-connection"),
+ key: "connect",
+ label: t("sql-editor.connect"),
item: dropdownContext.value,
},
];
+ if (allowAdmin.value) {
+ items.push({
+ key: "connect-in-admin-mode",
+ label: t("sql-editor.connect-in-admin-mode"),
+ item: dropdownContext.value,
+ });
+ }
+ return items;
}
});
@@ -140,24 +157,36 @@ const selectedKeys = computed(() => {
return [];
});
+const allowAdmin = computed(() =>
+ hasWorkspacePermission(
+ "bb.permission.workspace.admin-sql-editor",
+ currentUser.value.role
+ )
+);
+
const treeData = computed(() => sqlEditorStore.connectionTree.data);
-const setConnection = (option: ConnectionAtom) => {
+const setConnection = (
+ option: ConnectionAtom,
+ extra: { sheetId?: SheetId; mode: TabMode } = {
+ sheetId: undefined,
+ mode: TabMode.ReadOnly,
+ }
+) => {
if (option) {
- const conn = emptyConnection();
+ const target: CoreTabInfo = {
+ connection: emptyConnection(),
+ ...extra,
+ };
+ const conn = target.connection;
+
const connect = () => {
- if (isSameConnection(tabStore.currentTab.connection, conn)) {
+ if (isSimilarTab(target, tabStore.currentTab)) {
// Don't go further if the connection doesn't change.
return;
}
- if (tabStore.currentTab.sheetId) {
- // We won't mutate a saved sheet's connection.
- // So we'll set connection in a temp or new tab.
- tabStore.selectOrAddTempTab();
- }
- tabStore.updateCurrentTab({
- connection: conn,
- });
+ tabStore.selectOrAddSimilarTab(target);
+ tabStore.updateCurrentTab(target);
};
// If selected item is instance node
@@ -310,8 +339,10 @@ const handleSelect = (key: string) => {
if (key === "alter-table") {
gotoAlterSchema(option.item);
- } else if (key === "open-connection") {
+ } else if (key === "connect") {
setConnection(option.item);
+ } else if (key === "connect-in-admin-mode") {
+ setConnection(option.item, { mode: TabMode.Admin });
}
showDropdown.value = false;
diff --git a/frontend/src/views/sql-editor/EditorCommon/AdminModeButton.vue b/frontend/src/views/sql-editor/EditorCommon/AdminModeButton.vue
new file mode 100644
index 0000000000..d5e72b843c
--- /dev/null
+++ b/frontend/src/views/sql-editor/EditorCommon/AdminModeButton.vue
@@ -0,0 +1,61 @@
+<template>
+ <NButton
+ v-if="showButton"
+ type="warning"
+ :disabled="tabStore.isDisconnected"
+ @click="enterAdminMode"
+ >
+ <heroicons-outline:wrench />
+ <span class="ml-1"> {{ $t("sql-editor.admin-mode.self") }} </span>
+ </NButton>
+</template>
+
+<script lang="ts" setup>
+import { computed } from "vue";
+
+import { TabMode } from "@/types";
+import { useCurrentUser, useTabStore, useWebTerminalStore } from "@/store";
+import { hasWorkspacePermission } from "@/utils";
+import { last } from "lodash-es";
+
+const emit = defineEmits<{
+ (e: "enter"): void;
+}>();
+
+const currentUser = useCurrentUser();
+
+const allowAdmin = computed(() =>
+ hasWorkspacePermission(
+ "bb.permission.workspace.admin-sql-editor",
+ currentUser.value.role
+ )
+);
+
+const tabStore = useTabStore();
+
+const showButton = computed(() => {
+ if (!allowAdmin.value) return false;
+ return tabStore.currentTab.mode === TabMode.ReadOnly;
+});
+
+const enterAdminMode = () => {
+ const current = tabStore.currentTab;
+ const statement = current.statement;
+ const target = {
+ connection: current.connection,
+ mode: TabMode.Admin,
+ };
+ tabStore.selectOrAddSimilarTab(target, true);
+ tabStore.updateCurrentTab({
+ ...target,
+ statement,
+ });
+ const queryItem = last(
+ useWebTerminalStore().getQueryListByTab(tabStore.currentTab)
+ );
+ if (queryItem) {
+ queryItem.sql = statement;
+ }
+ emit("enter");
+};
+</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
index 8a6f771689..ce2ad06ffd 100644
--- a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
@@ -22,7 +22,7 @@
</NButton>
</div>
<div class="action-right space-x-2 flex justify-end items-center">
- <TabModeSelect />
+ <AdminModeButton />
<template v-if="showSheetsFeature">
<NButton
@@ -66,7 +66,7 @@ import {
import type { ExecuteConfig, ExecuteOption } from "@/types";
import { TabMode, UNKNOWN_ID } from "@/types";
import SharePopover from "./SharePopover.vue";
-import TabModeSelect from "./TabModeSelect.vue";
+import AdminModeButton from "./AdminModeButton.vue";
const emit = defineEmits<{
(e: "save-sheet", content?: string): void;
diff --git a/frontend/src/views/sql-editor/EditorCommon/ExecuteHint.vue b/frontend/src/views/sql-editor/EditorCommon/ExecuteHint.vue
index 9ec376891a..82e50cda2d 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ExecuteHint.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ExecuteHint.vue
@@ -32,15 +32,20 @@
</section>
</NAlert>
- <div class="execute-hint-content mt-4 flex justify-end space-x-2">
- <NButton @click="handleClose">{{ $t("common.close") }}</NButton>
- <NButton type="primary" @click="gotoAlterSchema">
- {{
- isDDLSQLStatement
- ? $t("database.alter-schema")
- : $t("database.change-data")
- }}
- </NButton>
+ <div class="execute-hint-content mt-4 flex justify-between">
+ <div class="flex justify-start items-center space-x-2">
+ <AdminModeButton @enter="$emit('close')" />
+ </div>
+ <div class="flex justify-end items-center space-x-2">
+ <NButton @click="handleClose">{{ $t("common.close") }}</NButton>
+ <NButton type="primary" @click="gotoAlterSchema">
+ {{
+ isDDLSQLStatement
+ ? $t("database.alter-schema")
+ : $t("database.change-data")
+ }}
+ </NButton>
+ </div>
</div>
</div>
</template>
@@ -52,6 +57,7 @@ import { useRouter } from "vue-router";
import { pushNotification, useTabStore, useDatabaseStore } from "@/store";
import { parseSQL, isDDLStatement } from "@/components/MonacoEditor/sqlParser";
import { UNKNOWN_ID } from "@/types";
+import AdminModeButton from "./AdminModeButton.vue";
const emit = defineEmits<{
(e: "close"): void;
diff --git a/frontend/src/views/sql-editor/EditorCommon/TabModeSelect.vue b/frontend/src/views/sql-editor/EditorCommon/TabModeSelect.vue
deleted file mode 100644
index 35803045c4..0000000000
--- a/frontend/src/views/sql-editor/EditorCommon/TabModeSelect.vue
+++ /dev/null
@@ -1,71 +0,0 @@
-<template>
- <NSelect
- v-model:value="tabStore.currentTab.mode"
- :options="tabModeOptions"
- :consistent-menu-width="false"
- :render-label="renderLabel"
- @update:value="onUpdate"
- />
-</template>
-
-<script lang="ts" setup>
-import { computed, h } from "vue";
-import { SelectOption } from "naive-ui";
-import { useI18n } from "vue-i18n";
-import WrenchIcon from "~icons/heroicons-outline/wrench";
-import LockIcon from "~icons/heroicons-outline/lock-closed";
-
-import { TabMode } from "@/types";
-import { useCurrentUser, useTabStore } from "@/store";
-import { hasWorkspacePermission } from "@/utils";
-
-const { t } = useI18n();
-
-const currentUser = useCurrentUser();
-
-const allowAdmin = computed(() =>
- hasWorkspacePermission(
- "bb.permission.workspace.admin-sql-editor",
- currentUser.value.role
- )
-);
-
-const tabStore = useTabStore();
-
-const tabModeOptions = computed((): SelectOption[] => {
- return [
- {
- value: TabMode.ReadOnly,
- label: t("sql-editor.tab-mode.readonly"),
- },
- {
- value: TabMode.Admin,
- label: t("sql-editor.tab-mode.admin"),
- disabled: !allowAdmin.value,
- },
- ];
-});
-
-const renderLabel = (option: SelectOption) => {
- const icon = option.value === TabMode.Admin ? WrenchIcon : LockIcon;
- const color =
- option.value === TabMode.Admin ? "admin text-error" : "read-only text-main";
- return h(
- "span",
- {
- class: ["tab-mode-select-label flex items-center gap-x-1", color],
- },
- [h(icon, { class: "h-4 w-4" }), h("span", {}, String(option.label))]
- );
-};
-
-const onUpdate = () => {
- tabStore.currentTab.isSaved = false;
-};
-</script>
-
-<style>
-.tab-mode-select-label.admin + .n-base-select-option__check {
- @apply text-error;
-}
-</style>
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/AdminLabel.vue b/frontend/src/views/sql-editor/TabList/TabItem/AdminLabel.vue
index 7b47526af6..9da9d6c995 100644
--- a/frontend/src/views/sql-editor/TabList/TabItem/AdminLabel.vue
+++ b/frontend/src/views/sql-editor/TabList/TabItem/AdminLabel.vue
@@ -1,5 +1,5 @@
<template>
- <label class="flex items-center text-sm ml-0.5">
+ <label class="flex items-center text-sm h-6 ml-0.5">
<template v-if="instance.id !== UNKNOWN_ID">
<span>{{ instance.environment.name }}</span>
<ProtectedEnvironmentIcon