From 5ce2cce85bf90a7a80f68c23d5f46a1b25e47b9b Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Sat, 16 May 2026 00:50:45 +0800 Subject: [PATCH] fix(schema): prevent repeated schema option loads --- src/composables/useSchemaOptions.ts | 8 ++++++++ tests/schemaOptions.test.ts | 12 ++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/schemaOptions.test.ts diff --git a/src/composables/useSchemaOptions.ts b/src/composables/useSchemaOptions.ts index 480b0cae81..f177a4c1bf 100644 --- a/src/composables/useSchemaOptions.ts +++ b/src/composables/useSchemaOptions.ts @@ -3,6 +3,10 @@ import { useConnectionStore } from "@/stores/connectionStore"; import { isSchemaAware as isSchemaAwareType, isSingleDatabase } from "@/lib/databaseCapabilities"; import * as api from "@/lib/api"; +export function hasSchemaOptionsCacheEntry(options: Record, key: string): boolean { + return Object.prototype.hasOwnProperty.call(options, key); +} + export function useSchemaOptions() { const connectionStore = useConnectionStore(); @@ -22,12 +26,16 @@ export function useSchemaOptions() { const dbType = connectionStore.getConfig(connectionId)?.db_type; if (!database && !isSingleDatabase(dbType)) return; const key = cacheKey(connectionId, database); + if (hasSchemaOptionsCacheEntry(schemaOptions.value, key)) return; if (loadingSchemaOptions.value[key]) return; loadingSchemaOptions.value[key] = true; try { await connectionStore.ensureConnected(connectionId); schemaOptions.value[key] = await api.listSchemas(connectionId, database); + } catch (e) { + schemaOptions.value[key] = []; + throw e; } finally { loadingSchemaOptions.value[key] = false; } diff --git a/tests/schemaOptions.test.ts b/tests/schemaOptions.test.ts new file mode 100644 index 0000000000..2fa0685080 --- /dev/null +++ b/tests/schemaOptions.test.ts @@ -0,0 +1,12 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { hasSchemaOptionsCacheEntry } from "../src/composables/useSchemaOptions.ts"; + +test("treats an empty schema option list as a loaded cache entry", () => { + const options = { + "conn:db": [], + }; + + assert.equal(hasSchemaOptionsCacheEntry(options, "conn:db"), true); + assert.equal(hasSchemaOptionsCacheEntry(options, "conn:other"), false); +});