Skip to content

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

fix: sql editor initial and permission check (#18166)

ecmadao6f7b017a255d
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index 7e3093f392..569e7ab863 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -24,6 +24,7 @@ import { debounce, head, omit } from "lodash-es";
import { computed, nextTick, onMounted, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";
+import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
import { ProvideAIContext } from "@/plugins/ai";
import {
SQL_EDITOR_DATABASE_MODULE,
@@ -57,7 +58,6 @@ import {
extractWorksheetUID,
getDefaultPagination,
getSheetStatement,
- hasProjectPermissionV2,
isDatabaseV1Queryable,
isWorksheetReadableV1,
projectNameFromSheetSlug,
@@ -95,17 +95,7 @@ const fallbackToFirstProject = async () => {
return head(projects)?.name ?? DEFAULT_PROJECT_NAME;
};
-const initProject = async (project: string) => {
- try {
- await projectStore.getOrFetchProjectByName(project);
- return true;
- } catch {
- // nothing
- }
- return false;
-};
-
-const initializeProjects = async () => {
+const initializeProject = async () => {
const projectInQuery = route.query.project as string;
const projectInParams = route.params.project as string;
let project: string = "";
@@ -122,34 +112,22 @@ const initializeProjects = async () => {
project = editorStore.storedLastViewedProject;
}
- if (isValidProjectName(project)) {
- initializeSuccess = await initProject(project);
- }
+ initializeSuccess = !!(await maybeSwitchProject(project));
if (!initializeSuccess) {
// Maybe the cached project name is valid, but users cannot get it any more (removed, not permission, etc).
// So we need to fallback to the first accessible project.
project = await fallbackToFirstProject();
- initializeSuccess = await initProject(project);
+ initializeSuccess = !!(await maybeSwitchProject(project));
}
- if (initializeSuccess) {
- editorStore.project = project;
- tabStore.maybeInitProject(editorStore.project);
- } else {
- editorStore.project = "";
- editorStore.storedLastViewedProject = "";
- }
-};
-
-const handleProjectSwitched = async () => {
- const { project } = editorStore;
- if (project) {
- await projectStore.getOrFetchProjectByName(project, true /* silent */);
+ if (!initializeSuccess) {
+ // clear last visit project.
+ editorStore.setProject("");
}
};
const connect = (connection: SQLEditorConnection) => {
- tabStore.selectOrAddSimilarNewTab(
+ const currentTab = tabStore.selectOrAddSimilarNewTab(
{
connection,
worksheet: "",
@@ -159,7 +137,7 @@ const connect = (connection: SQLEditorConnection) => {
/* defaultTitle */ suggestedTabTitleForSQLEditorConnection(connection),
/* ignoreMode */ true
);
- if (tabStore.currentTab?.mode === "ADMIN") {
+ if (currentTab.mode === "ADMIN") {
// Don't enter ADMIN mode initially
tabStore.updateCurrentTab({
mode: DEFAULT_SQL_EDITOR_TAB_MODE,
@@ -167,25 +145,7 @@ const connect = (connection: SQLEditorConnection) => {
}
};
-const prepareSheetLegacy = async () => {
- const sheetSlug = (route.params.sheetSlug as string) || "";
- if (!sheetSlug) {
- return false;
- }
-
- const projectName = projectNameFromSheetSlug(sheetSlug);
-
- try {
- const project = await projectStore.getOrFetchProjectByName(projectName);
- if (!hasProjectPermissionV2(project, "bb.sql.select")) {
- return false;
- }
- } catch {
- // Nothing
- }
- await maybeSwitchProject(projectName);
-
- const sheetName = worksheetNameFromSlug(sheetSlug);
+const switchWorksheet = async (sheetName: string) => {
const openingSheetTab = tabStore.tabList.find(
(tab) => tab.worksheet == sheetName
);
@@ -211,14 +171,20 @@ const prepareSheetLegacy = async () => {
return false;
}
+ const connection = await extractWorksheetConnection(sheet);
if (openingSheetTab) {
// Switch to a sheet tab if it's open already.
// and don't touch it
tabStore.setCurrentTabId(openingSheetTab.id);
+ tabStore.updateCurrentTab({
+ connection,
+ worksheet: sheet.name,
+ statement: getSheetStatement(sheet),
+ status: "CLEAN",
+ });
return true;
}
- const connection = await extractWorksheetConnection(sheet);
// Open the sheet in a new tab otherwise.
tabStore.addTab({
connection,
@@ -231,74 +197,31 @@ const prepareSheetLegacy = async () => {
return true;
};
-const prepareSheet = async () => {
- const projectId = route.params.project;
- const sheetId = route.params.sheet;
- if (typeof projectId !== "string" || !projectId) {
- return false;
- }
- if (typeof sheetId !== "string" || !sheetId) {
+const prepareSheetLegacy = async () => {
+ const sheetSlug = (route.params.sheetSlug as string) || "";
+ if (!sheetSlug) {
return false;
}
- const projectName = `projects/${projectId}`;
- const sheetName = `worksheets/${sheetId}`;
-
- const project = await projectStore.getOrFetchProjectByName(projectName);
- if (!hasProjectPermissionV2(project, "bb.sql.select")) {
- return false;
- }
+ const projectName = projectNameFromSheetSlug(sheetSlug);
await maybeSwitchProject(projectName);
- const openingSheetTab = tabStore.tabList.find(
- (tab) => tab.worksheet == sheetName
- );
-
- const sheet = await worksheetStore.getOrFetchWorksheetByName(sheetName);
+ const sheetName = worksheetNameFromSlug(sheetSlug);
+ return await switchWorksheet(sheetName);
+};
- if (!sheet) {
- if (openingSheetTab) {
- // If a sheet is open in a tab but it returns 404 NOT_FOUND
- // that means the sheet has been deleted somewhere else.
- // We need to turn the sheet to an unsaved tab.
- openingSheetTab.worksheet = "";
- openingSheetTab.status = "DIRTY";
- }
+const prepareSheet = async () => {
+ const projectId = route.params.project;
+ const sheetId = route.params.sheet;
+ if (typeof projectId !== "string" || !projectId) {
return false;
}
- if (!isWorksheetReadableV1(sheet)) {
- pushNotification({
- module: "bytebase",
- style: "CRITICAL",
- title: t("common.access-denied"),
- });
+ if (typeof sheetId !== "string" || !sheetId) {
return false;
}
- const connection = await extractWorksheetConnection(sheet);
- if (openingSheetTab) {
- // Switch to a sheet tab if it's open already.
- // and don't touch it
- tabStore.setCurrentTabId(openingSheetTab.id);
- tabStore.updateCurrentTab({
- connection,
- worksheet: sheet.name,
- statement: getSheetStatement(sheet),
- status: "CLEAN",
- });
- return true;
- }
-
- // Open the sheet in a new tab otherwise.
- tabStore.addTab({
- connection,
- worksheet: sheet.name,
- title: sheet.title,
- statement: getSheetStatement(sheet),
- status: "CLEAN",
- });
-
- return true;
+ await maybeSwitchProject(`projects/${projectId}`);
+ return await switchWorksheet(`worksheets/${sheetId}`);
};
const prepareConnectionParams = async () => {
@@ -309,36 +232,30 @@ const prepareConnectionParams = async () => {
) {
return false;
}
- const instanceName = route.params.instance;
- const databaseName = route.params.database;
- if (
- typeof instanceName !== "string" ||
- !instanceName ||
- typeof databaseName !== "string" ||
- !databaseName
- ) {
+
+ const databaseName = `instances/${route.params.instance}/databases/${route.params.database}`;
+ if (!isValidDatabaseName(databaseName)) {
return false;
}
- const database = await useDatabaseV1Store().getOrFetchDatabaseByName(
- `instances/${instanceName}/databases/${databaseName}`
- );
- if (isValidDatabaseName(database.name)) {
- if (!isDatabaseV1Queryable(database)) {
- router.push({
- name: "error.403",
- });
+ const database =
+ await useDatabaseV1Store().getOrFetchDatabaseByName(databaseName);
+ await maybeSwitchProject(database.project);
+ if (!isDatabaseV1Queryable(database)) {
+ const tabs = tabStore.tabList.filter(
+ (tab) => tab.connection.database === database.name
+ );
+ for (const tab of tabs) {
+ tabStore.removeTab(tab);
}
-
- // connected to db
- await maybeSwitchProject(database.project);
- connect({
- instance: database.instance,
- database: database.name,
- });
- return true;
+ return false;
}
- return false;
+ // connected to db
+ connect({
+ instance: database.instance,
+ database: database.name,
+ });
+ return true;
};
const initializeConnectionFromQuery = async () => {
@@ -354,7 +271,6 @@ const initializeConnectionFromQuery = async () => {
if (await prepareSheet()) {
return;
}
-
if (await prepareConnectionParams()) {
return;
}
@@ -417,47 +333,52 @@ const syncURLWithConnection = () => {
}
}
}
- if (databaseName) {
+ if (isValidDatabaseName(databaseName)) {
const database =
await databaseStore.getOrFetchDatabaseByName(databaseName);
- if (isValidDatabaseName(database.name)) {
- if (schema) {
- query.schema = schema;
- }
- if (table) {
- query.table = table;
- query.schema = schema ?? "";
- }
- router.replace({
- name: SQL_EDITOR_DATABASE_MODULE,
+ if (!isDatabaseV1Queryable(database)) {
+ return router.replace({
+ name: SQL_EDITOR_PROJECT_MODULE,
params: {
project: extractProjectResourceName(database.project),
- instance: extractInstanceResourceName(database.instance),
- database: database.databaseName,
},
query,
});
- return;
}
+ if (schema) {
+ query.schema = schema;
+ }
+ if (table) {
+ query.table = table;
+ query.schema = schema ?? "";
+ }
+ router.replace({
+ name: SQL_EDITOR_DATABASE_MODULE,
+ params: {
+ project: extractProjectResourceName(database.project),
+ instance: extractInstanceResourceName(database.instance),
+ database: database.databaseName,
+ },
+ query,
+ });
+ return;
}
- if (instanceName) {
- if (isValidInstanceName(instanceName)) {
- if (table) {
- query.table = table;
- query.schema = schema ?? "";
- }
- router.replace({
- name: SQL_EDITOR_INSTANCE_MODULE,
- params: {
- project: extractProjectResourceName(editorStore.project),
- instance: extractInstanceResourceName(instanceName),
- },
- query,
- });
- return;
+ if (isValidInstanceName(instanceName)) {
+ if (table) {
+ query.table = table;
+ query.schema = schema ?? "";
}
+ router.replace({
+ name: SQL_EDITOR_INSTANCE_MODULE,
+ params: {
+ project: extractProjectResourceName(editorStore.project),
+ instance: extractInstanceResourceName(instanceName),
+ },
+ query,
+ });
+ return;
}
- if (projectName) {
+ if (isValidProjectName(projectName)) {
router.replace({
name: SQL_EDITOR_PROJECT_MODULE,
params: {
@@ -496,33 +417,24 @@ onMounted(async () => {
policyStore.fetchPolicies({
resourceType: PolicyResourceType.WORKSPACE,
}),
- initializeProjects(),
+ initializeProject(),
]);
- tabStore.maybeInitProject(editorStore.project);
- editorStore.projectContextReady = true;
- nextTick(() => {
- editorEvents.emit("project-context-ready", {
- project: editorStore.project,
- });
- restoreLastVisitedSidebarTab();
- });
-
- watch(
- () => editorStore.project,
- async () => {
- editorStore.projectContextReady = false;
- await handleProjectSwitched();
- tabStore.maybeInitProject(editorStore.project);
- editorStore.projectContextReady = true;
- nextTick(() => {
- editorEvents.emit("project-context-ready", {
- project: editorStore.project,
- });
- });
- }
- );
await initializeConnectionFromQuery();
syncURLWithConnection();
});
+
+useEmitteryEventListener(
+ editorEvents,
+ "project-context-ready",
+ ({ project }) => {
+ if (!project) {
+ return;
+ }
+ tabStore.maybeInitProject(project);
+ nextTick(() => {
+ restoreLastVisitedSidebarTab();
+ });
+ }
+);
</script>
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index b1c853fab0..cf1400fba6 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2135,7 +2135,6 @@
"last-synced": "Last synced: {time}",
"click-to-sync-now": "Click to sync now",
"sync-in-progress": "Sync in progress",
- "create-a-worksheet": "Create a new worksheet",
"select-a-database-to-start": "Select a database to start",
"show-empty-environments": "Show empty environments",
"select-encoding": "Select encoding",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 9a816727f2..ac297e5f46 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -2135,7 +2135,6 @@
"last-synced": "Última sincronización: {hora}",
"click-to-sync-now": "Haga clic para sincronizar ahora",
"sync-in-progress": "Sincronización en progreso",
- "create-a-worksheet": "Crear una hoja de trabajo",
"select-a-database-to-start": "Seleccione una base de datos para comenzar",
"show-empty-environments": "Mostrar entornos vacíos",
"select-encoding": "Seleccionar codificación",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index f847886121..6fcef13cd2 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -2135,7 +2135,6 @@
"last-synced": "最終同期時間: {time}",
"click-to-sync-now": "クリックして今すぐ同期します",
"sync-in-progress": "同期中",
- "create-a-worksheet": "新しいワークシートを作成する",
"select-a-database-to-start": "開始するにはデータベースを選択してください",
"show-empty-environments": "空の環境を表示",
"select-encoding": "エンコーディングの選択",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index e99b4f5798..62449d90ea 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -2135,7 +2135,6 @@
"last-synced": "Đã đồng bộ lần cuối: {time}",
"click-to-sync-now": "Nhấp để đồng bộ ngay",
"sync-in-progress": "Đang đồng bộ",
- "create-a-worksheet": "Tạo một trang tính mới",
"select-a-database-to-start": "Chọn một cơ sở dữ liệu để bắt đầu",
"show-empty-environments": "Hiển thị môi trường trống",
"select-encoding": "Chọn mã hóa",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index f53e581c6e..8f4dcf04d8 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2135,7 +2135,6 @@
"last-synced": "上次同步时间:{time}",
"click-to-sync-now": "点击立即同步",
"sync-in-progress": "正在同步",
- "create-a-worksheet": "新建工作表",
"select-a-database-to-start": "选择数据库以开始",
"show-empty-environments": "显示空环境",
"select-encoding": "选择编码",
diff --git a/frontend/src/router/sqlEditor.ts b/frontend/src/router/sqlEditor.ts
index 03f5f54480..4acf99141f 100644
--- a/frontend/src/router/sqlEditor.ts
+++ b/frontend/src/router/sqlEditor.ts
@@ -57,12 +57,6 @@ const sqlEditorRoutes: RouteRecordRaw[] = [
meta: { title: () => "Bytebase SQL Editor" },
component: () => import("../views/sql-editor/SQLEditorPage.vue"),
},
- {
- path: "sheet/:sheetSlug",
- name: SQL_EDITOR_SHARE_MODULE_LEGACY,
- meta: { title: () => "Bytebase SQL Editor" },
- component: () => import("../views/sql-editor/SQLEditorPage.vue"),
- },
],
},
];
diff --git a/frontend/src/store/modules/sqlEditor/editor.ts b/frontend/src/store/modules/sqlEditor/editor.ts
index 35ec114399..04921186a3 100644
--- a/frontend/src/store/modules/sqlEditor/editor.ts
+++ b/frontend/src/store/modules/sqlEditor/editor.ts
@@ -1,8 +1,8 @@
import { useLocalStorage } from "@vueuse/core";
import { includes } from "lodash-es";
import { defineStore } from "pinia";
-import { computed, ref, watch } from "vue";
-import { type ComposedDatabase, isValidProjectName } from "@/types";
+import { computed, ref } from "vue";
+import { type ComposedDatabase } from "@/types";
import { QueryOption_RedisRunCommandsOn } from "@/types/proto-es/v1/sql_service_pb";
import { hasWorkspacePermissionV2 } from "@/utils";
@@ -34,8 +34,6 @@ export const useSQLEditorStore = defineStore("sqlEditor", () => {
}
);
- // empty to "ALL" projects for high-privileged users
- const project = ref<string>("");
// `false` if we are preparing project-scoped resources
// we should render a skeleton layout with spinner placeholders
const projectContextReady = ref<boolean>(false);
@@ -48,18 +46,18 @@ export const useSQLEditorStore = defineStore("sqlEditor", () => {
return hasWorkspacePermissionV2("bb.projects.list");
});
- watch(project, (project) => {
- if (isValidProjectName(project)) {
- storedLastViewedProject.value = project;
- }
- });
+ const setProject = (project: string) => {
+ storedLastViewedProject.value = project;
+ projectContextReady.value = true;
+ };
const isShowExecutingHint = ref(false);
const executingHintDatabase = ref<ComposedDatabase | undefined>();
return {
resultRowsLimit,
- project,
+ project: computed(() => storedLastViewedProject.value),
+ setProject,
projectContextReady,
storedLastViewedProject,
allowViewALLProjects,
diff --git a/frontend/src/store/modules/sqlEditor/tab.ts b/frontend/src/store/modules/sqlEditor/tab.ts
index 85456cfec6..050516cbbd 100644
--- a/frontend/src/store/modules/sqlEditor/tab.ts
+++ b/frontend/src/store/modules/sqlEditor/tab.ts
@@ -243,7 +243,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
} else {
tabIdList.value.push(id);
}
- currentTabId.value = id;
+ setCurrentTabId(id);
tabsById.set(id, newTab);
return newTab;
@@ -399,7 +399,7 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
) {
curr.title = defaultTitle;
}
- return;
+ return curr;
}
}
const similarNewTab = tabList.value.find(
@@ -407,8 +407,9 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
);
if (similarNewTab) {
setCurrentTabId(similarNewTab.id);
+ return similarNewTab;
} else {
- addTab(
+ return addTab(
{
...tab,
title: defaultTitle,
diff --git a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
index d8090faf44..6ab6585db5 100644
--- a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
@@ -94,9 +94,9 @@ const allowCreateProject = computed(() => {
const handleSwitchProject = (name: string | undefined) => {
if (!name || !isValidProjectName(name)) {
- project.value = "";
+ editorStore.setProject("");
} else {
- project.value = name;
+ editorStore.setProject(name);
}
};
</script>
diff --git a/frontend/src/views/sql-editor/EditorPanel/StandardPanel/StandardPanel.vue b/frontend/src/views/sql-editor/EditorPanel/StandardPanel/StandardPanel.vue
index 587ba2e267..c0fbecbac4 100644
--- a/frontend/src/views/sql-editor/EditorPanel/StandardPanel/StandardPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/StandardPanel/StandardPanel.vue
@@ -47,9 +47,7 @@
</Suspense>
</Pane>
</Splitpanes>
- <template v-else>
- <Welcome />
- </template>
+ <Welcome v-else />
<ExecutingHintModal />
diff --git a/frontend/src/views/sql-editor/EditorPanel/Welcome/Welcome.vue b/frontend/src/views/sql-editor/EditorPanel/Welcome/Welcome.vue
index 977aaba4ce..bd1aaac7b1 100644
--- a/frontend/src/views/sql-editor/EditorPanel/Welcome/Welcome.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/Welcome/Welcome.vue
@@ -1,13 +1,11 @@
<template>
<div
- class="w-full flex-1 flex flex-col items-center justify-start gap-y-4"
- style="padding-top: calc(clamp(40px, 15vh, 200px))"
+ class="w-full flex-1 flex flex-col items-center justify-center gap-y-4"
>
<BytebaseLogo />
<div
- class="hidden lg:grid items-center gap-4"
- :class="showCreateInstanceButton ? 'grid-cols-3' : 'grid-cols-2'"
+ class="flex items-center flex-wrap gap-4"
>
<Button
v-if="showCreateInstanceButton"
@@ -19,84 +17,50 @@
</template>
{{ $t("sql-editor.add-a-new-instance") }}
</Button>
- <Button type="primary" secondary @click="changeConnection">
+ <Button v-if="showConnectButton" type="primary" secondary @click="changeConnection">
<template #icon>
<LinkIcon :stroke-width="1.5" class="w-8 h-8" />
</template>
{{ $t("sql-editor.connect-to-a-database") }}
</Button>
- <Button type="default" @click="createNewWorksheet">
- <template #icon>
- <SquarePenIcon :stroke-width="1.5" class="w-8 h-8" />
- </template>
- {{ $t("sql-editor.create-a-worksheet") }}
- </Button>
- </div>
- <div class="flex lg:hidden flex-col items-start gap-y-2 w-max">
- <NButton
- type="primary"
- class="w-full! justify-start!"
- @click="changeConnection"
- >
- <template #icon>
- <LinkIcon class="w-4 h-4" />
- </template>
- {{ $t("sql-editor.connect-to-a-database") }}
- </NButton>
- <NButton
- v-if="showCreateInstanceButton"
- type="default"
- class="w-full! justify-start!"
- @click="gotoInstanceCreatePage"
- >
- <template #icon>
- <LayersIcon class="w-4 h-4" />
- </template>
- {{ $t("sql-editor.add-a-new-instance") }}
- </NButton>
- <NButton
- type="default"
- class="w-full! justify-start!"
- @click="createNewWorksheet"
- >
- <template #icon>
- <SquarePenIcon class="w-4 h-4" />
- </template>
- {{ $t("sql-editor.create-a-worksheet") }}
- </NButton>
</div>
</div>
</template>
<script setup lang="ts">
-import { LayersIcon, LinkIcon, SquarePenIcon } from "lucide-vue-next";
-import { NButton } from "naive-ui";
-import { computed, nextTick } from "vue";
+import { LayersIcon, LinkIcon } from "lucide-vue-next";
+import { storeToRefs } from "pinia";
+import { computed } from "vue";
import { useRouter } from "vue-router";
import BytebaseLogo from "@/components/BytebaseLogo.vue";
import { INSTANCE_ROUTE_DASHBOARD } from "@/router/dashboard/workspaceRoutes";
-import { useSQLEditorTabStore } from "@/store";
-import { hasWorkspacePermissionV2 } from "@/utils";
+import { useProjectV1Store, useSQLEditorStore } from "@/store";
+import { hasProjectPermissionV2, hasWorkspacePermissionV2 } from "@/utils";
import { useSQLEditorContext } from "../../context";
import Button from "./Button.vue";
const { showConnectionPanel, asidePanelTab } = useSQLEditorContext();
const router = useRouter();
+const projectStore = useProjectV1Store();
+const { project: projectName } = storeToRefs(useSQLEditorStore());
+
+const project = computed(() =>
+ projectStore.getProjectByName(projectName.value)
+);
const showCreateInstanceButton = computed(() => {
return hasWorkspacePermissionV2("bb.instances.create");
});
+const showConnectButton = computed(() =>
+ hasProjectPermissionV2(project.value, "bb.sql.select")
+);
+
const changeConnection = () => {
asidePanelTab.value = "SCHEMA";
showConnectionPanel.value = true;
};
-const createNewWorksheet = () => {
- useSQLEditorTabStore().addTab();
- nextTick(() => changeConnection());
-};
-
const gotoInstanceCreatePage = () => {
router.push({
name: INSTANCE_ROUTE_DASHBOARD,
diff --git a/frontend/src/views/sql-editor/Sheet/context.ts b/frontend/src/views/sql-editor/Sheet/context.ts
index 74cecfd50f..75caf72b6a 100644
--- a/frontend/src/views/sql-editor/Sheet/context.ts
+++ b/frontend/src/views/sql-editor/Sheet/context.ts
@@ -24,6 +24,7 @@ import {
import {
emptySQLEditorConnection,
getSheetStatement,
+ isDatabaseV1Queryable,
isWorksheetReadableV1,
suggestedTabTitleForSQLEditorConnection,
useDynamicLocalStorage,
@@ -550,6 +551,9 @@ export const extractWorksheetConnection = async (worksheet: Worksheet) => {
const database = await useDatabaseV1Store().getOrFetchDatabaseByName(
worksheet.database
);
+ if (!isDatabaseV1Queryable(database)) {
+ return connection;
+ }
connection.instance = database.instance;
connection.database = database.name;
setDefaultDataSourceForConn(connection, database);
diff --git a/frontend/src/views/sql-editor/context.ts b/frontend/src/views/sql-editor/context.ts
index 482f94fb61..6da23baab0 100644
--- a/frontend/src/views/sql-editor/context.ts
+++ b/frontend/src/views/sql-editor/context.ts
@@ -3,8 +3,9 @@ import Emittery from "emittery";
import { type IRange } from "monaco-editor";
import type { InjectionKey, Ref } from "vue";
import { inject, provide, ref } from "vue";
-import { useSQLEditorStore } from "@/store";
+import { useProjectV1Store, useSQLEditorStore } from "@/store";
import type { SQLEditorTab } from "@/types";
+import { isValidProjectName } from "@/types";
import type { GetSchemaStringRequest_ObjectType } from "@/types/proto-es/v1/database_service_pb";
export type AsidePanelTab = "SCHEMA" | "WORKSHEET" | "HISTORY";
@@ -53,7 +54,7 @@ export type SQLEditorContext = {
events: SQLEditorEvents;
- maybeSwitchProject: (project: string) => Promise<string>;
+ maybeSwitchProject: (project: string) => Promise<string | undefined>;
handleAIPanelResize: (panes: { size: number }[], index?: number) => void;
};
@@ -67,6 +68,8 @@ export const useSQLEditorContext = () => {
export const provideSQLEditorContext = () => {
const editorStore = useSQLEditorStore();
+ const projectStore = useProjectV1Store();
+
const context: SQLEditorContext = {
asidePanelTab: ref("WORKSHEET"),
showConnectionPanel: ref(false),
@@ -76,18 +79,32 @@ export const provideSQLEditorContext = () => {
pendingInsertAtCaret: ref(),
events: new Emittery(),
- maybeSwitchProject: async (project) => {
- if (editorStore.project !== project) {
- editorStore.project = project;
- await context.events.once("project-context-ready");
- return project;
+ maybeSwitchProject: async (projectName) => {
+ if (!isValidProjectName(projectName)) {
+ return;
+ }
+ editorStore.projectContextReady = false;
+ try {
+ const project = await projectStore.getOrFetchProjectByName(projectName);
+ editorStore.setProject(project.name);
+ context.events.emit("project-context-ready", { project: project.name });
+ return project.name;
+ } catch {
+ // Nothing
+ } finally {
+ editorStore.projectContextReady = true;
}
- return Promise.resolve(editorStore.project);
},
handleAIPanelResize: (panes, index = 0) => {
- if (panes.length <= index) return;
- const { size } = panes[index];
- storedAIPanelSize.value = size;
+ try {
+ if (!panes || !Array.isArray(panes) || panes.length <= index) return;
+ const pane = panes[index];
+ if (!pane || typeof pane.size !== "number") return;
+ storedAIPanelSize.value = pane.size;
+ } catch (error) {
+ // Silently ignore errors from splitpanes during initialization
+ console.debug("Splitpanes resize handler error (ignored):", error);
+ }
},
};