From 5a0d167a2154306663ebac057b152f788a5af450 Mon Sep 17 00:00:00 2001 From: mango <1711456624@qq.com> Date: Fri, 7 Aug 2026 21:15:59 +0800 Subject: [PATCH] fix(query): sync toolbar connection context --- .../QueryEditor.external-sql-save.test.tsx | 49 +++++++++++++++++++ frontend/src/components/QueryEditor.tsx | 8 ++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/QueryEditor.external-sql-save.test.tsx b/frontend/src/components/QueryEditor.external-sql-save.test.tsx index 659eebcd..30e3f6ed 100644 --- a/frontend/src/components/QueryEditor.external-sql-save.test.tsx +++ b/frontend/src/components/QueryEditor.external-sql-save.test.tsx @@ -10,6 +10,7 @@ import type { SavedQuery, TabData } from '../types'; import { ORACLE_ROWID_LOCATOR_COLUMN } from '../utils/rowLocator'; import { setGlobalImeCompositionActive } from '../utils/shortcuts'; import { clearQueryEditorResultSession } from '../utils/queryEditorResultSessionCache'; +import { resolveNewQueryContext } from '../utils/newQueryContext'; import { QUERY_TAB_RENAME_REQUEST_EVENT } from '../utils/queryTabTitle'; import { clearQueryTabDraft, clearSQLFileTabDraft, getQueryTabDraft, getSQLFileTabDraft } from '../utils/sqlFileTabDrafts'; import { clearQueryEditorInlineRuntimeReadinessCache } from './queryEditor/QueryEditorAiAssist'; @@ -56,6 +57,7 @@ const storeState = vi.hoisted(() => ({ clearSqlLogs: vi.fn(), addSqlLog: vi.fn(), addTab: vi.fn(), + activeContext: null as { connectionId: string; dbName: string } | null, setActiveContext: vi.fn(), updateQueryTabDraft: vi.fn(), savedQueries: [] as SavedQuery[], @@ -853,6 +855,10 @@ describe('QueryEditor external SQL save', () => { runtimeEventListeners.clear(); storeState.addTab.mockReset(); storeState.setActiveContext.mockReset(); + storeState.activeContext = null; + storeState.setActiveContext.mockImplementation((context: { connectionId: string; dbName: string } | null) => { + storeState.activeContext = context; + }); storeState.saveQuery.mockReset(); storeState.saveQuery.mockImplementation(async (query: SavedQuery) => query); storeState.savedQueries = []; @@ -3231,6 +3237,49 @@ describe('QueryEditor external SQL save', () => { }); }); + it('syncs a cleared database to the active context when the toolbar switches connections', async () => { + storeState.connections = [ + ...createDefaultConnections(), + { + id: 'conn-2', + name: 'analytics', + config: { + type: 'mysql', + host: '127.0.0.2', + port: 3306, + user: 'root', + password: '', + database: '', + }, + }, + ]; + + let renderer!: ReactTestRenderer; + await act(async () => { + renderer = create(); + }); + + const toolbar = renderer.root.findByType(QueryEditorToolbar); + await act(async () => { + toolbar.props.onConnectionChange('conn-2'); + }); + + expect(storeState.setActiveContext).toHaveBeenLastCalledWith({ + connectionId: 'conn-2', + dbName: '', + }); + expect(storeState.activeContext).toEqual({ connectionId: 'conn-2', dbName: '' }); + expect(resolveNewQueryContext({ + sidebarContext: storeState.activeContext, + activeTab: createTab({ connectionId: 'conn-1', dbName: 'main' }), + validConnectionIds: new Set(storeState.connections.map((connection) => connection.id)), + })).toEqual({ connectionId: 'conn-2', dbName: '' }); + + await act(async () => { + renderer.unmount(); + }); + }); + it('loads table completions after selecting a database in a connection-scoped query tab', async () => { let renderer!: ReactTestRenderer; autoFetchState.visible = true; diff --git a/frontend/src/components/QueryEditor.tsx b/frontend/src/components/QueryEditor.tsx index 96680c52..0a324169 100644 --- a/frontend/src/components/QueryEditor.tsx +++ b/frontend/src/components/QueryEditor.tsx @@ -10611,8 +10611,14 @@ const QueryEditor: React.FC<{ tab: TabData; isActive?: boolean }> = ({ tab, isAc formatSettingsMenu={formatSettingsMenu} templateMenuItems={elasticsearchTemplateMenuItems} onConnectionChange={(val) => { - setCurrentConnectionId(val); + const connectionId = String(val || '').trim(); + currentConnectionIdRef.current = connectionId; + currentDbRef.current = ''; + setCurrentConnectionId(connectionId); setCurrentDb(''); + if (connectionId) { + setActiveContext({ connectionId, dbName: '' }); + } }} onDatabaseChange={handleDatabaseChange} onMaxRowsChange={(maxRows) => setQueryOptions({ maxRows })}