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 manualTotalRowCountLoading = ref(false);
|
||||
let manualTotalRowCountGeneration = 0;
|
||||
function invalidateManualTotalRowCount() {
|
||||
manualTotalRowCountGeneration++;
|
||||
manualTotalRowCount.value = undefined;
|
||||
}
|
||||
const esDeepPageJumpConfirmOpen = ref(false);
|
||||
const pendingEsDeepPageJump = ref<{ targetPage: number; requestCount: number; updateCurrentPage: boolean }>();
|
||||
// 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 ?? ""],
|
||||
(values, previousValues) => {
|
||||
if (!didDataGridInfiniteScrollContextChange(values, previousValues)) return;
|
||||
manualTotalRowCount.value = undefined;
|
||||
invalidateManualTotalRowCount();
|
||||
},
|
||||
{ flush: "sync" },
|
||||
);
|
||||
|
||||
function syncOrderByInputWithSort(column: string | null, direction: "asc" | "desc" | null) {
|
||||
@@ -3844,12 +3850,13 @@ function jumpToCountedLastPage(total: number) {
|
||||
requestServerPageJump(lastPageNum);
|
||||
}
|
||||
|
||||
async function beginManualTotalRowCount(): Promise<boolean> {
|
||||
if (manualTotalRowCountLoading.value) return false;
|
||||
async function beginManualTotalRowCount(): Promise<number | undefined> {
|
||||
if (manualTotalRowCountLoading.value) return undefined;
|
||||
const generation = manualTotalRowCountGeneration;
|
||||
manualTotalRowCountLoading.value = true;
|
||||
// Flush busy UI (overlay / spinner) before the slow COUNT starts.
|
||||
await nextTick();
|
||||
return true;
|
||||
return generation;
|
||||
}
|
||||
|
||||
async function lastPage() {
|
||||
@@ -3865,9 +3872,12 @@ async function lastPage() {
|
||||
}
|
||||
// Navicat-style: always re-COUNT when jumping to the last page.
|
||||
if (props.countTotalRows) {
|
||||
if (!(await beginManualTotalRowCount())) return;
|
||||
const generation = await beginManualTotalRowCount();
|
||||
if (generation === undefined) return;
|
||||
try {
|
||||
if (generation !== manualTotalRowCountGeneration) return;
|
||||
const total = await props.countTotalRows();
|
||||
if (generation !== manualTotalRowCountGeneration) return;
|
||||
if (typeof total !== "number" || !Number.isFinite(total) || total < 0) return;
|
||||
manualTotalRowCount.value = total;
|
||||
jumpToCountedLastPage(total);
|
||||
@@ -3886,12 +3896,15 @@ async function lastPage() {
|
||||
return;
|
||||
}
|
||||
if (props.connectionId && (props.countSql || props.tableMeta)) {
|
||||
if (!(await beginManualTotalRowCount())) return;
|
||||
const generation = await beginManualTotalRowCount();
|
||||
if (generation === undefined) return;
|
||||
try {
|
||||
if (generation !== manualTotalRowCountGeneration) return;
|
||||
const countTarget = await buildCurrentCountTarget();
|
||||
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));
|
||||
if (generation !== manualTotalRowCountGeneration) return;
|
||||
const total = Number(result.rows?.[0]?.[0] ?? 0);
|
||||
if (!Number.isFinite(total) || total < 0) return;
|
||||
manualTotalRowCount.value = total;
|
||||
@@ -3947,10 +3960,13 @@ async function buildCurrentCountTarget(): Promise<{ sql: string; schema?: string
|
||||
}
|
||||
|
||||
async function calculateTotalRowCount() {
|
||||
if (!(await beginManualTotalRowCount())) return;
|
||||
const generation = await beginManualTotalRowCount();
|
||||
if (generation === undefined) return;
|
||||
try {
|
||||
if (generation !== manualTotalRowCountGeneration) return;
|
||||
if (props.countTotalRows) {
|
||||
const total = await props.countTotalRows();
|
||||
if (generation !== manualTotalRowCountGeneration) return;
|
||||
if (typeof total === "number" && Number.isFinite(total) && total >= 0) {
|
||||
manualTotalRowCount.value = total;
|
||||
}
|
||||
@@ -3958,8 +3974,9 @@ async function calculateTotalRowCount() {
|
||||
}
|
||||
if (!props.connectionId) return;
|
||||
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));
|
||||
if (generation !== manualTotalRowCountGeneration) return;
|
||||
const total = Number(result.rows?.[0]?.[0] ?? 0);
|
||||
if (Number.isFinite(total) && total >= 0) {
|
||||
manualTotalRowCount.value = total;
|
||||
@@ -4662,6 +4679,9 @@ function resetInfiniteScrollState() {
|
||||
}
|
||||
|
||||
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();
|
||||
if (infiniteScrollEnabled.value || loadAllRowsActive.value) {
|
||||
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");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user