feat(history): make query history retention configurable

This commit is contained in:
cowking authored and GitHub committed 2026-09-22 01:17:20 +08:00
1 parent 6d74bcbadb
commit 576943c543
25 files changed
+667 -10

No files matched your search

@@ -1,4 +1,5 @@
<script setup lang="ts">
import { HISTORY_RETENTION_LIMITS, useHistoryRetentionSetting } from "@/composables/useHistoryRetentionSetting";
import { ref, watch, shallowRef, computed, onMounted, onUnmounted, nextTick } from "vue";
import type { Ref } from "vue";
import type { EditorView as EditorViewType } from "@codemirror/view";
@@ -293,6 +294,8 @@ import { buildConnectionGroupIdPathMap, connectionGroupDestinationRows, connecti
const { t, locale } = useI18n();
const { toast } = useToast();
const settingsStore = useSettingsStore();
const historyRetention = useHistoryRetentionSetting();
const { draft: editHistoryRetentionLimit, loaded: historyRetentionLoaded, loading: historyRetentionLoading, saving: historyRetentionSaving, loadError: historyRetentionLoadError } = historyRetention;
const connectionStore = useConnectionStore();
const hasSqlServerConnection = computed(() => connectionStore.connections.some((connection) => effectiveDatabaseTypeForConnection(connection) === "sqlserver"));
const savedSqlStore = useSavedSqlStore();
@@ -1839,6 +1842,7 @@ watch(
(open) => {
if (open) {
syncEditorSettingsDraftFromStore();
void historyRetention.load();
editShowTrayIcon.value = settingsStore.desktopSettings.show_tray_icon;
editQuitOnClose.value = settingsStore.desktopSettings.quit_on_close;
editIconTheme.value = settingsStore.desktopSettings.icon_theme;
@@ -1847,6 +1851,7 @@ watch(
editDuckDbWorkerProcessIsolation.value = settingsStore.desktopSettings.duckdb_worker_process_isolation;
editSidebarTablePageSize.value = settingsStore.desktopSettings.sidebar_table_page_size ?? DEFAULT_SIDEBAR_TABLE_PAGE_SIZE;
} else {
historyRetention.discard();
clearThemePalettePreview();
clearUiFontFamilyPreview();
restoreLocaleOptionPreview();
@@ -2035,10 +2040,11 @@ const hasBlockingShortcutConflicts = computed(() => {
});
const hasBlockingFormatterConfig = computed(() => activeSettingsTab.value === "formatter" && !sqlFormatterConfigValid.value);
const hasBlockingQueryResultRowLimit = computed(() => editQueryResultMaxRowsEnabled.value && editQueryResultMaxRows.value < editPageSize.value);
const hasApplyBlocker = computed(() => hasBlockingShortcutConflicts.value || hasBlockingFormatterConfig.value || hasBlockingQueryResultRowLimit.value);
const hasApplyBlocker = computed(() => historyRetention.invalid.value || historyRetentionSaving.value || hasBlockingShortcutConflicts.value || hasBlockingFormatterConfig.value || hasBlockingQueryResultRowLimit.value);
function hasChanges(): boolean {
return (
historyRetention.changed.value ||
hasImportedSettingsPendingApply.value ||
hasEditorDraftChanges.value ||
editShowTrayIcon.value !== settingsStore.desktopSettings.show_tray_icon ||
@@ -2054,6 +2060,7 @@ function hasChanges(): boolean {
async function persistSettings() {
if (hasApplyBlocker.value) return;
await historyRetention.save();
const editorSettingsPatch = editorSettingsPatchFromDraft(currentEditorSettingsDraft(), editEditorSettingsBase.value);
const sidebarObjectDisplayChanged = editorSettingsPatch.sidebarObjectDisplay !== undefined && editorSettingsPatch.sidebarObjectDisplay !== settingsStore.editorSettings.sidebarObjectDisplay;
const sidebarTablePageSizeChanged = editSidebarTablePageSize.value !== (settingsStore.desktopSettings.sidebar_table_page_size ?? DEFAULT_SIDEBAR_TABLE_PAGE_SIZE);
@@ -2230,6 +2237,7 @@ function resetDefaultsForTab(tab: SettingsCategory) {
editSidebarCopyTableNameIncludeSchema.value = DEFAULT_EDITOR_SETTINGS.sidebarCopyTableNameIncludeSchema;
editToolbarItems.value = { ...DEFAULT_EDITOR_SETTINGS.toolbarItems };
} else if (tab === "data") {
historyRetention.reset();
editShowColumnCommentsInHeader.value = DEFAULT_EDITOR_SETTINGS.showColumnCommentsInHeader;
editShowColumnTypesInHeader.value = DEFAULT_EDITOR_SETTINGS.showColumnTypesInHeader;
editDataGridShowTransposeFieldMetadata.value = DEFAULT_EDITOR_SETTINGS.dataGridShowTransposeFieldMetadata;
@@ -2287,6 +2295,7 @@ function resetDefaultsForTab(tab: SettingsCategory) {
}
function resetAllDefaults() {
historyRetention.reset();
// Same contract as resetDefaultsForTab: a full reset also exits any
// in-progress shortcut capture (#9066).
editingShortcutId.value = null;
@@ -7424,6 +7433,21 @@ onUnmounted(() => {
<!-- Data Tab -->
<section v-else-if="activeSettingsTab === 'data'" data-settings-search-id="data" :class="['flex flex-col gap-5 py-2', settingsSearchTargetClass('data')]">
<div data-settings-search-id="history-retention" :class="['space-y-2', settingsSearchTargetClass('history-retention')]">
<Label for="history-retention-limit">{{ t("settings.historyRetentionLimit") }}</Label>
<p class="text-xs text-muted-foreground">{{ t("settings.historyRetentionDescription") }}</p>
<Select :model-value="String(editHistoryRetentionLimit)" :disabled="!historyRetentionLoaded || historyRetentionSaving" @update:model-value="(value) => (editHistoryRetentionLimit = Number(value))">
<SelectTrigger id="history-retention-limit" class="w-40"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem v-for="limit in HISTORY_RETENTION_LIMITS" :key="limit" :value="String(limit)">{{ limit === 0 ? t("settings.historyRetentionUnlimited") : String(limit) }}</SelectItem>
</SelectContent>
</Select>
<p v-if="historyRetentionLoading" class="text-xs text-muted-foreground">{{ t("common.loading") }}</p>
<div v-if="historyRetentionLoadError" class="flex items-center gap-2 text-xs text-destructive" role="alert">
<span>{{ t("settings.historyRetentionLoadFailed", { error: historyRetentionLoadError }) }}</span>
<Button type="button" variant="outline" size="sm" @click="historyRetention.load">{{ t("common.retry") }}</Button>
</div>
</div>
<div data-settings-search-id="data-grid-filter-view" :class="['overflow-hidden rounded-md border bg-muted/20', settingsSearchTargetClass('data-grid-filter-view')]">
<div class="space-y-3 p-3">
<div class="flex items-start justify-between gap-4">
@@ -0,0 +1,261 @@
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, type App } from "vue";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createPinia } from "pinia";
import { DEFAULT_EDITOR_SETTINGS } from "@/stores/settingsStore";
const hoisted = vi.hoisted(() => ({ shortcuts: {} as Record<string, string>, limit: 5000, failLoad: false, failSave: false, closes: [] as boolean[], saves: [] as number[] }));
vi.mock("vue-i18n", async () => {
const { ref } = await import("vue");
const locale = ref("en");
const t = (key: string) => key;
return {
createI18n: () => ({ global: { t, locale }, install: () => undefined }),
useI18n: () => ({ t, locale }),
};
});
vi.mock("@/components/ui/dialog", async () => {
const { defineComponent, h } = await import("vue");
const passthrough = defineComponent({
setup(_props, { slots }) {
return () => h("div", slots.default?.());
},
});
return {
Dialog: defineComponent({
props: { open: { type: Boolean, default: false } },
setup:
(props, { slots }) =>
() =>
props.open ? h("div", slots.default?.()) : null,
}),
DialogContent: passthrough,
DialogFooter: passthrough,
DialogHeader: passthrough,
DialogTitle: passthrough,
};
});
vi.mock("@/components/ui/button", async () => {
const { defineComponent, h } = await import("vue");
return {
Button: defineComponent({
inheritAttrs: false,
setup(props, { attrs, slots }) {
return () => h("button", { ...attrs, disabled: (props as { disabled?: boolean }).disabled ? true : undefined }, slots.default?.());
},
props: { disabled: { type: Boolean, default: false } },
}),
};
});
// ChangelogPanel fires a network fetch on mount (About tab); irrelevant here.
vi.mock("@/components/settings/ChangelogPanel.vue", async () => {
const { defineComponent } = await import("vue");
return { default: defineComponent({ setup: () => () => null }) };
});
// Back the store composable with a stub so the dialog boots without the Tauri
// backend, but read `shortcuts` through the hoisted object so each test can seed
// the configuration it wants before mounting.
vi.mock("@/stores/settingsStore", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/stores/settingsStore")>();
const store = {
settingsPageActive: false,
editorSettings: {
...actual.DEFAULT_EDITOR_SETTINGS,
get shortcuts() {
return hoisted.shortcuts;
},
},
desktopSettings: actual.DEFAULT_DESKTOP_SETTINGS,
mcpGlobalPolicy: { configured: false, readOnly: false },
aiConfigs: [],
aiDefaultTemplatesByDbType: {},
defaultAiMode: "ask",
restoreLastConversation: false,
initMcpGlobalPolicy: vi.fn(async () => undefined),
updateMcpGlobalPolicy: vi.fn(async () => undefined),
initAiConfigs: vi.fn(async () => undefined),
initDesktopSettings: vi.fn(async () => undefined),
updateEditorSettingsAndPersist: vi.fn(async () => undefined),
updateEditorSettings: vi.fn(),
persistEditorSettings: vi.fn(async () => undefined),
updateDesktopSettings: vi.fn(async () => undefined),
reloadAiConfigs: vi.fn(async () => undefined),
removeTemplateFromDefaultAndLastUsed: vi.fn(),
setDefaultTemplatesForDbType: vi.fn(),
updateAiConfigItem: vi.fn(),
createAiConfig: vi.fn(),
deleteAiConfig: vi.fn(),
setDefaultAiConfig: vi.fn(),
setDefaultAiMode: vi.fn(),
setRestoreLastConversation: vi.fn(),
};
return { ...actual, useSettingsStore: () => store };
});
vi.mock("@/lib/backend/api", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/backend/api")>();
return {
...actual,
loadHistoryRetentionLimit: async () => {
if (hoisted.failLoad) throw new Error("load failed");
return hoisted.limit;
},
saveHistoryRetentionLimit: async (limit: number) => {
if (hoisted.failSave) throw new Error("save failed");
hoisted.saves.push(limit);
hoisted.limit = limit;
},
};
});
import EditorSettingsDialog from "../EditorSettingsDialog.vue";
if (!("ResizeObserver" in globalThis)) {
(globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};
}
const mountedApps: Array<{ app: App; host: HTMLElement }> = [];
afterEach(() => {
for (const { app, host } of mountedApps.splice(0)) {
app.unmount();
host.remove();
}
});
async function flushAsyncUpdates() {
for (let i = 0; i < 5; i++) {
await nextTick();
await Promise.resolve();
}
}
function defaults() {
return { ...DEFAULT_EDITOR_SETTINGS.shortcuts };
}
async function mountDataTab() {
const host = document.createElement("div");
document.body.append(host);
const app = createApp(defineComponent({ setup: () => () => h(EditorSettingsDialog, { variant: "page", initialTab: "data", "onUpdate:open": (open: boolean) => hoisted.closes.push(open) }) }));
app.use(createPinia());
app.mount(host);
mountedApps.push({ app, host });
await flushAsyncUpdates();
return host;
}
function button(host: HTMLElement, text: string) {
return [...host.querySelectorAll<HTMLButtonElement>("button")].find((item) => item.textContent?.trim() === text)!;
}
beforeEach(() => {
hoisted.shortcuts = defaults();
hoisted.limit = 5000;
hoisted.failLoad = false;
hoisted.failSave = false;
hoisted.saves = [];
hoisted.closes = [];
});
// Mounts the complete settings workbench; concurrent Rust/typecheck jobs can
// delay rendering on developer machines without changing the assertions.
describe("query history retention settings", { timeout: 30_000 }, () => {
it("loads the server limit and applies a selected limit only on Apply", async () => {
const host = await mountDataTab();
const control = host.querySelector<HTMLElement>("#history-retention-limit");
expect(control, "retention setting must be available in Data settings").not.toBeNull();
expect(control!.textContent).toContain("5000");
expect(host.textContent).toContain("settings.historyRetentionDescription");
expect(hoisted.saves).toEqual([]);
control!.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true, cancelable: true }));
await flushAsyncUpdates();
const options = [...document.querySelectorAll<HTMLElement>('[role="option"]')];
expect(options.map((option) => option.textContent?.trim())).toEqual(["200", "1000", "5000", "10000", "settings.historyRetentionUnlimited"]);
const option = options[0];
option.focus();
option.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }));
await flushAsyncUpdates();
expect(control!.textContent).toContain("200");
expect(hoisted.saves).toEqual([]);
const apply = button(host, "settings.apply");
expect(apply.disabled).toBe(false);
apply.click();
await flushAsyncUpdates();
expect(hoisted.limit).toBe(200);
expect(apply.disabled).toBe(true);
});
it("keeps the draft on save failure and can retry", async () => {
const host = await mountDataTab();
button(host, "settings.resetDefaults").click();
await flushAsyncUpdates();
expect(host.querySelector("#history-retention-limit")!.textContent).toContain("1000");
hoisted.failSave = true;
button(host, "settings.apply").click();
await flushAsyncUpdates();
expect(hoisted.limit).toBe(5000);
expect(button(host, "settings.apply").disabled).toBe(false);
hoisted.failSave = false;
button(host, "settings.apply").click();
await flushAsyncUpdates();
expect(hoisted.limit).toBe(1000);
});
it("Apply and Close waits for persistence and reloads the saved value", async () => {
const host = await mountDataTab();
button(host, "settings.resetDefaults").click();
await flushAsyncUpdates();
hoisted.failSave = true;
button(host, "settings.applyAndClose").click();
await flushAsyncUpdates();
expect(hoisted.closes).toEqual([]);
expect(hoisted.limit).toBe(5000);
hoisted.failSave = false;
button(host, "settings.applyAndClose").click();
await flushAsyncUpdates();
expect(hoisted.closes).toEqual([false]);
expect(hoisted.limit).toBe(1000);
const reopened = await mountDataTab();
expect(reopened.querySelector("#history-retention-limit")!.textContent).toContain("1000");
});
it("discarding an unapplied change does not save it", async () => {
const host = await mountDataTab();
button(host, "settings.resetDefaults").click();
await flushAsyncUpdates();
button(host, "common.close").click();
await flushAsyncUpdates();
button(host, "settings.unsavedChangesCloseDiscard").click();
await flushAsyncUpdates();
expect(hoisted.closes).toEqual([false]);
expect(hoisted.saves).toEqual([]);
const reopened = await mountDataTab();
expect(reopened.querySelector("#history-retention-limit")!.textContent).toContain("5000");
});
it("does not overwrite server history retention when loading fails", async () => {
hoisted.failLoad = true;
const host = await mountDataTab();
expect((host.querySelector("#history-retention-limit") as HTMLButtonElement).disabled).toBe(true);
expect(host.textContent).toContain("settings.historyRetentionLoadFailed");
button(host, "settings.resetDefaults").click();
button(host, "settings.apply").click();
await flushAsyncUpdates();
expect(hoisted.saves).toEqual([]);
hoisted.failLoad = false;
button(host, "common.retry").click();
await flushAsyncUpdates();
expect(host.querySelector("#history-retention-limit")!.textContent).toContain("5000");
});
});