diff --git a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue index 886d0e2f1b..2c32c55b53 100644 --- a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue +++ b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue @@ -6144,7 +6144,11 @@ function buildDatabaseSidebarMenu(context: SidebarMenuFactoryContext): boolean { function buildSpecialSidebarMenu(context: SidebarMenuFactoryContext): boolean { const { node, items } = context; - if (node.type === "saved-sql-root") { + if (node.type === "saved-sql-root" || node.type === "saved-sql-folder") { + if (supportsConnectionQueryActions(currentDatabaseType())) { + items.push({ label: t("contextMenu.newQuery"), action: newQuery, icon: TerminalSquare }); + items.push({ label: "", separator: true }); + } items.push({ label: t("savedSql.pasteFile"), action: () => requestPasteTreeClipboard(), @@ -6157,6 +6161,9 @@ function buildSpecialSidebarMenu(context: SidebarMenuFactoryContext): boolean { if (node.type === "saved-sql-file") { items.push({ label: t("savedSql.open"), action: openSavedSqlFile, icon: FileCode }); + if (supportsConnectionQueryActions(currentDatabaseType())) { + items.push({ label: t("contextMenu.newQuery"), action: newQuery, icon: TerminalSquare }); + } items.push({ label: "", separator: true }); items.push({ label: t("savedSql.copyFile"), action: copySavedSqlFiles, icon: Copy, shortcut: shortcutCopyName.value }); items.push({ diff --git a/apps/desktop/src/components/sidebar/__tests__/SidebarTreeRuntimeHost.savedSqlContextMenu.spec.ts b/apps/desktop/src/components/sidebar/__tests__/SidebarTreeRuntimeHost.savedSqlContextMenu.spec.ts new file mode 100644 index 0000000000..b7d0083910 --- /dev/null +++ b/apps/desktop/src/components/sidebar/__tests__/SidebarTreeRuntimeHost.savedSqlContextMenu.spec.ts @@ -0,0 +1,159 @@ +// @vitest-environment happy-dom +import { createApp, defineComponent, h, nextTick, ref, type App } from "vue"; +import { createPinia, setActivePinia } from "pinia"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import i18n from "@/i18n"; +import type { ContextMenuItem } from "@/components/ui/CustomContextMenu.vue"; +import type { TreeNode } from "@/types/database"; + +vi.mock("@/lib/backend/api", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, listPlugins: vi.fn().mockResolvedValue([]) }; +}); + +import SidebarTreeRuntimeHost from "@/components/sidebar/SidebarTreeRuntimeHost.vue"; +import { useConnectionStore } from "@/stores/connectionStore"; +import { useQueryStore } from "@/stores/queryStore"; + +const mysqlConnection = { + id: "conn-mysql", + name: "MySQL Test", + db_type: "mysql", + driver_profile: "mysql", + host: "localhost", + port: 3306, + username: "root", + password: "", +}; + +const nacosConnection = { + id: "conn-nacos", + name: "Nacos Test", + db_type: "nacos", + driver_profile: "nacos", + host: "localhost", + port: 8848, + username: "", + password: "", +}; + +const mountedApps: App[] = []; + +async function flush(): Promise { + await nextTick(); + await new Promise((resolve) => setTimeout(resolve, 0)); + await nextTick(); +} + +async function mountHost(connection = mysqlConnection) { + const pinia = createPinia(); + setActivePinia(pinia); + const connectionStore = useConnectionStore(); + connectionStore.connections = [connection]; + + const rootNode: TreeNode = { + id: connection.id, + label: connection.name, + type: "connection", + connectionId: connection.id, + }; + + const host = ref | null>(null); + const root = defineComponent({ setup: () => () => h(SidebarTreeRuntimeHost, { ref: host, node: rootNode, depth: 0 }) }); + const container = document.createElement("div"); + document.body.appendChild(container); + const app = createApp(root); + app.use(pinia); + app.use(i18n); + app.mount(container); + mountedApps.push(app); + await flush(); + return host.value as unknown as { buildContextMenu(node: TreeNode): ContextMenuItem[] }; +} + +function menuLabels(items: ContextMenuItem[]): string[] { + return items.flatMap((item) => [...(item.label ? [item.label] : []), ...(item.children ? menuLabels(item.children) : [])]); +} + +const tr = (key: string) => i18n.global.t(key); + +describe("SidebarTreeRuntimeHost saved SQL context menu (#9127)", () => { + afterEach(() => { + for (const app of mountedApps.splice(0)) app.unmount(); + document.body.innerHTML = ""; + }); + + it("includes new query option on saved-sql-root node and executes newQuery with database context", async () => { + const host = await mountHost(mysqlConnection); + const connectionStore = useConnectionStore(); + const queryStore = useQueryStore(); + vi.spyOn(connectionStore, "ensureConnected").mockResolvedValue(undefined as any); + const createTabSpy = vi.spyOn(queryStore, "createTab").mockReturnValue("tab-1"); + + const savedSqlRootNode: TreeNode = { + id: "conn-mysql:app:__queries", + label: "tree.queries", + type: "saved-sql-root", + connectionId: mysqlConnection.id, + database: "app", + catalog: "def", + schema: "public", + }; + + const items = host.buildContextMenu(savedSqlRootNode); + expect(menuLabels(items)).toContain(tr("contextMenu.newQuery")); + expect(menuLabels(items)).toContain(tr("savedSql.pasteFile")); + + const newQueryItem = items.find((item) => item.label === tr("contextMenu.newQuery")); + expect(newQueryItem).toBeDefined(); + + await newQueryItem?.action?.(); + expect(createTabSpy).toHaveBeenCalledWith("conn-mysql", "app", undefined, "query", "public", undefined, "def"); + }); + + it("includes new query option on saved-sql-file node", async () => { + const host = await mountHost(mysqlConnection); + const connectionStore = useConnectionStore(); + const queryStore = useQueryStore(); + vi.spyOn(connectionStore, "ensureConnected").mockResolvedValue(undefined as any); + const createTabSpy = vi.spyOn(queryStore, "createTab").mockReturnValue("tab-2"); + + const savedSqlFileNode: TreeNode = { + id: "conn-mysql:app:__queries:file:q1", + label: "select_users.sql", + type: "saved-sql-file", + connectionId: mysqlConnection.id, + database: "app", + schema: "public", + savedSqlId: "q1", + }; + + const items = host.buildContextMenu(savedSqlFileNode); + expect(menuLabels(items)).toContain(tr("savedSql.open")); + expect(menuLabels(items)).toContain(tr("contextMenu.newQuery")); + expect(menuLabels(items)).toContain(tr("savedSql.copyFile")); + expect(menuLabels(items)).toContain(tr("savedSql.pasteFile")); + + const newQueryItem = items.find((item) => item.label === tr("contextMenu.newQuery")); + expect(newQueryItem).toBeDefined(); + + await newQueryItem?.action?.(); + expect(createTabSpy).toHaveBeenCalledWith("conn-mysql", "app", undefined, "query", "public", undefined, undefined); + }); + + it("omits new query option on saved-sql-root when database type does not support queries", async () => { + const host = await mountHost(nacosConnection); + + const savedSqlRootNode: TreeNode = { + id: "conn-nacos:app:__queries", + label: "tree.queries", + type: "saved-sql-root", + connectionId: nacosConnection.id, + database: "app", + }; + + const items = host.buildContextMenu(savedSqlRootNode); + expect(menuLabels(items)).not.toContain(tr("contextMenu.newQuery")); + expect(menuLabels(items)).toContain(tr("savedSql.pasteFile")); + }); +}); diff --git a/apps/desktop/src/lib/__tests__/savedSql/savedSqlDatabaseTree.spec.ts b/apps/desktop/src/lib/__tests__/savedSql/savedSqlDatabaseTree.spec.ts index d999a7edc6..d4f9487b81 100644 --- a/apps/desktop/src/lib/__tests__/savedSql/savedSqlDatabaseTree.spec.ts +++ b/apps/desktop/src/lib/__tests__/savedSql/savedSqlDatabaseTree.spec.ts @@ -65,6 +65,16 @@ describe("database saved SQL tree", () => { expect(root).toMatchObject({ id: "conn-1:app:__queries", label: "tree.queries", type: "saved-sql-root", children: [] }); }); + it("preserves database schema on the saved-sql-root node", () => { + const root = buildDatabaseSavedSqlRootNode({ ...database, schema: "public" }, []); + expect(root).toMatchObject({ + id: "conn-1:app:__queries", + type: "saved-sql-root", + database: "app", + schema: "public", + }); + }); + it("collapses the Queries node by default when opening a connection", () => { expect(buildDatabaseSavedSqlRootNode(database, files)?.isExpanded).toBe(false); expect(buildDatabaseSavedSqlRootNode(database, [])?.isExpanded).toBe(false); diff --git a/apps/desktop/src/lib/savedSql/savedSqlClipboard.ts b/apps/desktop/src/lib/savedSql/savedSqlClipboard.ts index b4d4659812..0f8e9ea7cc 100644 --- a/apps/desktop/src/lib/savedSql/savedSqlClipboard.ts +++ b/apps/desktop/src/lib/savedSql/savedSqlClipboard.ts @@ -13,7 +13,7 @@ export function savedSqlClipboardFileIds(nodes: readonly TreeNode[]): string[] { } export function savedSqlPasteTargetForNode(node: Pick): SavedSqlPasteTarget | null { - if (node.type !== "database" && node.type !== "saved-sql-root" && node.type !== "saved-sql-file") return null; + if (node.type !== "database" && node.type !== "saved-sql-root" && node.type !== "saved-sql-file" && node.type !== "saved-sql-folder") return null; if (!node.connectionId || node.database === undefined) return null; return { connectionId: node.connectionId, diff --git a/apps/desktop/src/lib/savedSql/savedSqlDatabaseTree.ts b/apps/desktop/src/lib/savedSql/savedSqlDatabaseTree.ts index 9ef74f7bb3..778d8285e1 100644 --- a/apps/desktop/src/lib/savedSql/savedSqlDatabaseTree.ts +++ b/apps/desktop/src/lib/savedSql/savedSqlDatabaseTree.ts @@ -60,7 +60,7 @@ function savedSqlFileNode(rootId: string, file: SavedSqlFile): TreeNode { }; } -export function buildDatabaseSavedSqlRootNode(databaseNode: Pick, source: SavedSqlDatabaseSource, existingRoot?: TreeNode): TreeNode | null { +export function buildDatabaseSavedSqlRootNode(databaseNode: Pick, source: SavedSqlDatabaseSource, existingRoot?: TreeNode): TreeNode | null { if (!databaseNode.connectionId || databaseNode.database === undefined) return null; const id = `${databaseNode.id}:__queries`; @@ -71,6 +71,7 @@ export function buildDatabaseSavedSqlRootNode(databaseNode: Pick, children: readonly TreeNode[], source: SavedSqlDatabaseSource): TreeNode[] { +export function withDatabaseSavedSqlRoot(databaseNode: Pick, children: readonly TreeNode[], source: SavedSqlDatabaseSource): TreeNode[] { const existingRoot = databaseNode.children?.find((child) => child.type === "saved-sql-root"); const existingMetadataChildren = databaseNode.children?.filter((child) => child.type !== "saved-sql-root") ?? []; const metadataChildren = children.filter((child) => child.type !== "saved-sql-root");