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): sync open executors after default database edit

Melody Ruchedd25928699a
diff --git a/apps/desktop/src/stores/__tests__/connectionStore.updateConnectionDatabaseTabs.spec.ts b/apps/desktop/src/stores/__tests__/connectionStore.updateConnectionDatabaseTabs.spec.ts
new file mode 100644
index 0000000000..6c1bdcd407
--- /dev/null
+++ b/apps/desktop/src/stores/__tests__/connectionStore.updateConnectionDatabaseTabs.spec.ts
@@ -0,0 +1,87 @@
+import { createPinia, setActivePinia } from "pinia";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { ConnectionConfig } from "@/types/database";
+
+function installLocalStorage() {
+ const data = new Map<string, string>();
+ vi.stubGlobal("localStorage", {
+ getItem: vi.fn((key: string) => data.get(key) ?? null),
+ setItem: vi.fn((key: string, value: string) => data.set(key, value)),
+ removeItem: vi.fn((key: string) => data.delete(key)),
+ });
+}
+
+function postgresConnection(overrides: Partial<ConnectionConfig> = {}): ConnectionConfig {
+ return {
+ id: "pg-1",
+ name: "Reporting",
+ db_type: "postgres",
+ host: "127.0.0.1",
+ port: 5432,
+ username: "postgres",
+ password: "secret",
+ database: "testdb",
+ read_only: false,
+ ...overrides,
+ } as ConnectionConfig;
+}
+
+async function createStores() {
+ vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
+ vi.doMock("@/lib/backend/api", () => ({
+ checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
+ disconnectDb: vi.fn().mockResolvedValue(undefined),
+ deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
+ loadSchemaCache: vi.fn().mockResolvedValue(null),
+ saveConnections: vi.fn().mockResolvedValue(undefined),
+ saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
+ connectionDatabaseInfo: vi.fn().mockResolvedValue(undefined),
+ }));
+ const { useConnectionStore } = await import("@/stores/connectionStore");
+ const { useQueryStore } = await import("@/stores/queryStore");
+ return { connectionStore: useConnectionStore(), queryStore: useQueryStore() };
+}
+
+describe("connectionStore updateConnection re-syncs open query tabs' database", () => {
+ beforeEach(() => {
+ vi.resetModules();
+ vi.unstubAllGlobals();
+ installLocalStorage();
+ setActivePinia(createPinia());
+ });
+
+ it("re-points a tab still on the connection's old default database, but leaves a tab the user pointed elsewhere alone", async () => {
+ const { connectionStore, queryStore } = await createStores();
+ const connection = postgresConnection();
+ connectionStore.connections = [connection];
+
+ // Opened before the edit, still on the connection's original default database (#7905 repro).
+ const staleTabId = queryStore.createTab("pg-1", "testdb", "stale query");
+ queryStore.setTableMeta(staleTabId, { schema: "public", database: "testdb", tableName: "users", tableType: "TABLE", columns: [], primaryKeys: [] });
+ // A tab the user explicitly pointed at a different database via the tab's own
+ // connection/database switcher — must not be silently reassigned.
+ const explicitTabId = queryStore.createTab("pg-1", "reporting_archive", "explicit query", "query", undefined, undefined, undefined, { forceNew: true });
+
+ await connectionStore.updateConnection({ ...connection, database: "hive313" });
+
+ expect(connectionStore.getConfig("pg-1")?.database).toBe("hive313");
+ const staleTab = queryStore.tabs.find((tab) => tab.id === staleTabId);
+ expect(staleTab?.database).toBe("hive313");
+ // The stale snapshot's cached metadata/result state must not survive the switch.
+ expect(staleTab?.tableMeta).toBeUndefined();
+
+ const explicitTab = queryStore.tabs.find((tab) => tab.id === explicitTabId);
+ expect(explicitTab?.database).toBe("reporting_archive");
+ });
+
+ it("does nothing to open tabs when the edited connection's database is unchanged", async () => {
+ const { connectionStore, queryStore } = await createStores();
+ const connection = postgresConnection();
+ connectionStore.connections = [connection];
+ const tabId = queryStore.createTab("pg-1", "testdb", "unaffected query");
+
+ await connectionStore.updateConnection({ ...connection, host: "new-host.internal" });
+
+ expect(queryStore.tabs.find((tab) => tab.id === tabId)?.database).toBe("testdb");
+ });
+});
diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts
index 7f2789f9cd..59a4587c87 100644
--- a/apps/desktop/src/stores/connectionStore.ts
+++ b/apps/desktop/src/stores/connectionStore.ts
@@ -4051,6 +4051,7 @@ export const useConnectionStore = defineStore("connection", () => {
const idx = connections.value.findIndex((c) => c.id === config.id);
if (idx < 0) return;
const previousConfig = normalizeConnection(connections.value[idx]);
+ const previousDatabase = previousConfig.database ?? "";
const runtimeConfigChanged = connectionConfigFingerprint(previousConfig) !== connectionConfigFingerprint(config);
const shouldReconnectPlugin = runtimeConfigChanged && previousConfig.db_type === "plugin" && config.db_type === "plugin" && connectedIds.value.has(config.id);
const nextConnections = [...connections.value];
@@ -4062,6 +4063,11 @@ export const useConnectionStore = defineStore("connection", () => {
rebuildTreeNodes();
if (!runtimeConfigChanged) return;
clearEtcdAccessCapabilities(config.id);
+ // Tabs opened before this edit keep whatever database they were created
+ // with (queryStore snapshots it onto the tab); re-point the ones still on
+ // the old default so their query executor stops running against it (#7905).
+ const { useQueryStore } = await import("@/stores/queryStore");
+ useQueryStore().syncTabsDatabaseForConnectionEdit(config.id, previousDatabase, config.database ?? "");
clearPrimaryVisibleObjectNames(config.id);
connectedIds.value.delete(config.id);
clearSidebarStorageCaches(config.id);
diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts
index 2694666094..c046869d0f 100644
--- a/apps/desktop/src/stores/queryStore.ts
+++ b/apps/desktop/src/stores/queryStore.ts
@@ -5537,6 +5537,20 @@ export const useQueryStore = defineStore("query", () => {
persistSavedSqlExecutionTarget(tab, options);
}
+ /**
+ * Editing a saved connection's default database (Connection dialog Save) only
+ * updates the connection record — tabs opened before the edit keep whatever
+ * database was snapshotted onto them at creation time (issue #7905). Re-point
+ * every open tab that was still on the connection's old default over to the
+ * new one, same as if the tab had just been created fresh.
+ */
+ function syncTabsDatabaseForConnectionEdit(connectionId: string, previousDatabase: string, nextDatabase: string) {
+ if (previousDatabase === nextDatabase) return;
+ for (const tab of tabs.value) {
+ if (tab.connectionId === connectionId && tab.database === previousDatabase) updateDatabase(tab.id, nextDatabase);
+ }
+ }
+
function updateCatalog(id: string, catalog: string | undefined, database: string, options: UpdateExecutionTargetOptions = {}) {
const tab = tabs.value.find((candidate) => candidate.id === id);
if (!tab || (tab.catalog === catalog && tab.database === database)) return;
@@ -9489,6 +9503,7 @@ export const useQueryStore = defineStore("query", () => {
reorderTab,
createExecutionTargetGuard,
updateDatabase,
+ syncTabsDatabaseForConnectionEdit,
updateCatalog,
updateSchema,
updateConnection,