diff --git a/apps/desktop/src/App.vue b/apps/desktop/src/App.vue index c31ee5c92d..3ce53aa596 100644 --- a/apps/desktop/src/App.vue +++ b/apps/desktop/src/App.vue @@ -44,6 +44,7 @@ import { resolveDefaultDatabase } from "@/lib/database/defaultDatabase"; import { findTreeNodeById, resolveNewQueryTarget, resolveNewQueryInitialSql } from "@/lib/sql/newQueryContext"; import { sqlObjectNavigationSourceKind, sqlObjectNavigationTableType, type SqlObjectNavigationTarget } from "@/lib/sql/sqlNavigation"; import { buildExecutableObjectSourceStatements, executeObjectSourceSave } from "@/lib/table/objectSourceEditor"; +import { schemaAfterConnectionSwitch } from "@/lib/schema/connectionSchemaInitialization"; import { resolveExecutableSql, resolveExecutableSqlWithBackend, type SqlExecutionSnapshot } from "@/lib/sql/sqlExecutionTarget"; import { uuid } from "@/lib/common/utils"; import { isMacOS, isWindows } from "@/lib/backend/platform"; @@ -1482,7 +1483,19 @@ async function changeActiveConnection(connectionId: string) { try { await connectionStore.ensureConnected(connectionId); const options = await getDatabaseOptions(connectionId); - queryStore.updateDatabase(tab.id, resolveDefaultDatabase(connection, options)); + const database = resolveDefaultDatabase(connection, options); + queryStore.updateDatabase(tab.id, database); + if (connection.db_type === "oracle") { + try { + // Oracle returns the session's current schema first; preserve that order before toolbar sorting. + const schema = schemaAfterConnectionSwitch(connection.db_type, await api.listSchemas(connectionId, database)); + if (schema && activeTab.value?.id === tab.id && activeTab.value.connectionId === connectionId) { + queryStore.updateSchema(tab.id, schema); + } + } catch { + // Schema metadata failure must not turn a successful connection switch into a connection error. + } + } } catch (e: any) { toast( t("connection.connectFailed", { diff --git a/apps/desktop/src/lib/__tests__/schema/connectionSchemaInitialization.spec.ts b/apps/desktop/src/lib/__tests__/schema/connectionSchemaInitialization.spec.ts new file mode 100644 index 0000000000..e6e10b1d65 --- /dev/null +++ b/apps/desktop/src/lib/__tests__/schema/connectionSchemaInitialization.spec.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from "vitest"; +import { schemaAfterConnectionSwitch } from "@/lib/schema/connectionSchemaInitialization"; + +describe("schemaAfterConnectionSwitch", () => { + it("selects the current Oracle schema from the raw ordered result", () => { + expect(schemaAfterConnectionSwitch("oracle", ["CONNECTED_USER", "APP", "SYSTEM"])).toBe("CONNECTED_USER"); + }); + + it("does not initialize schemas for non-Oracle connections", () => { + expect(schemaAfterConnectionSwitch("postgres", ["public", "archive"])).toBeUndefined(); + }); +}); diff --git a/apps/desktop/src/lib/schema/connectionSchemaInitialization.ts b/apps/desktop/src/lib/schema/connectionSchemaInitialization.ts new file mode 100644 index 0000000000..08a112f621 --- /dev/null +++ b/apps/desktop/src/lib/schema/connectionSchemaInitialization.ts @@ -0,0 +1,6 @@ +import type { DatabaseType } from "@/types/database"; + +export function schemaAfterConnectionSwitch(databaseType: DatabaseType | undefined, orderedSchemaNames: string[]): string | undefined { + if (databaseType !== "oracle") return undefined; + return orderedSchemaNames[0]; +} diff --git a/apps/desktop/src/stores/__tests__/queryStore.multiStatementError.spec.ts b/apps/desktop/src/stores/__tests__/queryStore.multiStatementError.spec.ts index 533dbdea70..ffd62bc684 100644 --- a/apps/desktop/src/stores/__tests__/queryStore.multiStatementError.spec.ts +++ b/apps/desktop/src/stores/__tests__/queryStore.multiStatementError.spec.ts @@ -155,7 +155,10 @@ describe("queryStore multi-statement errors", () => { store.updateSchema(tabId, undefined); - expect(store.tabs.find((tab) => tab.id === tabId)?.completionContextVersion).toBe(1); + expect(store.tabs.find((tab) => tab.id === tabId)).toMatchObject({ + schema: undefined, + completionContextVersion: 1, + }); }); it("preserves the selected statement's absolute editor range", async () => {