Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(query): rebind data and structure tabs after connection delete

javalover123b7f655f379b4
diff --git a/apps/desktop/src/lib/__tests__/app/openTabsPersistence.spec.ts b/apps/desktop/src/lib/__tests__/app/openTabsPersistence.spec.ts
index a499599e5c..0fd3e22f8c 100644
--- a/apps/desktop/src/lib/__tests__/app/openTabsPersistence.spec.ts
+++ b/apps/desktop/src/lib/__tests__/app/openTabsPersistence.spec.ts
@@ -399,6 +399,22 @@ describe("openTabsPersistence detached connection tabs", () => {
expect(tabs[0].detachedConnectionName).toBe("prod");
});
+ it("keeps a detached data/structure tab even though its connection no longer exists", () => {
+ // keep-all-tabs 会连同非 SQL 页签一起保留,它们带着 detachedConnectionName 等新建同名连接重绑;
+ // 若恢复阶段按 validConnectionIds 丢掉,重启后 keep-all-tabs 就形同虚设。
+ const saved = serializeOpenTabs([
+ queryTab({ id: "orphan-data", connectionId: "deleted-conn", database: "app", title: "users", mode: "data", detachedConnectionName: "prod" }),
+ queryTab({ id: "orphan-structure", connectionId: "deleted-conn", database: "app", title: "users 结构", mode: "structure", detachedConnectionName: "prod" }),
+ // 没有脱离标记的非 SQL 页签仍然要按连接有效性过滤掉。
+ queryTab({ id: "stray-data", connectionId: "deleted-conn", database: "app", title: "orders", mode: "data" }),
+ ]);
+
+ const { tabs } = restoreOpenTabsPayload({ tabs: saved, activeTabId: "orphan-data" }, { validConnectionIds: ["other-conn"] });
+
+ expect(tabs.map((tab) => tab.id)).toEqual(["orphan-data", "orphan-structure"]);
+ expect(tabs[0].detachedConnectionName).toBe("prod");
+ });
+
it("omits the detached connection name when the tab is bound to a live connection", () => {
const [saved] = serializeOpenTabs([queryTab({})]);
diff --git a/apps/desktop/src/lib/app/openTabsPersistence.ts b/apps/desktop/src/lib/app/openTabsPersistence.ts
index f2790ddcdc..e335b6007e 100644
--- a/apps/desktop/src/lib/app/openTabsPersistence.ts
+++ b/apps/desktop/src/lib/app/openTabsPersistence.ts
@@ -284,7 +284,9 @@ function restoreOpenTabsArray(parsed: unknown, rawActiveTabId: string | null, op
if (options.queryOnly && mode !== "query") return false;
if (options.filter === "pinned" && !tab.pinned && !unfinishedTypeChange) return false;
if (options.filter === "recovery" && !unfinishedTypeChange) return false;
- if (mode !== "query" && validConnectionIds && !validConnectionIds.has(tab.connectionId)) return false;
+ // 删除连接时按策略保留下来的非 SQL 页签会带 detachedConnectionName,等新建同名连接后
+ // 重绑;不能在恢复阶段因为连接「暂时不存在」就把它们丢掉,否则 keep-all-tabs 形同虚设。
+ if (mode !== "query" && !tab.detachedConnectionName && validConnectionIds && !validConnectionIds.has(tab.connectionId)) return false;
return true;
});
const tabs: QueryTab[] = filtered.map((tab) => {
diff --git a/apps/desktop/src/stores/__tests__/connectionStore.deleteTabs.spec.ts b/apps/desktop/src/stores/__tests__/connectionStore.deleteTabs.spec.ts
index dba68994e5..bb210f6454 100644
--- a/apps/desktop/src/stores/__tests__/connectionStore.deleteTabs.spec.ts
+++ b/apps/desktop/src/stores/__tests__/connectionStore.deleteTabs.spec.ts
@@ -298,6 +298,38 @@ describe("connectionStore delete-time tab handling", () => {
expect(queryStore.tabs.find((tab) => tab.id === sqlId)?.connectionId).toBe("conn-a");
});
+ it("rebinds kept data and structure tabs to a same-name connection created afterwards", async () => {
+ installApiMocks();
+ const { useConnectionStore } = await import("@/stores/connectionStore");
+ const { useQueryStore } = await import("@/stores/queryStore");
+ const { useSettingsStore } = await import("@/stores/settingsStore");
+ useSettingsStore().updateEditorSettings({ deleteConnectionTabHandlingMode: "keep-all-tabs", rememberConnectionDatabaseOnDelete: true });
+
+ const store = useConnectionStore();
+ store.connections = [pgConnection("conn-a", "prod", "app")];
+ const queryStore = useQueryStore();
+ const sqlId = queryStore.createTab("conn-a", "app", "draft.sql", "query");
+ const dataId = queryStore.createTab("conn-a", "app", "users", "data", "public", undefined, undefined, { forceNew: true });
+ const structureId = queryStore.createTab("conn-a", "app", "users 结构", "structure", "public", undefined, undefined, { forceNew: true });
+
+ await store.removeConnection("conn-a");
+
+ // 三个页签都保留,且都要记录原连接名——非 SQL 页签漏标记就会一直指向已删除的连接。
+ expect(queryStore.tabs.map((tab) => tab.id).sort()).toEqual([sqlId, dataId, structureId].sort());
+ for (const id of [sqlId, dataId, structureId]) {
+ expect(queryStore.tabs.find((tab) => tab.id === id)?.detachedConnectionName).toBe("prod");
+ }
+
+ // DeepLink 打开连接弹窗、用户保存后走的就是 addConnection。
+ await store.addConnection(pgConnection("conn-b", "prod", "app"));
+
+ for (const id of [sqlId, dataId, structureId]) {
+ const tab = queryStore.tabs.find((item) => item.id === id);
+ expect(tab?.connectionId).toBe("conn-b");
+ expect(tab?.detachedConnectionName).toBeUndefined();
+ }
+ });
+
it("keeps tabs untouched when a new connection has a different name", async () => {
installApiMocks();
const { useConnectionStore } = await import("@/stores/connectionStore");
diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts
index 572b20c6ce..e0468c5f61 100644
--- a/apps/desktop/src/stores/queryStore.ts
+++ b/apps/desktop/src/stores/queryStore.ts
@@ -2765,7 +2765,9 @@ export const useQueryStore = defineStore("query", () => {
const handoffs = await api.listDetachedTabHandoffs().catch(() => []);
for (const handoff of handoffs) {
if (!handoff || handoff.schemaVersion !== 1 || !handoff.tabId || !handoff.tab || typeof handoff.tab.id !== "string" || handoff.tab.id !== handoff.tabId) continue;
- if (validConnectionIds && handoff.tab.mode !== "query" && !validConnectionIds.has(handoff.tab.connectionId)) continue;
+ // 与恢复持久化页签同理:带 detachedConnectionName 的非 SQL 页签是按删除策略特意保留的,
+ // 等新建同名连接后重绑,不能因为连接暂时不存在就丢弃。
+ if (validConnectionIds && handoff.tab.mode !== "query" && !handoff.tab.detachedConnectionName && !validConnectionIds.has(handoff.tab.connectionId)) continue;
try {
await adoptDetachedTab(handoff);
await api.deleteDetachedTabHandoff(handoff.tabId);
@@ -5744,18 +5746,20 @@ export const useQueryStore = defineStore("query", () => {
* 关闭一律走非 force 路径(`closeScopedTabsWhere` → `beginBatchClose`):干净页签立即关闭,
* 未保存的 SQL 草稿与表结构草稿都要经保存/放弃确认,删除连接不能静默丢改动。
*
- * 该连接下的 SQL 页签都会记录原连接名(`detachedConnectionName`),便于新建同名连接后
- * 重新绑定;包括因用户在确认框中选择取消而残留的页签。
+ * 该连接下的**所有**页签都会记录原连接名(`detachedConnectionName`),便于新建同名连接后
+ * 重新绑定;包括因用户在确认框中选择取消而残留的页签。数据/结构等非 SQL 页签同样要标记:
+ * `keep-all-tabs` 会把它们一并保留,不标记就会一直指向已删除的连接
+ * ("Connection config not found")。
*/
function detachConnectionTabsForDelete(connectionId: string, options: { keep?: DeletedConnectionTabKeepMode; connectionName?: string } = {}) {
const keepMode = options.keep ?? "none";
const keep = keepMode === "all" ? () => true : keepMode === "pinned-sql" ? (tab: QueryTab) => tab.mode === "query" && tab.pinned === true : keepMode === "sql" ? (tab: QueryTab) => tab.mode === "query" : () => false;
const connectionName = options.connectionName?.trim();
- // 先给该连接下的 SQL 页签打上脱离标记。关闭流程可能因为「未保存」确认被用户取消,
+ // 先给该连接下的页签打上脱离标记。关闭流程可能因为「未保存」确认被用户取消,
// 取消后残留的页签同样需要能按连接名重绑到新建的同名连接。
if (connectionName) {
for (const tab of tabs.value) {
- if (tab.connectionId !== connectionId || tab.mode !== "query") continue;
+ if (tab.connectionId !== connectionId) continue;
tab.detachedConnectionName = connectionName;
}
}