From d9fbb1978709e21003d09a18f0ffbe6da29099e4 Mon Sep 17 00:00:00 2001 From: JunChang Peng <34200974+junchangpeng@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:30:32 +0800 Subject: [PATCH] feat(database): show a table's DDL from the data view (#6909) The data view's toolbar gets a DDL button that opens the table's definition through the same openDdl the tree's Go to DDL uses, so checking an index or constraint no longer means leaving the rows. It is disabled while the DDL loads, for custom JDBC sources (no vendor DDL) and for catalog-qualified tables. Closes #6910 Co-authored-by: junchangpeng <34200974+junchangpeng@users.noreply.github.com> Co-authored-by: junchangpeng <34200974+junchangpeng@users.noreply.github.com> --- src/features/database/TableEditor.tsx | 24 +++++++++++++++++++-- src/features/database/table-editor.test.tsx | 23 ++++++++++++++++++++ src/platform/locales/text/zh.json | 1 + 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/features/database/TableEditor.tsx b/src/features/database/TableEditor.tsx index 8f0aa41d8..aa1bd7042 100644 --- a/src/features/database/TableEditor.tsx +++ b/src/features/database/TableEditor.tsx @@ -14,7 +14,7 @@ import { type DbTablePage, type RequestId, } from "@/platform/ipc"; -import { EmptyState, Icon, IconButton, Select, Spinner, Toolbar, showContextMenu } from "@/ui"; +import { Button, EmptyState, Icon, IconButton, Select, Spinner, Toolbar, showContextMenu } from "@/ui"; import { notifications, type EditorInput } from "@/workbench/contributions"; import { SqlFilterInput } from "./SqlFilterInput"; import { ColumnsPopup } from "./ColumnsPopup"; @@ -31,7 +31,7 @@ import { type Cell, type PendingEdits, } from "./model"; -import { openDialog, sourceById, withConnection } from "./store"; +import { openDialog, openDdl, sourceById, withConnection } from "./store"; import type { TableRef } from "./tableInput"; import styles from "./Database.module.css"; import { registerTableEditor, tableEditorChanged, type TableEditorHandle } from "./tableEditorFocus"; @@ -98,6 +98,9 @@ export function TableEditor({ input }: { input: EditorInput }) { [input.key], ); const [nonce, setNonce] = useState(0); + // The DDL travels to the backend and back, so the button shows it is working instead of + // leaving a click with nothing on screen for a few hundred milliseconds. + const [ddlBusy, setDdlBusy] = useState(false); const [edits, setEdits] = useState(emptyEdits); const [panel, setPanel] = useState(null); const [searchOpen, setSearchOpen] = useState(false); @@ -282,6 +285,14 @@ export function TableEditor({ input }: { input: EditorInput }) { if (page && r) setColumnsAt({ x: r.left + r.width / 2, y: r.top + 48 }); }; const addRow = () => setEdits((e) => ({ ...e, inserts: [...e.inserts, new Map()] })); + const showDdl = async () => { + setDdlBusy(true); + try { + await openDdl(ref.id, ref.schema, ref.table); + } finally { + setDdlBusy(false); + } + }; const restoreRows = (rows: number[]) => setEdits((e) => ({ ...e, deletes: new Set([...e.deletes].filter((d) => !rows.includes(d))) })); const showExport = (at: { x: number; y: number }) => @@ -350,6 +361,15 @@ export function TableEditor({ input }: { input: EditorInput }) { aria-pressed={searchOpen} onClick={() => setSearchOpen(!searchOpen)} /> + {editable && ( <> diff --git a/src/features/database/table-editor.test.tsx b/src/features/database/table-editor.test.tsx index 20734baca..e84e8e865 100644 --- a/src/features/database/table-editor.test.tsx +++ b/src/features/database/table-editor.test.tsx @@ -13,6 +13,7 @@ const backend = vi.hoisted(() => ({ page: vi.fn(), catalogPage: vi.fn(), applyEdits: vi.fn(), + openDdl: vi.fn(), })); vi.mock("@/platform/ipc", async (importOriginal) => { @@ -32,6 +33,7 @@ vi.mock("./store", () => ({ sourceById: () => ({ driver: backend.driver, readOnly: false }), withConnection: (_id: string, run: () => unknown) => run(), openDialog: vi.fn(), + openDdl: backend.openDdl, })); vi.mock("@tanstack/react-virtual", () => ({ @@ -126,6 +128,27 @@ describe("table editor capabilities", () => { }, ); + it("opens the table's DDL from the data view's toolbar", async () => { + backend.driver = "postgres"; + await act(async () => { + render(); + }); + const button = await screen.findByRole("button", { name: "DDL" }); + fireEvent.click(button); + expect(backend.openDdl).toHaveBeenCalledWith("source", "public", "users"); + }); + + it("does not offer DDL where the source cannot report it", async () => { + // A custom JDBC source answers no vendor DDL, and a catalog-qualified table is not the + // connection's own database, so neither can ask `db_ddl` for a definition. + backend.driver = "jdbc"; + await act(async () => { + render(); + }); + await screen.findByRole("gridcell", { name: "Ann" }); + expect(screen.getByRole("button", { name: "DDL" })).toHaveProperty("disabled", true); + }); + it("keeps keyed PostgreSQL rows editable", async () => { backend.driver = "postgres"; await act(async () => { diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index d84debd98..8ef0d1090 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -2217,6 +2217,7 @@ "Go to Child Commit": "转到子提交", "Go to Class…": "转到类…", "Go to DDL": "转到 DDL", + "Show DDL": "显示 DDL", "Go to File": "转到文件", "Go to File…": "转到文件…", "Go to Line:Column": "转到行:列",