Compare commits

...
Author SHA1 Message Date
l0ng-ai 20c64d4f3e fix(database): don't leave the tree loading after a disconnect or rename
Disconnecting a data source (Deactivate, or Auto-disconnect when idle)
forgets what it loaded, but its expanded tree nodes stayed expanded and
showed Loading until collapsed by hand, since only expanding a node loads
its children. A disconnect now also collapses the nodes of what it
forgot: the source with all its databases, or one database. Renaming a
source dropped its metadata the same way; it now saves like any edit,
which reloads what the tree had expanded.

Fixes #4388
2026-11-19 10:50:38 +08:00
3 changed files with 48 additions and 8 deletions

No files matched your search

+14
View File
@@ -337,6 +337,20 @@ export function targetOf(id: string, database: string | null, current: string |
/** Key of the Database tree node of database `name` of data source `id`. */
export const databaseKey = (id: string, name: string) => `${id}/@${name}`;
/**
* `expanded` without the Database tree nodes that show what target `id` loaded (a data source
* id: the source and all its databases). Once that is forgotten, an expanded node would wait
* for children nothing loads until it is expanded again.
*/
export function collapseTarget(expanded: ReadonlySet<string>, id: string): Set<string> {
const { id: source, database } = splitTarget(id);
const under = (key: string, node: string) => key === node || key.startsWith(`${node}/`);
const shows = database
? (key: string) => under(key, databaseKey(source, database)) || under(key, id)
: (key: string) => under(key, source) || key.startsWith(`${source}:`);
return new Set([...expanded].filter((key) => !shows(key)));
}
const SSL_MODES: Record<string, DbSslMode> = {
disable: "disable",
disabled: "disable",
+30 -2
View File
@@ -274,6 +274,31 @@ describe("database metadata request lifetimes", () => {
expect(useDatabase.getState().connected.has("pg:app")).toBe(false);
});
it("collapses the tree nodes of what a disconnect forgets", async () => {
const tree = [
"pg",
"pg/@app",
"pg/public",
"pg/public/tables",
"pg:other/public",
"pg/@other",
"pg2",
"pg2/public",
];
useDatabase.setState({ treeExpanded: new Set(tree) });
await disconnect("pg:other");
expect([...useDatabase.getState().treeExpanded]).toEqual([
"pg",
"pg/@app",
"pg/public",
"pg/public/tables",
"pg2",
"pg2/public",
]);
await disconnect("pg");
expect([...useDatabase.getState().treeExpanded]).toEqual(["pg2", "pg2/public"]);
});
it("disconnects one database without invalidating its sibling", async () => {
const response = deferred();
backend.schemas.mockReturnValueOnce(response.promise);
@@ -334,11 +359,14 @@ describe("database metadata request lifetimes", () => {
});
describe("connection and metadata continuations", () => {
it("reloads every level the tree had expanded after a data source is edited", async () => {
it.each([
["edited", () => saveSource({ ...pg, host: "new-host" }, undefined)],
["renamed", () => renameSource("pg", "Renamed")],
])("reloads every level the tree had expanded after a data source is %s", async (_, change) => {
await loadSchemas("pg");
await loadTables("pg", "public");
await loadDetails("pg", "public", "users");
await saveSource({ ...pg, host: "new-host" }, undefined);
await change();
const st = useDatabase.getState();
expect(st.schemas.pg).toEqual({ status: "ok", value: ["public"] });
expect(st.tables["pg\u0000public"]?.status).toBe("ok");
+4 -6
View File
@@ -38,6 +38,7 @@ import { workflowSettings } from "@/platform/workflowSettings";
import { ask, confirm } from "@/workbench/messages";
import {
activeSchema,
collapseTarget,
colorVar,
connectRemedy,
databaseKey,
@@ -437,12 +438,8 @@ export async function duplicateSource(id: string): Promise<void> {
export async function renameSource(id: string, name: string): Promise<void> {
const src = sourceById(id);
if (!src || !name.trim() || name.trim() === src.name) return;
const sources = useDatabase.getState().sources.map((s) => (s.id === src.id ? { ...s, name: name.trim() } : s));
forget(id);
const current = sourceMutation(id);
await call(commands.dbSaveSources(sources));
if (!current()) return;
await loadSources();
// Saved like any edit, so what the tree had loaded under the source is loaded again.
await saveSource({ ...src, name: name.trim() }, undefined);
}
/** Removes the data source after asking (Database tree › Drop on a source). */
@@ -572,6 +569,7 @@ export async function disconnect(id: string): Promise<void> {
if (!targets.includes(id)) targets.push(id);
const current = projectRequest();
forget(id);
useDatabase.setState((s) => ({ treeExpanded: collapseTarget(s.treeExpanded, id) }));
const intent = {};
for (const target of targets) connectionIntents.set(target, intent);
if (!splitTarget(id).database) sourceConnectionIntents.set(id, {});