feat(history): record truncate, empty, and drop SQL from table actions

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-04 03:29:33 +08:00
1 parent 238cf81370
commit a9990d5fdb
7 files changed
+526 -7

No files matched your search

@@ -49,7 +49,7 @@ function tableNode(database: string | null | undefined): TreeNode {
} as TreeNode;
}
function runtime(database: string | null | undefined, config: Pick<ConnectionConfig, "db_type"> & Partial<Pick<ConnectionConfig, "driver_profile">> = { db_type: "saphana" }, databaseType: DatabaseType = "saphana") {
function runtime(database: string | null | undefined, config: Pick<ConnectionConfig, "db_type"> & Partial<Pick<ConnectionConfig, "driver_profile">> = { db_type: "saphana" }, databaseType: DatabaseType = "saphana", historyStore?: any) {
const node = tableNode(database);
const activeNode = shallowRef(node);
const connectionStore = {
@@ -66,6 +66,7 @@ function runtime(database: string | null | undefined, config: Pick<ConnectionCon
executeWithProductionGuard: mocks.executeWithProductionGuard,
closeDroppedTableObjectTabsForNode: mocks.closeDroppedTableObjectTabsForNode,
refreshMutatedTableDataTabsForNode: mocks.refreshMutatedTableDataTabsForNode,
historyStore,
});
return { feature, node };
}
@@ -456,4 +457,55 @@ describe("useSidebarTableMutationRuntime SAP HANA schema-scoped actions", () =>
expect(sidebarDangerRunningExecutionId.value).toBe("");
expect(sidebarDangerRunningCancel.value).toBeNull();
});
it.each(actions)("records executed SQL into historyStore on $action success", async ({ action, sql }) => {
const addMock = vi.fn().mockResolvedValue(undefined);
const historyStore = { add: addMock };
const { feature } = runtime("", { db_type: "saphana" }, "saphana", historyStore);
await feature[action]();
expect(addMock).toHaveBeenCalledOnce();
expect(addMock).toHaveBeenCalledWith(
expect.objectContaining({
connection_id: "hana-1",
database: "",
sql,
success: true,
target: "ORDERS",
}),
);
});
it("records failed mutation into historyStore when execution fails", async () => {
const addMock = vi.fn().mockResolvedValue(undefined);
const historyStore = { add: addMock };
const { feature } = runtime("", { db_type: "saphana" }, "saphana", historyStore);
mocks.executeWithProductionGuard.mockRejectedValueOnce(new Error("Database write error"));
await feature.confirmDropTable();
expect(addMock).toHaveBeenCalledOnce();
expect(addMock).toHaveBeenCalledWith(
expect.objectContaining({
connection_id: "hana-1",
database: "",
sql: 'DROP TABLE "APP"."ORDERS";',
success: false,
error: "Database write error",
target: "ORDERS",
}),
);
});
it("does not record into historyStore when production confirmation is declined", async () => {
const addMock = vi.fn().mockResolvedValue(undefined);
const historyStore = { add: addMock };
const { feature } = runtime("", { db_type: "saphana" }, "saphana", historyStore);
mocks.executeWithProductionGuard.mockResolvedValueOnce(undefined);
await feature.confirmDropTable();
expect(addMock).not.toHaveBeenCalled();
});
});
@@ -24,6 +24,7 @@ import { isQueryTimeoutErrorMessage } from "@/lib/sql/queryError";
import { uuid } from "@/lib/common/utils";
import * as api from "@/lib/backend/api";
import { normalizeBackendError, type BackendError } from "@/lib/backend/errorUtils";
import { recordTableMutationHistory, type TableMutationHistoryStore } from "@/lib/history/tableMutationHistory";
import {
sidebarDangerTarget,
sidebarDangerRunningExecutionId,
@@ -57,6 +58,7 @@ interface SidebarTableMutationRuntimeOptions {
executeWithProductionGuard: (node: Pick<TreeNode, "connectionId" | "database" | "schema">, sql: string, options?: { database?: string; schema?: string; executionId?: string; isCancelledBeforeDispatch?: () => boolean; markDispatched?: () => void }) => Promise<unknown>;
closeDroppedTableObjectTabsForNode: (node: TreeNode) => void;
refreshMutatedTableDataTabsForNode: (node: TreeNode) => Promise<void>;
historyStore?: TableMutationHistoryStore;
}
export function useSidebarTableMutationRuntime(options: SidebarTableMutationRuntimeOptions) {
@@ -458,9 +460,13 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
const { node, buildSql, onSuccess } = config;
if (!node.connectionId || node.database == null) return;
const { executionId, isCancelledBeforeDispatch, markDispatched, wasCancelled, cancelConfirmed, waitForCancelConfirmation, markHandedOff } = beginDangerRunningExecution(node.label);
const start = Date.now();
let executedSql = "";
let successRecorded = false;
try {
await connectionStore.ensureConnected(node.connectionId);
const sql = await buildSql();
executedSql = sql;
if (isCancelledBeforeDispatch()) throw new Error(DANGER_OPERATION_CANCELLED_BEFORE_DISPATCH_MESSAGE);
const executed = await options.executeWithProductionGuard(node, sql, { database: node.database, schema: node.schema, executionId, isCancelledBeforeDispatch, markDispatched });
if (executed === undefined) {
@@ -469,6 +475,18 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
endDangerRunningExecution();
return;
}
if (options.historyStore) {
await recordTableMutationHistory(options.historyStore, {
connectionId: node.connectionId,
connectionName: connectionStore.getConfig(node.connectionId)?.name,
database: node.database,
sql,
elapsedMs: Date.now() - start,
success: true,
target: node.label,
}).catch((err) => console.warn("[DBX] failed to record table mutation history", err));
successRecorded = true;
}
await onSuccess(node);
endDangerRunningExecution();
} catch (error: any) {
@@ -476,6 +494,18 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
const backendError = normalizeBackendError(error) ?? undefined;
const cancellationWasRequested = wasCancelled();
const cancellationWasConfirmed = cancelConfirmed() || (cancellationWasRequested && (await waitForCancelConfirmation()));
if (executedSql && !successRecorded && options.historyStore && !cancellationWasRequested && !cancellationWasConfirmed && !isCancelledBeforeDispatch()) {
await recordTableMutationHistory(options.historyStore, {
connectionId: node.connectionId,
connectionName: connectionStore.getConfig(node.connectionId)?.name,
database: node.database,
sql: executedSql,
elapsedMs: Date.now() - start,
success: false,
error: message,
target: node.label,
}).catch((err) => console.warn("[DBX] failed to record table mutation history", err));
}
toastDangerOperationError(node.label, message, cancellationWasRequested, cancellationWasConfirmed, backendError);
if (cancellationWasConfirmed || !isQueryTimeoutErrorMessage(message, backendError)) {
endDangerRunningExecution();