From b6874ff76d25b22cedfc8d33779053623e9ff130 Mon Sep 17 00:00:00 2001 From: Ali Waseem Date: Tue, 30 Dec 2025 09:29:38 -0700 Subject: [PATCH] fix: update to rollback and changes with insert,update or delete on pretty explai (#41651) update to rollback and changes with insert,update or delete --- .../components/interfaces/SQLEditor/SQLEditor.tsx | 9 ++++++++- apps/studio/data/sql/utils/transaction.ts | 12 +++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx index c58b1f93a4b..80897fab857 100644 --- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx +++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx @@ -20,6 +20,7 @@ import { constructHeaders, isValidConnString } from 'data/fetchers' import { lintKeys } from 'data/lint/keys' import { useReadReplicasQuery } from 'data/read-replicas/replicas-query' import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation' +import { wrapWithRollback } from 'data/sql/utils/transaction' import { useSendEventMutation } from 'data/telemetry/send-event-mutation' import { isError } from 'data/utils/error-check' import { useOrgAiOptInLevel } from 'hooks/misc/useOrgOptedIntoAi' @@ -409,10 +410,16 @@ export const SQLEditor = () => { // Wrap the query with EXPLAIN ANALYZE only if it's not already an EXPLAIN query const explainSql = isExplainSql(sql) ? sql : `EXPLAIN ANALYZE ${sql}` + // Wrap EXPLAIN queries in a transaction with rollback to prevent data modifications + // This ensures EXPLAIN ANALYZE INSERT/UPDATE/DELETE queries don't actually modify data + const explainSqlWithTransaction = wrapWithRollback( + wrapWithRoleImpersonation(explainSql, impersonatedRoleState) + ) + executeExplain({ projectRef: project.ref, connectionString: connectionString, - sql: wrapWithRoleImpersonation(explainSql, impersonatedRoleState), + sql: explainSqlWithTransaction, isRoleImpersonationEnabled: isRoleImpersonationEnabled(impersonatedRoleState.role), handleError: (error) => { throw error diff --git a/apps/studio/data/sql/utils/transaction.ts b/apps/studio/data/sql/utils/transaction.ts index 36fa24b2571..e1e716da170 100644 --- a/apps/studio/data/sql/utils/transaction.ts +++ b/apps/studio/data/sql/utils/transaction.ts @@ -1,9 +1,19 @@ export function wrapWithTransaction(sql: string) { return /* SQL */ ` begin; - + ${sql} commit; ` } + +export function wrapWithRollback(sql: string) { + return /* SQL */ ` + begin; + + ${sql} + + rollback; + ` +}