feat(sidebar): add new query option to saved sql context menu
This commit is contained in:
1 parent
7b47b64391
commit
500c0ae692
5 files changed
+181
-4
No files matched your search
@@ -6144,7 +6144,11 @@ function buildDatabaseSidebarMenu(context: SidebarMenuFactoryContext): boolean {
|
|||||||
function buildSpecialSidebarMenu(context: SidebarMenuFactoryContext): boolean {
|
function buildSpecialSidebarMenu(context: SidebarMenuFactoryContext): boolean {
|
||||||
const { node, items } = context;
|
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({
|
items.push({
|
||||||
label: t("savedSql.pasteFile"),
|
label: t("savedSql.pasteFile"),
|
||||||
action: () => requestPasteTreeClipboard(),
|
action: () => requestPasteTreeClipboard(),
|
||||||
@@ -6157,6 +6161,9 @@ function buildSpecialSidebarMenu(context: SidebarMenuFactoryContext): boolean {
|
|||||||
|
|
||||||
if (node.type === "saved-sql-file") {
|
if (node.type === "saved-sql-file") {
|
||||||
items.push({ label: t("savedSql.open"), action: openSavedSqlFile, icon: FileCode });
|
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: "", separator: true });
|
||||||
items.push({ label: t("savedSql.copyFile"), action: copySavedSqlFiles, icon: Copy, shortcut: shortcutCopyName.value });
|
items.push({ label: t("savedSql.copyFile"), action: copySavedSqlFiles, icon: Copy, shortcut: shortcutCopyName.value });
|
||||||
items.push({
|
items.push({
|
||||||
|
|||||||
Vendored
+159
@@ -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<typeof import("@/lib/backend/api")>();
|
||||||
|
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<Element>[] = [];
|
||||||
|
|
||||||
|
async function flush(): Promise<void> {
|
||||||
|
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<InstanceType<typeof SidebarTreeRuntimeHost> | 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"));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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: [] });
|
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", () => {
|
it("collapses the Queries node by default when opening a connection", () => {
|
||||||
expect(buildDatabaseSavedSqlRootNode(database, files)?.isExpanded).toBe(false);
|
expect(buildDatabaseSavedSqlRootNode(database, files)?.isExpanded).toBe(false);
|
||||||
expect(buildDatabaseSavedSqlRootNode(database, [])?.isExpanded).toBe(false);
|
expect(buildDatabaseSavedSqlRootNode(database, [])?.isExpanded).toBe(false);
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export function savedSqlClipboardFileIds(nodes: readonly TreeNode[]): string[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function savedSqlPasteTargetForNode(node: Pick<TreeNode, "type" | "connectionId" | "catalog" | "database" | "schema">): SavedSqlPasteTarget | null {
|
export function savedSqlPasteTargetForNode(node: Pick<TreeNode, "type" | "connectionId" | "catalog" | "database" | "schema">): 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;
|
if (!node.connectionId || node.database === undefined) return null;
|
||||||
return {
|
return {
|
||||||
connectionId: node.connectionId,
|
connectionId: node.connectionId,
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ function savedSqlFileNode(rootId: string, file: SavedSqlFile): TreeNode {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildDatabaseSavedSqlRootNode(databaseNode: Pick<TreeNode, "id" | "connectionId" | "catalog" | "database">, source: SavedSqlDatabaseSource, existingRoot?: TreeNode): TreeNode | null {
|
export function buildDatabaseSavedSqlRootNode(databaseNode: Pick<TreeNode, "id" | "connectionId" | "catalog" | "database" | "schema">, source: SavedSqlDatabaseSource, existingRoot?: TreeNode): TreeNode | null {
|
||||||
if (!databaseNode.connectionId || databaseNode.database === undefined) return null;
|
if (!databaseNode.connectionId || databaseNode.database === undefined) return null;
|
||||||
|
|
||||||
const id = `${databaseNode.id}:__queries`;
|
const id = `${databaseNode.id}:__queries`;
|
||||||
@@ -71,6 +71,7 @@ export function buildDatabaseSavedSqlRootNode(databaseNode: Pick<TreeNode, "id"
|
|||||||
connectionId: databaseNode.connectionId,
|
connectionId: databaseNode.connectionId,
|
||||||
catalog: databaseNode.catalog,
|
catalog: databaseNode.catalog,
|
||||||
database: databaseNode.database,
|
database: databaseNode.database,
|
||||||
|
schema: databaseNode.schema,
|
||||||
// Default collapsed: opening a connection should not expand the Queries
|
// Default collapsed: opening a connection should not expand the Queries
|
||||||
// node until the user asks for it; an existing node's state is preserved.
|
// node until the user asks for it; an existing node's state is preserved.
|
||||||
isExpanded: existingRoot?.isExpanded ?? false,
|
isExpanded: existingRoot?.isExpanded ?? false,
|
||||||
@@ -82,7 +83,7 @@ export function buildDatabaseSavedSqlRootNode(databaseNode: Pick<TreeNode, "id"
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function withDatabaseSavedSqlRoot(databaseNode: Pick<TreeNode, "id" | "connectionId" | "catalog" | "database" | "children">, children: readonly TreeNode[], source: SavedSqlDatabaseSource): TreeNode[] {
|
export function withDatabaseSavedSqlRoot(databaseNode: Pick<TreeNode, "id" | "connectionId" | "catalog" | "database" | "schema" | "children">, children: readonly TreeNode[], source: SavedSqlDatabaseSource): TreeNode[] {
|
||||||
const existingRoot = databaseNode.children?.find((child) => child.type === "saved-sql-root");
|
const existingRoot = databaseNode.children?.find((child) => child.type === "saved-sql-root");
|
||||||
const existingMetadataChildren = databaseNode.children?.filter((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");
|
const metadataChildren = children.filter((child) => child.type !== "saved-sql-root");
|
||||||
|
|||||||
Reference in new issue
Block a user