feat(execution): multi-database SQL execution with scoped targets

This commit is contained in:
miracle authored and GitHub committed 2026-08-09 14:24:10 +08:00
1 parent 328a2aa107
commit 71ccb3d97d
46 files changed
+4421 -182

No files matched your search

@@ -0,0 +1,23 @@
import { beforeEach, describe, expect, it } from "vitest";
import { createPinia, setActivePinia } from "pinia";
import { useProductionSafetyStore } from "@/stores/productionSafetyStore";
describe("productionSafetyStore scope cancellation", () => {
beforeEach(() => setActivePinia(createPinia()));
it("cancels queued requests in the scope before advancing the confirmation queue", async () => {
const store = useProductionSafetyStore();
const first = store.requestConfirmation({ sql: "UPDATE a SET value = 1", scopeId: "batch-1" });
const second = store.requestConfirmation({ sql: "UPDATE b SET value = 1", scopeId: "batch-1" });
const unrelated = store.requestConfirmation({ sql: "UPDATE c SET value = 1", scopeId: "batch-2" });
store.cancelScope("batch-1");
expect(store.pending?.scopeId).toBe("batch-2");
expect(await first).toBe(false);
expect(await second).toBe(false);
store.confirm();
expect(await unrelated).toBe(true);
expect(store.pending).toBeUndefined();
});
});
@@ -622,6 +622,31 @@ describe("queryStore multi-statement errors", () => {
expect(tab.activeResultRunId).toBeUndefined();
});
it("uses the immutable target context instead of the result tab namespace", async () => {
mocks.executeMulti.mockResolvedValue([{ columns: ["value"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 }]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("mysql-1", "source-db", "Target", "query", "source-schema", "SELECT 1", "source-catalog");
await store.executeTabSql(tabId, "SELECT 1", {
targetContext: { scope: "database", database: "target-db", schema: "target-schema" },
});
expect(mocks.executeMulti).toHaveBeenCalledWith("mysql-1", "target-db", "SELECT 1", "target-schema", expect.any(String), expect.objectContaining({ catalog: undefined }));
});
it("clears the database and schema for a connection-scoped target", async () => {
mocks.getConnectionConfig.mockReturnValue({ id: "etcd-1", name: "etcd", db_type: "etcd", database: "stale" });
mocks.executeMulti.mockResolvedValue([{ columns: ["value"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 }]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("etcd-1", "stale", "Target", "query", "stale-schema", "GET /", "stale-catalog");
await store.executeTabSql(tabId, "GET /", { targetContext: { scope: "connection" } });
expect(mocks.executeMulti).toHaveBeenCalledWith("etcd-1", "", "GET /", undefined, expect.any(String), expect.objectContaining({ catalog: undefined }));
});
it("restores the retained result when a new-result execution fails before dispatch", async () => {
mocks.executeMulti.mockResolvedValueOnce([{ columns: ["value"], rows: [[1]], affected_rows: 0, execution_time_ms: 1 }]);
const { useQueryStore } = await import("@/stores/queryStore");
@@ -0,0 +1,23 @@
import { beforeEach, describe, expect, it } from "vitest";
import { createPinia, setActivePinia } from "pinia";
import { useSqlExecutionDangerStore } from "@/stores/sqlExecutionDangerStore";
describe("sqlExecutionDangerStore", () => {
beforeEach(() => setActivePinia(createPinia()));
it("cancels all requests in a batch without promoting another request from that batch", async () => {
const store = useSqlExecutionDangerStore();
const first = store.requestConfirmation({ sql: "DROP TABLE a", kind: "sql", scopeId: "batch-1" });
const second = store.requestConfirmation({ sql: "DROP TABLE b", kind: "sql", scopeId: "batch-1" });
const unrelated = store.requestConfirmation({ sql: "DROP TABLE c", kind: "sql", scopeId: "batch-2" });
store.cancelScope("batch-1");
expect(store.pending?.scopeId).toBe("batch-2");
expect(await first).toBe(false);
expect(await second).toBe(false);
store.confirm();
expect(await unrelated).toBe(true);
expect(store.pending).toBeUndefined();
});
});
@@ -0,0 +1,76 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createPinia, setActivePinia } from "pinia";
import { useSqlExecutionTargetGroupStore } from "@/stores/sqlExecutionTargetGroupStore";
import { SQL_EXECUTION_TARGET_GROUPS_STORAGE_KEY } from "@/stores/sqlExecutionTargetGroupStore";
function installLocalStorage() {
const data = new Map<string, string>();
vi.stubGlobal("localStorage", {
getItem: (key: string) => data.get(key) ?? null,
setItem: (key: string, value: string) => data.set(key, value),
removeItem: (key: string) => data.delete(key),
});
return data;
}
const targets = [
{ connectionId: "conn-1", database: "app", schema: "public" },
{ connectionId: "conn-2", database: "app", schema: "public" },
] as const;
const resolveDatabaseType = () => "postgres" as const;
describe("sqlExecutionTargetGroupStore", () => {
beforeEach(() => {
installLocalStorage();
setActivePinia(createPinia());
});
it("deduplicates targets and persists reusable target references", () => {
const store = useSqlExecutionTargetGroupStore();
const group = store.createGroup({
name: "Production",
databaseType: "postgres",
targets: [...targets, targets[0]],
resolveDatabaseType,
});
expect(group.targets).toHaveLength(2);
expect(group.targets[0]).toEqual(targets[0]);
expect(JSON.parse(globalThis.localStorage.getItem(SQL_EXECUTION_TARGET_GROUPS_STORAGE_KEY) || "{}")).not.toHaveProperty("password");
setActivePinia(createPinia());
const reloaded = useSqlExecutionTargetGroupStore();
expect(reloaded.getGroup(group.id)?.targets).toEqual([...targets]);
});
it("rejects duplicate names case-insensitively and supports update/clone/delete", () => {
const store = useSqlExecutionTargetGroupStore();
const group = store.createGroup({ name: "Shared", databaseType: "mysql", targets: [targets[0]], resolveDatabaseType: () => "mysql" });
expect(() => store.createGroup({ name: " shared ", databaseType: "mysql", targets: [targets[1]], resolveDatabaseType: () => "mysql" })).toThrow("目标组名称已存在");
const updated = store.updateGroup(group.id, { name: "Shared App", targets: [targets[1]], resolveDatabaseType: () => "mysql" });
expect(updated?.name).toBe("Shared App");
expect(updated?.targets).toEqual([targets[1]]);
const clone = store.cloneGroup(group.id, "Shared Copy", undefined, () => "mysql");
expect(clone?.id).not.toBe(group.id);
expect(clone?.targets).toEqual([targets[1]]);
store.deleteGroup(group.id);
expect(store.getGroup(group.id)).toBeUndefined();
expect(store.getGroup(clone!.id)).toBeDefined();
});
it("rejects a mixed effective database type at the store boundary", () => {
const store = useSqlExecutionTargetGroupStore();
expect(() =>
store.createGroup({
name: "Mixed",
databaseType: "postgres",
targets,
resolveDatabaseType: (target) => (target.connectionId === "conn-1" ? "postgres" : "mysql"),
}),
).toThrow("目标组只能包含同一种数据库类型");
});
});