feat(execution): multi-database SQL execution with scoped targets
This commit is contained in:
1 parent
328a2aa107
commit
71ccb3d97d
46 files changed
+4421
-182
No files matched your search
@@ -18,6 +18,7 @@ const mocks = vi.hoisted(() => ({
|
||||
tabs: [] as QueryTab[],
|
||||
setTableMeta: vi.fn(),
|
||||
clearInvalidDataTabSort: vi.fn(),
|
||||
activeResultExecutionTarget: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("vue-i18n", () => ({
|
||||
@@ -45,6 +46,7 @@ vi.mock("@/stores/connectionStore", () => ({
|
||||
vi.mock("@/stores/queryStore", () => ({
|
||||
useQueryStore: () => ({
|
||||
executeTabSql: mocks.executeTabSql,
|
||||
activeResultExecutionTarget: mocks.activeResultExecutionTarget,
|
||||
setExecuting: mocks.setExecuting,
|
||||
updateSql: mocks.updateSql,
|
||||
tabs: mocks.tabs,
|
||||
@@ -120,6 +122,7 @@ describe("useDataGridActions", () => {
|
||||
mocks.buildTableSelectSql.mockResolvedValue("SELECT * FROM public.users LIMIT 100 OFFSET 0");
|
||||
mocks.buildSortedQuerySql.mockResolvedValue({ ok: true, sql: "SELECT sorted" });
|
||||
mocks.ensureConnected.mockResolvedValue(undefined);
|
||||
mocks.activeResultExecutionTarget.mockReturnValue(undefined);
|
||||
mocks.getColumns.mockResolvedValue([{ name: "id", data_type: "integer", is_nullable: false, column_default: null, is_primary_key: true, extra: null }]);
|
||||
mocks.listIndexes.mockResolvedValue([]);
|
||||
});
|
||||
@@ -405,4 +408,38 @@ describe("useDataGridActions", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("uses the active multi-database result target for pagination", async () => {
|
||||
const tab = {
|
||||
id: "tab-1",
|
||||
connectionId: "source-1",
|
||||
database: "source_db",
|
||||
title: "Query",
|
||||
sql: "SELECT * FROM users",
|
||||
resultBaseSql: "SELECT * FROM users",
|
||||
resultPageLimit: 100,
|
||||
resultPageOffset: 0,
|
||||
result: { columns: ["id"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 },
|
||||
mode: "query",
|
||||
isDirty: false,
|
||||
isExecuting: false,
|
||||
isExplaining: false,
|
||||
} as QueryTab;
|
||||
const target = { connectionId: "target-2", database: "reporting", schema: "audit" };
|
||||
mocks.activeResultExecutionTarget.mockReturnValue(target);
|
||||
mocks.getConfig.mockImplementation((id: string) => ({ id, db_type: "postgres" }));
|
||||
const actions = useDataGridActions(computed(() => tab));
|
||||
|
||||
await actions.onPaginate(100, 100);
|
||||
|
||||
expect(mocks.executeTabSql).toHaveBeenCalledWith(
|
||||
"tab-1",
|
||||
"SELECT * FROM users",
|
||||
expect.objectContaining({
|
||||
executionTarget: target,
|
||||
targetContext: { scope: "database", database: "reporting", schema: "audit" },
|
||||
pagination: { offset: 100, limit: 100, sessionId: undefined },
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,188 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { ref } from "vue";
|
||||
import { useMultiDbExecution } from "@/composables/useMultiDbExecution";
|
||||
|
||||
const targets = [
|
||||
{ connectionId: "conn-1", database: "app" },
|
||||
{ connectionId: "conn-2", database: "report" },
|
||||
{ connectionId: "conn-3", database: "audit" },
|
||||
] as const;
|
||||
|
||||
describe("useMultiDbExecution", () => {
|
||||
it("executes serially and continues after a target failure", async () => {
|
||||
const executionOrder: string[] = [];
|
||||
const executor = useMultiDbExecution(
|
||||
{
|
||||
executeTarget: async ({ target }) => {
|
||||
executionOrder.push(target.connectionId);
|
||||
return target.connectionId === "conn-1" ? { status: "failed", errorMessage: "boom" } : { status: "success" };
|
||||
},
|
||||
},
|
||||
{ sourceTabId: ref("source") },
|
||||
);
|
||||
|
||||
const batch = await executor.start("ALTER TABLE users ADD COLUMN active BOOLEAN", targets);
|
||||
|
||||
expect(executionOrder).toEqual(["conn-1", "conn-2", "conn-3"]);
|
||||
expect(batch?.items.map((item) => item.status)).toEqual(["failed", "success", "success"]);
|
||||
expect(batch?.status).toBe("completed");
|
||||
});
|
||||
|
||||
it("executes all targets concurrently in parallel mode", async () => {
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const started: string[] = [];
|
||||
let running = 0;
|
||||
let maxRunning = 0;
|
||||
const executor = useMultiDbExecution(
|
||||
{
|
||||
executeTarget: async ({ target }) => {
|
||||
started.push(target.connectionId);
|
||||
running += 1;
|
||||
maxRunning = Math.max(maxRunning, running);
|
||||
await gate;
|
||||
running -= 1;
|
||||
return { status: "success" as const, durationMs: 12 };
|
||||
},
|
||||
},
|
||||
{ sourceTabId: "source" },
|
||||
);
|
||||
|
||||
const run = executor.start("UPDATE users SET active = TRUE", targets, {}, "parallel");
|
||||
await Promise.resolve();
|
||||
|
||||
expect(started).toEqual(["conn-1", "conn-2", "conn-3"]);
|
||||
expect(maxRunning).toBe(3);
|
||||
|
||||
release();
|
||||
const batch = await run;
|
||||
expect(batch?.mode).toBe("parallel");
|
||||
expect(batch?.items.map((item) => item.status)).toEqual(["success", "success", "success"]);
|
||||
expect(batch?.items.every((item) => item.durationMs === 12)).toBe(true);
|
||||
expect(batch?.durationMs).toEqual(expect.any(Number));
|
||||
});
|
||||
|
||||
it("takes a target snapshot before execution starts", async () => {
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const executor = useMultiDbExecution(
|
||||
{
|
||||
executeTarget: async () => {
|
||||
await gate;
|
||||
return { status: "success" };
|
||||
},
|
||||
},
|
||||
{ sourceTabId: "source" },
|
||||
);
|
||||
const mutableTargets = targets.map((target) => ({ ...target }));
|
||||
const run = executor.start("SELECT 1", mutableTargets);
|
||||
await Promise.resolve();
|
||||
|
||||
mutableTargets[0].database = "changed-after-confirmation";
|
||||
expect(executor.batch.value?.items[0].target.database).toBe("app");
|
||||
|
||||
release();
|
||||
await run;
|
||||
});
|
||||
|
||||
it("keeps the source offset in the immutable batch context", async () => {
|
||||
let receivedOffset: number | undefined;
|
||||
const executor = useMultiDbExecution(
|
||||
{
|
||||
executeTarget: async ({ context }) => {
|
||||
receivedOffset = context.sourceOffset;
|
||||
return { status: "success" };
|
||||
},
|
||||
},
|
||||
{ sourceTabId: "source" },
|
||||
);
|
||||
|
||||
await executor.start("SELECT 1", [targets[0]], { sourceOffset: 17 });
|
||||
|
||||
expect(receivedOffset).toBe(17);
|
||||
expect(Object.isFrozen(executor.batch.value?.context)).toBe(true);
|
||||
});
|
||||
|
||||
it("cancels the current target and marks remaining targets as not executed", async () => {
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const cancelTarget = vi.fn(async () => {
|
||||
release();
|
||||
});
|
||||
const executor = useMultiDbExecution(
|
||||
{
|
||||
executeTarget: async () => {
|
||||
await gate;
|
||||
return { status: "cancelled" };
|
||||
},
|
||||
cancelTarget,
|
||||
},
|
||||
{ sourceTabId: "source" },
|
||||
);
|
||||
|
||||
const run = executor.start("DROP TABLE users", targets);
|
||||
await Promise.resolve();
|
||||
await executor.cancel();
|
||||
await run;
|
||||
|
||||
expect(cancelTarget).toHaveBeenCalledWith("source", expect.any(String));
|
||||
expect(executor.batch.value?.items.map((item) => item.status)).toEqual(["cancelled", "not_executed", "not_executed"]);
|
||||
expect(executor.batch.value?.status).toBe("cancelled");
|
||||
});
|
||||
|
||||
it("enters cancelling before waiting for the current target to settle", async () => {
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const executor = useMultiDbExecution(
|
||||
{
|
||||
executeTarget: async () => {
|
||||
await gate;
|
||||
return { status: "cancelled" };
|
||||
},
|
||||
cancelTarget: async () => release(),
|
||||
},
|
||||
{ sourceTabId: "source" },
|
||||
);
|
||||
|
||||
const run = executor.start("DROP TABLE users", targets);
|
||||
await Promise.resolve();
|
||||
const cancel = executor.cancel();
|
||||
expect(executor.batch.value?.status).toBe("cancelling");
|
||||
await cancel;
|
||||
await run;
|
||||
expect(executor.batch.value?.status).toBe("cancelled");
|
||||
});
|
||||
|
||||
it("does not create a target tab when cancellation arrives during validation", async () => {
|
||||
let releaseValidation!: () => void;
|
||||
const validationFinished = new Promise<void>((resolve) => {
|
||||
releaseValidation = resolve;
|
||||
});
|
||||
const executor = useMultiDbExecution(
|
||||
{
|
||||
validateTarget: async () => {
|
||||
await validationFinished;
|
||||
return { valid: true };
|
||||
},
|
||||
executeTarget: async () => ({ status: "success" as const }),
|
||||
},
|
||||
{ sourceTabId: "source" },
|
||||
);
|
||||
|
||||
const run = executor.start("ALTER TABLE users ADD COLUMN active BOOLEAN", targets);
|
||||
await Promise.resolve();
|
||||
await executor.cancel();
|
||||
releaseValidation();
|
||||
await run;
|
||||
|
||||
expect(executor.batch.value?.items.map((item) => item.status)).toEqual(["cancelled", "not_executed", "not_executed"]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user