From 81a746b8e2ff5b2b52ec264559edbb83d8d73405 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Thu, 3 Dec 2026 11:09:45 +0800 Subject: [PATCH] fix(database): show a reloaded DDL in its open editor (#7659) Reload in a generated DDL file and Go to DDL on an already open DDL file wrote the file but left the open editor stale: the file lives outside the project. The open document is now reloaded after the write. --- src/features/database/ddlFile.test.ts | 32 +++++++++++++++++++++++++++ src/features/database/store.ts | 11 +++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/features/database/ddlFile.test.ts diff --git a/src/features/database/ddlFile.test.ts b/src/features/database/ddlFile.test.ts new file mode 100644 index 000000000..8f8eaf14d --- /dev/null +++ b/src/features/database/ddlFile.test.ts @@ -0,0 +1,32 @@ +import { afterEach, expect, it, vi } from "vitest"; +import { registerAction } from "@/platform/actions"; +import { setInvokeRoute } from "@/platform/tauriCore"; +import { reloadDdl, useDatabase } from "./store"; + +afterEach(() => setInvokeRoute(null)); + +it("reloads the DDL into the open editor of its generated file", async () => { + const path = "/data/consoles/db/s1/main.goods.sql"; + const written: [string, string][] = []; + setInvokeRoute((cmd, args) => { + const a = args as Record; + if (cmd === "db_ddl") return Promise.resolve("CREATE TABLE goods (id INTEGER, note TEXT);"); + if (cmd === "db_export_file") written.push([a.path!, a.text!]); + return Promise.resolve(null); + }); + const reloaded = vi.fn(); + const unregister = registerAction({ + id: "file.reloadFromDisk", + title: "Reload from Disk", + run: ({ arg }) => reloaded(arg), + }); + useDatabase.setState({ ddlFiles: { [path]: { id: "s1", schema: "main", table: "goods" } } }); + try { + await reloadDdl(path); + } finally { + unregister(); + } + expect(written.map(([p]) => p)).toEqual([path]); + expect(written[0]![1]).toContain("note"); + expect(reloaded).toHaveBeenCalledWith([path]); +}); diff --git a/src/features/database/store.ts b/src/features/database/store.ts index 005f413bb..874798b36 100644 --- a/src/features/database/store.ts +++ b/src/features/database/store.ts @@ -2,6 +2,7 @@ * Database tools state: data sources, connection status, the introspection * cache of the tree, query consoles (file → data source) and their results. */ +import { executeAction } from "@/platform/actions"; import { useProject } from "@/platform/project"; import { isRemoteWindow } from "@/platform/remote"; import { Channel } from "@tauri-apps/api/core"; @@ -2034,6 +2035,12 @@ export function readableDdl(text: string, driver: DbDriver | undefined): string return driver === "sqlite" ? formatted(text, driver) : text; } +/** Writes a generated DDL file; its file lives outside the project, so an open editor is reloaded here. */ +async function writeDdl(path: string, text: string): Promise { + await call(commands.dbExportFile(path, text)); + await executeAction("file.reloadFromDisk", [path]); +} + /** Go to DDL: the definition of a table / view, in a SQL file next to the data source's console. */ export async function openDdl(id: string, schema: string, name: string, catalog?: string): Promise { try { @@ -2043,7 +2050,7 @@ export async function openDdl(id: string, schema: string, name: string, catalog? ]); const safe = (s: string) => s.replace(/[^\w.-]+/g, "_"); const path = console.replace(/[^\\/]*$/, `${safe(schema)}.${safe(name)}.sql`); - await call(commands.dbExportFile(path, readableDdl(text, sourceById(id)?.driver))); + await writeDdl(path, readableDdl(text, sourceById(id)?.driver)); bindConsole(path, id); // Reload on the generated file needs the table it came from; the path cannot be parsed back // into one (`safe` above is lossy). @@ -2064,7 +2071,7 @@ export async function reloadDdl(path: string): Promise { if (!target) return; try { const text = await withConnection(target.id, () => call(commands.dbDdl(target.id, target.schema, target.table))); - await call(commands.dbExportFile(path, readableDdl(text, sourceById(target.id)?.driver))); + await writeDdl(path, readableDdl(text, sourceById(target.id)?.driver)); notifications().notify({ level: "info", title: `Reloaded the DDL of ${target.table}`, key: "db-ddl-reload" }); } catch (e) { notifications().notify({ level: "error", title: `Cannot get the DDL of ${target.table}`, body: message(e) });