feat(query): show live statement execution progress

This commit is contained in:
greathaoqi authored and GitHub committed 2026-07-30 00:44:10 +08:00
1 parent 1774126f76
commit ed018dbc77
27 files changed
+974 -85

No files matched your search

@@ -58,6 +58,19 @@ describe("queryStore cancel timeout recovery", () => {
const store = useQueryStore();
const tabId = store.createTab("duckdb-1", "main", "query_1");
store.setExecutingWithId(tabId, "exec-1");
store.tabs[0]!.batchSqlExecution = {
executionId: "exec-1",
submittedSql: "SELECT 1; SELECT 2",
editorFingerprint: "fingerprint",
sourceOffset: 0,
completed: 0,
total: 2,
startedAt: Date.now(),
items: [
{ statementIndex: 0, sql: "SELECT 1", from: 0, to: 8, status: "running" },
{ statementIndex: 1, sql: "SELECT 2", from: 10, to: 18, status: "pending" },
],
};
const cancel = store.cancelTabExecution(tabId);
await vi.advanceTimersByTimeAsync(1);
@@ -71,6 +84,10 @@ describe("queryStore cancel timeout recovery", () => {
isExecuting: false,
isCancelling: false,
executionId: undefined,
batchSqlExecution: {
finishedAt: expect.any(Number),
items: [{ status: "cancelled" }, { status: "skipped" }],
},
});
expect(store.tabs[0]?.result?.rows[0]?.[0]).toContain("Query canceled");
}, 15_000);
@@ -8,6 +8,7 @@ const mocks = vi.hoisted(() => ({
closeQuerySession: vi.fn(),
ensureConnected: vi.fn(),
executeMulti: vi.fn(),
executeMultiWithProgress: vi.fn(),
executeQuery: vi.fn(),
getConnectionConfig: vi.fn(),
prepareQueryPaginationExecutionPlan: vi.fn(),
@@ -21,6 +22,7 @@ vi.mock("@/lib/backend/api", () => ({
closeClientConnectionSession: mocks.closeClientConnectionSession,
closeQuerySession: mocks.closeQuerySession,
executeMulti: mocks.executeMulti,
executeMultiWithProgress: mocks.executeMultiWithProgress,
executeQuery: mocks.executeQuery,
prepareQueryPaginationExecutionPlan: mocks.prepareQueryPaginationExecutionPlan,
saveOpenTabsState: mocks.saveOpenTabsState,
@@ -99,6 +101,25 @@ describe("queryStore multi-statement errors", () => {
useAgentResultSession: false,
}));
mocks.analyzeEditableQueryEditability.mockResolvedValue({ editable: false, reason: "multiple-statements" });
mocks.executeMultiWithProgress.mockImplementation(async (connectionId, database, sql, onProgress, schema, options) => {
const results = await mocks.executeMulti(connectionId, database, sql, schema, options?.executionId, options);
const total = results.length;
results.forEach((result: any, index: number) => {
const statementIndex = result.statement_index ?? index;
const success = result.execution_error !== true;
onProgress({
executionId: options?.executionId,
statementIndex,
completed: index + 1,
total,
success,
executionTimeMs: result.execution_time_ms,
affectedRows: result.affected_rows,
error: success ? undefined : String(result.rows[0]?.[0] ?? ""),
});
});
return results;
});
});
it("opens the first error result from a mixed result batch", async () => {
@@ -117,6 +138,126 @@ describe("queryStore multi-statement errors", () => {
expect(tab.result?.columns).toEqual(["Error"]);
});
it("updates live per-statement progress before the batch promise resolves", async () => {
const pendingExecution = deferred<any[]>();
let reportProgress!: (progress: any) => void;
mocks.executeMultiWithProgress.mockImplementationOnce((_connectionId, _database, _sql, onProgress) => {
reportProgress = onProgress;
return pendingExecution.promise;
});
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("mysql-1", "app", "Query", "query", undefined, "SELECT 1;\nSELECT 2;\nSELECT bad");
const execution = store.executeTabSql(tabId, "SELECT 1;\nSELECT 2;\nSELECT bad");
await vi.waitFor(() => expect(store.tabs.find((item) => item.id === tabId)?.batchSqlExecution?.items[0]?.status).toBe("running"));
const executionId = store.tabs.find((item) => item.id === tabId)!.executionId!;
reportProgress({
executionId,
statementIndex: 0,
completed: 1,
total: 3,
success: true,
executionTimeMs: 4,
affectedRows: 1,
});
expect(store.tabs.find((item) => item.id === tabId)?.batchSqlExecution).toMatchObject({
completed: 1,
total: 3,
items: [{ status: "success", executionTimeMs: 4, affectedRows: 1 }, { status: "running" }, { status: "pending" }],
});
pendingExecution.resolve([
{ columns: [], rows: [], affected_rows: 1, execution_time_ms: 4, statement_index: 0 },
{ columns: ["Error"], rows: [["bad statement"]], affected_rows: 0, execution_time_ms: 2, statement_index: 1, execution_error: true },
]);
await execution;
expect(store.tabs.find((item) => item.id === tabId)?.batchSqlExecution).toMatchObject({
completed: 2,
items: [{ status: "success" }, { status: "error", error: "bad statement" }, { status: "skipped" }],
});
});
it("records a top-level batch failure on the current statement", async () => {
mocks.executeMultiWithProgress.mockRejectedValueOnce(new Error("transport failed"));
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("mysql-1", "app", "Query", "query", undefined, "SELECT 1;\nSELECT 2");
await store.executeTabSql(tabId, "SELECT 1;\nSELECT 2");
expect(store.tabs.find((item) => item.id === tabId)?.batchSqlExecution).toMatchObject({
completed: 1,
items: [{ status: "error", error: "transport failed" }, { status: "skipped" }],
});
});
it("keeps completed progress and records a later top-level batch failure", async () => {
mocks.executeMultiWithProgress.mockImplementationOnce((_connectionId, _database, _sql, onProgress, _schema, options) => {
onProgress({
executionId: options?.executionId,
statementIndex: 0,
completed: 1,
total: 3,
success: true,
executionTimeMs: 4,
affectedRows: 1,
});
return Promise.reject(new Error("connection lost"));
});
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("mysql-1", "app", "Query", "query", undefined, "SELECT 1;\nSELECT 2;\nSELECT 3");
await store.executeTabSql(tabId, "SELECT 1;\nSELECT 2;\nSELECT 3");
expect(store.tabs.find((item) => item.id === tabId)?.batchSqlExecution).toMatchObject({
completed: 2,
items: [{ status: "success" }, { status: "error", error: "connection lost" }, { status: "skipped" }],
});
});
it("updates the live marker state for a single statement", async () => {
const pendingExecution = deferred<any[]>();
mocks.executeMulti.mockImplementationOnce(() => pendingExecution.promise);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const sql = "SELECT 1";
const tabId = store.createTab("mysql-1", "app", "Query", "query", undefined, sql);
const execution = store.executeTabSql(tabId, sql);
await vi.waitFor(() => expect(store.tabs.find((item) => item.id === tabId)?.batchSqlExecution?.items[0]?.status).toBe("running"));
expect(mocks.executeMultiWithProgress).not.toHaveBeenCalled();
pendingExecution.resolve([{ columns: ["Error"], rows: [["bad statement"]], affected_rows: 0, execution_time_ms: 3, statement_index: 0, execution_error: true }]);
await execution;
expect(store.tabs.find((item) => item.id === tabId)?.batchSqlExecution).toMatchObject({
completed: 1,
total: 1,
items: [{ status: "error", executionTimeMs: 3, affectedRows: 0, error: "bad statement" }],
});
});
it("marks the active statement cancelled and leaves later statements unexecuted", async () => {
const pendingExecution = deferred<never>();
mocks.executeMultiWithProgress.mockImplementationOnce(() => pendingExecution.promise);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("mysql-1", "app", "Query", "query", undefined, "SELECT 1;\nSELECT 2");
const execution = store.executeTabSql(tabId, "SELECT 1;\nSELECT 2");
await vi.waitFor(() => expect(store.tabs.find((item) => item.id === tabId)?.batchSqlExecution?.items[0]?.status).toBe("running"));
await expect(store.cancelTabExecution(tabId)).resolves.toBe(true);
pendingExecution.reject(new Error("Query canceled"));
await execution;
expect(store.tabs.find((item) => item.id === tabId)?.batchSqlExecution?.items).toMatchObject([{ status: "cancelled" }, { status: "skipped" }]);
});
it("opens a later PostgreSQL error result from a mixed result batch", async () => {
mocks.getConnectionConfig.mockReturnValue({
id: "postgres-1",
@@ -457,19 +598,37 @@ describe("queryStore multi-statement errors", () => {
const tab = store.tabs.find((item) => item.id === tabId)!;
const retainedRunId = tab.activeResultRunId!;
const retainedRunCacheKey = tab.resultRuns?.find((run) => run.id === retainedRunId)?.resultCacheKey;
expect(tab.batchSqlExecution?.submittedSql).toBe("SELECT 1 AS value");
const execution = store.executeCurrentSql("SELECT 2 AS value", { openInNewResultTab: true });
await vi.waitFor(() => expect(mocks.executeMulti).toHaveBeenCalledTimes(2));
expect(await store.setActiveResultRun(tabId, retainedRunId)).toBe(true);
expect(tab.batchSqlExecution?.submittedSql).toBe("SELECT 1 AS value");
pendingExecution.resolve([{ columns: ["value"], rows: [[2]], affected_rows: 0, execution_time_ms: 1 }]);
await execution;
expect(tab.resultRuns).toHaveLength(2);
expect(tab.activeResultRunId).not.toBe(retainedRunId);
expect(tab.result).toMatchObject({ rows: [[2]] });
expect(tab.batchSqlExecution?.submittedSql).toBe("SELECT 2 AS value");
expect(tab.resultRuns?.find((run) => run.id === tab.activeResultRunId)?.resultCacheKey).not.toBe(retainedRunCacheKey);
expect(await store.setActiveResultRun(tabId, retainedRunId)).toBe(true);
expect(tab.result).toMatchObject({ rows: [[1]] });
expect(tab.batchSqlExecution?.submittedSql).toBe("SELECT 1 AS value");
const newRunId = tab.resultRuns?.find((run) => run.id !== retainedRunId)?.id;
expect(newRunId).toBeTruthy();
expect(await store.setActiveResultRun(tabId, newRunId!)).toBe(true);
await vi.waitFor(() => expect(mocks.tabResultSnapshots.has(retainedRunCacheKey!)).toBe(true));
const retainedRun = tab.resultRuns?.find((run) => run.id === retainedRunId);
expect(retainedRun).toBeTruthy();
retainedRun!.result = undefined;
retainedRun!.results = undefined;
retainedRun!.batchSqlExecution = undefined;
expect(await store.setActiveResultRun(tabId, retainedRunId)).toBe(true);
expect(tab.result).toMatchObject({ rows: [[1]] });
expect(tab.batchSqlExecution?.submittedSql).toBe("SELECT 1 AS value");
});
it("restores the retained result when a new-result execution returns no result", async () => {