From 30a48a878cc1efc2a56c8258aa0e5e8141d50dce Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Thu, 28 May 2026 18:54:51 +0800 Subject: [PATCH] fix(query): reload connection config before execute --- apps/desktop/src/stores/connectionStore.ts | 32 +++++++++++++++------- apps/desktop/src/stores/queryStore.ts | 1 + packages/app-tests/queryStore.test.ts | 17 ++++++++++++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts index 04d5fdf46f..6f2a1494eb 100644 --- a/apps/desktop/src/stores/connectionStore.ts +++ b/apps/desktop/src/stores/connectionStore.ts @@ -128,6 +128,7 @@ export const useConnectionStore = defineStore("connection", () => { let layoutPersistTimer: ReturnType | null = null; const staleTreeRefreshIds = new Set(); let beforeConnectHandler: BeforeConnectHandler | null = null; + let initFromDiskPromise: Promise | null = null; function startEditing(id: string) { editingConnectionId.value = id; @@ -660,7 +661,11 @@ export const useConnectionStore = defineStore("connection", () => { async function ensureConnected(connectionId: string) { if (connectedIds.value.has(connectionId)) return; - const config = getConfig(connectionId); + let config = getConfig(connectionId); + if (!config) { + await initFromDisk(); + config = getConfig(connectionId); + } if (!config) { const error = new Error("Connection config not found"); recordConnectionError(connectionId, error); @@ -1737,15 +1742,22 @@ export const useConnectionStore = defineStore("connection", () => { } async function initFromDisk() { - pinnedTreeNodeIds.value = await loadPinnedTreeNodeIds(); - const saved = await api.loadConnections(); - connections.value = saved.map(normalizeConnection); - const savedLayout = await api.loadSidebarLayout(); - sidebarLayout.value = reconcileLayout( - connections.value.map((c) => c.id), - savedLayout, - ); - rebuildTreeNodes(); + if (!initFromDiskPromise) { + initFromDiskPromise = (async () => { + pinnedTreeNodeIds.value = await loadPinnedTreeNodeIds(); + const saved = await api.loadConnections(); + connections.value = saved.map(normalizeConnection); + const savedLayout = await api.loadSidebarLayout(); + sidebarLayout.value = reconcileLayout( + connections.value.map((c) => c.id), + savedLayout, + ); + rebuildTreeNodes(); + })().finally(() => { + initFromDiskPromise = null; + }); + } + await initFromDiskPromise; } function addEphemeralConnection(config: ConnectionConfig) { diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts index 1f327e7435..35c9c0f14a 100644 --- a/apps/desktop/src/stores/queryStore.ts +++ b/apps/desktop/src/stores/queryStore.ts @@ -558,6 +558,7 @@ export const useQueryStore = defineStore("query", () => { let useAgentResultSession = false; try { const connStore = useConnectionStore(); + await connStore.ensureConnected(tab.connectionId); const conn = connStore.getConfig(tab.connectionId); const useAgentCursor = !!conn?.db_type && AGENT_DRIVER_TYPES.has(conn.db_type); const queryTimeoutSecs = diff --git a/packages/app-tests/queryStore.test.ts b/packages/app-tests/queryStore.test.ts index 31a97fb00c..5294f88d5a 100644 --- a/packages/app-tests/queryStore.test.ts +++ b/packages/app-tests/queryStore.test.ts @@ -1,7 +1,9 @@ import { strict as assert } from "node:assert"; import test from "node:test"; import { createPinia, setActivePinia } from "pinia"; +import { useConnectionStore } from "../../apps/desktop/src/stores/connectionStore.ts"; import { useQueryStore } from "../../apps/desktop/src/stores/queryStore.ts"; +import type { ConnectionConfig } from "../../apps/desktop/src/types/database.ts"; import type { QueryResult } from "../../apps/desktop/src/types/database.ts"; function installMemoryStorage() { @@ -22,6 +24,18 @@ function installMemoryStorage() { }; } +function conn(id: string): ConnectionConfig { + return { + id, + name: id, + db_type: "postgres", + host: "localhost", + port: 5432, + username: "postgres", + password: "", + }; +} + test("setErrorResult stops loading and shows the error result", () => { setActivePinia(createPinia()); const store = useQueryStore(); @@ -41,11 +55,14 @@ test("setErrorResult stops loading and shows the error result", () => { test("evicting cached tab results releases multi-result payloads and sessions", async () => { const restoreStorage = installMemoryStorage(); setActivePinia(createPinia()); + const connectionStore = useConnectionStore(); const store = useQueryStore(); const originalFetch = globalThis.fetch; let executeCount = 0; const closedSessions: string[] = []; + connectionStore.addEphemeralConnection(conn("conn-1")); + globalThis.fetch = (async (input, init) => { const url = String(input); if (url === "/api/query/execute-multi") {