feat(export): support safe background query exports

This commit is contained in:
zhangyaxi authored and GitHub committed 2026-07-29 03:18:06 +08:00
1 parent 2f44d36018
commit a07def6042
17 files changed
+577 -195

No files matched your search

+105 -1
View File
@@ -6,6 +6,7 @@ import { decodeQueryResultArchive } from "../../apps/desktop/src/lib/query/query
import { analyzeEditableQueryEditability } from "../../apps/desktop/src/lib/sql/sqlAnalysis.ts";
import { resultSqlForGrid } from "../../apps/desktop/src/lib/tabs/tabPresentation.ts";
import { parseMongoCommand } from "../../apps/desktop/src/lib/mongo/mongoShellCommand.ts";
import { useExportTracker } from "../../apps/desktop/src/composables/useExportTracker.ts";
import { useConnectionStore } from "../../apps/desktop/src/stores/connectionStore.ts";
import { useQueryStore } from "../../apps/desktop/src/stores/queryStore.ts";
import { useSettingsStore } from "../../apps/desktop/src/stores/settingsStore.ts";
@@ -4505,14 +4506,117 @@ test("buildQueryResultExportRequest uses sorted SQL and independent row-limit se
assert.equal(request?.rowLimit, null);
assert.equal(request?.totalRows, 123456);
assert.equal(request?.keysetOptimizationEnabled, false);
assert.equal(request?.clientSessionId, `${tabId}:export`);
assert.equal(request?.clientSessionId, `${tabId}:export:export-1`);
assert.match(request?.executionId ?? "", /^[0-9a-f-]{36}$/i);
const concurrentRequest = await store.buildQueryResultExportRequest(tabId, {
exportId: "export-2",
filePath: "C:\\tmp\\events-2.csv",
format: "csv",
});
assert.equal(concurrentRequest?.clientSessionId, `${tabId}:export:export-2`);
assert.notEqual(concurrentRequest?.clientSessionId, request?.clientSessionId);
} finally {
globalThis.fetch = originalFetch;
restoreStorage();
}
});
test("same-tab direct exports use isolated sessions and cancel handlers", async () => {
const restoreStorage = installMemoryStorage();
const originalFetch = globalThis.fetch;
const originalEventSource = Object.getOwnPropertyDescriptor(globalThis, "EventSource");
const startedRequests: Array<Record<string, any>> = [];
const cancelRequests: Array<Record<string, any>> = [];
const createdExportIds: string[] = [];
class FakeEventSource {
static instances: FakeEventSource[] = [];
onopen: ((event: Event) => void) | null = null;
onmessage: ((event: MessageEvent) => void) | null = null;
onerror: ((event: Event) => void) | null = null;
constructor(readonly url: string) {
FakeEventSource.instances.push(this);
}
close() {}
emitOpen() {
this.onopen?.({} as Event);
}
emitProgress(progress: Record<string, unknown>) {
this.onmessage?.({ data: JSON.stringify(progress) } as MessageEvent);
}
}
Object.defineProperty(globalThis, "EventSource", { configurable: true, value: FakeEventSource });
setActivePinia(createPinia());
const connectionStore = useConnectionStore();
const store = useQueryStore();
const tracker = useExportTracker();
connectionStore.addEphemeralConnection(conn("conn-1"));
const tabId = store.createTab("conn-1", "analytics", "Query", "query", "public");
globalThis.fetch = withConnectionHealthMock(async (input, init) => {
const path = String(input);
if (path === "/api/export/query-result") {
startedRequests.push(JSON.parse(String(init?.body ?? "{}")));
return new Response("null", { status: 200, headers: { "Content-Type": "application/json" } });
}
if (path === "/api/export/query-result/cancel") {
cancelRequests.push(JSON.parse(String(init?.body ?? "{}")));
return new Response("null", { status: 200, headers: { "Content-Type": "application/json" } });
}
return new Response("unexpected request", { status: 500 });
});
try {
await store.exportQuerySqlDirect(tabId, "SELECT 1", "csv", "/tmp/first.csv");
await store.exportQuerySqlDirect(tabId, "SELECT 2", "csv", "/tmp/second.csv");
await waitFor(() => FakeEventSource.instances.length === 2);
FakeEventSource.instances.forEach((source) => source.emitOpen());
await waitFor(() => startedRequests.length === 2);
const first = startedRequests[0].request;
const second = startedRequests[1].request;
createdExportIds.push(first.exportId, second.exportId);
assert.equal(first.clientSessionId, `${tabId}:export:${first.exportId}`);
assert.equal(second.clientSessionId, `${tabId}:export:${second.exportId}`);
assert.notEqual(first.clientSessionId, second.clientSessionId);
assert.notEqual(first.executionId, second.executionId);
await tracker.cancelTask(first.exportId);
assert.deepEqual(cancelRequests, [{ exportId: first.exportId, executionId: first.executionId }]);
assert.equal(tracker.tasks.value.find((task) => task.exportId === second.exportId)?.status, "Running");
FakeEventSource.instances[0].emitProgress({
exportId: first.exportId,
tableName: "",
rowsExported: 0,
totalRows: null,
status: "Cancelled",
errorMessage: "Export cancelled",
});
FakeEventSource.instances[1].emitProgress({
exportId: second.exportId,
tableName: "",
rowsExported: 0,
totalRows: null,
status: "Cancelled",
errorMessage: "Export cancelled",
});
await waitFor(() => tracker.tasks.value.filter((task) => task.exportId === first.exportId || task.exportId === second.exportId).every((task) => task.status === "Cancelled"));
} finally {
createdExportIds.forEach((exportId) => tracker.removeTask(exportId));
globalThis.fetch = originalFetch;
if (originalEventSource) Object.defineProperty(globalThis, "EventSource", originalEventSource);
else Reflect.deleteProperty(globalThis, "EventSource");
restoreStorage();
}
});
test("buildQueryResultExportRequest uses exportRowLimit when enabled", async () => {
const restoreStorage = installMemoryStorage();
setActivePinia(createPinia());