Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: update to rollback and changes with insert,update or delete on pretty explai (#41651)

Ali Waseemb6874ff76d25
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;
+ `
+}