fix(sidebar): keep destructive table operations cancellable
This commit is contained in:
1 parent
dd77817c7e
commit
89e0f1734b
20 files changed
+627
-25
No files matched your search
@@ -6,8 +6,14 @@ import type { DatabaseType, TreeNode } from "@/types/database";
|
||||
import { supportsTableTruncate } from "@/lib/database/databaseCapabilities";
|
||||
import { buildDropTableSql, buildEmptyTableSql, buildMysqlAutoIncrementSql, buildTruncateTableSql, supportsDropTableCascade, supportsNativeMysqlAutoIncrement, supportsTruncateTableCascade, type MysqlAutoIncrementSqlOptions, type TableAdminSqlOptions } from "@/lib/database/dbAdminSql";
|
||||
import { isSqlServerLinkedNode } from "@/lib/database/sqlServerLinkedServers";
|
||||
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 {
|
||||
sidebarDangerTarget,
|
||||
sidebarDangerRunningExecutionId,
|
||||
sidebarDangerRunningCancel,
|
||||
showDropTableConfirm,
|
||||
showEmptyTableConfirm,
|
||||
showMysqlAutoIncrementConfirm,
|
||||
@@ -28,7 +34,7 @@ interface SidebarTableMutationRuntimeOptions {
|
||||
connectionStore: ReturnType<typeof useConnectionStore>;
|
||||
currentDatabaseType: () => DatabaseType | undefined;
|
||||
databaseTypeForNode: (node: TreeNode) => DatabaseType | undefined;
|
||||
executeWithProductionGuard: (node: Pick<TreeNode, "connectionId" | "database" | "schema">, sql: string, options?: { database?: string; schema?: string }) => Promise<unknown>;
|
||||
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>;
|
||||
}
|
||||
@@ -151,19 +157,134 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
|
||||
await connectionStore.refreshObjectListTreeNode(node.connectionId, node.database, node.schema);
|
||||
}
|
||||
|
||||
// Bounded, undelayed retry: register_task on the backend runs as the very
|
||||
// first statement of the execute_query command, so if cancelQuery races a
|
||||
// just-dispatched execution, a couple of retries are enough for the
|
||||
// registration to land — no artificial delay needed.
|
||||
const CANCEL_QUERY_MAX_ATTEMPTS = 5;
|
||||
// Overall cap on the whole retry sequence, mirroring queryStore.ts's
|
||||
// withCancelQueryTimeout/CANCEL_QUERY_TIMEOUT_MS for the main editor. If
|
||||
// api.cancelQuery itself hangs (e.g. a wedged connection), the retry loop
|
||||
// alone would never settle, which would permanently disable every future
|
||||
// danger dialog's Cancel Query button (sidebarDangerDialogCancelling is a
|
||||
// shared singleton — see ConnectionTree.vue).
|
||||
const CANCEL_QUERY_OVERALL_TIMEOUT_MS = 10_000;
|
||||
|
||||
async function confirmCancelWithRetry(executionId: string): Promise<boolean> {
|
||||
for (let attempt = 0; attempt < CANCEL_QUERY_MAX_ATTEMPTS; attempt++) {
|
||||
let confirmed = false;
|
||||
try {
|
||||
confirmed = await api.cancelQuery(executionId);
|
||||
} catch {
|
||||
confirmed = false;
|
||||
}
|
||||
if (confirmed) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function confirmCancelWithRetryAndTimeout(executionId: string): Promise<boolean> {
|
||||
const timeout = new Promise<boolean>((resolve) => {
|
||||
setTimeout(() => resolve(false), CANCEL_QUERY_OVERALL_TIMEOUT_MS);
|
||||
});
|
||||
return Promise.race([confirmCancelWithRetry(executionId), timeout]);
|
||||
}
|
||||
|
||||
const DANGER_OPERATION_CANCELLED_BEFORE_DISPATCH_MESSAGE = "Operation cancelled before it was sent to the database.";
|
||||
|
||||
interface DangerRunningExecution {
|
||||
executionId: string;
|
||||
isCancelledBeforeDispatch: () => boolean;
|
||||
markDispatched: () => void;
|
||||
wasCancelled: () => boolean;
|
||||
cancelConfirmed: () => boolean;
|
||||
markHandedOff: () => void;
|
||||
}
|
||||
|
||||
function beginDangerRunningExecution(nodeLabel: string): DangerRunningExecution {
|
||||
const executionId = uuid();
|
||||
let dispatched = false;
|
||||
let cancelledByUser = false;
|
||||
let cancelConfirmedFlag = false;
|
||||
// Set once the owning confirm*Table() call has already given up waiting
|
||||
// (a client-observed timeout) and deferred final cleanup to a later
|
||||
// confirmed cancel — see markHandedOff below.
|
||||
let handedOff = false;
|
||||
|
||||
sidebarDangerRunningExecutionId.value = executionId;
|
||||
sidebarDangerRunningCancel.value = async () => {
|
||||
cancelledByUser = true;
|
||||
// Nothing has reached the backend yet: the pending dispatch will see
|
||||
// isCancelledBeforeDispatch() and skip the network call entirely, so
|
||||
// this is already a confirmed cancellation.
|
||||
const confirmed = dispatched ? await confirmCancelWithRetryAndTimeout(executionId) : true;
|
||||
if (confirmed) cancelConfirmedFlag = true;
|
||||
if (handedOff && confirmed && sidebarDangerRunningExecutionId.value === executionId) {
|
||||
toast(t("contextMenu.tableOperationCancelled", { name: nodeLabel }), 3000);
|
||||
endDangerRunningExecution();
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
executionId,
|
||||
isCancelledBeforeDispatch: () => cancelledByUser && !dispatched,
|
||||
markDispatched: () => {
|
||||
dispatched = true;
|
||||
},
|
||||
wasCancelled: () => cancelledByUser,
|
||||
cancelConfirmed: () => cancelConfirmedFlag,
|
||||
markHandedOff: () => {
|
||||
handedOff = true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function endDangerRunningExecution() {
|
||||
sidebarDangerRunningExecutionId.value = "";
|
||||
sidebarDangerRunningCancel.value = null;
|
||||
}
|
||||
|
||||
function toastDangerOperationError(name: string, message: string, wasCancelled: boolean, cancelConfirmed: boolean, backendError?: BackendError) {
|
||||
if (cancelConfirmed) {
|
||||
toast(t("contextMenu.tableOperationCancelled", { name }), 3000);
|
||||
} else if (wasCancelled) {
|
||||
toast(t("contextMenu.tableOperationCancelUnconfirmed", { name, message }), 8000);
|
||||
} else if (isQueryTimeoutErrorMessage(message, backendError)) {
|
||||
toast(t("contextMenu.tableOperationTimedOut", { name, message }), 8000);
|
||||
} else {
|
||||
toast(t("contextMenu.tableOperationFailed", { message }), 5000);
|
||||
}
|
||||
}
|
||||
|
||||
async function confirmDropTable() {
|
||||
const node = sidebarDangerTarget.value ?? activeNode.value;
|
||||
if (!node.connectionId || node.database == null) return;
|
||||
const { executionId, isCancelledBeforeDispatch, markDispatched, wasCancelled, cancelConfirmed, markHandedOff } = beginDangerRunningExecution(node.label);
|
||||
try {
|
||||
await connectionStore.ensureConnected(node.connectionId);
|
||||
const sql = dropTablePreviewSql.value || (await buildDropTableSql(tableAdminSqlOptionsForNode(node, { cascade: dropTableCascade.value && supportsDropTableCascade(databaseTypeForNode(node)) })));
|
||||
await options.executeWithProductionGuard(node, sql, { database: node.database, schema: node.schema });
|
||||
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) {
|
||||
// The user declined the production-safety confirmation: the SQL was
|
||||
// never sent, so this must not be reported as a successful drop.
|
||||
endDangerRunningExecution();
|
||||
return;
|
||||
}
|
||||
toast(t("contextMenu.dropTableSuccess", { name: node.label }), 3000);
|
||||
options.closeDroppedTableObjectTabsForNode(node);
|
||||
connectionStore.removeTreeNode(node.id);
|
||||
options.releaseActiveNodeReference([node.id]);
|
||||
endDangerRunningExecution();
|
||||
} catch (error: any) {
|
||||
toast(t("contextMenu.tableOperationFailed", { message: error?.message || String(error) }), 5000);
|
||||
const message = error?.message || String(error);
|
||||
const backendError = normalizeBackendError(error) ?? undefined;
|
||||
toastDangerOperationError(node.label, message, wasCancelled(), cancelConfirmed(), backendError);
|
||||
if (cancelConfirmed() || !isQueryTimeoutErrorMessage(message, backendError)) {
|
||||
endDangerRunningExecution();
|
||||
} else {
|
||||
markHandedOff();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,15 +296,31 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
|
||||
async function confirmEmptyTable() {
|
||||
const node = sidebarDangerTarget.value ?? activeNode.value;
|
||||
if (!node.connectionId || node.database == null) return;
|
||||
const { executionId, isCancelledBeforeDispatch, markDispatched, wasCancelled, cancelConfirmed, markHandedOff } = beginDangerRunningExecution(node.label);
|
||||
try {
|
||||
await connectionStore.ensureConnected(node.connectionId);
|
||||
const sql = emptyTablePreviewSql.value || (await buildEmptyTableSql(tableAdminSqlOptionsForNode(node)));
|
||||
await options.executeWithProductionGuard(node, sql, { database: node.database, schema: node.schema });
|
||||
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) {
|
||||
// The user declined the production-safety confirmation: the SQL was
|
||||
// never sent, so this must not be reported as a successful empty.
|
||||
endDangerRunningExecution();
|
||||
return;
|
||||
}
|
||||
const messageKey = databaseTypeForNode(node) === "clickhouse" ? "contextMenu.emptyTableSubmitted" : "contextMenu.emptyTableSuccess";
|
||||
toast(t(messageKey, { name: node.label }), 3000);
|
||||
await options.refreshMutatedTableDataTabsForNode(node);
|
||||
endDangerRunningExecution();
|
||||
} catch (error: any) {
|
||||
toast(t("contextMenu.tableOperationFailed", { message: error?.message || String(error) }), 5000);
|
||||
const message = error?.message || String(error);
|
||||
const backendError = normalizeBackendError(error) ?? undefined;
|
||||
toastDangerOperationError(node.label, message, wasCancelled(), cancelConfirmed(), backendError);
|
||||
if (cancelConfirmed() || !isQueryTimeoutErrorMessage(message, backendError)) {
|
||||
endDangerRunningExecution();
|
||||
} else {
|
||||
markHandedOff();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,14 +333,30 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
|
||||
async function confirmTruncateTable() {
|
||||
const node = sidebarDangerTarget.value ?? activeNode.value;
|
||||
if (!node.connectionId || node.database == null) return;
|
||||
const { executionId, isCancelledBeforeDispatch, markDispatched, wasCancelled, cancelConfirmed, markHandedOff } = beginDangerRunningExecution(node.label);
|
||||
try {
|
||||
await connectionStore.ensureConnected(node.connectionId);
|
||||
const sql = truncateTablePreviewSql.value || (await buildTruncateTableSql(tableAdminSqlOptionsForNode(node, { cascade: truncateTableCascade.value && supportsTruncateTableCascade(databaseTypeForNode(node)) })));
|
||||
await options.executeWithProductionGuard(node, sql, { database: node.database, schema: node.schema });
|
||||
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) {
|
||||
// The user declined the production-safety confirmation: the SQL was
|
||||
// never sent, so this must not be reported as a successful truncate.
|
||||
endDangerRunningExecution();
|
||||
return;
|
||||
}
|
||||
toast(t("contextMenu.truncateTableSuccess", { name: node.label }), 3000);
|
||||
await options.refreshMutatedTableDataTabsForNode(node);
|
||||
endDangerRunningExecution();
|
||||
} catch (error: any) {
|
||||
toast(t("contextMenu.tableOperationFailed", { message: error?.message || String(error) }), 5000);
|
||||
const message = error?.message || String(error);
|
||||
const backendError = normalizeBackendError(error) ?? undefined;
|
||||
toastDangerOperationError(node.label, message, wasCancelled(), cancelConfirmed(), backendError);
|
||||
if (cancelConfirmed() || !isQueryTimeoutErrorMessage(message, backendError)) {
|
||||
endDangerRunningExecution();
|
||||
} else {
|
||||
markHandedOff();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user