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.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-03 11:09:45 +08:00
1 джерело 0aaa586cec
коміт 81a746b8e2
2 files changed
+41 -2

No files matched your search

+32
Переглянути файл
@@ -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<string, string>;
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]);
});
+9 -2
Переглянути файл
@@ -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<void> {
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<void> {
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<void> {
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) });