fix(grid): clear stale manual row count on data refresh
This commit is contained in:
6 files changed
+195
-10
No files matched your search
@@ -3357,6 +3357,11 @@ watch(
|
|||||||
);
|
);
|
||||||
const manualTotalRowCount = ref<number | undefined>(undefined);
|
const manualTotalRowCount = ref<number | undefined>(undefined);
|
||||||
const manualTotalRowCountLoading = ref(false);
|
const manualTotalRowCountLoading = ref(false);
|
||||||
|
let manualTotalRowCountGeneration = 0;
|
||||||
|
function invalidateManualTotalRowCount() {
|
||||||
|
manualTotalRowCountGeneration++;
|
||||||
|
manualTotalRowCount.value = undefined;
|
||||||
|
}
|
||||||
const esDeepPageJumpConfirmOpen = ref(false);
|
const esDeepPageJumpConfirmOpen = ref(false);
|
||||||
const pendingEsDeepPageJump = ref<{ targetPage: number; requestCount: number; updateCurrentPage: boolean }>();
|
const pendingEsDeepPageJump = ref<{ targetPage: number; requestCount: number; updateCurrentPage: boolean }>();
|
||||||
// One "load all" click fetches the whole remaining segment in a single request;
|
// One "load all" click fetches the whole remaining segment in a single request;
|
||||||
@@ -3608,8 +3613,9 @@ watch(
|
|||||||
() => [props.countSql ?? "", props.tableMeta?.schema ?? "", props.tableMeta?.tableName ?? "", currentWhereInput() ?? "", props.database ?? "", props.connectionId ?? ""],
|
() => [props.countSql ?? "", props.tableMeta?.schema ?? "", props.tableMeta?.tableName ?? "", currentWhereInput() ?? "", props.database ?? "", props.connectionId ?? ""],
|
||||||
(values, previousValues) => {
|
(values, previousValues) => {
|
||||||
if (!didDataGridInfiniteScrollContextChange(values, previousValues)) return;
|
if (!didDataGridInfiniteScrollContextChange(values, previousValues)) return;
|
||||||
manualTotalRowCount.value = undefined;
|
invalidateManualTotalRowCount();
|
||||||
},
|
},
|
||||||
|
{ flush: "sync" },
|
||||||
);
|
);
|
||||||
|
|
||||||
function syncOrderByInputWithSort(column: string | null, direction: "asc" | "desc" | null) {
|
function syncOrderByInputWithSort(column: string | null, direction: "asc" | "desc" | null) {
|
||||||
@@ -3844,12 +3850,13 @@ function jumpToCountedLastPage(total: number) {
|
|||||||
requestServerPageJump(lastPageNum);
|
requestServerPageJump(lastPageNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function beginManualTotalRowCount(): Promise<boolean> {
|
async function beginManualTotalRowCount(): Promise<number | undefined> {
|
||||||
if (manualTotalRowCountLoading.value) return false;
|
if (manualTotalRowCountLoading.value) return undefined;
|
||||||
|
const generation = manualTotalRowCountGeneration;
|
||||||
manualTotalRowCountLoading.value = true;
|
manualTotalRowCountLoading.value = true;
|
||||||
// Flush busy UI (overlay / spinner) before the slow COUNT starts.
|
// Flush busy UI (overlay / spinner) before the slow COUNT starts.
|
||||||
await nextTick();
|
await nextTick();
|
||||||
return true;
|
return generation;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function lastPage() {
|
async function lastPage() {
|
||||||
@@ -3865,9 +3872,12 @@ async function lastPage() {
|
|||||||
}
|
}
|
||||||
// Navicat-style: always re-COUNT when jumping to the last page.
|
// Navicat-style: always re-COUNT when jumping to the last page.
|
||||||
if (props.countTotalRows) {
|
if (props.countTotalRows) {
|
||||||
if (!(await beginManualTotalRowCount())) return;
|
const generation = await beginManualTotalRowCount();
|
||||||
|
if (generation === undefined) return;
|
||||||
try {
|
try {
|
||||||
|
if (generation !== manualTotalRowCountGeneration) return;
|
||||||
const total = await props.countTotalRows();
|
const total = await props.countTotalRows();
|
||||||
|
if (generation !== manualTotalRowCountGeneration) return;
|
||||||
if (typeof total !== "number" || !Number.isFinite(total) || total < 0) return;
|
if (typeof total !== "number" || !Number.isFinite(total) || total < 0) return;
|
||||||
manualTotalRowCount.value = total;
|
manualTotalRowCount.value = total;
|
||||||
jumpToCountedLastPage(total);
|
jumpToCountedLastPage(total);
|
||||||
@@ -3886,12 +3896,15 @@ async function lastPage() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (props.connectionId && (props.countSql || props.tableMeta)) {
|
if (props.connectionId && (props.countSql || props.tableMeta)) {
|
||||||
if (!(await beginManualTotalRowCount())) return;
|
const generation = await beginManualTotalRowCount();
|
||||||
|
if (generation === undefined) return;
|
||||||
try {
|
try {
|
||||||
|
if (generation !== manualTotalRowCountGeneration) return;
|
||||||
const countTarget = await buildCurrentCountTarget();
|
const countTarget = await buildCurrentCountTarget();
|
||||||
const sql = countTarget?.sql;
|
const sql = countTarget?.sql;
|
||||||
if (!sql) return;
|
if (!sql || generation !== manualTotalRowCountGeneration) return;
|
||||||
const result = await api.executeQuery(props.connectionId, props.executionDatabase ?? props.database ?? "", sql, countTarget.schema, undefined, dataGridCountQueryOptions(connectionStore.getConfig(props.connectionId), settingsStore.editorSettings.globalQueryTimeoutSecs));
|
const result = await api.executeQuery(props.connectionId, props.executionDatabase ?? props.database ?? "", sql, countTarget.schema, undefined, dataGridCountQueryOptions(connectionStore.getConfig(props.connectionId), settingsStore.editorSettings.globalQueryTimeoutSecs));
|
||||||
|
if (generation !== manualTotalRowCountGeneration) return;
|
||||||
const total = Number(result.rows?.[0]?.[0] ?? 0);
|
const total = Number(result.rows?.[0]?.[0] ?? 0);
|
||||||
if (!Number.isFinite(total) || total < 0) return;
|
if (!Number.isFinite(total) || total < 0) return;
|
||||||
manualTotalRowCount.value = total;
|
manualTotalRowCount.value = total;
|
||||||
@@ -3947,10 +3960,13 @@ async function buildCurrentCountTarget(): Promise<{ sql: string; schema?: string
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function calculateTotalRowCount() {
|
async function calculateTotalRowCount() {
|
||||||
if (!(await beginManualTotalRowCount())) return;
|
const generation = await beginManualTotalRowCount();
|
||||||
|
if (generation === undefined) return;
|
||||||
try {
|
try {
|
||||||
|
if (generation !== manualTotalRowCountGeneration) return;
|
||||||
if (props.countTotalRows) {
|
if (props.countTotalRows) {
|
||||||
const total = await props.countTotalRows();
|
const total = await props.countTotalRows();
|
||||||
|
if (generation !== manualTotalRowCountGeneration) return;
|
||||||
if (typeof total === "number" && Number.isFinite(total) && total >= 0) {
|
if (typeof total === "number" && Number.isFinite(total) && total >= 0) {
|
||||||
manualTotalRowCount.value = total;
|
manualTotalRowCount.value = total;
|
||||||
}
|
}
|
||||||
@@ -3958,8 +3974,9 @@ async function calculateTotalRowCount() {
|
|||||||
}
|
}
|
||||||
if (!props.connectionId) return;
|
if (!props.connectionId) return;
|
||||||
const countTarget = await buildCurrentCountTarget();
|
const countTarget = await buildCurrentCountTarget();
|
||||||
if (!countTarget?.sql) return;
|
if (!countTarget?.sql || generation !== manualTotalRowCountGeneration) return;
|
||||||
const result = await api.executeQuery(props.connectionId, props.executionDatabase ?? props.database ?? "", countTarget.sql, countTarget.schema, undefined, dataGridCountQueryOptions(connectionStore.getConfig(props.connectionId), settingsStore.editorSettings.globalQueryTimeoutSecs));
|
const result = await api.executeQuery(props.connectionId, props.executionDatabase ?? props.database ?? "", countTarget.sql, countTarget.schema, undefined, dataGridCountQueryOptions(connectionStore.getConfig(props.connectionId), settingsStore.editorSettings.globalQueryTimeoutSecs));
|
||||||
|
if (generation !== manualTotalRowCountGeneration) return;
|
||||||
const total = Number(result.rows?.[0]?.[0] ?? 0);
|
const total = Number(result.rows?.[0]?.[0] ?? 0);
|
||||||
if (Number.isFinite(total) && total >= 0) {
|
if (Number.isFinite(total) && total >= 0) {
|
||||||
manualTotalRowCount.value = total;
|
manualTotalRowCount.value = total;
|
||||||
@@ -4662,6 +4679,9 @@ function resetInfiniteScrollState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function prepareFullReload() {
|
function prepareFullReload() {
|
||||||
|
// Counts belong to the previous data snapshot; paging can reuse them, but
|
||||||
|
// refresh/rollback must also reject any pending COUNT from that snapshot.
|
||||||
|
invalidateManualTotalRowCount();
|
||||||
const viewportAnchor = captureViewportAnchorForRefresh();
|
const viewportAnchor = captureViewportAnchorForRefresh();
|
||||||
if (infiniteScrollEnabled.value || loadAllRowsActive.value) {
|
if (infiniteScrollEnabled.value || loadAllRowsActive.value) {
|
||||||
resetInfiniteScrollState();
|
resetInfiniteScrollState();
|
||||||
|
|||||||
+165
@@ -0,0 +1,165 @@
|
|||||||
|
// @vitest-environment happy-dom
|
||||||
|
|
||||||
|
import { createApp, defineComponent, h, markRaw, nextTick, reactive, type App } from "vue";
|
||||||
|
import { createPinia, setActivePinia } from "pinia";
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import i18n from "@/i18n";
|
||||||
|
import { TooltipProvider } from "@/components/ui/tooltip";
|
||||||
|
import type { QueryResult } from "@/types/database";
|
||||||
|
|
||||||
|
vi.mock("@/composables/useDataGridColumnResize", async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import("@/composables/useDataGridColumnResize")>();
|
||||||
|
const { ref } = await import("vue");
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
useDataGridColumnResize: () => ({
|
||||||
|
initColumnWidths: vi.fn(),
|
||||||
|
onResizeStart: vi.fn(),
|
||||||
|
autoFitColumn: vi.fn(),
|
||||||
|
renderedColumnWidths: ref([120]),
|
||||||
|
totalWidth: ref(120),
|
||||||
|
columnVars: ref({ "--total-w": "120px" }),
|
||||||
|
getIsResizing: () => false,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
import DataGrid from "../DataGrid.vue";
|
||||||
|
import { useSettingsStore } from "@/stores/settingsStore";
|
||||||
|
|
||||||
|
const mountedApps: Array<{ app: App; host: HTMLElement }> = [];
|
||||||
|
|
||||||
|
function pageResult(): QueryResult {
|
||||||
|
return markRaw({ columns: ["id"], rows: Array.from({ length: 100 }, (_, i) => [i + 1]), affected_rows: 100, execution_time_ms: 1, has_more: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
function mountGrid(countTotalRows = vi.fn<() => Promise<number | undefined>>().mockResolvedValue(250)) {
|
||||||
|
const pinia = createPinia();
|
||||||
|
setActivePinia(pinia);
|
||||||
|
useSettingsStore().updateEditorSettings({ dataGridRenderMode: "canvas", infiniteScroll: false });
|
||||||
|
const state = reactive({ result: pageResult(), pageOffset: 0, loading: false, totalRowCount: undefined as number | undefined, countSql: "SELECT COUNT(*) FROM users WHERE active = 1" });
|
||||||
|
const onReload = vi.fn(async () => {
|
||||||
|
state.loading = true;
|
||||||
|
await nextTick();
|
||||||
|
state.result = pageResult();
|
||||||
|
state.loading = false;
|
||||||
|
});
|
||||||
|
const onPaginate = vi.fn();
|
||||||
|
const Root = defineComponent({
|
||||||
|
setup: () => () =>
|
||||||
|
h(
|
||||||
|
TooltipProvider,
|
||||||
|
{ delayDuration: 0 },
|
||||||
|
{ default: () => h(DataGrid, { ...state, pageLimit: 100, context: "table-data", databaseType: "mysql", connectionId: "test", database: "db", tableMeta: { tableName: "users", schema: "db", columns: [], primaryKeys: [] }, paginationEnabled: true, countTotalRows, onReload, onPaginate }) },
|
||||||
|
),
|
||||||
|
});
|
||||||
|
const host = document.createElement("div");
|
||||||
|
document.body.append(host);
|
||||||
|
const app = createApp(Root);
|
||||||
|
app.use(pinia);
|
||||||
|
app.use(i18n);
|
||||||
|
app.mount(host);
|
||||||
|
mountedApps.push({ app, host });
|
||||||
|
return { host, state, countTotalRows, onReload, onPaginate };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function settle() {
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
await nextTick();
|
||||||
|
await Promise.resolve();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function clickButton(host: HTMLElement, label: string) {
|
||||||
|
const button = Array.from(host.querySelectorAll<HTMLButtonElement>("button")).find((item) => item.getAttribute("aria-label") === label || item.textContent?.trim() === label);
|
||||||
|
if (!button) throw new Error(`Button missing: ${label}`);
|
||||||
|
button.click();
|
||||||
|
}
|
||||||
|
|
||||||
|
function totalText(host: HTMLElement) {
|
||||||
|
return host.textContent ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
for (const { app, host } of mountedApps.splice(0)) {
|
||||||
|
app.unmount();
|
||||||
|
host.remove();
|
||||||
|
}
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("DataGrid manual count freshness", () => {
|
||||||
|
it("invalidates a manual count on refresh and allows counting the updated rows", async () => {
|
||||||
|
const { host, countTotalRows, onReload } = mountGrid();
|
||||||
|
await settle();
|
||||||
|
clickButton(host, String(i18n.global.t("grid.calculateTotalRowsInline")));
|
||||||
|
await settle();
|
||||||
|
expect(totalText(host)).toContain("250");
|
||||||
|
|
||||||
|
clickButton(host, String(i18n.global.t("grid.refresh")));
|
||||||
|
await settle();
|
||||||
|
expect(onReload).toHaveBeenCalledOnce();
|
||||||
|
expect(totalText(host)).not.toContain("250");
|
||||||
|
countTotalRows.mockResolvedValue(199);
|
||||||
|
clickButton(host, String(i18n.global.t("grid.calculateTotalRowsInline")));
|
||||||
|
await settle();
|
||||||
|
expect(totalText(host)).toContain("199");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps the count while paging through the same result", async () => {
|
||||||
|
const { host, state } = mountGrid();
|
||||||
|
await settle();
|
||||||
|
clickButton(host, String(i18n.global.t("grid.calculateTotalRowsInline")));
|
||||||
|
await settle();
|
||||||
|
state.pageOffset = 100;
|
||||||
|
state.result = pageResult();
|
||||||
|
await settle();
|
||||||
|
expect(totalText(host)).toContain("250");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not let an old manual count override a refreshed automatic count", async () => {
|
||||||
|
let resolveCount!: (total: number) => void;
|
||||||
|
const countTotalRows = vi.fn(() => new Promise<number>((resolve) => (resolveCount = resolve)));
|
||||||
|
const { host, state } = mountGrid(countTotalRows);
|
||||||
|
await settle();
|
||||||
|
clickButton(host, String(i18n.global.t("grid.calculateTotalRowsInline")));
|
||||||
|
await settle();
|
||||||
|
clickButton(host, String(i18n.global.t("grid.refresh")));
|
||||||
|
state.totalRowCount = 199;
|
||||||
|
await settle();
|
||||||
|
resolveCount(250);
|
||||||
|
await settle();
|
||||||
|
expect(totalText(host)).toContain("199");
|
||||||
|
expect(totalText(host)).not.toContain("250");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("discards a pending count after the query context changes", async () => {
|
||||||
|
let resolveCount!: (total: number) => void;
|
||||||
|
const { host, state } = mountGrid(vi.fn(() => new Promise<number>((resolve) => (resolveCount = resolve))));
|
||||||
|
await settle();
|
||||||
|
clickButton(host, String(i18n.global.t("grid.calculateTotalRowsInline")));
|
||||||
|
await settle();
|
||||||
|
state.countSql = "SELECT COUNT(*) FROM other_users";
|
||||||
|
await settle();
|
||||||
|
resolveCount(250);
|
||||||
|
await settle();
|
||||||
|
expect(totalText(host)).not.toContain("250");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not navigate using a last-page count completed after refresh", async () => {
|
||||||
|
let resolveCount!: (total: number) => void;
|
||||||
|
const { host, onPaginate } = mountGrid(vi.fn(() => new Promise<number>((resolve) => (resolveCount = resolve))));
|
||||||
|
await settle();
|
||||||
|
const pageInput = host.querySelector<HTMLInputElement>(`input[aria-label="${i18n.global.t("grid.jumpToPage")}"]`)!;
|
||||||
|
const lastPageButton = pageInput.nextElementSibling!.nextElementSibling as HTMLButtonElement;
|
||||||
|
expect(lastPageButton.disabled).toBe(false);
|
||||||
|
lastPageButton.click();
|
||||||
|
await settle();
|
||||||
|
clickButton(host, String(i18n.global.t("grid.refresh")));
|
||||||
|
await settle();
|
||||||
|
resolveCount(250);
|
||||||
|
await settle();
|
||||||
|
expect(onPaginate).not.toHaveBeenCalled();
|
||||||
|
expect(totalText(host)).not.toContain("250");
|
||||||
|
});
|
||||||
|
});
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 79 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 79 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 79 KiB |
+1
-1
@@ -59,7 +59,7 @@ test("load-all selects the last loaded row only after a valid append completes",
|
|||||||
assert.match(source, /\(\) => \[props\.loading, props\.result\.rows\.length, props\.result\.appended_from_row_count\] as const[\s\S]*?\{ flush: "post" \}/);
|
assert.match(source, /\(\) => \[props\.loading, props\.result\.rows\.length, props\.result\.appended_from_row_count\] as const[\s\S]*?\{ flush: "post" \}/);
|
||||||
assert.match(source, /props\.result\.appended_from_row_count !== requestedOffset[\s\S]*?return;[\s\S]*?if \(shouldSelectLastRow\) selectAndRevealLastLoadedRow\(\)/);
|
assert.match(source, /props\.result\.appended_from_row_count !== requestedOffset[\s\S]*?return;[\s\S]*?if \(shouldSelectLastRow\) selectAndRevealLastLoadedRow\(\)/);
|
||||||
assert.match(source, /function selectAndRevealLastLoadedRow\(\)[\s\S]*?selectRow\(rowIndex\)[\s\S]*?remainingFrames = 12[\s\S]*?scrollCanvasRowIntoView\(rowIndex, "end"\)[\s\S]*?scrollDomRowIntoView\(rowIndex, "end"\)[\s\S]*?requestAnimationFrame\(revealWhenReady\)/);
|
assert.match(source, /function selectAndRevealLastLoadedRow\(\)[\s\S]*?selectRow\(rowIndex\)[\s\S]*?remainingFrames = 12[\s\S]*?scrollCanvasRowIntoView\(rowIndex, "end"\)[\s\S]*?scrollDomRowIntoView\(rowIndex, "end"\)[\s\S]*?requestAnimationFrame\(revealWhenReady\)/);
|
||||||
assert.match(source, /\(\) => \[props\.countSql \?\? ""[\s\S]*?manualTotalRowCount\.value = undefined/);
|
// Count invalidation is verified by mounted-grid tests in DataGridTotalRowCountRefresh.spec.ts.
|
||||||
assert.match(source, /if \(infiniteScrollLoadAllPending\) \{[\s\S]*?infiniteScrollLoadAllPending = false;[\s\S]*?selectAndRevealLastLoadedRow\(\)/);
|
assert.match(source, /if \(infiniteScrollLoadAllPending\) \{[\s\S]*?infiniteScrollLoadAllPending = false;[\s\S]*?selectAndRevealLastLoadedRow\(\)/);
|
||||||
assert.match(source, /dataGridInfiniteScrollAppendCompletion\(previousResult, result,[\s\S]*?loadAllRowsActive\.value = false/);
|
assert.match(source, /dataGridInfiniteScrollAppendCompletion\(previousResult, result,[\s\S]*?loadAllRowsActive\.value = false/);
|
||||||
assert.match(querySurfacesSource, /paginate: \[tabId: string, offset: number, limit: number, whereInput\?: string, orderBy\?: string, appendResult\?: boolean\]/);
|
assert.match(querySurfacesSource, /paginate: \[tabId: string, offset: number, limit: number, whereInput\?: string, orderBy\?: string, appendResult\?: boolean\]/);
|
||||||
|
|||||||
Reference in new issue
Block a user