fix(plugins): pass schema to result views

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-10-03 23:42:38 +08:00
1 parent ff3c036f79
commit fc28c4a41e
5 files changed
+87 -6

No files matched your search

@@ -1382,13 +1382,19 @@ function openPluginResultView(pluginId: string, contributionId: string, label: s
// bounded snapshot — plugins that need more rows re-run the statement through // bounded snapshot — plugins that need more rows re-run the statement through
// `host.queryData` (host.data:read, with the user's consent). // `host.queryData` (host.data:read, with the user's consent).
const cappedRows = result.rows.slice(0, 500); const cappedRows = result.rows.slice(0, 500);
const connectionId = activeResultConnectionId.value || "";
const database = activeResultDatabase.value || "";
const schema = activeResultSchema.value || "";
queryStore.openPluginWorkbench(pluginId, contributionId, { queryStore.openPluginWorkbench(pluginId, contributionId, {
title: label, title: label,
connectionId: props.activeTab.connectionId || "", connectionId,
database: props.activeTab.database || "", database,
context: { context: {
connectionId: props.activeTab.connectionId || "", connectionId,
database: props.activeTab.database || "", 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), sql: resultSqlForGrid(props.activeTab),
result: { columns: result.columns, rows: cappedRows, truncated: result.rows.length > cappedRows.length }, result: { columns: result.columns, rows: cappedRows, truncated: result.rows.length > cappedRows.length },
}, },
@@ -50,6 +50,12 @@ const connection = {
password: "", password: "",
}; };
const targetConnection = {
...connection,
id: "conn-2",
name: "Target MySQL",
};
function result(sourceStatement?: string): QueryResult { function result(sourceStatement?: string): QueryResult {
return { return {
columns: ["id"], columns: ["id"],
@@ -81,8 +87,9 @@ async function mountContentArea(tab: QueryTab) {
const pinia = createPinia(); const pinia = createPinia();
setActivePinia(pinia); setActivePinia(pinia);
const connectionStore = useConnectionStore(); const connectionStore = useConnectionStore();
connectionStore.connections = [connection]; connectionStore.connections = [connection, targetConnection];
const queryStore = useQueryStore(); const queryStore = useQueryStore();
queryStore.tabs = [tab];
const openPluginWorkbench = vi.spyOn(queryStore, "openPluginWorkbench"); const openPluginWorkbench = vi.spyOn(queryStore, "openPluginWorkbench");
const state = reactive({ activeTab: tab }); const state = reactive({ activeTab: tab });
const host = document.createElement("div"); 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" }),
}),
);
});
}); });
+12
View File
@@ -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/);
});
+1 -1
View File
@@ -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. 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` ### `context-menu`
@@ -366,6 +366,7 @@
const data = await window.dbxPlugin.queryData({ const data = await window.dbxPlugin.queryData({
connectionId: resultContext.connectionId, connectionId: resultContext.connectionId,
database: resultContext.database || undefined, database: resultContext.database || undefined,
schema: resultContext.schema || undefined,
sql: resultContext.sql, sql: resultContext.sql,
maxRows: 1000, maxRows: 1000,
}); });