feat(editor): merge multi-database query results into one view

This commit is contained in:
goodvecn-dev authored and GitHub committed 2026-09-23 10:36:40 +08:00
1 parent 9d07708617
commit 8b822e974b
31 files changed
+3085 -87

No files matched your search

@@ -20,4 +20,71 @@ describe("sqlExecutionDangerStore", () => {
expect(await unrelated).toBe(true);
expect(store.pending).toBeUndefined();
});
it("asks a batch once and reuses the answer for its remaining targets", async () => {
const store = useSqlExecutionDangerStore();
const first = store.requestConfirmation({ sql: "UPDATE t", kind: "sql", scopeId: "batch-1" });
expect(store.pending?.scopeId).toBe("batch-1");
store.confirm();
expect(await first).toBe(true);
// The second target of the same batch must not prompt (nor ask for the
// confirmation code) again.
const second = store.requestConfirmation({ sql: "UPDATE t", kind: "sql", scopeId: "batch-1" });
expect(store.pending).toBeUndefined();
expect(await second).toBe(true);
});
it("keeps a declined batch from prompting the rest of its targets", async () => {
const store = useSqlExecutionDangerStore();
const first = store.requestConfirmation({ sql: "DROP TABLE a", kind: "sql", scopeId: "batch-2" });
store.cancel();
expect(await first).toBe(false);
const second = store.requestConfirmation({ sql: "DROP TABLE a", kind: "sql", scopeId: "batch-2" });
expect(store.pending).toBeUndefined();
expect(await second).toBe(false);
});
it("resolves already-queued targets of the same batch with the first answer", async () => {
const store = useSqlExecutionDangerStore();
// Parallel execution can queue several targets before the operator answers.
const first = store.requestConfirmation({ sql: "UPDATE t", kind: "sql", scopeId: "batch-3" });
const second = store.requestConfirmation({ sql: "UPDATE t", kind: "sql", scopeId: "batch-3" });
const other = store.requestConfirmation({ sql: "UPDATE u", kind: "sql", scopeId: "batch-4" });
store.confirm();
expect(await first).toBe(true);
expect(await second).toBe(true);
expect(store.pending?.scopeId).toBe("batch-4");
store.cancel();
expect(await other).toBe(false);
});
it("prompts again after the batch scope is cancelled", async () => {
const store = useSqlExecutionDangerStore();
const first = store.requestConfirmation({ sql: "UPDATE t", kind: "sql", scopeId: "batch-5" });
store.confirm();
await first;
store.cancelScope("batch-5");
const again = store.requestConfirmation({ sql: "UPDATE t", kind: "sql", scopeId: "batch-5" });
expect(store.pending?.scopeId).toBe("batch-5");
store.cancel();
expect(await again).toBe(false);
});
it("keeps prompting for requests without a scope", async () => {
const store = useSqlExecutionDangerStore();
const first = store.requestConfirmation({ sql: "DROP TABLE a", kind: "sql" });
store.confirm();
expect(await first).toBe(true);
const second = store.requestConfirmation({ sql: "DROP TABLE a", kind: "sql" });
expect(store.pending).toBeDefined();
store.cancel();
expect(await second).toBe(false);
});
});
@@ -8,6 +8,12 @@ export interface SqlExecutionDangerRequest {
connectionName?: string;
database?: string;
targetLabel?: string;
/**
* Every data source the pending batch will touch. Shown in the confirmation
* so the operator confirms the whole fan-out, not just the target that
* happened to reach the prompt first.
*/
targets?: string[];
databaseType?: DatabaseType;
/** Identifies a cancellable execution batch without coupling this store to its orchestrator. */
scopeId?: string;
@@ -18,13 +24,41 @@ interface QueuedDangerRequest {
resolve: (confirmed: boolean) => void;
}
/**
* A batch asks once per target, but the operator answers once per batch: the
* decision is remembered per `scopeId` so the remaining targets reuse it
* instead of re-prompting (and re-asking for the confirmation code).
*/
const MAX_REMEMBERED_SCOPES = 50;
export const useSqlExecutionDangerStore = defineStore("sqlExecutionDanger", () => {
const pending = ref<SqlExecutionDangerRequest>();
const queue: QueuedDangerRequest[] = [];
const scopeOutcome = new Map<string, boolean>();
let resolvePending: ((confirmed: boolean) => void) | undefined;
function rememberScopeOutcome(scopeId: string | undefined, confirmed: boolean): void {
if (!scopeId) return;
scopeOutcome.delete(scopeId);
scopeOutcome.set(scopeId, confirmed);
while (scopeOutcome.size > MAX_REMEMBERED_SCOPES) {
const oldest = scopeOutcome.keys().next().value;
if (oldest === undefined) break;
scopeOutcome.delete(oldest);
}
}
function rememberedOutcome(request: SqlExecutionDangerRequest): boolean | undefined {
return request.scopeId ? scopeOutcome.get(request.scopeId) : undefined;
}
function requestConfirmation(request: SqlExecutionDangerRequest): Promise<boolean> {
return new Promise<boolean>((resolve) => {
const remembered = rememberedOutcome(request);
if (remembered !== undefined) {
resolve(remembered);
return;
}
if (pending.value) {
queue.push({ request, resolve });
return;
@@ -38,13 +72,28 @@ export const useSqlExecutionDangerStore = defineStore("sqlExecutionDanger", () =
resolvePending = resolve;
}
/** Promotes queued requests, resolving the ones whose batch is already decided. */
function promoteNext(): void {
while (queue.length > 0) {
const next = queue.shift()!;
const remembered = rememberedOutcome(next.request);
if (remembered !== undefined) {
next.resolve(remembered);
continue;
}
begin(next.request, next.resolve);
return;
}
}
function settle(confirmed: boolean): void {
const request = pending.value;
const resolve = resolvePending;
resolvePending = undefined;
pending.value = undefined;
rememberScopeOutcome(request?.scopeId, confirmed);
resolve?.(confirmed);
const next = queue.shift();
if (next) begin(next.request, next.resolve);
promoteNext();
}
function confirm(): void {
@@ -72,6 +121,8 @@ export const useSqlExecutionDangerStore = defineStore("sqlExecutionDanger", () =
queue.splice(0, queue.length, ...retained);
cancelled.forEach((entry) => entry.resolve(false));
if (pending.value?.scopeId === scopeId) settle(false);
// A cancelled batch must prompt again if the same scope id is ever reused.
scopeOutcome.delete(scopeId);
}
return { pending, requestConfirmation, confirm, cancel, cancelAll, cancelScope };