fix(sidebar): batch persisted cache deletes for discarded tree nodes

Closes #9779
This commit is contained in:
t8y2 committed 2026-09-21 01:44:23 +08:00
1 parent 216afec5e4
commit 34e9ffd040
2 files changed
+182 -4

No files matched your search

@@ -3360,6 +3360,138 @@ describe("connectionStore metadata loading", () => {
expect(store.restoreFilteredObjectGroupChildren(untouchedGroup)).toBe(false);
});
async function installAggregatedCacheDeleteStore(options?: { initialChildren?: TreeNode[] }) {
const tables = Array.from({ length: 5 }, (_, index) => ({
name: `t_${String(index + 1).padStart(4, "0")}`,
table_type: "TABLE" as const,
comment: null,
}));
const listTables = vi.fn(() => Promise.resolve(tables));
const deleteSchemaCachePrefix = vi.fn().mockResolvedValue(undefined);
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix,
listTables,
loadSchemaCache: vi.fn().mockResolvedValue(null),
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSchemaCache: 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 settingsStore = useSettingsStore();
settingsStore.editorSettings.sidebarObjectDisplay = "grouped";
const connection = mysqlConnection();
const tablesGroup: TreeNode = {
id: "mysql-1:app:__tables",
label: "tree.tables",
type: "group-tables",
connectionId: connection.id,
database: "app",
isExpanded: true,
children: options?.initialChildren ?? [],
};
store.connections = [connection];
store.connectedIds.add(connection.id);
store.treeNodes = [
{
id: connection.id,
label: connection.name,
type: "connection",
connectionId: connection.id,
isExpanded: true,
children: [
{
id: "mysql-1:app",
label: "app",
type: "database",
connectionId: connection.id,
database: "app",
isExpanded: true,
children: [tablesGroup],
},
],
},
];
return { store, tablesGroup, listTables, deleteSchemaCachePrefix, tables };
}
it("aggregates persisted cache deletes for discarded sibling nodes into one ancestor prefix", async () => {
const { store, tablesGroup, listTables, deleteSchemaCachePrefix, tables } = await installAggregatedCacheDeleteStore();
await store.loadObjectGroupChildren(tablesGroup, { force: true });
expect(tablesGroup.children?.map((child) => child.label)).toEqual(tables.map((table) => table.name));
// Nothing was discarded yet, so no persisted cache delete may fire.
expect(deleteSchemaCachePrefix).not.toHaveBeenCalled();
// The refreshed list drops four of the five tables: their per-node cache
// deletes must collapse into a single ancestor-prefix request pair.
listTables.mockImplementation(() => Promise.resolve(tables.slice(4)));
await store.loadObjectGroupChildren(tablesGroup, { force: true });
expect(tablesGroup.children?.map((child) => child.label)).toEqual(["t_0005"]);
expect(deleteSchemaCachePrefix.mock.calls.map((call) => call[0]).sort()).toEqual(["mysql-1%3Aapp%3A__tables:", "mysql-1:app:__tables:"]);
});
it("keeps per-node persisted deletes for discarded ids outside the ancestor prefix", async () => {
const strayNode: TreeNode = {
id: "foreign:id",
label: "legacy",
type: "table",
connectionId: "mysql-1",
database: "app",
isExpanded: false,
children: [],
};
const { store, tablesGroup, deleteSchemaCachePrefix, tables } = await installAggregatedCacheDeleteStore({ initialChildren: [strayNode] });
await store.loadObjectGroupChildren(tablesGroup, { force: true });
expect(tablesGroup.children?.map((child) => child.label)).toEqual(tables.map((table) => table.name));
// The stray child does not live under the group's id prefix, so it cannot be
// covered by the ancestor delete and must keep its own request pair.
expect(deleteSchemaCachePrefix.mock.calls.map((call) => call[0]).sort()).toEqual(["foreign%3Aid:", "foreign:id:"]);
});
it("shares one persisted delete between nested discarded prefixes", async () => {
const strayParent: TreeNode = {
id: "x:a",
label: "outer",
type: "table",
connectionId: "mysql-1",
database: "app",
isExpanded: false,
children: [],
};
const strayChild: TreeNode = {
id: "x:a:b",
label: "inner",
type: "table",
connectionId: "mysql-1",
database: "app",
isExpanded: false,
children: [],
};
const { store, tablesGroup, deleteSchemaCachePrefix } = await installAggregatedCacheDeleteStore({ initialChildren: [strayParent, strayChild] });
await store.loadObjectGroupChildren(tablesGroup, { force: true });
// "x:a:b" is covered by the "x:a" prefix, so only the covering pair fires.
expect(deleteSchemaCachePrefix.mock.calls.map((call) => call[0]).sort()).toEqual(["x%3Aa:", "x:a:"]);
expect(deleteSchemaCachePrefix).toHaveBeenCalledTimes(2);
});
it("issues no persisted cache delete when the refreshed list keeps every child", async () => {
const { store, tablesGroup, deleteSchemaCachePrefix, tables } = await installAggregatedCacheDeleteStore();
await store.loadObjectGroupChildren(tablesGroup, { force: true });
await store.loadObjectGroupChildren(tablesGroup, { force: true });
expect(tablesGroup.children?.map((child) => child.label)).toEqual(tables.map((table) => table.name));
expect(deleteSchemaCachePrefix).not.toHaveBeenCalled();
});
it("pages table-scoped search results instead of truncating at the search budget", async () => {
const tables = Array.from({ length: 5 }, (_, index) => ({
name: `t_${String(index + 1).padStart(4, "0")}`,