Skip to content

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

feat: remove comments from sql before checking for destructive statement

kishanhitkb9696c3cc33f
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts b/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts
index 56674169a89..4c020c819c5 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts
@@ -1256,5 +1256,4 @@ export const sqlAiDisclaimerComment = stripIndent`
export const untitledSnippetTitle = 'Untitled query'
-// TODO: Update this to exclude commented out code
export const destructiveSqlRegex = [/(drop|delete|truncate)\s/i]
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
index 9757e2566c8..082a544b246 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
@@ -36,7 +36,7 @@ import {
useStore,
} from 'hooks'
import { IS_PLATFORM } from 'lib/constants'
-import { uuidv4 } from 'lib/helpers'
+import { removeCommentsFromSql, uuidv4 } from 'lib/helpers'
import { useProfile } from 'lib/profile'
import Telemetry from 'lib/telemetry'
import { getSqlEditorStateSnapshot, useSqlEditorStateSnapshot } from 'state/sql-editor'
@@ -220,10 +220,14 @@ const SQLEditor = () => {
const sql = snippet
? (selectedValue || editorRef.current?.getValue()) ?? snippet.snippet.content.sql
: selectedValue || editorRef.current?.getValue()
+ console.log('sql', sql)
- const containsDestructiveOperations = destructiveSqlRegex.some((regex) => regex.test(sql))
+ const containsDestructiveOperations = destructiveSqlRegex.some((regex) =>
+ regex.test(removeCommentsFromSql(sql))
+ )
if (!force && containsDestructiveOperations) {
+ console.log('Regex match')
setIsConfirmModalOpen(true)
return
}
diff --git a/studio/lib/helpers.ts b/studio/lib/helpers.ts
index 71246f591b5..9b480438bc0 100644
--- a/studio/lib/helpers.ts
+++ b/studio/lib/helpers.ts
@@ -266,3 +266,16 @@ export const isValidHttpUrl = (value: string) => {
}
return url.protocol === 'http:' || url.protocol === 'https:'
}
+
+/**
+ * Helper function to remove comments from SQL.
+ */
+export const removeCommentsFromSql = (sql: string) => {
+ // Removing single-line comments:
+ let cleanedSql = sql.replace(/--.*$/gm, '')
+
+ // Removing multi-line comments:
+ cleanedSql = cleanedSql.replace(/\/\*[\s\S]*?\*\//gm, '')
+
+ return cleanedSql
+}