fix(sidebar): re-read displayed range when paging window drifts

This commit is contained in:
zipg authored and GitHub committed 2026-09-25 16:00:09 +08:00
1 parent 577154cf5a
commit 94a8fd6790
5 files changed
+352 -63

No files matched your search

@@ -3163,10 +3163,10 @@ describe("connectionStore metadata loading", () => {
await store.loadObjectGroupChildren(tablesGroup, { force: true });
expect(tablesGroup.children?.some((child) => child.label?.startsWith("fresh_"))).toBe(true);
resolveSecondPage([{ name: "t_0202", table_type: "TABLE", comment: null }]);
resolveSecondPage([{ name: "t_0201", table_type: "TABLE", comment: null }]);
await loadMorePromise;
expect(tablesGroup.children?.some((child) => child.label === "t_0202")).toBe(false);
expect(tablesGroup.children?.some((child) => child.label === "t_0201")).toBe(false);
expect(tablesGroup.children?.some((child) => child.label?.startsWith("fresh_"))).toBe(true);
});
@@ -3800,12 +3800,12 @@ describe("connectionStore metadata loading", () => {
};
connectionNode.children![0].children = [liveTablesGroup];
resolveSecondPage([{ name: "t_0202", table_type: "TABLE", comment: null }]);
resolveSecondPage([{ name: "t_0201", table_type: "TABLE", comment: null }]);
await loadMorePromise;
expect(staleTablesGroup.children?.some((child) => child.label === "t_0202")).toBe(false);
expect(staleTablesGroup.children?.some((child) => child.label === "t_0201")).toBe(false);
expect(liveTablesGroup.children?.some((child) => child.id === concurrentTable.id)).toBe(true);
expect(liveTablesGroup.children?.some((child) => child.label === "t_0202")).toBe(true);
expect(liveTablesGroup.children?.some((child) => child.label === "t_0201")).toBe(true);
});
it("keeps table column loaded markers when a tables group appends via load-more", async () => {
@@ -3817,7 +3817,7 @@ describe("connectionStore metadata loading", () => {
const listTables = vi
.fn()
.mockResolvedValueOnce(firstPage)
.mockResolvedValueOnce([{ name: "t_0202", table_type: "TABLE", comment: null }]);
.mockResolvedValueOnce([{ name: "t_0201", table_type: "TABLE", comment: null }]);
const getColumns = vi.fn().mockResolvedValue([{ name: "id", data_type: "INT", is_nullable: false, column_default: null, is_primary_key: true, extra: null, comment: null }]);
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
@@ -3889,6 +3889,192 @@ describe("connectionStore metadata loading", () => {
expect(getColumns).not.toHaveBeenCalled();
});
it("re-reads the displayed table window when the next page no longer opens with the anchored row", async () => {
const firstPage = Array.from({ length: 201 }, (_, index) => ({
name: `t_${String(index + 1).padStart(4, "0")}`,
table_type: "TABLE" as const,
comment: null,
}));
// Between the two page loads a table that sorts to the very front was created, so every
// later offset now points one row earlier: offset 200 no longer opens with the first
// page's peek row t_0201 but with t_0199. Appending that page would leave the freshly
// created t_0000b (and the row the window slid past) out of the tree for good (#9400).
const afterCreate = [
{ name: "t_0000b", table_type: "TABLE" as const, comment: null },
...firstPage,
...Array.from({ length: 200 }, (_, index) => ({
name: `t_${String(index + 202).padStart(4, "0")}`,
table_type: "TABLE" as const,
comment: null,
})),
];
const listTables = vi
.fn()
.mockResolvedValueOnce(firstPage)
.mockImplementation((_connectionId: string, _database: string, _schema: string, _searchFilter?: string, limit?: number, offset?: number) => Promise.resolve(afterCreate.slice(offset ?? 0, (offset ?? 0) + (limit ?? afterCreate.length))));
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
listTables,
loadSchemaCache: vi.fn().mockResolvedValue(null),
saveSchemaCache: vi.fn().mockResolvedValue(undefined),
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 settingsStore = useSettingsStore();
settingsStore.editorSettings.sidebarObjectDisplay = "grouped";
settingsStore.desktopSettings.sidebar_table_page_size = 200;
const connection = mysqlConnection();
const tablesGroup: TreeNode = {
id: "mysql-1:app:__tables",
label: "tree.tables",
type: "group-tables",
connectionId: connection.id,
database: "app",
isExpanded: false,
children: [],
};
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],
},
],
},
];
await store.loadObjectGroupChildren(tablesGroup);
const loadMoreNode = tablesGroup.children?.at(-1);
expect(loadMoreNode?.type).toBe("load-more");
expect(loadMoreNode?.loadMore?.offset).toBe(200);
expect(listTables.mock.calls[0].slice(3, 6)).toEqual([undefined, 201, 0]);
await store.loadMoreObjectGroupChildren(loadMoreNode!);
// The drifted page is replaced by one re-read of the whole displayed range (offset 0,
// pageSize 200 + the 200 rows already shown), so nothing is skipped or repeated.
expect(listTables).toHaveBeenCalledTimes(3);
expect(listTables.mock.calls[1].slice(3, 6)).toEqual([undefined, 201, 200]);
expect(listTables.mock.calls[2].slice(3, 6)).toEqual([undefined, 401, 0]);
const labels = (tablesGroup.children ?? []).filter((child) => child.type === "table").map((child) => child.label);
expect(labels).toContain("t_0000b");
expect(labels).toContain("t_0201");
expect(labels.filter((label) => label === "t_0199")).toHaveLength(1);
expect(labels.filter((label) => label === "t_0200")).toHaveLength(1);
const nextLoadMore = tablesGroup.children?.at(-1);
expect(nextLoadMore?.type).toBe("load-more");
expect(nextLoadMore?.loadMore?.offset).toBe(400);
expect(nextLoadMore?.loadMore?.anchor).toBeDefined();
});
it("re-reads the displayed window when the next page comes back empty after rows were dropped", async () => {
const firstPage = Array.from({ length: 201 }, (_, index) => ({
name: `t_${String(index + 1).padStart(4, "0")}`,
table_type: "TABLE" as const,
comment: null,
}));
// The three leading tables were dropped between the two page loads, so offset 200 now
// points past the end of the list. Keeping the empty page would leave the deleted rows
// on screen and the rows the window slid onto (t_0201) out of the tree forever (#9400).
const afterDrop = firstPage.slice(3);
const listTables = vi
.fn()
.mockResolvedValueOnce(firstPage)
.mockImplementation((_connectionId: string, _database: string, _schema: string, _searchFilter?: string, limit?: number, offset?: number) => Promise.resolve(afterDrop.slice(offset ?? 0, (offset ?? 0) + (limit ?? afterDrop.length))));
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
listTables,
loadSchemaCache: vi.fn().mockResolvedValue(null),
saveSchemaCache: vi.fn().mockResolvedValue(undefined),
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 settingsStore = useSettingsStore();
settingsStore.editorSettings.sidebarObjectDisplay = "grouped";
settingsStore.desktopSettings.sidebar_table_page_size = 200;
const connection = mysqlConnection();
const tablesGroup: TreeNode = {
id: "mysql-1:app:__tables",
label: "tree.tables",
type: "group-tables",
connectionId: connection.id,
database: "app",
isExpanded: false,
children: [],
};
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],
},
],
},
];
await store.loadObjectGroupChildren(tablesGroup);
const loadMoreNode = tablesGroup.children?.at(-1);
expect(loadMoreNode?.type).toBe("load-more");
expect(loadMoreNode?.loadMore?.offset).toBe(200);
await store.loadMoreObjectGroupChildren(loadMoreNode!);
expect(listTables.mock.calls[1].slice(3, 6)).toEqual([undefined, 201, 200]);
expect(listTables.mock.calls[2].slice(3, 6)).toEqual([undefined, 401, 0]);
// The window slides onto t_0201 instead of staying on the pre-drop window, and the
// list is now known to be complete, so the dead load-more node goes away. Rows the
// server no longer returns are left to the sidebar's own staleness prunes.
const labels = (tablesGroup.children ?? []).filter((child) => child.type === "table").map((child) => child.label);
expect(labels).toContain("t_0201");
expect(labels.filter((label) => label === "t_0201")).toHaveLength(1);
expect(tablesGroup.children?.some((child) => child.type === "load-more")).toBe(false);
});
it("applies SQL Server database object loads to the current tree node after an in-tree replacement", async () => {
let resolveSchemas!: (schemas: string[]) => void;
const listSchemas = vi.fn(
+117 -56
View File
@@ -151,6 +151,7 @@ import {
objectTypesForGroupNode,
tablePartitionGroups,
withoutTableTreeLoadMoreNodes,
tablePageRowAnchorKey,
type TableTreeLoadMoreParent,
type DatabaseObjectTreeKind,
} from "@/lib/table/tableTree";
@@ -2554,7 +2555,67 @@ export const useConnectionStore = defineStore("connection", () => {
void invalidateObjectDdlCache(match);
}
function buildLoadMoreNode(parent: TreeNode, offset: number, pageSize: number): TreeNode {
type PagedTableWindow = {
children: TreeNode[];
objectCount: number;
hasMore: boolean;
nextOffset: number;
loadMoreParent?: TableTreeLoadMoreParent;
/** Folded identity of the row that opened this page. */
firstAnchor?: string;
/** Folded identity of the page's peek row, i.e. the row the next page must open with. */
nextAnchor?: string;
};
/** Structural view shared by `TableInfo` and `ObjectInfo` rows of a paged list. */
type PagedRowIdentity = {
name: string;
schema?: string | null;
parent_schema?: string | null;
parent_name?: string | null;
};
function tablePageAnchorKeyOf(row: PagedRowIdentity, fallbackSchema?: string) {
// Partitioned tables need the parent in the key: their child tables may share a name.
return tablePageRowAnchorKey(row.name, row.parent_schema ?? row.schema ?? fallbackSchema, row.parent_name);
}
/**
* Anchors for one fetched page. Paged loads ask for `pageSize + 1` rows and keep only
* `pageSize` of them, so the extra probe row is exactly the row the next page has to
* open with. Remembering it is what makes offset paging able to notice that the
* ordered window moved (objects created or dropped above it) instead of silently
* skipping rows (#9400).
*/
function tablePageAnchors(rows: readonly PagedRowIdentity[], pageSize: number, hasMore: boolean, fallbackSchema?: string) {
const first = rows[0];
const peek = hasMore ? rows[pageSize] : undefined;
return {
firstAnchor: first ? tablePageAnchorKeyOf(first, fallbackSchema) : undefined,
nextAnchor: peek ? tablePageAnchorKeyOf(peek, fallbackSchema) : undefined,
};
}
/**
* Offset paging is not snapshot consistent: when the object set changes above the
* window between two page loads, the same offset points at a different row, so the
* next page would be appended with a gap (or an overlap) in it (#9400). A page that
* does not open with the anchored row — including one that comes back empty because
* rows above the window were dropped — re-reads the whole displayed range instead:
* offset 0 always starts at the same row, so one request lands the tree exactly on
* the current server state, and the widened window still reports the correct
* `nextOffset` / `hasMore` for continuing.
*/
async function loadTablePageCheckingAnchor<T extends PagedTableWindow>(anchor: string | undefined, offset: number, pageSize: number, fetchPage: (offset: number, pageSize: number) => Promise<T>): Promise<T> {
const page = await fetchPage(offset, pageSize);
if (!anchor || offset <= 0) return page;
// An empty page at a non-zero offset is drift too: the objects above the window
// were removed, so the offset now points past the end of the list.
if (page.firstAnchor === anchor) return page;
return fetchPage(0, offset + pageSize);
}
function buildLoadMoreNode(parent: TreeNode, offset: number, pageSize: number, anchor?: string): TreeNode {
return {
id: `${parent.id}:__load_more:${offset}`,
label: "tree.loadMore",
@@ -2567,6 +2628,7 @@ export const useConnectionStore = defineStore("connection", () => {
parentId: parent.id,
offset,
pageSize,
...(anchor ? { anchor } : {}),
},
};
}
@@ -2668,7 +2730,7 @@ export const useConnectionStore = defineStore("connection", () => {
searchFilter?: string;
pagedSearch?: boolean;
force?: boolean;
}): Promise<{ children: TreeNode[]; objectCount: number; hasMore: boolean; nextOffset: number; loadMoreParent?: TableTreeLoadMoreParent }> {
}): Promise<PagedTableWindow> {
if (!options.node.connectionId || options.node.database == null) {
return { children: [], objectCount: 0, hasMore: false, nextOffset: options.offset };
}
@@ -2725,20 +2787,11 @@ export const useConnectionStore = defineStore("connection", () => {
hasMore,
nextOffset: options.offset + pageTables.length,
loadMoreParent: lastTable?.parent_name ? { schema: lastTable.parent_schema, name: lastTable.parent_name } : undefined,
...tablePageAnchors(tables, options.pageSize, hasMore, options.effectiveSchema),
};
}
async function loadPagedObjectGroupChildren(options: {
node: TreeNode;
parentNodeId: string;
querySchema: string;
effectiveSchema?: string;
objectTypes: DatabaseObjectTreeKind[];
offset: number;
pageSize: number;
searchFilter?: string;
force?: boolean;
}): Promise<{ children: TreeNode[]; objectCount: number; hasMore: boolean; nextOffset: number }> {
async function loadPagedObjectGroupChildren(options: { node: TreeNode; parentNodeId: string; querySchema: string; effectiveSchema?: string; objectTypes: DatabaseObjectTreeKind[]; offset: number; pageSize: number; searchFilter?: string; force?: boolean }): Promise<PagedTableWindow> {
if (!options.node.connectionId || options.node.database == null) {
return { children: [], objectCount: 0, hasMore: false, nextOffset: options.offset };
}
@@ -2787,6 +2840,7 @@ export const useConnectionStore = defineStore("connection", () => {
objectCount: children.length,
hasMore,
nextOffset: options.offset + pageObjects.length,
...tablePageAnchors(objects, options.pageSize, hasMore, options.effectiveSchema),
};
}
@@ -2802,7 +2856,7 @@ export const useConnectionStore = defineStore("connection", () => {
searchFilter?: string;
pagedSearch?: boolean;
force?: boolean;
}): Promise<{ children: TreeNode[]; objectCount: number; hasMore: boolean; nextOffset: number; loadMoreParent?: TableTreeLoadMoreParent }> {
}): Promise<PagedTableWindow> {
const searchFilter = (options.searchFilter ?? sidebarSearchQuery.value) || undefined;
const tableNameFilter = activeTableNameFilterForScope({
connectionId: options.connectionId,
@@ -2850,6 +2904,7 @@ export const useConnectionStore = defineStore("connection", () => {
hasMore,
nextOffset: options.offset + pageTables.length,
loadMoreParent: lastTable?.parent_name ? { schema: lastTable.parent_schema, name: lastTable.parent_name } : undefined,
...tablePageAnchors(tables, options.pageSize, hasMore, options.effectiveSchema),
};
}
@@ -6328,7 +6383,7 @@ export const useConnectionStore = defineStore("connection", () => {
pagedSearch: isSidebarTableSearch,
force: options?.force,
});
children = page.hasMore && (!searchFilter || isSidebarTableSearch) ? appendTableTreeLoadMoreNode(page.children, buildLoadMoreNode(node, page.nextOffset, pageSize), page.loadMoreParent) : page.children;
children = page.hasMore && (!searchFilter || isSidebarTableSearch) ? appendTableTreeLoadMoreNode(page.children, buildLoadMoreNode(node, page.nextOffset, pageSize, page.nextAnchor), page.loadMoreParent) : page.children;
nextObjectCount = page.objectCount;
} else if (simpleObjectDisplay) {
// The synthetic public scope contains no tables. Avoid issuing a
@@ -6490,7 +6545,7 @@ export const useConnectionStore = defineStore("connection", () => {
pagedSearch: isSidebarTableSearch,
force: options?.force,
});
children = page.hasMore && (!searchFilter || isSidebarTableSearch) ? appendTableTreeLoadMoreNode(page.children, buildLoadMoreNode(node, page.nextOffset, sidebarObjectGroupPageSize()), page.loadMoreParent) : page.children;
children = page.hasMore && (!searchFilter || isSidebarTableSearch) ? appendTableTreeLoadMoreNode(page.children, buildLoadMoreNode(node, page.nextOffset, sidebarObjectGroupPageSize(), page.nextAnchor), page.loadMoreParent) : page.children;
nextObjectCount = page.objectCount;
} else {
const pageSize = sidebarObjectGroupPageSize();
@@ -6505,7 +6560,7 @@ export const useConnectionStore = defineStore("connection", () => {
searchFilter: searchFilter || undefined,
force: options?.force,
});
children = page.hasMore && !searchFilter ? [...page.children, buildLoadMoreNode(node, page.nextOffset, pageSize)] : page.children;
children = page.hasMore && !searchFilter ? [...page.children, buildLoadMoreNode(node, page.nextOffset, pageSize, page.nextAnchor)] : page.children;
nextObjectCount = page.objectCount;
}
if (isTreeLoadSearchChanged(searchFilter, options)) return;
@@ -6614,24 +6669,26 @@ export const useConnectionStore = defineStore("connection", () => {
const config = getConfig(parentConnectionId);
const querySchema = connectionObjectTreeQuerySchema(config, parentDatabase, parent.schema);
const effectiveSchema = connectionObjectTreeNodeSchema(config, parentDatabase, parent.schema);
const page = await loadPagedSimpleTableChildren({
nodeId: parent.schema ? `${parentConnectionId}:${parentDatabase}:${parent.schema}` : `${parentConnectionId}:${parentDatabase}`,
connectionId: parentConnectionId,
database: parentDatabase,
querySchema,
effectiveSchema,
nonTableObjectTypes: [],
offset: loadMore.offset,
pageSize: loadMore.pageSize,
searchFilter: options?.searchFilter,
pagedSearch: !!options?.searchFilter,
force: false,
});
const page = await loadTablePageCheckingAnchor(loadMore.anchor, loadMore.offset, loadMore.pageSize, (offset, pageSize) =>
loadPagedSimpleTableChildren({
nodeId: parent.schema ? `${parentConnectionId}:${parentDatabase}:${parent.schema}` : `${parentConnectionId}:${parentDatabase}`,
connectionId: parentConnectionId,
database: parentDatabase,
querySchema,
effectiveSchema,
nonTableObjectTypes: [],
offset,
pageSize,
searchFilter: options?.searchFilter,
pagedSearch: !!options?.searchFilter,
force: false,
}),
);
const targetParent = treeNodeLoadRelatedTarget(load, parent);
if (!targetParent || !parentEpoch.isCurrent()) 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.loadMoreParent) : mergedChildren;
const nextChildren = page.hasMore ? appendTableTreeLoadMoreNode(mergedChildren, buildLoadMoreNode(targetParent, page.nextOffset, loadMore.pageSize, page.nextAnchor), page.loadMoreParent) : mergedChildren;
targetParent.objectCount = mergedChildren.length;
setChildren(targetParent, nextChildren);
if (!options?.searchFilter) {
@@ -6656,40 +6713,44 @@ export const useConnectionStore = defineStore("connection", () => {
let mergedChildren: TreeNode[];
let nextChildren: TreeNode[];
if (wantsOnlyTablesOrViews) {
const page = await loadPagedTableGroupChildren({
node: parent,
parentNodeId,
querySchema,
effectiveSchema,
objectTypes,
offset: loadMore.offset,
pageSize: loadMore.pageSize,
searchFilter: options?.searchFilter,
pagedSearch: !!options?.searchFilter,
force: false,
});
const page = await loadTablePageCheckingAnchor(loadMore.anchor, loadMore.offset, loadMore.pageSize, (offset, pageSize) =>
loadPagedTableGroupChildren({
node: parent,
parentNodeId,
querySchema,
effectiveSchema,
objectTypes,
offset,
pageSize,
searchFilter: options?.searchFilter,
pagedSearch: !!options?.searchFilter,
force: false,
}),
);
const targetParent = treeNodeLoadRelatedTarget(load, parent);
if (!targetParent || !parentEpoch.isCurrent()) 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.loadMoreParent) : mergedChildren;
nextChildren = page.hasMore ? appendTableTreeLoadMoreNode(mergedChildren, buildLoadMoreNode(targetParent, page.nextOffset, loadMore.pageSize, page.nextAnchor), page.loadMoreParent) : mergedChildren;
} else {
const page = await loadPagedObjectGroupChildren({
node: parent,
parentNodeId,
querySchema,
effectiveSchema,
objectTypes,
offset: loadMore.offset,
pageSize: loadMore.pageSize,
searchFilter: options?.searchFilter,
force: false,
});
const page = await loadTablePageCheckingAnchor(loadMore.anchor, loadMore.offset, loadMore.pageSize, (offset, pageSize) =>
loadPagedObjectGroupChildren({
node: parent,
parentNodeId,
querySchema,
effectiveSchema,
objectTypes,
offset,
pageSize,
searchFilter: options?.searchFilter,
force: false,
}),
);
const targetParent = treeNodeLoadRelatedTarget(load, parent);
if (!targetParent || !parentEpoch.isCurrent()) return;
const currentChildren = withoutLoadMoreNodes(targetParent.children);
mergedChildren = mergeLocatedTreeChildren(targetParent, currentChildren, page.children, parentConnectionId, parentDatabase);
nextChildren = page.hasMore ? [...mergedChildren, buildLoadMoreNode(targetParent, page.nextOffset, loadMore.pageSize)] : mergedChildren;
nextChildren = page.hasMore ? [...mergedChildren, buildLoadMoreNode(targetParent, page.nextOffset, loadMore.pageSize, page.nextAnchor)] : mergedChildren;
targetParent.objectCount = mergedChildren.length;
setChildren(targetParent, nextChildren);
if (!options?.searchFilter) {