feat(transactions): add manual transactions to more execution paths
This commit is contained in:
1 parent
88ba58a79c
commit
a924285136
38 files changed
+2471
-146
No files matched your search
@@ -9,6 +9,116 @@ const targets = [
|
||||
] as const;
|
||||
|
||||
describe("useMultiDbExecution", () => {
|
||||
it("retains separate transactions until each target is explicitly settled", async () => {
|
||||
const commits = targets.map(() => vi.fn().mockResolvedValue(undefined));
|
||||
const executor = useMultiDbExecution(
|
||||
{
|
||||
executeTarget: async ({ target, context }) => {
|
||||
expect(context.manualTransaction).toBe(true);
|
||||
return { status: "pending_commit", transaction: { canCommit: true, finish: commits[targets.findIndex((candidate) => candidate.connectionId === target.connectionId)] } };
|
||||
},
|
||||
},
|
||||
{ sourceTabId: "source" },
|
||||
);
|
||||
const batch = await executor.start("UPDATE t SET n = 1", targets, { manualTransaction: true });
|
||||
expect(executor.hasTransactions.value).toBe(true);
|
||||
expect(commits.every((commit) => commit.mock.calls.length === 0)).toBe(true);
|
||||
executor.reset();
|
||||
expect(executor.batch.value?.id).toBe(batch?.id);
|
||||
expect(await executor.start("SELECT 2", targets)).toBeUndefined();
|
||||
await executor.finishTransaction(batch!.items[0].id, "commit");
|
||||
await executor.finishTransaction(batch!.items[1].id, "rollback");
|
||||
expect(batch!.items.map((item) => item.status)).toEqual(["success", "rolled_back", "pending_commit"]);
|
||||
expect(commits[0]).toHaveBeenCalledWith("commit");
|
||||
expect(commits[1]).toHaveBeenCalledWith("rollback");
|
||||
expect(commits[2]).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("preserves a failed settlement for rollback and does not run it twice concurrently", async () => {
|
||||
let reject!: (reason: Error) => void;
|
||||
const finish = vi.fn(
|
||||
() =>
|
||||
new Promise<void>((_resolve, fail) => {
|
||||
reject = fail;
|
||||
}),
|
||||
);
|
||||
const executor = useMultiDbExecution({ executeTarget: async () => ({ status: "pending_commit", transaction: { canCommit: true, finish } }) }, { sourceTabId: "source" });
|
||||
const batch = await executor.start("UPDATE t SET n = 1", [targets[0]], { manualTransaction: true });
|
||||
const committing = executor.finishTransaction(batch!.items[0].id, "commit");
|
||||
expect(await executor.finishTransaction(batch!.items[0].id, "commit")).toBe(false);
|
||||
reject(new Error("connection lost"));
|
||||
expect(await committing).toBe(false);
|
||||
expect(batch!.items[0].errorMessage).toBe("connection lost");
|
||||
expect(executor.hasTransactions.value).toBe(true);
|
||||
finish.mockResolvedValueOnce(undefined);
|
||||
expect(await executor.finishTransaction(batch!.items[0].id, "rollback")).toBe(true);
|
||||
expect(finish).toHaveBeenCalledTimes(2);
|
||||
expect(executor.hasTransactions.value).toBe(false);
|
||||
});
|
||||
|
||||
it("rolls back an execution that finishes after disposal without starting the next target", async () => {
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const finish = vi.fn().mockResolvedValue(undefined);
|
||||
const executeTarget = vi.fn(async () => {
|
||||
await gate;
|
||||
return { status: "pending_commit" as const, transaction: { canCommit: true, finish } };
|
||||
});
|
||||
const executor = useMultiDbExecution({ executeTarget }, { sourceTabId: "source" });
|
||||
const run = executor.start("UPDATE t SET n = 1", targets, { manualTransaction: true });
|
||||
const disposing = executor.dispose();
|
||||
expect(finish).not.toHaveBeenCalled();
|
||||
release();
|
||||
await run;
|
||||
expect(await disposing).toBe(true);
|
||||
expect(executeTarget).toHaveBeenCalledTimes(1);
|
||||
expect(finish).toHaveBeenCalledExactlyOnceWith("rollback");
|
||||
expect(executor.batch.value!.items.map((item) => item.status)).toEqual(["rolled_back", "not_executed", "not_executed"]);
|
||||
});
|
||||
|
||||
it("waits for a commit in flight before disposing the other transactions", async () => {
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const finish = vi.fn(async (action: string) => {
|
||||
if (action === "commit") await gate;
|
||||
});
|
||||
const executor = useMultiDbExecution({ executeTarget: async () => ({ status: "pending_commit", transaction: { canCommit: true, finish } }) }, { sourceTabId: "source" });
|
||||
const batch = await executor.start("UPDATE t SET n = 1", targets.slice(0, 2), { manualTransaction: true });
|
||||
const commit = executor.finishTransaction(batch!.items[0].id, "commit");
|
||||
const disposing = executor.dispose();
|
||||
release();
|
||||
await commit;
|
||||
expect(await disposing).toBe(true);
|
||||
expect(batch!.items.map((item) => item.status)).toEqual(["success", "rolled_back"]);
|
||||
expect(finish.mock.calls.map((call) => call[0])).toEqual(["commit", "rollback"]);
|
||||
});
|
||||
|
||||
it("retains failed cleanup without allowing commit or reset", async () => {
|
||||
const finish = vi.fn().mockRejectedValue(new Error("rollback unavailable"));
|
||||
const executor = useMultiDbExecution({ executeTarget: async () => ({ status: "failed", transaction: { canCommit: false, finish } }) }, { sourceTabId: "source" });
|
||||
const batch = await executor.start("UPDATE t SET n = 1", [targets[0]], { manualTransaction: true });
|
||||
expect(await executor.finishTransaction(batch!.items[0].id, "commit")).toBe(false);
|
||||
expect(finish).not.toHaveBeenCalled();
|
||||
expect(await executor.rollbackPending()).toBe(false);
|
||||
executor.reset();
|
||||
expect(executor.hasTransactions.value).toBe(true);
|
||||
expect(batch!.items[0].errorMessage).toBe("rollback unavailable");
|
||||
});
|
||||
|
||||
it("reports an unknown commit outcome instead of marking it rolled back", async () => {
|
||||
const finish = vi.fn().mockResolvedValue("Verify data: commit result unknown");
|
||||
const executor = useMultiDbExecution({ executeTarget: async () => ({ status: "pending_commit", transaction: { canCommit: false, finish } }) }, { sourceTabId: "source" });
|
||||
const batch = await executor.start("UPDATE t SET n = 1", [targets[0]], { manualTransaction: true });
|
||||
expect(await executor.finishTransaction(batch!.items[0].id, "rollback")).toBe(false);
|
||||
expect(batch!.items[0].status).toBe("failed");
|
||||
expect(batch!.items[0].errorMessage).toBe("Verify data: commit result unknown");
|
||||
expect(executor.hasTransactions.value).toBe(false);
|
||||
});
|
||||
|
||||
it("executes serially and continues after a target failure", async () => {
|
||||
const executionOrder: string[] = [];
|
||||
const executor = useMultiDbExecution(
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
import { computed, ref } from "vue";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { useSqlExecution } from "../useSqlExecution";
|
||||
import { useQueryStore } from "@/stores/queryStore";
|
||||
import { useHistoryStore } from "@/stores/historyStore";
|
||||
import * as api from "@/lib/backend/api";
|
||||
import type { ConnectionConfig, QueryTab } from "@/types/database";
|
||||
|
||||
vi.mock("vue-i18n", () => ({ createI18n: () => ({ global: { locale: { value: "en" }, setLocaleMessage: vi.fn() } }), useI18n: () => ({ t: (key: string) => key }) }));
|
||||
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: vi.fn() }) }));
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
saveEditorSettings: vi.fn(),
|
||||
saveHistory: vi.fn(),
|
||||
unlockConnectionWrites: vi.fn(),
|
||||
lockConnectionWrites: vi.fn(),
|
||||
connectionWriteUnlockState: vi.fn().mockResolvedValue(0),
|
||||
beginManualTransaction: vi.fn(),
|
||||
executeInManualTransaction: vi.fn(),
|
||||
commitManualTransaction: vi.fn(),
|
||||
rollbackManualTransaction: vi.fn(),
|
||||
}));
|
||||
|
||||
function setup() {
|
||||
const connection = { id: "target-connection", name: "Test", db_type: "oceanbase-oracle", host: "localhost", port: 2881, username: "test", password: "" } as ConnectionConfig;
|
||||
const tab = { id: "source", connectionId: "source-connection", database: "source_db", sql: "INSERT INTO t VALUES (1)", title: "SQL", mode: "query", isDirty: false, isExecuting: false, isCancelling: false, isExplaining: false } as QueryTab;
|
||||
const store = useQueryStore();
|
||||
store.tabs.push(tab);
|
||||
const ordinary = vi.spyOn(store, "executeTabSql");
|
||||
const cleanup = vi.spyOn(store, "removeMultiDbExecutionWorker").mockResolvedValue(undefined);
|
||||
const capture = vi.spyOn(store, "captureMultiDbExecutionWorkerResult").mockReturnValue("result-run");
|
||||
const history = vi.spyOn(useHistoryStore(), "add").mockResolvedValue(undefined);
|
||||
const execution = useSqlExecution({ activeTab: computed(() => tab), activeConnection: computed(() => connection), executableSql: computed(() => tab.sql), activeOutputView: ref("result"), requestDangerConfirmation: async () => true });
|
||||
const input = {
|
||||
tab,
|
||||
connection,
|
||||
sql: tab.sql,
|
||||
scopeId: "batch",
|
||||
executionTarget: { connectionId: connection.id, database: "target_db", schema: "target_schema" },
|
||||
resultRun: { batchId: "batch", title: "Target", target: { connectionId: connection.id, database: "target_db" } },
|
||||
manualTransaction: true,
|
||||
};
|
||||
return { execution, input, ordinary, cleanup, capture, history };
|
||||
}
|
||||
|
||||
describe("manual multi-database submission", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.stubGlobal("localStorage", { getItem: () => null, setItem: vi.fn(), removeItem: vi.fn() });
|
||||
setActivePinia(createPinia());
|
||||
vi.mocked(api.beginManualTransaction).mockResolvedValue("session");
|
||||
vi.mocked(api.executeInManualTransaction).mockResolvedValue([{ columns: [], rows: [], affected_rows: 1, execution_time_ms: 1 }]);
|
||||
vi.mocked(api.commitManualTransaction).mockResolvedValue({ columns: [], rows: [], affected_rows: 0, execution_time_ms: 0 });
|
||||
vi.mocked(api.rollbackManualTransaction).mockResolvedValue({ columns: [], rows: [], affected_rows: 0, execution_time_ms: 0 });
|
||||
});
|
||||
|
||||
it("uses the captured target and records success only after an explicit commit", async () => {
|
||||
const { execution, input, ordinary, cleanup, capture, history } = setup();
|
||||
input.tab.resultCountSql = "SELECT COUNT(*) FROM unrelated_source";
|
||||
input.tab.resultPageSql = "SELECT * FROM unrelated_source";
|
||||
const result = await execution.executeTargetSql(input);
|
||||
expect(api.beginManualTransaction).toHaveBeenCalledExactlyOnceWith("target-connection", "target_db", "target_schema", undefined);
|
||||
expect(api.executeInManualTransaction).toHaveBeenCalledExactlyOnceWith("session", input.sql, "target_db", "target_schema", 100000);
|
||||
expect(ordinary).not.toHaveBeenCalled();
|
||||
expect(result.status).toBe("pending_commit");
|
||||
expect(capture.mock.calls[0][3].status).toBe("pending_commit");
|
||||
const worker = useQueryStore().getExecutionTab(capture.mock.calls[0][1]);
|
||||
expect(worker?.resultCountSql).toBeUndefined();
|
||||
expect(worker?.resultPageSql).toBeUndefined();
|
||||
expect(worker?.resultBaseSql).toBe(input.sql);
|
||||
expect(input.tab.resultCountSql).toBe("SELECT COUNT(*) FROM unrelated_source");
|
||||
expect(history).not.toHaveBeenCalled();
|
||||
expect(cleanup).not.toHaveBeenCalled();
|
||||
await result.transaction!.finish("commit");
|
||||
expect(api.commitManualTransaction).toHaveBeenCalledExactlyOnceWith("session");
|
||||
expect(history).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ connection_id: "target-connection", database: "target_db", success: true }));
|
||||
expect(cleanup).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("rolls back pending changes without writing a successful history entry", async () => {
|
||||
const { execution, input, history, cleanup } = setup();
|
||||
const result = await execution.executeTargetSql(input);
|
||||
await result.transaction!.finish("rollback");
|
||||
expect(api.rollbackManualTransaction).toHaveBeenCalledExactlyOnceWith("session");
|
||||
expect(api.commitManualTransaction).not.toHaveBeenCalled();
|
||||
expect(history).not.toHaveBeenCalled();
|
||||
expect(cleanup).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not replay a failed statement or an expired session", async () => {
|
||||
const { execution, input, ordinary } = setup();
|
||||
vi.mocked(api.executeInManualTransaction).mockRejectedValueOnce(new Error("Transaction session not found"));
|
||||
vi.mocked(api.rollbackManualTransaction).mockRejectedValueOnce(new Error("Transaction session not found"));
|
||||
const result = await execution.executeTargetSql(input);
|
||||
expect(result.status).toBe("failed");
|
||||
expect(result.transaction).toBeUndefined();
|
||||
expect(api.beginManualTransaction).toHaveBeenCalledTimes(1);
|
||||
expect(api.executeInManualTransaction).toHaveBeenCalledTimes(1);
|
||||
expect(ordinary).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("cleans up a late session if cancellation arrives while begin is pending", async () => {
|
||||
const { execution, input } = setup();
|
||||
let cancelled = false;
|
||||
let release!: (session: string) => void;
|
||||
vi.mocked(api.beginManualTransaction).mockImplementationOnce(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
release = resolve;
|
||||
}),
|
||||
);
|
||||
const running = execution.executeTargetSql({ ...input, isCancellationRequested: () => cancelled });
|
||||
await vi.waitFor(() => expect(api.beginManualTransaction).toHaveBeenCalled());
|
||||
cancelled = true;
|
||||
release("late-session");
|
||||
const result = await running;
|
||||
expect(result.status).toBe("cancelled");
|
||||
expect(api.executeInManualTransaction).not.toHaveBeenCalled();
|
||||
expect(api.rollbackManualTransaction).toHaveBeenCalledExactlyOnceWith("late-session");
|
||||
});
|
||||
|
||||
it("retains failed rollback for cleanup and disallows commit", async () => {
|
||||
const { execution, input, cleanup } = setup();
|
||||
vi.mocked(api.executeInManualTransaction).mockRejectedValueOnce(new Error("statement failed"));
|
||||
vi.mocked(api.rollbackManualTransaction).mockRejectedValueOnce(new Error("transport unavailable"));
|
||||
const result = await execution.executeTargetSql(input);
|
||||
expect(result.status).toBe("failed");
|
||||
expect(result.transaction?.canCommit).toBe(false);
|
||||
expect(cleanup).not.toHaveBeenCalled();
|
||||
await result.transaction!.finish("rollback");
|
||||
expect(cleanup).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not retry an uncertain commit or report a success history entry", async () => {
|
||||
const { execution, input, history } = setup();
|
||||
const result = await execution.executeTargetSql(input);
|
||||
vi.mocked(api.commitManualTransaction).mockRejectedValueOnce(new Error("transport unavailable"));
|
||||
await expect(result.transaction!.finish("commit")).rejects.toThrow("transport unavailable");
|
||||
await expect(result.transaction!.finish("commit")).rejects.toThrow("multiDbExecute.manualCommitUnavailable");
|
||||
expect(api.commitManualTransaction).toHaveBeenCalledTimes(1);
|
||||
expect(history).not.toHaveBeenCalled();
|
||||
await result.transaction!.finish("rollback");
|
||||
});
|
||||
|
||||
it("does not turn a completed commit into a pending transaction when history fails", async () => {
|
||||
const { execution, input, history, cleanup } = setup();
|
||||
history.mockRejectedValueOnce(new Error("history unavailable"));
|
||||
const result = await execution.executeTargetSql(input);
|
||||
await expect(result.transaction!.finish("commit")).resolves.toBeUndefined();
|
||||
await result.transaction!.finish("commit");
|
||||
expect(api.commitManualTransaction).toHaveBeenCalledTimes(1);
|
||||
expect(cleanup).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not claim rollback after a lost commit response and a missing session", async () => {
|
||||
const { execution, input, cleanup, history } = setup();
|
||||
const result = await execution.executeTargetSql(input);
|
||||
vi.mocked(api.commitManualTransaction).mockRejectedValueOnce(new Error("response lost"));
|
||||
await expect(result.transaction!.finish("commit")).rejects.toThrow("response lost");
|
||||
vi.mocked(api.rollbackManualTransaction).mockRejectedValueOnce(new Error("Transaction session not found"));
|
||||
expect(await result.transaction!.finish("rollback")).toBe("toolbar.commitOutcomeUnknown");
|
||||
expect(cleanup).toHaveBeenCalledTimes(1);
|
||||
expect(history).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("stops later statements in the same target after cancellation", async () => {
|
||||
const { execution, input } = setup();
|
||||
let cancelled = false;
|
||||
vi.mocked(api.executeInManualTransaction).mockImplementationOnce(async () => {
|
||||
cancelled = true;
|
||||
return [{ columns: [], rows: [], affected_rows: 1, execution_time_ms: 1 }];
|
||||
});
|
||||
const result = await execution.executeTargetSql({ ...input, sql: "INSERT INTO t VALUES (1); INSERT INTO t VALUES (2);", isCancellationRequested: () => cancelled });
|
||||
expect(result.status).toBe("cancelled");
|
||||
expect(api.executeInManualTransaction).toHaveBeenCalledExactlyOnceWith("session", "INSERT INTO t VALUES (1)", "target_db", "target_schema", 100000);
|
||||
expect(api.rollbackManualTransaction).toHaveBeenCalledExactlyOnceWith("session");
|
||||
});
|
||||
|
||||
it("keeps an Oracle procedural block intact while submitting subsequent statements on the same session", async () => {
|
||||
const { execution, input } = setup();
|
||||
const block = "BEGIN INSERT INTO t VALUES (1); INSERT INTO t VALUES (2); END;";
|
||||
const result = await execution.executeTargetSql({ ...input, sql: `${block}\nINSERT INTO t VALUES (3);` });
|
||||
expect(result.status).toBe("pending_commit");
|
||||
expect(api.executeInManualTransaction.mock.calls.map((call) => [call[0], call[1]])).toEqual([
|
||||
["session", block],
|
||||
["session", "INSERT INTO t VALUES (3)"],
|
||||
]);
|
||||
await result.transaction!.finish("rollback");
|
||||
});
|
||||
|
||||
it("preserves each submitted statement and editor range for the result summary", async () => {
|
||||
const { execution, input, capture } = setup();
|
||||
const first = "INSERT INTO t VALUES (1)";
|
||||
const second = "INSERT INTO t VALUES (2)";
|
||||
const result = await execution.executeTargetSql({ ...input, sql: `${first};\n${second};`, sourceOffset: 10 });
|
||||
const worker = useQueryStore().getExecutionTab(capture.mock.calls[0][1]);
|
||||
expect(worker?.results).toEqual([
|
||||
expect.objectContaining({ statement_index: 0, sourceStatement: first, sourceFrom: 10, sourceTo: 10 + first.length }),
|
||||
expect.objectContaining({ statement_index: 1, sourceStatement: second, sourceFrom: 12 + first.length, sourceTo: 12 + first.length + second.length }),
|
||||
]);
|
||||
await result.transaction!.finish("rollback");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user