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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(tdengine): preserve child tables and optimize tree loading (#12759)
Guoyu Su81b601bbdb33
diff --git a/apps/desktop/src/lib/__tests__/table/tableTree.spec.ts b/apps/desktop/src/lib/__tests__/table/tableTree.spec.tsindex 857bbec83f..67a4f3095e 100644--- a/apps/desktop/src/lib/__tests__/table/tableTree.spec.ts+++ b/apps/desktop/src/lib/__tests__/table/tableTree.spec.ts@@ -9,9 +9,11 @@ import {mergeTableTreePageChildren,tablePageRowAnchorKey,tablePartitionGroups,+ syncTablePartitionGroups,withoutTableTreeLoadMoreNodes,} from "@/lib/table/tableTree";import type { ObjectInfo, TableInfo, TreeNode } from "@/types/database";+import { decorateDatabaseSavedSqlTreeNodes } from "@/lib/savedSql/savedSqlDatabaseTree";const context = {nodeId: "connection:db",@@ -20,6 +22,61 @@ const context = {};describe("case-sensitive database objects", () => {+ it("publishes each changed partition group once per page while retaining exact identities", () => {+ const current = buildTableTreeNodes({+ ...context,+ tables: [+ { name: "stable", table_type: "STABLE" },+ { name: "child_0", table_type: "TABLE", parent_name: "stable" },+ ],+ });+ const group = tablePartitionGroups(current[0])[0];+ let children = group.children;+ let publications = 0;+ Object.defineProperty(group, "children", {+ configurable: true,+ get: () => children,+ set: (value: TreeNode[]) => {+ publications++;+ children = value;+ },+ });+ const names = [...Array.from({ length: 200 }, (_, i) => `child_${200 - i}`), "Foo", "foo", "child_0"];+ const page = buildTableTreeNodes({ ...context, tables: names.map((name) => ({ name, table_type: "TABLE", parent_name: "stable" })) });+ mergeTableTreePageChildren(current, page, context.connectionId, context.database);+ expect(publications).toBe(1);+ expect(group.objectCount).toBe(203);+ expect(new Set(children?.map((child) => child.label))).toEqual(new Set(["child_0", ...names]));+ expect(children?.slice(0, 3).map((child) => child.label)).toEqual(["child_0", "child_1", "child_2"]);+ expect(children?.slice(-2).map((child) => child.label)).toEqual(["Foo", "foo"]);+ });++ it("retains all 12722 child identities across decorated thousand-row TDengine pages", () => {+ const expected = new Map<string, string[]>();+ const rows: TableInfo[] = [];+ for (const [parent, count] of [+ ["stable_a", 5476],+ ["stable_b", 7246],+ ] as const) {+ const names = Array.from({ length: count }, (_, index) => `${parent}_${String(index).padStart(5, "0")}`);+ expected.set(parent, names);+ rows.push({ name: parent, table_type: "STABLE" }, ...names.map((name) => ({ name, table_type: "TABLE", parent_name: parent })));+ }+ let current: TreeNode[] = [];+ for (let offset = 0; offset < rows.length; offset += 1000) {+ const page = buildTableTreeNodes({ ...context, tables: rows.slice(offset, offset + 1000) });+ current = mergeTableTreePageChildren(current, page, context.connectionId, context.database);+ current = decorateDatabaseSavedSqlTreeNodes(current, new Map(), current);+ }+ expect(current.map((node) => node.label)).toEqual([...expected.keys()]);+ for (const node of current) {+ const group = tablePartitionGroups(node)[0];+ expect(group.children?.map((child) => child.label)).toEqual(expected.get(node.label));+ expect(new Set(group.children?.map((child) => child.id)).size).toBe(expected.get(node.label)!.length);+ expect(group.objectCount).toBe(expected.get(node.label)!.length);+ }+ });+const views: ObjectInfo[] = [{ name: "dbx_issue4529_case_V1", object_type: "VIEW", schema: "dbx_test" },{ name: "dbx_issue4529_case_v1", object_type: "VIEW", schema: "dbx_test" },@@ -454,6 +511,72 @@ describe("PostgreSQL table hierarchy", () => {});describe("TDengine table hierarchy", () => {+ it("synchronizes only partition groups and keeps hidden-only loaded children usable", () => {+ const nodes = decorateDatabaseSavedSqlTreeNodes(+ buildTableTreeNodes({+ ...context,+ tables: [+ { name: "meters", table_type: "STABLE" },+ { name: "meter_0", table_type: "TABLE", parent_name: "meters" },+ ],+ }),+ [],+ );+ const table = nodes[0];+ const columns: TreeNode = { id: "columns", label: "Columns", type: "group-columns", children: [] };+ table.hiddenChildren!.push(columns);+ const cursor: TreeNode = { id: "more", label: "More", type: "load-more", loadMore: { parentId: context.nodeId, offset: 2, pageSize: 2 } };+ appendTableTreeLoadMoreNode(nodes, cursor, { name: "meters" });+ expect(table.hiddenChildren).toContain(columns);+ expect(table.hiddenChildren!.find((node) => node.type === "group-partitions")).toBe(table.children![0]);+ expect(table.children![0].children).toContain(cursor);+ table.children = [];+ const hidden = table.hiddenChildren;+ syncTablePartitionGroups(table);+ expect(table.hiddenChildren).toBe(hidden);+ const merged = mergeTableTreePageChildren(withoutTableTreeLoadMoreNodes(nodes), buildTableTreeNodes({ ...context, tables: [{ name: "meter_1", table_type: "TABLE", parent_name: "meters" }] }), context.connectionId, context.database);+ expect(merged[0].children).toEqual([]);+ expect(tablePartitionGroups(merged[0])[0].children!.map((node) => node.label)).toEqual(["meter_0", "meter_1"]);+ expect(tablePartitionGroups(merged[0])[0].objectCount).toBe(2);+ });++ it("merges into the visible multi-level partition parent after saved-SQL decoration", () => {+ const first = decorateDatabaseSavedSqlTreeNodes(+ buildTableTreeNodes({+ ...context,+ schema: "public",+ tables: [+ { name: "events", table_type: "TABLE" },+ { name: "events_2026", table_type: "TABLE", parent_name: "events" },+ { name: "events_jan", table_type: "TABLE", parent_name: "events_2026" },+ ],+ }),+ [],+ );+ const page = () =>+ buildTableTreeNodes({+ ...context,+ schema: "public",+ tables: [+ { name: "events_feb", table_type: "TABLE", parent_name: "events_2026" },+ { name: "orphan", table_type: "TABLE", parent_name: "absent" },+ ],+ });+ const merged = mergeTableTreePageChildren(first, page(), context.connectionId, context.database);+ const parent = merged[0].children![0].children![0];+ expect(parent.label).toBe("events_2026");+ expect(parent.partitionParentName).toBe("events");+ expect(parent.children![0].children!.map((node) => [node.label, node.partitionParentName])).toEqual([+ ["events_feb", "events_2026"],+ ["events_jan", "events_2026"],+ ]);+ expect(parent.hiddenChildren![0]).toBe(parent.children![0]);+ expect(merged.map((node) => node.label)).toEqual(["events", "orphan"]);+ const repeated = mergeTableTreePageChildren(decorateDatabaseSavedSqlTreeNodes(merged, []), page(), context.connectionId, context.database);+ expect(repeated[0].children![0].children![0].children![0].objectCount).toBe(2);+ expect(repeated).toHaveLength(2);+ });+it("groups child tables under their supertable and keeps ordinary tables flat", () => {const tables: TableInfo[] = [{ name: "meters", table_type: "STABLE", comment: null },diff --git a/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts b/apps/desktop/src/lib/sidebar/sidebarSearchTree.tsindex 22f148d00c..87a14c4a4d 100644--- a/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts+++ b/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts@@ -83,9 +83,22 @@ export function reuseLiveSidebarTreeNodes(indexedNodes: TreeNode[], liveNodes: rexport const localTableSearchParentTypes = new Set<TreeNodeType>(["database", "schema", "linked-server-schema", "group-tables"]);const localTableSearchChildTypes = new Set<TreeNodeType>(["table", "view", "materialized_view"]);-export function filterLocallySearchedTables(nodes: TreeNode[], options: { enabled: boolean; queries: Readonly<Record<string, string>>; indexedResults: Readonly<Record<string, TableInfo[] | null>> }): TreeNode[] {+interface LocalTableSearchOptions {+ enabled: boolean;+ queries: Readonly<Record<string, string>>;+ indexedResults: Readonly<Record<string, TableInfo[] | null>>;+}++export function filterLocallySearchedTables(nodes: TreeNode[], options: LocalTableSearchOptions): TreeNode[] {+ // Avoid cloning and tracking the entire reactive tree when there is no local+ // search. Check the query map once, not at each recursive subtree.+ if (!options.enabled || !Object.values(options.queries).some((query) => query.trim())) return nodes;+ return filterLocallySearchedTableNodes(nodes, options);+}++function filterLocallySearchedTableNodes(nodes: TreeNode[], options: LocalTableSearchOptions): TreeNode[] {return nodes.map((node) => {- const children = node.children ? filterLocallySearchedTables(node.children, options) : undefined;+ const children = node.children ? filterLocallySearchedTableNodes(node.children, options) : undefined;const query = options.enabled && localTableSearchParentTypes.has(node.type) ? options.queries[node.id]?.trim() : "";if (!query || !children) return children === node.children ? node : { ...node, children };diff --git a/apps/desktop/src/lib/table/tableTree.ts b/apps/desktop/src/lib/table/tableTree.tsindex a503794210..adcdbe274d 100644--- a/apps/desktop/src/lib/table/tableTree.ts+++ b/apps/desktop/src/lib/table/tableTree.ts@@ -367,6 +367,19 @@ export function tablePartitionGroups(node: TreeNode): TreeNode[] {return partitionGroupChildren(node);}+// Saved-SQL decoration and persisted-tree decoding can clone visible children+// independently of hiddenChildren. Before mutating or releasing a table, retain+// its visible partition groups as the authoritative loaded state (including the+// load-more cursor), rather than resurrecting an older hidden snapshot.+export function syncTablePartitionGroups(node: TreeNode): void {+ if (node.type !== "table") return;+ const groups = node.children?.filter((child) => child.type === "group-partitions") ?? [];+ if (!groups.length) return;+ const hiddenGroups = node.hiddenChildren?.filter((child) => child.type === "group-partitions") ?? [];+ if (groups.length === hiddenGroups.length && groups.every((group, index) => group === hiddenGroups[index])) return;+ node.hiddenChildren = [...(node.hiddenChildren?.filter((child) => child.type !== "group-partitions") ?? []), ...groups];+}+export function hasTablePartitionGroups(node: TreeNode): boolean {return partitionGroupChildren(node).length > 0;}@@ -411,8 +424,9 @@ export function appendTableTreeLoadMoreNode(children: TreeNode[], loadMoreNode:// paginates the whole schema, so nesting its global cursor under whichever// partition happens to end the page can hide all remaining schema objects.const childGroup = parentNode && isSuperTable(parentNode) ? tablePartitionGroups(parentNode)[0] : undefined;- if (!childGroup) return [...children, loadMoreNode];+ if (!childGroup || !parentNode) return [...children, loadMoreNode];+ syncTablePartitionGroups(parentNode);childGroup.children = [...(childGroup.children ?? []), loadMoreNode];return children;}@@ -439,6 +453,9 @@ export function mergeTableTreePageChildren(currentChildren: TreeNode[], pageChilconst nodeKey = (node: TreeNode) => exactObjectIdentityKey("TABLE", node.schema, node.label);const collect = (nodes: readonly TreeNode[]) => {for (const node of nodes) {+ // Do not let a cloned hidden parent overwrite its visible counterpart in+ // the index when a later page adds another level of partitions.+ syncTablePartitionGroups(node);if (node.type === "table") {nodesByKey.set(nodeKey(node), node);}@@ -498,12 +515,21 @@ export function mergeTableTreePageChildren(currentChildren: TreeNode[], pageChilreturn group;};+ // Accumulate a page per parent: scanning/copying/sorting the growing group+ // for every child makes large STABLEs quadratic, especially through Vue proxies.+ const pendingGroups = new Map<TreeNode, { children: TreeNode[]; keys: Set<string> }>();const addToParent = (parent: TreeNode, child: TreeNode) => {const group = ensurePartitionGroup(parent);- const children = group.children ?? [];- if (!children.some((node) => nodeKey(node) === nodeKey(child))) {- group.children = sortDatabaseObjectsByName([...children, child], (node) => node.label);- group.objectCount = group.children.length;+ let pending = pendingGroups.get(group);+ if (!pending) {+ const children = [...(group.children ?? [])];+ pending = { children, keys: new Set(children.map(nodeKey)) };+ pendingGroups.set(group, pending);+ }+ const key = nodeKey(child);+ if (!pending.keys.has(key)) {+ pending.keys.add(key);+ pending.children.push(child);}};@@ -541,6 +567,10 @@ export function mergeTableTreePageChildren(currentChildren: TreeNode[], pageChilfor (const node of incomingNodes) {addNode(node);}+ for (const [group, pending] of pendingGroups) {+ group.children = sortDatabaseObjectsByName(pending.children, (node) => node.label);+ group.objectCount = group.children.length;+ }return sortDatabaseObjectsByName(roots, (node) => node.label);}diff --git a/apps/desktop/src/stores/__tests__/connectionStore.treeMemory.spec.ts b/apps/desktop/src/stores/__tests__/connectionStore.treeMemory.spec.tsindex a762f8b353..90739338bb 100644--- a/apps/desktop/src/stores/__tests__/connectionStore.treeMemory.spec.ts+++ b/apps/desktop/src/stores/__tests__/connectionStore.treeMemory.spec.ts@@ -1,7 +1,10 @@import { createPinia, setActivePinia } from "pinia";import { beforeEach, describe, expect, it, vi } from "vitest";import { isReactive } from "vue";-import type { ColumnInfo, ConnectionConfig, TreeNode } from "@/types/database";+import type { ColumnInfo, ConnectionConfig, TableInfo, TreeNode } from "@/types/database";+import { encodeSchemaTreeCache, SCHEMA_TREE_CACHE_TTL_MS } from "@/lib/metadata/schemaTreeCache";+import { decorateDatabaseSavedSqlTreeNodes } from "@/lib/savedSql/savedSqlDatabaseTree";+import { appendTableTreeLoadMoreNode, buildTableTreeNodes, mergeTableTreePageChildren, withoutTableTreeLoadMoreNodes } from "@/lib/table/tableTree";function installLocalStorage() {const data = new Map<string, string>();@@ -53,6 +56,46 @@ function findById(nodes: TreeNode[], id: string): TreeNode | undefined {const GROUP_ID = "pg-1:app:public:users:__columns";+async function setupTdengineCache() {+ const cache = new Map<string, any>();+ const rows: TableInfo[] = [{ name: "stable", table_type: "STABLE" }, ...Array.from({ length: 450 }, (_, index) => ({ name: `child_${String(index).padStart(3, "0")}`, table_type: "TABLE", parent_name: "stable" }))];+ const listTables = vi.fn((_id: string, _db: string, _schema: string, _search?: string, limit?: number, offset?: number) => Promise.resolve(rows.slice(offset ?? 0, (offset ?? 0) + (limit ?? rows.length))));+ const loadSchemaCache = vi.fn(async (key: string) => JSON.parse(JSON.stringify(cache.get(key) ?? null)));+ const saveSchemaCache = vi.fn(async (key: string, value: unknown) => {+ cache.set(key, JSON.parse(JSON.stringify(value)));+ });+ vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));+ vi.doMock("@/lib/backend/api", () => ({+ checkConnectionHealth: vi.fn().mockResolvedValue(undefined),+ listInstalledAgents: vi.fn().mockResolvedValue([]),+ deleteSchemaCachePrefix: vi.fn(async (prefix: string) => {+ for (const key of cache.keys()) if (key.startsWith(prefix)) cache.delete(key);+ }),+ listTables,+ loadSchemaCache,+ saveSchemaCache,+ saveConnections: vi.fn().mockResolvedValue(undefined),+ saveSidebarLayout: vi.fn().mockResolvedValue(undefined),+ }));+ const { useConnectionStore } = await import("@/stores/connectionStore");+ const { useSettingsStore } = await import("@/stores/settingsStore");+ const store = useConnectionStore();+ const settings = useSettingsStore();+ settings.editorSettings.sidebarObjectDisplay = "grouped";+ settings.desktopSettings.sidebar_table_page_size = 420;+ const connection = { ...postgresConnection(), db_type: "tdengine", sidebar_auto_load_all_tables: true } as ConnectionConfig;+ store.connections = [connection];+ store.connectedIds.add(connection.id);+ store.treeNodes = [{ id: connection.id, label: "TDengine", type: "connection", connectionId: connection.id, children: [{ id: `${connection.id}:app:__tables`, label: "Tables", type: "group-tables", connectionId: connection.id, database: "app", children: [] }] }];+ const group = store.treeNodes[0].children![0];+ const release = () => {+ group.isExpanded = false;+ expect(store.releaseCollapsedTreeNodeChildren(group.id)).toBe(true);+ expect(group.children).toEqual([]);+ };+ return { store, group, cache, rows, listTables, loadSchemaCache, saveSchemaCache, release };+}+async function setupStoreWithColumnGroup() {vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));@@ -130,6 +173,205 @@ describe("connectionStore schema tree memory", () => {setActivePinia(createPinia());});+ it("reuses a fresh complete TDengine auto-all snapshot after large-group release", async () => {+ const { store, group, listTables, release } = await setupTdengineCache();+ await store.loadObjectGroupChildren(group);+ expect(listTables.mock.calls.map((call) => call[5])).toEqual([0, 420]);+ const ids = group.children![0].children![0].children!.map((node) => node.id);+ for (let round = 0; round < 2; round++) {+ release();+ listTables.mockClear();+ await store.loadObjectGroupChildren(group);+ expect(listTables).not.toHaveBeenCalled();+ expect(group.children![0].children![0].children!.map((node) => node.id)).toEqual(ids);+ expect(group.children![0].children![0].objectCount).toBe(450);+ expect(group.isExpanded).toBe(true);+ }+ });++ it("continues a fresh partial TDengine snapshot from its nested cursor", async () => {+ const { store, group, listTables, release } = await setupTdengineCache();+ store.connections[0].sidebar_auto_load_all_tables = false;+ await store.loadObjectGroupChildren(group, { force: true });+ expect(group.children![0].children![0].children!.at(-1)?.loadMore?.offset).toBe(420);+ release();+ store.connections[0].sidebar_auto_load_all_tables = true;+ listTables.mockClear();+ await store.loadObjectGroupChildren(group);+ expect(listTables.mock.calls.map((call) => call[5])).toEqual([420]);+ expect(group.children![0].children![0].objectCount).toBe(450);+ expect(group.children![0].children![0].children!.some((node) => node.type === "load-more")).toBe(false);+ });++ it.each(["stale", "force", "legacy", "missing", "v2", "invalid-node", "invalid-cursor", "invalid-count"])("reloads TDengine %s cache through one controlled full load", async (mode) => {+ const { store, group, cache, listTables, release } = await setupTdengineCache();+ await store.loadObjectGroupChildren(group);+ release();+ for (const [key, value] of cache) {+ if (mode === "stale") value.cachedAt = new Date(Date.now() - SCHEMA_TREE_CACHE_TTL_MS - 1).toISOString();+ if (mode === "legacy") cache.set(key, value.children);+ if (mode === "missing") cache.delete(key);+ if (mode === "v2") value.version = 2;+ if (mode === "invalid-node") value.children = [null];+ if (mode === "invalid-cursor") value.children.push({ id: "bad-cursor", type: "load-more", label: "more", connectionId: group.connectionId, database: group.database });+ if (mode === "invalid-count") value.children[0].children[0].objectCount++;+ }+ listTables.mockClear();+ await store.loadObjectGroupChildren(group, mode === "force" ? { force: true } : undefined);+ expect(listTables.mock.calls.map((call) => call[5])).toEqual([0, 420]);+ expect(group.children![0].children![0].objectCount).toBe(450);+ });++ it.each(["cancel", "search", "scoped-search", "disconnect", "refresh", "invalidate", "config"])("rejects delayed TDengine cache hydration after %s", async (action) => {+ const { store, group, loadSchemaCache, listTables, cache, release } = await setupTdengineCache();+ await store.loadObjectGroupChildren(group);+ release();+ const payload = JSON.parse(JSON.stringify([...cache.values()][0]));+ let resolveCache!: (value: any) => void;+ loadSchemaCache.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ resolveCache = resolve;+ }),+ );+ listTables.mockClear();+ const pending = store.loadObjectGroupChildren(group);+ await vi.waitFor(() => expect(resolveCache).toBeDefined());+ if (action === "cancel") store.cancelTreeNodeLoad(group.id);+ if (action === "search") store.sidebarSearchQuery = "needle";+ if (action === "scoped-search") store.setSidebarTableSearchQuery(group.id, "needle");+ if (action === "disconnect") store.connectedIds.delete(group.connectionId!);+ if (action === "refresh") await store.loadObjectGroupChildren(group, { force: true });+ if (action === "invalidate") store.invalidateMetadataCache(group.connectionId!, "app");+ if (action === "config") store.connections[0].host = "another-host";+ group.isExpanded = false;+ const currentChildren = group.children;+ resolveCache(payload);+ await pending;+ expect(group.children).toBe(currentChildren);+ expect(group.isExpanded).toBe(false);+ expect(listTables).toHaveBeenCalledTimes(action === "refresh" ? 2 : 0);+ });++ it("refreshes an expanded TDengine parent's child identities, not just its count", async () => {+ const { store, group, rows } = await setupTdengineCache();+ await store.loadObjectGroupChildren(group);+ group.children![0].isExpanded = true;+ rows.splice(1, rows.length - 1, ...Array.from({ length: 450 }, (_, i) => ({ name: `new_${String(i).padStart(3, "0")}`, table_type: "TABLE", parent_name: "stable" })));+ await store.loadObjectGroupChildren(group, { force: true });+ const children = group.children![0].children!.find((node) => node.type === "group-partitions")!;+ expect(children.children?.map((node) => node.label)).toEqual(rows.slice(1).map((row) => row.name));+ expect(children.objectCount).toBe(450);+ });++ it("deletes an obsolete in-flight TDengine save before a newer refresh is persisted", async () => {+ const { store, group, rows, cache, saveSchemaCache } = await setupTdengineCache();+ const save = saveSchemaCache.getMockImplementation()!;+ let resolveSave!: () => void;+ saveSchemaCache.mockImplementationOnce(async (key, value) => {+ await new Promise<void>((resolve) => {+ resolveSave = resolve;+ });+ await save(key, value);+ });+ const oldLoad = store.loadObjectGroupChildren(group);+ await vi.waitFor(() => expect(resolveSave).toBeDefined());+ rows.splice(0, rows.length, { name: "replacement", table_type: "STABLE" });+ const refresh = store.loadObjectGroupChildren(group, { force: true });+ await vi.waitFor(() => expect(group.children?.[0]?.label).toBe("replacement"));+ resolveSave();+ await Promise.all([oldLoad, refresh]);+ const snapshots = [...cache.values()];+ expect(snapshots).toHaveLength(1);+ expect(snapshots[0].children.map((node: TreeNode) => node.label)).toEqual(["replacement"]);+ expect(group.children?.map((node) => node.label)).toEqual(["replacement"]);+ });++ it("retains valid partial progress on cancel while a TDengine cache save is pending", async () => {+ const { store, group, saveSchemaCache, listTables, release } = await setupTdengineCache();+ const save = saveSchemaCache.getMockImplementation()!;+ let resolveSave!: () => void;+ saveSchemaCache.mockImplementationOnce(async (key, value) => {+ await new Promise<void>((resolve) => {+ resolveSave = resolve;+ });+ await save(key, value);+ });+ const pending = store.loadObjectGroupChildren(group);+ await vi.waitFor(() => expect(resolveSave).toBeDefined());+ store.cancelTreeNodeLoad(group.id);+ group.isExpanded = false;+ resolveSave();+ await pending;+ expect(group.isExpanded).toBe(false);+ expect(listTables.mock.calls.map((call) => call[5])).toEqual([0]);+ release();+ listTables.mockClear();+ await store.loadObjectGroupChildren(group);+ expect(listTables.mock.calls.map((call) => call[5])).toEqual([420]);+ expect(group.children![0].children![0].objectCount).toBe(450);+ });++ it("does not reuse a persisted TDengine tree after metadata invalidation", async () => {+ const { store, group, listTables, release } = await setupTdengineCache();+ await store.loadObjectGroupChildren(group);+ release();+ store.invalidateMetadataCache(group.connectionId!, "app");+ listTables.mockClear();+ await store.loadObjectGroupChildren(group);+ expect(listTables.mock.calls.map((call) => call[5])).toEqual([0, 420]);+ });++ it.each([0, 420])("does not resume an obsolete TDengine page at offset %s after invalidation during persistence", async (offset) => {+ const { store, group, saveSchemaCache, listTables, cache } = await setupTdengineCache();+ const save = saveSchemaCache.getMockImplementation()!;+ if (offset > 0) saveSchemaCache.mockImplementationOnce(save);+ let resolveSave!: () => void;+ saveSchemaCache.mockImplementationOnce(async (key, value) => {+ await new Promise<void>((resolve) => {+ resolveSave = resolve;+ });+ await save(key, value);+ });+ const pending = store.loadObjectGroupChildren(group);+ await vi.waitFor(() => expect(resolveSave).toBeDefined());+ store.invalidateMetadataCache(group.connectionId!, "app");+ group.isExpanded = false;+ resolveSave();+ await pending;+ expect(listTables.mock.calls.map((call) => call[5])).toEqual(offset > 0 ? [0, 420] : [0]);+ expect(cache.size).toBe(0);+ expect(group.isExpanded).toBe(false);+ });++ it("does not mistake a self-consistent but incomplete historical v3 snapshot for a full TDengine tree", async () => {+ const { store, group, cache, listTables, loadSchemaCache, rows } = await setupTdengineCache();+ const legacyChildren = buildTableTreeNodes({ nodeId: "pg-1:app", connectionId: "pg-1", database: "app", tables: rows.slice(0, 100) });+ const legacyKey = "pg-1:app::group-tables:objects-v9";+ cache.set(legacyKey, encodeSchemaTreeCache(legacyChildren));+ await store.loadObjectGroupChildren(group);+ expect(loadSchemaCache).not.toHaveBeenCalledWith(legacyKey);+ expect(loadSchemaCache).toHaveBeenCalledWith("pg-1:app::group-tables:objects-tdengine-v1");+ expect(listTables.mock.calls.map((call) => call[5])).toEqual([0, 420]);+ expect(group.children![0].children![0].objectCount).toBe(450);+ expect(cache.has(legacyKey)).toBe(true);+ });++ it("retries a failed partial TDengine page without restarting page zero", async () => {+ const { store, group, listTables, release } = await setupTdengineCache();+ store.connections[0].sidebar_auto_load_all_tables = false;+ await store.loadObjectGroupChildren(group, { force: true });+ release();+ store.connections[0].sidebar_auto_load_all_tables = true;+ listTables.mockClear();+ listTables.mockRejectedValueOnce(new Error("temporary metadata failure"));+ await expect(store.loadObjectGroupChildren(group)).rejects.toThrow("temporary metadata failure");+ expect(group.children![0].children![0].objectCount).toBe(419);+ await store.loadObjectGroupChildren(group);+ expect(listTables.mock.calls.map((call) => call[5])).toEqual([420, 420]);+ expect(group.children![0].children![0].objectCount).toBe(450);+ });+it("marks leaf column nodes raw so Vue does not deep-wrap them", async () => {const { loadStore } = await setupStoreWithColumnGroup();const getColumns = vi.fn().mockResolvedValue(columns(5));@@ -178,4 +420,69 @@ describe("connectionStore schema tree memory", () => {expect(findById(store.treeNodes, GROUP_ID)?.children).toHaveLength(20);expect(store.isTreeNodeChildrenLoaded(GROUP_ID)).toBe(true);});++ it.each([false, true])("preserves decorated paged child tables through release/reopen (partial=%s)", async (partial) => {+ const { loadStore } = await setupStoreWithColumnGroup();+ const getColumns = vi.fn();+ const store = await loadStore(getColumns);+ store.connections[0].db_type = "tdengine";+ const context = { nodeId: "pg-1:app:__tables", connectionId: "pg-1", database: "app" };+ const childTables = (start: number, end: number) => Array.from({ length: end - start }, (_, index) => ({ name: `meter_${start + index}`, table_type: "CHILD TABLE", parent_name: "meters" }));+ const firstPage = buildTableTreeNodes({ ...context, tables: [{ name: "meters", table_type: "STABLE" }, ...childTables(0, 200)] });+ const decorated = decorateDatabaseSavedSqlTreeNodes(firstPage, []);+ // The real saved-SQL projection clones visible nodes but retains hidden ones.+ expect(decorated[0].children?.[0]).not.toBe(decorated[0].hiddenChildren?.[0]);+ let tables = mergeTableTreePageChildren(decorated, buildTableTreeNodes({ ...context, tables: childTables(200, 450) }), context.connectionId, context.database);+ const cursor: TreeNode = { id: `${context.nodeId}:__load_more`, label: "More", type: "load-more", connectionId: context.connectionId, database: context.database, loadMore: { parentId: context.nodeId, offset: 451, pageSize: 250, anchor: "\u0000meters\u0000meter_450" } };+ if (partial) tables = appendTableTreeLoadMoreNode(tables, cursor, { name: "meters" });+ store.treeNodes = decorateDatabaseSavedSqlTreeNodes(tables, []);+ const tableId = tables[0].id;+ const group = () => findById(store.treeNodes, tableId)!.children!.find((node) => node.type === "group-partitions")!;+ const expectedIds = group()+ .children!.filter((node) => node.type === "table")+ .map((node) => node.id);++ for (let round = 0; round < 3; round += 1) {+ await store.loadTableGroups(context.connectionId, context.database, "meters", undefined, tableId);+ findById(store.treeNodes, tableId)!.isExpanded = false;+ expect(store.releaseCollapsedTreeNodeChildren(tableId)).toBe(true);+ expect(findById(store.treeNodes, tableId)?.children).toEqual([]);+ expect(store.isTreeNodeChildrenLoaded(tableId)).toBe(false);+ await store.loadTableGroups(context.connectionId, context.database, "meters", undefined, tableId);+ expect(group().objectCount).toBe(450);+ expect(+ group()+ .children!.filter((node) => node.type === "table")+ .map((node) => node.id),+ ).toEqual(expectedIds);+ expect(group().children!.filter((node) => node.type === "load-more")).toEqual(partial ? [cursor] : []);+ }++ if (partial) {+ // Continue from the preserved cursor, including an overlapping row.+ store.treeNodes = decorateDatabaseSavedSqlTreeNodes(mergeTableTreePageChildren(withoutTableTreeLoadMoreNodes(store.treeNodes), buildTableTreeNodes({ ...context, tables: childTables(449, 500) }), context.connectionId, context.database), []);+ expect(store.releaseCollapsedTreeNodeChildren(tableId)).toBe(true);+ await store.loadTableGroups(context.connectionId, context.database, "meters", undefined, tableId);+ expect(group().objectCount).toBe(500);+ expect(new Set(group().children!.map((node) => node.id)).size).toBe(500);+ expect(group().children!.some((node) => node.type === "load-more")).toBe(false);+ }+ expect(getColumns).not.toHaveBeenCalled();+ });++ it("preserves the visible authoritative group from a legacy serialized tree on release", async () => {+ const { loadStore } = await setupStoreWithColumnGroup();+ const store = await loadStore(vi.fn());+ store.connections[0].db_type = "tdengine";+ const table = buildTableTreeNodes({ nodeId: "pg-1:app", connectionId: "pg-1", database: "app", tables: [{ name: "meters", table_type: "STABLE" }, ...Array.from({ length: 450 }, (_, index) => ({ name: `meter_${index}`, table_type: "CHILD TABLE", parent_name: "meters" }))] })[0];+ const legacy = JSON.parse(JSON.stringify(table)) as TreeNode;+ legacy.hiddenChildren![0].children = legacy.hiddenChildren![0].children!.slice(0, 200);+ legacy.hiddenChildren![0].objectCount = 200;+ // No new page or table-group load repairs this old snapshot before collapse.+ store.treeNodes = [legacy];+ expect(store.releaseCollapsedTreeNodeChildren(legacy.id)).toBe(true);+ await store.loadTableGroups("pg-1", "app", "meters", undefined, legacy.id);+ expect(store.treeNodes[0].children?.[0].objectCount).toBe(450);+ expect(store.treeNodes[0].children?.[0].children).toHaveLength(450);+ });});diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.tsindex 0f59530c7e..5d7a4d077d 100644--- a/apps/desktop/src/stores/connectionStore.ts+++ b/apps/desktop/src/stores/connectionStore.ts@@ -156,6 +156,7 @@ import {objectGroupRefreshParentId,objectTypesForGroupNode,tablePartitionGroups,+ syncTablePartitionGroups,withoutTableTreeLoadMoreNodes,tablePageRowAnchorKey,type TableTreeLoadMoreParent,@@ -730,6 +731,11 @@ export const useConnectionStore = defineStore("connection", () => {const connectionConnectedAtMs = new Map<string, number>();const connectionErrorRevisions = new Map<string, number>();const tableListSourceRevisions = new Map<string, number>();+ // Only TDengine table groups reuse persisted snapshots without a background+ // refresh. Keep invalidation/write ordering explicit; these maps hold no trees.+ const tdengineTreeCacheRevisions = new Map<string, number>();+ const tdengineTreeCacheSavedRevisions = new Map<string, number>();+ const tdengineTreeCacheWrites = new Map<string, Promise<void>>();const treeNodeLoads = new TreeNodeLoadRegistry();const filteredObjectGroupChildrenIds = new Set<string>();// A remote sidebar search swaps an object group's children for a filtered@@ -2472,7 +2478,12 @@ export const useConnectionStore = defineStore("connection", () => {const objectTreeProfileCacheKey = driverProfileObjectTreeProfileForConnection(config)?.cacheKey;// objects-v9: table nodes now carry canonical tableName metadata for// table-scoped plugin context; older cached nodes lack that identity.- const baseCacheVersion = objectTreeCacheVersion(config, node.database, node.schema, config?.db_type === "oracle" ? "objects-v8" : "objects-v9");+ // Old TDengine snapshots can have already lost full visible partitions and+ // retain only a self-consistent first-page hidden group without a cursor.+ // Neither v3 nor objectCount proves completeness. Only reuse snapshots saved+ // by the reconciled page pipeline; leave other database/object keys unchanged.+ const defaultCacheVersion = config?.db_type === "tdengine" && node.type === "group-tables" ? "objects-tdengine-v1" : config?.db_type === "oracle" ? "objects-v8" : "objects-v9";+ const baseCacheVersion = objectTreeCacheVersion(config, node.database, node.schema, defaultCacheVersion);const cacheVersion = objectTreeProfileCacheKey ? `${baseCacheVersion}:${objectTreeProfileCacheKey}` : baseCacheVersion;return schemaCacheKey(node.connectionId || "", node.database || "", node.schema || "", node.type, cacheVersion);}@@ -2643,6 +2654,11 @@ export const useConnectionStore = defineStore("connection", () => {}function invalidateMetadataCaches(match: MetadataCacheInvalidation): number {+ for (const config of connections.value) {+ if (config.db_type === "tdengine" && (!match.connectionId || match.connectionId === config.id)) {+ tdengineTreeCacheRevisions.set(config.id, (tdengineTreeCacheRevisions.get(config.id) ?? 0) + 1);+ }+ }return metadataListPageCache.invalidate(match) + invalidateTableMetadataCache(match) + invalidateObjectBrowserRowsCache(match);}@@ -3187,7 +3203,7 @@ export const useConnectionStore = defineStore("connection", () => {}));}- async function loadPersistedTreeChildren(node: TreeNode, cacheKey: string, load: TreeNodeLoadHandle): Promise<PersistedTreeChildrenLoadResult> {+ async function loadPersistedTreeChildren(node: TreeNode, cacheKey: string, load: TreeNodeLoadHandle, options?: { freshOnly?: boolean; isCurrent?: () => boolean; acceptChildren?: (children: TreeNode[]) => boolean }): Promise<PersistedTreeChildrenLoadResult> {const trace = createMetadataLoadTrace({kind: "persisted-tree-cache",connectionId: node.connectionId,@@ -3198,7 +3214,7 @@ export const useConnectionStore = defineStore("connection", () => {});const payload = await api.loadSchemaCache<unknown>(cacheKey).catch(() => null);const decoded = decodeSchemaTreeCache<TreeNode[]>(payload);- if (!decoded) {+ if (!decoded || (options?.freshOnly && (decoded.isStale || (payload as { version?: unknown })?.version !== 3)) || options?.isCurrent?.() === false || options?.acceptChildren?.(decoded.children) === false) {logMetadataLoadTrace(metadataTraceLogger, trace, "cache-miss", { cacheStatus: "miss" });return { hit: false, isStale: false };}@@ -3227,10 +3243,71 @@ export const useConnectionStore = defineStore("connection", () => {return { hit: true, isStale: decoded.isStale };}- async function savePersistedTreeChildren(cacheKey: string, children: TreeNode[]) {+ function isTdengineTableGroup(node: TreeNode): boolean {+ return node.type === "group-tables" && !!node.connectionId && getConfig(node.connectionId)?.db_type === "tdengine";+ }++ function tdengineTreeCacheRevision(node: TreeNode): number {+ return node.connectionId ? (tdengineTreeCacheRevisions.get(node.connectionId) ?? 0) : 0;+ }++ function isReusableTdengineTreeSnapshot(parent: TreeNode, children: TreeNode[]): boolean {+ const pending: unknown[] = [...children];+ while (pending.length) {+ const value = pending.pop();+ if (!value || typeof value !== "object") return false;+ const node = value as TreeNode;+ if (typeof node.id !== "string" || typeof node.type !== "string" || typeof node.label !== "string") return false;+ if (node.connectionId !== parent.connectionId || node.database !== parent.database) return false;+ if (node.type === "load-more") {+ const cursor = node.loadMore;+ if (!cursor || cursor.parentId !== parent.id || !Number.isSafeInteger(cursor.offset) || cursor.offset <= 0 || !Number.isSafeInteger(cursor.pageSize) || cursor.pageSize <= 0 || cursor.searchFilter || cursor.searchQuery) return false;+ }+ if (node.type === "group-partitions" && (!Array.isArray(node.children) || node.objectCount !== node.children.filter((child) => child?.type === "table").length)) return false;+ for (const descendants of [node.children, node.hiddenChildren]) {+ if (descendants === undefined) continue;+ if (!Array.isArray(descendants)) return false;+ for (const child of descendants) pending.push(child);+ }+ }+ return true;+ }++ async function savePersistedTreeChildren(cacheKey: string, children: TreeNode[], parent?: TreeNode) {// 分组是显示层投影,绝不写入元数据缓存——否则重启后会随缓存复现。const cacheChildren = stripTableVGroupsFromChildren(children);- await api.saveSchemaCache(cacheKey, encodeSchemaTreeCache(stripTreeNodeExpansionState(stripDatabaseSavedSqlTreeNodes(cacheChildren)))).catch(() => undefined);+ const payload = encodeSchemaTreeCache(stripTreeNodeExpansionState(stripDatabaseSavedSqlTreeNodes(cacheChildren)));+ if (!parent || !isTdengineTableGroup(parent)) {+ await api.saveSchemaCache(cacheKey, payload).catch(() => undefined);+ return;+ }+ const revision = tdengineTreeCacheRevision(parent);+ const isCurrent = () => isTdengineTableGroup(parent) && revision === tdengineTreeCacheRevision(parent);+ const pending = (tdengineTreeCacheWrites.get(cacheKey) ?? Promise.resolve())+ .catch(() => undefined)+ .then(async () => {+ if (!isCurrent()) return;+ // A failed save must not make an older snapshot reusable in this session.+ tdengineTreeCacheSavedRevisions.set(cacheKey, -1);+ try {+ await api.saveSchemaCache(cacheKey, payload);+ if (!isCurrent()) {+ // Invalidation can finish while this backend save is in flight. Delete+ // its late result *before* a newer queued save, never after it.+ await api.deleteSchemaCachePrefix(cacheKey).catch(() => undefined);+ return;+ }+ tdengineTreeCacheSavedRevisions.set(cacheKey, revision);+ } catch {+ // Tree loading remains usable if persistence is unavailable.+ }+ });+ tdengineTreeCacheWrites.set(cacheKey, pending);+ try {+ await pending;+ } finally {+ if (tdengineTreeCacheWrites.get(cacheKey) === pending) tdengineTreeCacheWrites.delete(cacheKey);+ }}function sidebarTableSearchTreeCacheKey(parent: TreeNode): string | null {@@ -3732,6 +3809,8 @@ export const useConnectionStore = defineStore("connection", () => {if (countTreeNodeDescendants(node, RELEASE_COLLAPSED_SUBTREE_MIN_DESCENDANTS) < RELEASE_COLLAPSED_SUBTREE_MIN_DESCENDANTS) {return false;}+ // Also reconcile legacy persisted trees that have not loaded another page.+ syncTablePartitionGroups(node);forgetLoadedChildrenIdsForSubtree(node);node.children = [];return true;@@ -4189,6 +4268,13 @@ export const useConnectionStore = defineStore("connection", () => {for (const id of removedIds) {clearConnectionError(id);connectionErrorRevisions.delete(id);+ tdengineTreeCacheRevisions.delete(id);+ const cachePrefix = `${schemaCacheKey(id)}:`;+ for (const key of tdengineTreeCacheSavedRevisions.keys()) {+ if (key.startsWith(cachePrefix)) tdengineTreeCacheSavedRevisions.delete(key);+ }+ // Pending writes retain their queue until settlement. Their configuration+ // guard rejects the removed connection and retires the queue in finally.clearEtcdAccessCapabilities(id);connectedIds.value.delete(id);clearSidebarStorageCaches(id);@@ -6977,6 +7063,7 @@ export const useConnectionStore = defineStore("connection", () => {}}+ const cacheRevision = tdengineTreeCacheRevision(node);const wantsOnlyTablesOrViews = objectTypes.every((objectType) => objectType === "TABLE" || objectType === "VIEW" || objectType === "MATERIALIZED_VIEW");let children: TreeNode[];let nextObjectCount: number;@@ -7012,6 +7099,7 @@ export const useConnectionStore = defineStore("connection", () => {children = page.hasMore && (!searchFilter || globalPageSearch !== undefined) ? [...page.children, buildLoadMoreNode(node, page.nextOffset, pageSize, page.nextAnchor, globalPageSearch)] : page.children;nextObjectCount = page.objectCount;}+ if (isTdengineTableGroup(node) && cacheRevision !== tdengineTreeCacheRevision(node)) return;if (isTreeLoadSearchChanged(searchFilter, options)) return;if (!tableNameFilterRevisionMatches(options)) return;const targetNode = treeNodeLoadTarget(load);@@ -7025,12 +7113,35 @@ export const useConnectionStore = defineStore("connection", () => {});}targetNode.objectCount = nextObjectCount;+ if (isTdengineTableGroup(targetNode) && options?.force) {+ // setChildren preserves expanded table subtrees. For an authoritative+ // first page, keep their unrelated metadata/expansion, but replace+ // partition membership so old children cannot shadow the new page.+ for (const child of children) {+ if (child.type !== "table") continue;+ const old = findNode(targetNode.children ?? [], child.id);+ if (!old) continue;+ const oldGroups = tablePartitionGroups(old);+ const nextGroups = tablePartitionGroups(child);+ for (const group of oldGroups) {+ forgetLoadedChildrenIdsForSubtree(group);+ treeNodeLoads.invalidatePrefix(group.id);+ }+ for (const group of nextGroups) {+ const previous = oldGroups.find((candidate) => candidate.id === group.id);+ if (previous) group.isExpanded = previous.isExpanded;+ }+ old.children = [...(old.children ?? []).filter((node) => node.type !== "group-partitions"), ...nextGroups];+ old.hiddenChildren = [...(old.hiddenChildren ?? []).filter((node) => node.type !== "group-partitions"), ...nextGroups];+ }+ }setChildren(targetNode, children);if (searchFilter) filteredObjectGroupChildrenIds.add(targetNode.id);else forgetFilteredObjectGroupChildren(targetNode.id);options?.onChildrenApplied?.(targetNode);if (!searchFilter && !isSidebarTableSearch && !tableNameFilter) {- await savePersistedTreeChildren(cacheKey, children);+ await savePersistedTreeChildren(cacheKey, children, targetNode);+ if (isTdengineTableGroup(node) && cacheRevision !== tdengineTreeCacheRevision(node)) return;pruneTableVGroupStaleMembers(targetNode, children, tableVGroupKindOfContainerNode(targetNode) !== null);}const currentTargetNode = treeNodeLoadTarget(load);@@ -7087,7 +7198,7 @@ export const useConnectionStore = defineStore("connection", () => {}}- async function loadMoreObjectGroupChildren(node: TreeNode, options?: { searchFilter?: string }) {+ async function loadMoreObjectGroupChildren(node: TreeNode, options?: { searchFilter?: string; force?: boolean }) {if (node.type !== "load-more" || !node.loadMore) return;const loadMore = node.loadMore;const parent = findNode(treeNodes.value, node.loadMore.parentId);@@ -7134,6 +7245,8 @@ export const useConnectionStore = defineStore("connection", () => {// Parent writes must honor the parent's generation too — load-more begins on the// placeholder node, so a replaced/invalidated parent must reject the merge.const parentEpoch = treeNodeLoads.observe(parent.id);+ const cacheRevision = tdengineTreeCacheRevision(parent);+ const cacheIsCurrent = () => !isTdengineTableGroup(parent) || cacheRevision === tdengineTreeCacheRevision(parent);try {await ensureConnected(parentConnectionId);if (!searchIsCurrent()) return;@@ -7156,11 +7269,11 @@ export const useConnectionStore = defineStore("connection", () => {pageSize,searchFilter,pagedSearch: !!searchFilter,- force: false,+ force: options?.force,}),);const targetParent = treeNodeLoadRelatedTarget(load, parent);- if (!targetParent || !parentEpoch.isCurrent() || !searchIsCurrent()) return;+ if (!targetParent || !parentEpoch.isCurrent() || !searchIsCurrent() || !cacheIsCurrent()) return;const currentChildren = withoutTableTreeLoadMoreNodes(targetParent.children);const mergedChildren = mergeTableTreePageChildren(currentChildren, page.children, parentConnectionId, parentDatabase);const nextChildren = page.hasMore ? appendTableTreeLoadMoreNode(mergedChildren, buildLoadMoreNode(targetParent, page.nextOffset, loadMore.pageSize, page.nextAnchor, loadMore.searchFilter), page.loadMoreParent) : mergedChildren;@@ -7199,11 +7312,11 @@ export const useConnectionStore = defineStore("connection", () => {pageSize,searchFilter,pagedSearch: !!searchFilter,- force: false,+ force: options?.force,}),);const targetParent = treeNodeLoadRelatedTarget(load, parent);- if (!targetParent || !parentEpoch.isCurrent() || !searchIsCurrent()) return;+ if (!targetParent || !parentEpoch.isCurrent() || !searchIsCurrent() || !cacheIsCurrent()) return;const currentChildren = withoutTableTreeLoadMoreNodes(targetParent.children);mergedChildren = mergeTableTreePageChildren(currentChildren, page.children, parentConnectionId, parentDatabase);nextChildren = page.hasMore ? appendTableTreeLoadMoreNode(mergedChildren, buildLoadMoreNode(targetParent, page.nextOffset, loadMore.pageSize, page.nextAnchor, loadMore.searchFilter), page.loadMoreParent) : mergedChildren;@@ -7218,11 +7331,11 @@ export const useConnectionStore = defineStore("connection", () => {offset,pageSize,searchFilter,- force: false,+ force: options?.force,}),);const targetParent = treeNodeLoadRelatedTarget(load, parent);- if (!targetParent || !parentEpoch.isCurrent() || !searchIsCurrent()) return;+ if (!targetParent || !parentEpoch.isCurrent() || !searchIsCurrent() || !cacheIsCurrent()) return;const currentChildren = withoutLoadMoreNodes(targetParent.children);mergedChildren = mergeLocatedTreeChildren(targetParent, currentChildren, page.children, parentConnectionId, parentDatabase);nextChildren = page.hasMore ? [...mergedChildren, buildLoadMoreNode(targetParent, page.nextOffset, loadMore.pageSize, page.nextAnchor, loadMore.searchFilter)] : mergedChildren;@@ -7239,11 +7352,12 @@ export const useConnectionStore = defineStore("connection", () => {return;}const targetParent = treeNodeLoadRelatedTarget(load, parent);- if (!targetParent || !parentEpoch.isCurrent() || !searchIsCurrent()) return;+ if (!targetParent || !parentEpoch.isCurrent() || !searchIsCurrent() || !cacheIsCurrent()) return;targetParent.objectCount = mergedChildren.length;setChildren(targetParent, nextChildren);if (!searchFilter) {- await savePersistedTreeChildren(objectGroupCacheKey(targetParent), nextChildren);+ await savePersistedTreeChildren(objectGroupCacheKey(targetParent), nextChildren, targetParent);+ if (!parentEpoch.isCurrent() || !searchIsCurrent() || !cacheIsCurrent()) return;// 各分组容器按自身类别回收完整列表的失效成员;分页中间态(含// load-more)由 prune 内部再挡一次。pruneTableVGroupStaleMembers(targetParent, nextChildren, tableVGroupKindOfContainerNode(parent) !== null);@@ -7251,13 +7365,14 @@ export const useConnectionStore = defineStore("connection", () => {const currentTargetParent = treeNodeLoadRelatedTarget(load, parent);if (currentTargetParent && parentEpoch.isCurrent()) currentTargetParent.isExpanded = true;} catch (e) {- if (!searchIsCurrent() || !parentEpoch.isCurrent()) return;+ if (!searchIsCurrent() || !parentEpoch.isCurrent() || !cacheIsCurrent()) return;recordMetadataLoadError(parentConnectionId, e, load);throw e;} finally {finishTreeNodeLoad(load);}},+ options,);}@@ -7444,32 +7559,97 @@ export const useConnectionStore = defineStore("connection", () => {if (!objectTypesForGroupNode(parent.type) || parent.tableName) return;const liveParent = treeNodeInSidebarTree(parent);if (!liveParent) return;+ const tdengine = isTdengineTableGroup(liveParent);+ const searchQuery = sidebarSearchQuery.value || "";+ const scopedSearchQuery = sidebarTableSearchQueries.value[liveParent.id]?.trim() || "";+ if (tdengine && scopedSearchQuery) return;+ const searchIsCurrent = () => (sidebarSearchQuery.value || "") === searchQuery && (sidebarTableSearchQueries.value[liveParent.id]?.trim() || "") === scopedSearchQuery;+ let forcePages = false;if (initialLoadOptions?.force || !isTreeNodeChildrenLoaded(liveParent.id) || filteredObjectGroupChildrenIds.has(liveParent.id)) {- let isPreparedPageCurrent: (() => boolean) | undefined;- await loadObjectGroupFirstPage(liveParent, {- ...initialLoadOptions,- force: true,- searchFilter: "",- allowGlobalSearchMismatch: true,- onChildrenApplied: (targetNode) => {- const epoch = treeNodeLoads.observe(targetNode.id);- isPreparedPageCurrent = () => epoch.isCurrent();- },- });- if (!isPreparedPageCurrent?.()) return;+ let cacheHit = false;+ // Do not use the generic first-page hydration path here: it unconditionally+ // starts a background refresh, racing the full/partial snapshot's drain.+ const canReuseCache =+ tdengine &&+ shouldAutoLoadAllTableGroupChildren(liveParent, initialLoadOptions) &&+ !initialLoadOptions?.force &&+ !filteredObjectGroupChildrenIds.has(liveParent.id) &&+ connectedIds.value.has(parent.connectionId) &&+ !scopedSearchQuery &&+ !activeTableNameFilterForScope({+ connectionId: liveParent.connectionId,+ database: liveParent.database,+ schema: liveParent.schema,+ nodeKind: liveParent.type,+ catalog: liveParent.catalog,+ });+ if (canReuseCache) {+ const cacheKey = objectGroupCacheKey(liveParent);+ const revision = tdengineTreeCacheRevision(liveParent);+ const configFingerprint = connectionConfigFingerprint(getConfig(parent.connectionId)!);+ const cacheLoad = beginTreeNodeLoad(liveParent);+ const isCurrent = () => {+ const config = getConfig(parent.connectionId!);+ return (+ !!config &&+ connectionConfigFingerprint(config) === configFingerprint &&+ shouldAutoLoadAllTableGroupChildren(liveParent, initialLoadOptions) &&+ searchIsCurrent() &&+ revision === tdengineTreeCacheRevision(liveParent) &&+ tableNameFilterRevisionMatches(initialLoadOptions) &&+ !activeTableNameFilterForScope({ connectionId: liveParent.connectionId, database: liveParent.database, schema: liveParent.schema, nodeKind: liveParent.type, catalog: liveParent.catalog })+ );+ };+ try {+ await tdengineTreeCacheWrites.get(cacheKey);+ if (!treeNodeLoadTarget(cacheLoad) || !isCurrent()) return;+ if ((tdengineTreeCacheSavedRevisions.get(cacheKey) ?? 0) === revision) {+ cacheHit = (await loadPersistedTreeChildren(liveParent, cacheKey, cacheLoad, { freshOnly: true, isCurrent, acceptChildren: (children) => isReusableTdengineTreeSnapshot(liveParent, children) })).hit;+ }+ // A cancelled/superseded cache miss must not fall through and restart+ // page zero, or re-expand a node the user collapsed while awaiting I/O.+ if (!treeNodeLoadTarget(cacheLoad) || !isCurrent()) return;+ if (cacheHit) forgetFilteredObjectGroupChildren(liveParent.id);+ } finally {+ finishTreeNodeLoad(cacheLoad);+ }+ }+ if (!cacheHit) {+ if (tdengine) {+ // A controlled reload must not mix a fresh first page with old cached+ // later pages, nor allow a previously-started save to revive old data.+ invalidateMetadataCaches({ connectionId: parent.connectionId, database: parent.database });+ forcePages = true;+ }+ let isPreparedPageCurrent: (() => boolean) | undefined;+ await loadObjectGroupFirstPage(liveParent, {+ ...initialLoadOptions,+ force: true,+ searchFilter: "",+ expectedSidebarSearchQuery: tdengine ? searchQuery : initialLoadOptions?.expectedSidebarSearchQuery,+ allowGlobalSearchMismatch: true,+ onChildrenApplied: (targetNode) => {+ const epoch = treeNodeLoads.observe(targetNode.id);+ const revision = tdengineTreeCacheRevision(targetNode);+ isPreparedPageCurrent = () => epoch.isCurrent() && (!tdengine || (searchIsCurrent() && revision === tdengineTreeCacheRevision(targetNode)));+ },+ });+ if (!isPreparedPageCurrent?.()) return;+ }}const refreshedParent = treeNodeInSidebarTree(parent);if (!refreshedParent) return;const load = beginTreeNodeLoad(refreshedParent);+ const cacheRevision = tdengineTreeCacheRevision(refreshedParent);try {const liveParent = treeNodeLoadTarget(load);if (!liveParent) return;let loadMoreNode = findTreeNodes(liveParent.children ?? [], (child) => child.type === "load-more")[0];while (loadMoreNode?.loadMore) {- await loadMoreObjectGroupChildren(loadMoreNode, { searchFilter: "" });- if (!load.isCurrent()) return;+ await loadMoreObjectGroupChildren(loadMoreNode, { searchFilter: "", force: forcePages });+ if (!load.isCurrent() || (tdengine && (!searchIsCurrent() || cacheRevision !== tdengineTreeCacheRevision(refreshedParent)))) return;const currentParent = treeNodeLoadTarget(load);if (!currentParent) return;loadMoreNode = findTreeNodes(currentParent.children ?? [], (child) => child.type === "load-more")[0];diff --git a/packages/app-tests/sidebarLocalTableSearchComment.test.ts b/packages/app-tests/sidebarLocalTableSearchComment.test.tsindex 5c1a854e2f..55b0f26d42 100644--- a/packages/app-tests/sidebarLocalTableSearchComment.test.ts+++ b/packages/app-tests/sidebarLocalTableSearchComment.test.ts@@ -8,6 +8,8 @@// match name OR comment across all three branches: the indexed branch, the// indexed === null fallback (index not built yet), and the no-index branch.import { describe, expect, it } from "vitest";+import { computed, reactive, ref } from "vue";+import { flattenTree } from "../../apps/desktop/src/composables/useFlatTree.ts";import { filterLocallySearchedTables } from "../../apps/desktop/src/lib/sidebar/sidebarSearchTree.ts";import type { TableInfo, TreeNode } from "@/types/database";@@ -58,3 +60,113 @@ describe("sidebar local table search matches table comments (#9094)", () => {}});});++describe("sidebar local table search no-op projection (#12280)", () => {+ const inactiveCases: Array<{ name: string; enabled: boolean; queries: Record<string, string> }> = [+ { name: "disabled with a retained query", enabled: false, queries: { "conn:db": "order" } },+ { name: "enabled with no queries", enabled: true, queries: {} },+ { name: "enabled with only blank queries", enabled: true, queries: { "conn:db": " \t\n", other: "" } },+ ];++ for (const { name, enabled, queries } of inactiveCases) {+ it(`preserves the input array and node identities when ${name}`, () => {+ const children = liveChildren();+ const parent = schemaNode(children);+ const nodes = [parent];+ const result = filterLocallySearchedTables(nodes, { enabled, queries, indexedResults: {} });+ expect(result).toBe(nodes);+ expect(result[0]).toBe(parent);+ expect(result[0].children).toBe(children);+ });++ it(`does not read descendants when ${name}`, () => {+ const parent = schemaNode([]);+ let childrenReads = 0;+ Object.defineProperty(parent, "children", {+ get() {+ childrenReads++;+ return [];+ },+ });+ filterLocallySearchedTables([parent], { enabled, queries, indexedResults: {} });+ expect(childrenReads).toBe(0);+ });+ }++ it("scans the query map only once for an active nested search", () => {+ let scans = 0;+ const queries = new Proxy(+ { "conn:db": "order" },+ {+ ownKeys(target) {+ scans++;+ return Reflect.ownKeys(target);+ },+ },+ );+ const nodes: TreeNode[] = [{ id: "conn", label: "Connection", type: "connection", children: [schemaNode(liveChildren())] }];+ const result = filterLocallySearchedTables(nodes, { enabled: true, queries, indexedResults: {} });+ expect(result[0].children![0].children!.map((node) => node.label)).toEqual(["t_order"]);+ expect(scans).toBe(1);+ });++ it.each(["missing", "null", "indexed"] as const)("keeps nested camelCase searches and live node state with %s results", (mode) => {+ const entries: TableInfo[] = [+ { name: "OrderLine", table_type: "TABLE", comment: null },+ { name: "customer", table_type: "TABLE", comment: null },+ ];+ const children: TreeNode[] = entries.map((entry) => ({ id: `conn:db:${entry.name}`, label: entry.name, type: "table", connectionId: "conn", database: "db" }));+ children[0].isExpanded = true;+ children[0].children = [{ id: "columns", label: "Columns", type: "group-columns" }];+ const parent = schemaNode(children);+ const nodes: TreeNode[] = [{ id: "conn", label: "Connection", type: "connection", children: [parent] }];+ const indexedResults = mode === "missing" ? {} : { [parent.id]: mode === "null" ? null : entries };+ const result = filterLocallySearchedTables(nodes, { enabled: true, queries: { [parent.id]: "OL" }, indexedResults });+ const matched = result[0].children![0].children!;+ expect(matched.map((node) => node.label)).toEqual(["OrderLine"]);+ expect(matched[0].isExpanded).toBe(true);+ expect(matched[0].children).toEqual(children[0].children);+ expect(matched[0].children![0]).toBe(children[0].children[0]);+ expect(nodes[0].children![0]).toBe(parent);+ expect(parent.children).toBe(children);+ expect(children).toHaveLength(2);+ });++ it("keeps downstream flattening reactive with the same root array", () => {+ const nodes = reactive<TreeNode[]>([{ ...schemaNode(liveChildren()), isExpanded: true }]);+ const projected = computed(() => filterLocallySearchedTables(nodes, { enabled: true, queries: {}, indexedResults: {} }));+ const labels = computed(() => flattenTree(projected.value).map(({ node }) => node.label));+ expect(projected.value).toBe(nodes);+ expect(labels.value).toEqual(["db", "t_order", "t_customer"]);+ nodes[0].children = [{ id: "replacement", label: "replacement", type: "table" }];+ expect(labels.value).toEqual(["db", "replacement"]);+ nodes[0].isExpanded = false;+ expect(labels.value).toEqual(["db"]);+ nodes[0].isExpanded = true;+ nodes[0].children.push({ id: "appended", label: "appended", type: "table" });+ expect(labels.value).toEqual(["db", "replacement", "appended"]);+ expect(projected.value).toBe(nodes);+ });++ it("tracks added, cleared and deleted queries and enabling local search", () => {+ const nodes = reactive<TreeNode[]>([schemaNode(liveChildren())]);+ const queries = reactive<Record<string, string>>({});+ const enabled = ref(true);+ const projected = computed(() => filterLocallySearchedTables(nodes, { enabled: enabled.value, queries, indexedResults: {} }));+ const labels = computed(() => projected.value[0].children!.map((node) => node.label));+ expect(labels.value).toEqual(["t_order", "t_customer"]);+ queries["conn:db"] = "订单";+ expect(labels.value).toEqual(["t_order"]);+ queries["conn:db"] = " ";+ expect(labels.value).toEqual(["t_order", "t_customer"]);+ queries["conn:db"] = "customer";+ expect(labels.value).toEqual(["t_customer"]);+ delete queries["conn:db"];+ expect(labels.value).toEqual(["t_order", "t_customer"]);+ enabled.value = false;+ queries["conn:db"] = "订单";+ expect(labels.value).toEqual(["t_order", "t_customer"]);+ enabled.value = true;+ expect(labels.value).toEqual(["t_order"]);+ });+});