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(grid): save mysql virtual unique key under loaded scope

Jeff63c03139b629
diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue
index 02c5eddf26..da103bc380 100644
--- a/apps/desktop/src/components/grid/DataGrid.vue
+++ b/apps/desktop/src/components/grid/DataGrid.vue
@@ -326,7 +326,7 @@ import {
splitForeignKeyDisplayValues,
type ForeignKeyDisplayConfig,
} from "@/lib/dataGrid/dataGridForeignKeyDisplay";
-import { removeVirtualRowIdentifier, saveVirtualRowIdentifier, type VirtualRowIdentifierScope } from "@/lib/table/virtualRowIdentifier";
+import { removeVirtualRowIdentifier, saveVirtualRowIdentifier, tableDataVirtualRowIdentifierScope, type VirtualRowIdentifierScope } from "@/lib/table/virtualRowIdentifier";
import { useToast } from "@/composables/useToast";
import { translateBackendError } from "@/i18n/backend-errors";
@@ -4618,13 +4618,14 @@ const hasResultToolbarLeadingSlot = computed(() => !!slots["result-toolbar-leadi
const hasResultToolbarActionsSlot = computed(() => !!slots["result-toolbar-actions"]);
const virtualRowIdentifierScope = computed<VirtualRowIdentifierScope | undefined>(() => {
if (!props.connectionId || !props.database || !props.tableMeta?.tableName) return undefined;
- return {
+ const scope = {
connectionId: props.connectionId,
database: props.tableMeta.database ?? props.database,
catalog: props.tableMeta.catalog,
schema: props.tableMeta.schema,
tableName: props.tableMeta.tableName,
};
+ return props.context === "table-data" ? tableDataVirtualRowIdentifierScope(connectionStore.getConfig(props.connectionId), scope) : scope;
});
const virtualRowIdentifierColumns = computed(() => props.tableMeta?.virtualPrimaryKeys ?? []);
const showVirtualRowIdentifierControl = computed(() => {
diff --git a/apps/desktop/src/lib/__tests__/table/virtualRowIdentifier.spec.ts b/apps/desktop/src/lib/__tests__/table/virtualRowIdentifier.spec.ts
index c3acb47ebf..3a6618bbdb 100644
--- a/apps/desktop/src/lib/__tests__/table/virtualRowIdentifier.spec.ts
+++ b/apps/desktop/src/lib/__tests__/table/virtualRowIdentifier.spec.ts
@@ -1,7 +1,7 @@
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it } from "vitest";
-import { loadVirtualRowIdentifier, removeVirtualRowIdentifier, saveVirtualRowIdentifier, virtualRowIdentifierScopeKey } from "@/lib/table/virtualRowIdentifier";
+import { loadVirtualRowIdentifier, removeVirtualRowIdentifier, saveVirtualRowIdentifier, tableDataVirtualRowIdentifierScope, virtualRowIdentifierScopeKey } from "@/lib/table/virtualRowIdentifier";
import type { ColumnInfo } from "@/types/database";
const scope = {
@@ -43,6 +43,22 @@ describe("virtualRowIdentifier", () => {
expect(loadVirtualRowIdentifier(scope, columns)).toEqual([]);
});
+ it("saves a MySQL table data tab key under the scope its metadata load reads", () => {
+ const tabScope = { connectionId: "mysql-1", database: "shop", tableName: "orders" };
+ const columns = [column("k1"), column("k2")];
+
+ expect(saveVirtualRowIdentifier(tableDataVirtualRowIdentifierScope({ db_type: "mysql" }, tabScope), ["k1"], columns)).toBe(true);
+
+ // Data tab metadata loads send the MySQL database name as the schema.
+ expect(loadVirtualRowIdentifier({ ...tabScope, schema: "shop" }, columns)).toEqual(["k1"]);
+ });
+
+ it("keeps an explicit schema for schema-aware table data tabs", () => {
+ const tabScope = { connectionId: "pg-1", database: "app", schema: "sales", tableName: "orders" };
+
+ expect(tableDataVirtualRowIdentifierScope({ db_type: "postgres" }, tabScope)).toEqual(tabScope);
+ });
+
it("clears a persisted key", () => {
const columns = [column("ID")];
expect(saveVirtualRowIdentifier(scope, ["ID"], columns)).toBe(true);
diff --git a/apps/desktop/src/lib/table/virtualRowIdentifier.ts b/apps/desktop/src/lib/table/virtualRowIdentifier.ts
index 814998bf1a..eea254f922 100644
--- a/apps/desktop/src/lib/table/virtualRowIdentifier.ts
+++ b/apps/desktop/src/lib/table/virtualRowIdentifier.ts
@@ -1,4 +1,5 @@
import { safeLocalStorageGet, safeLocalStorageRemove, safeLocalStorageSet } from "@/lib/backend/safeStorage";
+import { metadataSchemaForConnection, type JdbcDialectConnection } from "@/lib/database/jdbcDialect";
import type { ColumnInfo } from "@/types/database";
const STORAGE_PREFIX = "dbx-table-virtual-row-identifier:";
@@ -21,6 +22,15 @@ export function virtualRowIdentifierScopeKey(scope: VirtualRowIdentifierScope):
return JSON.stringify([scope.connectionId, scope.database, scope.catalog ?? "", scope.schema ?? "", scope.tableName]);
}
+/**
+ * Table data tabs load the key with the schema they send to the metadata
+ * request, which for database-only engines such as MySQL is the database name
+ * rather than the tab's empty schema. Saving and clearing must use that scope.
+ */
+export function tableDataVirtualRowIdentifierScope(connection: JdbcDialectConnection | undefined, scope: VirtualRowIdentifierScope): VirtualRowIdentifierScope {
+ return { ...scope, schema: metadataSchemaForConnection(connection, scope.database, scope.schema) };
+}
+
export function resolveRowIdentifierColumns(requestedColumns: readonly string[], availableColumns: readonly Pick<ColumnInfo, "name">[]): string[] | null {
if (requestedColumns.length === 0 || availableColumns.length === 0) return null;
const resolved: string[] = [];