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(kingbase): refresh mode before frontend connection readiness (#12827)

Hand Sonic302dbf60943b
diff --git a/apps/desktop/src/stores/__tests__/connectionStore.kingbase.spec.ts b/apps/desktop/src/stores/__tests__/connectionStore.kingbase.spec.ts
new file mode 100644
index 0000000000..808c0aeffe
--- /dev/null
+++ b/apps/desktop/src/stores/__tests__/connectionStore.kingbase.spec.ts
@@ -0,0 +1,82 @@
+import { createPinia, setActivePinia } from "pinia";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { ConnectionConfig } from "@/types/database";
+
+const api = vi.hoisted(() => ({
+ connectDb: vi.fn(),
+ connectionDatabaseInfo: vi.fn(),
+ saveConnectionDatabaseInfo: vi.fn(),
+ saveConnections: vi.fn(),
+ saveSidebarLayout: vi.fn(),
+ connectionIdentifierQuote: vi.fn(),
+}));
+vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
+vi.mock("@/lib/backend/api", () => api);
+
+// Cold module transformation is setup work, not part of the behavioral timeout.
+import { useConnectionStore } from "@/stores/connectionStore";
+
+function kingbaseConnection(): ConnectionConfig {
+ return {
+ id: "kingbase-info",
+ name: "Synthetic Kingbase",
+ db_type: "kingbase",
+ host: "example.test",
+ port: 54321,
+ username: "synthetic",
+ password: "synthetic-fixture",
+ database: "app",
+ };
+}
+
+describe("connectionStore Kingbase mode lifecycle", () => {
+ beforeEach(() => {
+ vi.resetAllMocks();
+ const data = new Map<string, string>();
+ vi.stubGlobal("localStorage", {
+ getItem: (key: string) => data.get(key) ?? null,
+ setItem: (key: string, value: string) => data.set(key, value),
+ removeItem: (key: string) => data.delete(key),
+ });
+ setActivePinia(createPinia());
+ api.connectDb.mockResolvedValue("kingbase-info");
+ api.saveConnectionDatabaseInfo.mockResolvedValue(undefined);
+ api.saveConnections.mockResolvedValue(undefined);
+ api.saveSidebarLayout.mockResolvedValue(undefined);
+ api.connectionIdentifierQuote.mockResolvedValue('"');
+ });
+
+ it.each(["connect", "ensureConnected"] as const)("refreshes mode before %s is ready and discards stale metadata on failure", async (method) => {
+ const config = { ...kingbaseConnection(), database_info: { compatibilityMode: "mysql" } };
+ let reject!: (error: Error) => void;
+ api.connectionDatabaseInfo.mockReturnValue(
+ new Promise((_, rejectPromise) => {
+ reject = rejectPromise;
+ }),
+ );
+ const store = useConnectionStore();
+ await store.addConnection(config);
+ const connecting = method === "connect" ? store.connect(config) : store.ensureConnected(config.id);
+ try {
+ await vi.waitFor(() => expect(api.connectionDatabaseInfo).toHaveBeenCalled());
+ expect(store.connectedIds.has(config.id)).toBe(false);
+ expect(store.getConfig(config.id)?.database_info?.compatibilityMode).toBeUndefined();
+ } finally {
+ reject(new Error("mode metadata unavailable"));
+ await connecting;
+ }
+ expect(store.connectedIds.has(config.id)).toBe(true);
+ expect(store.getConfig(config.id)?.database_info?.compatibilityMode).toBeUndefined();
+ });
+
+ it("refreshes and persists the reported mode", async () => {
+ const mode = "oracle";
+ const config = kingbaseConnection();
+ api.connectionDatabaseInfo.mockResolvedValue({ productName: "KingbaseES", compatibilityMode: mode });
+ const store = useConnectionStore();
+ await store.addConnection(config);
+ await store.connect(config);
+ expect(store.getConfig(config.id)?.database_info?.compatibilityMode).toBe(mode);
+ expect(api.saveConnectionDatabaseInfo).toHaveBeenCalledWith(config.id, expect.objectContaining({ compatibilityMode: mode }));
+ });
+});
diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts
index 6601bdddbe..01b59c90c1 100644
--- a/apps/desktop/src/stores/connectionStore.ts
+++ b/apps/desktop/src/stores/connectionStore.ts
@@ -4408,6 +4408,10 @@ export const useConnectionStore = defineStore("connection", () => {
async function refreshConnectedDatabaseInfo(connectionId: string, config: ConnectionConfig): Promise<void> {
const expectedConfigFingerprint = connectionConfigFingerprint(config);
+ const current = getConfig(connectionId);
+ if (config.db_type === "kingbase" && current?.database_info && connectionConfigFingerprint(current) === expectedConfigFingerprint) {
+ current.database_info = { ...current.database_info, compatibilityMode: undefined };
+ }
try {
const detected = await api.connectionDatabaseInfo(connectionId);
const normalized = normalizeDatabaseConnectionInfo(detected, configuredDatabaseProductName(config), config.database);
@@ -4859,10 +4863,9 @@ export const useConnectionStore = defineStore("connection", () => {
await ensureLocalConnectionAttemptActiveAfterConnectResult(config.id, localAttempt, id);
await syncMongoLegacyDriverFallback(id, config);
await ensureLocalConnectionAttemptActiveAfterConnectResult(config.id, localAttempt, id);
- const refreshDwsMetadataBeforeReady = config.db_type === "gaussdb" && gaussdbConnectionMode(config) === "dws";
- if (config.db_type !== "plugin" && refreshDwsMetadataBeforeReady) {
- // Refresh the product version before exposing the connection so `dbVersion`
- // is current. Support comes from the explicit profile, not the version.
+ const refreshMetadataBeforeReady = config.db_type === "kingbase" || (config.db_type === "gaussdb" && gaussdbConnectionMode(config) === "dws");
+ if (config.db_type !== "plugin" && refreshMetadataBeforeReady) {
+ // Kingbase parsing needs the live mode; DWS needs the current product version.
// Metadata remains optional: refreshConnectedDatabaseInfo swallows failures.
await refreshConnectedDatabaseInfo(id, { ...config, id });
await ensureLocalConnectionAttemptActiveAfterConnectResult(config.id, localAttempt, id);
@@ -4874,7 +4877,7 @@ export const useConnectionStore = defineStore("connection", () => {
clearConnectionClosedByUser(config.id);
clearConnectionClosedByUser(id);
if (config.db_type !== "plugin") {
- if (!refreshDwsMetadataBeforeReady) void refreshConnectedDatabaseInfo(id, { ...config, id });
+ if (!refreshMetadataBeforeReady) void refreshConnectedDatabaseInfo(id, { ...config, id });
await refreshConnectionIdentifierQuote(id, { ...config, id });
ensureLocalConnectionAttemptActive(config.id, localAttempt);
// Compatibility modes warm asynchronously; the QueryEditor watcher and the
@@ -5335,10 +5338,9 @@ export const useConnectionStore = defineStore("connection", () => {
await ensureLocalConnectionAttemptActiveAfterConnectResult(connectionId, localAttempt, id);
await syncMongoLegacyDriverFallback(connectionId, config);
await ensureLocalConnectionAttemptActiveAfterConnectResult(connectionId, localAttempt, id);
- const refreshDwsMetadataBeforeReady = config.db_type === "gaussdb" && gaussdbConnectionMode(config) === "dws";
- if (config.db_type !== "plugin" && refreshDwsMetadataBeforeReady) {
- // Refresh the product version before reconnect completion so `dbVersion`
- // reflects the new server session; support comes from the explicit profile.
+ const refreshMetadataBeforeReady = config.db_type === "kingbase" || (config.db_type === "gaussdb" && gaussdbConnectionMode(config) === "dws");
+ if (config.db_type !== "plugin" && refreshMetadataBeforeReady) {
+ // Refresh mode/version before reconnect completion, never infer mode from version.
await refreshConnectedDatabaseInfo(connectionId, config);
await ensureLocalConnectionAttemptActiveAfterConnectResult(connectionId, localAttempt, id);
}
@@ -5346,7 +5348,7 @@ export const useConnectionStore = defineStore("connection", () => {
// 任何路径真的连上之后,"用户已关闭"标记就不能再拦后续的被动唤醒。
clearConnectionClosedByUser(connectionId);
if (config.db_type !== "plugin") {
- if (!refreshDwsMetadataBeforeReady) void refreshConnectedDatabaseInfo(connectionId, config);
+ if (!refreshMetadataBeforeReady) void refreshConnectedDatabaseInfo(connectionId, config);
await refreshConnectionIdentifierQuote(connectionId, config);
ensureLocalConnectionAttemptActive(connectionId, localAttempt);
void refreshConnectionDatabaseModes(connectionId, config);