From fc28c4a41e5bb626b10f15e9a56c788ba71cac3f Mon Sep 17 00:00:00 2001 From: Abeautifulsnow <1491444340@qq.com> Date: Sat, 3 Oct 2026 23:42:38 +0800 Subject: [PATCH] fix(plugins): pass schema to result views --- .../src/components/layout/ContentArea.vue | 14 ++-- .../ContentArea.pluginResultView.spec.ts | 64 ++++++++++++++++++- .../app-tests/pluginResultViewExample.test.ts | 12 ++++ plugins/README.md | 2 +- .../examples/hello-workbench/ui/index.html | 1 + 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 packages/app-tests/pluginResultViewExample.test.ts diff --git a/apps/desktop/src/components/layout/ContentArea.vue b/apps/desktop/src/components/layout/ContentArea.vue index e670679bac..73dba21939 100644 --- a/apps/desktop/src/components/layout/ContentArea.vue +++ b/apps/desktop/src/components/layout/ContentArea.vue @@ -1382,13 +1382,19 @@ function openPluginResultView(pluginId: string, contributionId: string, label: s // bounded snapshot — plugins that need more rows re-run the statement through // `host.queryData` (host.data:read, with the user's consent). const cappedRows = result.rows.slice(0, 500); + const connectionId = activeResultConnectionId.value || ""; + const database = activeResultDatabase.value || ""; + const schema = activeResultSchema.value || ""; queryStore.openPluginWorkbench(pluginId, contributionId, { title: label, - connectionId: props.activeTab.connectionId || "", - database: props.activeTab.database || "", + connectionId, + database, context: { - connectionId: props.activeTab.connectionId || "", - database: props.activeTab.database || "", + connectionId, + database, + // The plugin re-runs `sql` through `host.queryData`, whose backend sets + // search_path from this; without it unqualified table names miss. + schema, sql: resultSqlForGrid(props.activeTab), result: { columns: result.columns, rows: cappedRows, truncated: result.rows.length > cappedRows.length }, }, diff --git a/apps/desktop/src/components/layout/__tests__/ContentArea.pluginResultView.spec.ts b/apps/desktop/src/components/layout/__tests__/ContentArea.pluginResultView.spec.ts index e644731b29..cee8e55a21 100644 --- a/apps/desktop/src/components/layout/__tests__/ContentArea.pluginResultView.spec.ts +++ b/apps/desktop/src/components/layout/__tests__/ContentArea.pluginResultView.spec.ts @@ -50,6 +50,12 @@ const connection = { password: "", }; +const targetConnection = { + ...connection, + id: "conn-2", + name: "Target MySQL", +}; + function result(sourceStatement?: string): QueryResult { return { columns: ["id"], @@ -81,8 +87,9 @@ async function mountContentArea(tab: QueryTab) { const pinia = createPinia(); setActivePinia(pinia); const connectionStore = useConnectionStore(); - connectionStore.connections = [connection]; + connectionStore.connections = [connection, targetConnection]; const queryStore = useQueryStore(); + queryStore.tabs = [tab]; const openPluginWorkbench = vi.spyOn(queryStore, "openPluginWorkbench"); const state = reactive({ activeTab: tab }); const host = document.createElement("div"); @@ -180,4 +187,59 @@ describe("ContentArea result-view context", () => { }), ); }); + + it("passes the active tab schema so plugin re-runs resolve unqualified names", async () => { + const openPluginWorkbench = await mountContentArea( + queryTab({ + schema: "public", + lastExecutedSql: "SELECT * FROM users", + result: result("SELECT * FROM users"), + }), + ); + + expect(openPluginWorkbench).toHaveBeenCalledWith( + "com.example.chart", + "result.chart", + expect.objectContaining({ + context: expect.objectContaining({ schema: "public" }), + }), + ); + }); + + it("uses the active multi-database result execution scope", async () => { + const activeResult = result("SELECT * FROM users"); + const openPluginWorkbench = await mountContentArea( + queryTab({ + schema: "source_schema", + result: activeResult, + activeResultRunId: "target-run", + resultRuns: [ + { + id: "target-run", + title: "Target run", + sequence: 1, + sql: "SELECT * FROM users", + createdAt: 1, + result: activeResult, + multiDbExecution: { + kind: "multi-db", + batchId: "batch-1", + target: { connectionId: targetConnection.id, database: "target_database", schema: "target_schema" }, + status: "success", + }, + }, + ], + }), + ); + + expect(openPluginWorkbench).toHaveBeenCalledWith( + "com.example.chart", + "result.chart", + expect.objectContaining({ + connectionId: targetConnection.id, + database: "target_database", + context: expect.objectContaining({ connectionId: targetConnection.id, database: "target_database", schema: "target_schema" }), + }), + ); + }); }); diff --git a/packages/app-tests/pluginResultViewExample.test.ts b/packages/app-tests/pluginResultViewExample.test.ts new file mode 100644 index 0000000000..f2a288909e --- /dev/null +++ b/packages/app-tests/pluginResultViewExample.test.ts @@ -0,0 +1,12 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { test } from "vitest"; + +const helloWorkbenchSource = readFileSync("plugins/examples/hello-workbench/ui/index.html", "utf8"); + +test("hello-workbench forwards the result schema when rerunning a query", () => { + const queryDataCall = /window\.dbxPlugin\.queryData\(\{([\s\S]*?)\}\)/.exec(helloWorkbenchSource); + + assert.ok(queryDataCall); + assert.match(queryDataCall[1], /schema:\s*resultContext\.schema \|\| undefined/); +}); diff --git a/plugins/README.md b/plugins/README.md index ee317f5fb2..237081fc61 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -435,7 +435,7 @@ A result view contributes a plugin-rendered visualization for query results. DBX A result view declares display metadata only: it carries no UI of its own and never names a workbench. The opened contribution id reaches the plugin UI in the init payload (`dbx-plugin-init` detail `contributionId`), so a plugin that declares several result views selects the matching one inside its single UI entrypoint. -The `context.result` snapshot is bounded — `{ columns, rows (<= 500), truncated }` plus `sql`, `connectionId`, and `database`. Plugins that need more rows can re-run a read-only statement with [`queryData`](#read-only-data-queries) (requires `host.data:read` and the user's consent for that connection). Requires a UI entrypoint. +The `context.result` snapshot is bounded — `{ columns, rows (<= 500), truncated }` plus `sql`, `connectionId`, `database`, and `schema`. Plugins that need more rows can re-run a read-only statement with [`queryData`](#read-only-data-queries) (requires `host.data:read` and the user's consent for that connection). Requires a UI entrypoint. ### `context-menu` diff --git a/plugins/examples/hello-workbench/ui/index.html b/plugins/examples/hello-workbench/ui/index.html index fe1362cf55..a37bced25c 100644 --- a/plugins/examples/hello-workbench/ui/index.html +++ b/plugins/examples/hello-workbench/ui/index.html @@ -366,6 +366,7 @@ const data = await window.dbxPlugin.queryData({ connectionId: resultContext.connectionId, database: resultContext.database || undefined, + schema: resultContext.schema || undefined, sql: resultContext.sql, maxRows: 1000, });