feat(plugins): expose database table menus and read-only schema (#7372)

Plugin API 1.20 adds wit/database.wit with describe-table behind the database.schema capability: a read-only view of a connected source's table (columns, keys, indexes) through DbManager::connected_table_details. Plugins can contribute database.table.context menu entries (needs_api 20) that receive the non-secret table identity and the when keys databaseDriver, databaseObjectKind, databaseSchema, databaseTable and databaseSelectionCount.

Co-authored-by: gejun123456 <[email protected]>
Цей коміт міститься в:
葛俊таgejun123456 authored and GitHub committed 2026-12-02 14:25:35 +08:00
1 джерело abc5b2b2c6
коміт b7c2d4eacb
201 files changed
+1180 -176

No files matched your search

+19
Переглянути файл
@@ -1,5 +1,6 @@
/** Menus of the Database tool window, laid out like IDEA's (New ▸, Copy/Paste ▸, SQL Scripts ▸ …). */
import { choosePath } from "@/platform/choosePath";
import { pluginDatabaseTableMenuItems } from "@/features/plugins/uiPoints";
import { tx } from "@/platform/i18n";
import type { MenuItem } from "@/ui";
import { notifications } from "@/workbench/contributions";
@@ -194,6 +195,24 @@ function batchSourceMenu(sources: Extract<DbNode, { kind: "source" }>[]): MenuIt
/** Context menu of a tree node (`null`: the empty area). */
export function nodeMenu(n: DbNode | null, selection: readonly DbNode[] = []): MenuItem[] {
const builtin = builtinNodeMenu(n, selection);
if (n?.kind !== "table") return builtin;
const selected = selection.some((node) => node.key === n.key) ? selection : [n];
// Mixed selections cannot be represented as a table command; do not silently drop nodes.
if (!selected.every((node) => node.kind === "table")) return builtin;
const tables = selected.map((node) => ({
sourceId: node.id,
catalog: node.catalog ?? null,
schema: node.schema,
table: node.table.name,
kind: node.table.kind,
driver: sourceById(node.id)?.driver ?? "",
}));
const plugins = pluginDatabaseTableMenuItems(tables);
return plugins.length ? [...builtin, sep, ...plugins] : builtin;
}
function builtinNodeMenu(n: DbNode | null, selection: readonly DbNode[] = []): MenuItem[] {
if (!n) return [{ label: "New", icon: "plus", submenu: newMenu() }];
if (n.kind === "workspace")
return [
+99
Переглянути файл
@@ -0,0 +1,99 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { PluginInfo } from "@/bindings";
import { registerAction } from "@/platform/actions";
import { nodeMenu } from "@/features/database/treeMenu";
import { useDatabase } from "@/features/database/store";
import type { DbNode } from "@/features/database/nodes";
import { defineBuiltinUiPoints } from "../points";
import { clearUiContributions, syncUiContributions } from "../projection";
import { pluginDatabaseTableMenuItems, type DatabaseTableContext } from "../uiPoints";
const initialDatabase = useDatabase.getState();
let stop: () => void;
let offs: (() => void)[];
beforeEach(() => {
stop = defineBuiltinUiPoints();
offs = [];
});
afterEach(() => {
useDatabase.setState(initialDatabase, true);
clearUiContributions();
stop();
offs.forEach((off) => off());
});
const plugin = { id: "mapper", name: "Mapper generator", enabled: true, status: "active", icon: null } as PluginInfo;
const table: DatabaseTableContext = {
sourceId: "pg:inventory",
catalog: null,
schema: "public",
table: "users",
kind: "table",
driver: "postgres",
};
function contribute(when?: string) {
offs.push(registerAction({ id: "plugin.mapper.generate", title: "Generate mapper", run: () => {} }));
syncUiContributions(
[
{
pluginId: "mapper",
point: "menus",
key: "database.table.context/generate",
decl: {
menu: "database.table.context",
command: "generate",
submenu: "Generate",
when,
},
},
],
[plugin],
);
}
describe("database table plugin menu", () => {
it("passes only non-secret table identities and checks when for every selected table", () => {
contribute("databaseDriver == postgres && databaseObjectKind == table && databaseSelectionCount == 1");
const [entry] = pluginDatabaseTableMenuItems([table]);
expect(entry).toEqual({
kind: "action",
action: "plugin.mapper.generate",
arg: JSON.stringify({ tables: [table] }),
});
expect(pluginDatabaseTableMenuItems([])).toEqual([]);
expect(pluginDatabaseTableMenuItems([table, { ...table, table: "orders" }])).toEqual([]);
expect(pluginDatabaseTableMenuItems([{ ...table, kind: "view" }])).toEqual([]);
expect(pluginDatabaseTableMenuItems([{ ...table, driver: "mysql" }])).toEqual([]);
});
it("strips extra fields instead of serializing connection settings", () => {
contribute();
const injected = { ...table, password: "do-not-send", host: "private-host", url: "private-url" };
const [entry] = pluginDatabaseTableMenuItems([injected]);
expect(entry).toMatchObject({ arg: JSON.stringify({ tables: [table] }) });
});
it("appends commands to built-in table menus and retains a multi-catalog selection", () => {
contribute();
useDatabase.setState({ sources: [{ id: "duck", name: "Local", driver: "jdbc", vendor: "duckdb" }] });
const a: DbNode = {
kind: "table",
key: "duck/a/main/users",
id: "duck",
catalog: "a",
schema: "main",
table: { name: "users", kind: "table" },
};
const b: DbNode = { ...a, key: "duck/b/main/users", catalog: "b" };
const items = nodeMenu(a, [a, b]);
expect(items.length).toBeGreaterThan(1);
const entry = items.at(-1)!;
expect(entry).toMatchObject({ action: "plugin.mapper.generate" });
if (entry.kind !== "action") throw new Error("expected command");
expect(JSON.parse(entry.arg as string).tables).toEqual([
{ sourceId: "duck", catalog: "a", schema: "main", table: "users", kind: "table", driver: "jdbc" },
{ sourceId: "duck", catalog: "b", schema: "main", table: "users", kind: "table", driver: "jdbc" },
]);
expect(
nodeMenu(a, [{ kind: "schema", key: "duck/main", id: "duck", schema: "main" }, a]).some(
(item) => item.kind === "action" && item.action === "plugin.mapper.generate",
),
).toBe(false);
});
});
+48
Переглянути файл
@@ -96,6 +96,7 @@ interface MenuDecl {
| "commit.message"
| "services.context"
| "services.toolbar"
| "database.table.context"
| "openIn";
view?: string | null;
command: string;
@@ -187,6 +188,53 @@ export function pluginViewItemMenuItems(pluginId: string, view: string, key: str
return flattenSingleGroups(out);
}
/** Non-secret table context passed to Database table menu commands (API 1.20). */
export interface DatabaseTableContext {
sourceId: string;
catalog: string | null;
schema: string;
table: string;
kind: string;
driver: string;
}
/** The right-clicked table is included in selection by the Database tree. */
export function pluginDatabaseTableMenuItems(tables: readonly DatabaseTableContext[]): MenuItem[] {
if (!tables.length) return [];
const out: MenuItem[] = [];
const groups = new Map<string, MenuItem[]>();
// Explicitly pick non-secret fields; never forward a DataSource or DbNode.
const arg = JSON.stringify({
tables: tables.map(({ sourceId, catalog, schema, table, kind, driver }) => ({
sourceId,
catalog,
schema,
table,
kind,
driver,
})),
});
for (const { pluginId, decl } of contextItems.values()) {
if (decl.menu !== "database.table.context") continue;
if (
!tables.every((table) =>
evaluateWhen(decl.when ?? undefined, {
...getContext(),
databaseDriver: table.driver,
databaseObjectKind: table.kind,
databaseSchema: table.schema,
databaseTable: table.table,
databaseSelectionCount: tables.length,
}),
)
)
continue;
const action = pluginActionId(pluginId, decl.command);
if (getAction(action)) addGrouped(out, groups, pluginId, decl.submenu, { kind: "action", action, arg });
}
return flattenSingleGroups(out);
}
/** The when keys of one selected service. */
export function serviceWhenContext(service: ServiceContext, count: number) {
return {