Skip to content

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

chore: sql run button only runs selected sql

Alaister Young6616d3345c09
diff --git a/studio/components/interfaces/SQLEditor/MonacoEditor.tsx b/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
index 5cbfc5b4b40..e062680c8e3 100644
--- a/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
@@ -1,19 +1,21 @@
import Editor, { Monaco, OnMount } from '@monaco-editor/react'
import { timeout } from 'lib/helpers'
-import { useEffect, useRef } from 'react'
+import { MutableRefObject, useEffect, useRef } from 'react'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
+export type IStandaloneCodeEditor = Parameters<OnMount>[0]
+
export type MonacoEditorProps = {
id: string
+ editorRef: MutableRefObject<IStandaloneCodeEditor | null>
isExecuting: boolean
- executeQuery: (overrideSql?: string) => void
+ executeQuery: () => void
}
-const MonacoEditor = ({ id, isExecuting, executeQuery }: MonacoEditorProps) => {
+const MonacoEditor = ({ id, editorRef, isExecuting, executeQuery }: MonacoEditorProps) => {
const snap = useSqlEditorStateSnapshot({ sync: true })
const snippet = snap.snippets[id]
- const editorRef = useRef<any>(null)
const monacoRef = useRef<Monaco | null>(null)
const executeQueryRef = useRef(executeQuery)
executeQueryRef.current = executeQuery
@@ -22,7 +24,9 @@ const MonacoEditor = ({ id, isExecuting, executeQuery }: MonacoEditorProps) => {
if (!editorRef.current || !monacoRef.current) return
const model = editorRef.current.getModel()
- monacoRef.current.editor.setModelMarkers(model, 'owner', [])
+ if (model !== null) {
+ monacoRef.current.editor.setModelMarkers(model, 'owner', [])
+ }
}, [])
useEffect(() => {
@@ -49,13 +53,7 @@ const MonacoEditor = ({ id, isExecuting, executeQuery }: MonacoEditorProps) => {
contextMenuGroupId: 'operation',
contextMenuOrder: 0,
run: () => {
- if (isExecuting) return
-
- const selectedValue = (editorRef?.current)
- .getModel()
- .getValueInRange(editorRef?.current?.getSelection())
-
- executeQueryRef.current?.(selectedValue || editorRef?.current?.getValue())
+ executeQueryRef.current()
},
})
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
index c7a1296c987..f9e9d0c71b7 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
@@ -1,14 +1,12 @@
import { useParams } from 'common'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
-import { useCallback } from 'react'
+import { useCallback, useRef } from 'react'
import Split from 'react-split'
import { getSqlEditorStateSnapshot, useSqlEditorStateSnapshot } from 'state/sql-editor'
+import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
import useLatest from 'hooks/misc/useLatest'
-import MonacoEditor from './MonacoEditor'
+import MonacoEditor, { IStandaloneCodeEditor } from './MonacoEditor'
import UtilityPanel from './UtilityPanel/UtilityPanel'
-import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
-
-export interface SQLEditorProps {}
const SQLEditor = () => {
const { ref, id } = useParams()
@@ -38,22 +36,26 @@ const SQLEditor = () => {
if (id) snap.setSplitSizes(id, sizes)
}, [])
- const executeQuery = useCallback(
- (overrideSql?: string) => {
- // use the latest state
- const state = getSqlEditorStateSnapshot()
- const snippet = idRef.current && state.snippets[idRef.current]
+ const editorRef = useRef<IStandaloneCodeEditor | null>(null)
- if (project && snippet && !isExecuting) {
- execute?.({
- projectRef: project.ref,
- connectionString: project.connectionString,
- sql: overrideSql ?? snippet.snippet.content.sql,
- })
- }
- },
- [isExecuting, project]
- )
+ const executeQuery = useCallback(() => {
+ // use the latest state
+ const state = getSqlEditorStateSnapshot()
+ const snippet = idRef.current && state.snippets[idRef.current]
+
+ if (project && snippet && !isExecuting && editorRef.current !== null) {
+ const editor = editorRef.current
+ const selection = editor.getSelection()
+ const selectedValue = selection ? editor.getModel()?.getValueInRange(selection) : undefined
+ const overrideSql = selectedValue || editorRef.current?.getValue()
+
+ execute({
+ projectRef: project.ref,
+ connectionString: project.connectionString,
+ sql: overrideSql ?? snippet.snippet.content.sql,
+ })
+ }
+ }, [isExecuting, project])
return (
<div className="flex h-full flex-col">
@@ -72,7 +74,12 @@ const SQLEditor = () => {
{isLoading ? (
<div className="flex h-full w-full items-center justify-center">Loading...</div>
) : (
- <MonacoEditor id={id!} isExecuting={isExecuting} executeQuery={executeQuery} />
+ <MonacoEditor
+ id={id}
+ editorRef={editorRef}
+ isExecuting={isExecuting}
+ executeQuery={executeQuery}
+ />
)}
</div>
@@ -80,7 +87,7 @@ const SQLEditor = () => {
{isLoading ? (
<div className="flex h-full w-full items-center justify-center">Loading...</div>
) : (
- <UtilityPanel id={id!} isExecuting={isExecuting} executeQuery={executeQuery} />
+ <UtilityPanel id={id} isExecuting={isExecuting} executeQuery={executeQuery} />
)}
</div>
</Split>
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
index c3c92adcbdc..4523ff57eca 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
@@ -8,7 +8,7 @@ import { detectOS } from 'lib/helpers'
export type UtilityActionsProps = {
id: string
isExecuting?: boolean
- executeQuery?: (overrideSql?: string) => void
+ executeQuery: () => void
}
const UtilityActions = ({ id, isExecuting = false, executeQuery }: UtilityActionsProps) => {
@@ -20,7 +20,7 @@ const UtilityActions = ({ id, isExecuting = false, executeQuery }: UtilityAction
{IS_PLATFORM && <FavoriteButton id={id} />}
<SizeToggleButton id={id} />
<Button
- onClick={() => executeQuery?.()}
+ onClick={() => executeQuery()}
disabled={isExecuting}
loading={isExecuting}
type="default"
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
index c7f1f8c0e5b..3cd087502f9 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityPanel.tsx
@@ -5,7 +5,7 @@ import UtilityTabResults from './UtilityTabResults'
export type UtilityPanelProps = {
id: string
isExecuting?: boolean
- executeQuery?: (overrideSql?: string) => void
+ executeQuery: () => void
}
const UtilityPanel = ({ id, isExecuting, executeQuery }: UtilityPanelProps) => {