1+ import { useParams } from 'common'
2+ import toast from 'react-hot-toast'
13import { format } from 'sql-formatter'
24import { AiIconAnimation , Button } from 'ui'
35
@@ -6,8 +8,9 @@ import { useSqlDebugMutation } from 'data/ai/sql-debug-mutation'
68import { useEntityDefinitionsQuery } from 'data/database/entity-definitions-query'
79import { useOrgSubscriptionQuery } from 'data/subscriptions/org-subscription-query'
810import { isError } from 'data/utils/error-check'
9- import { useLocalStorageQuery , useSelectedOrganization , useSelectedProject , useStore } from 'hooks'
11+ import { useLocalStorageQuery , useSelectedOrganization , useSelectedProject } from 'hooks'
1012import { IS_PLATFORM , LOCAL_STORAGE_KEYS , OPT_IN_TAGS } from 'lib/constants'
13+ import { useDatabaseSelectorStateSnapshot } from 'state/database-selector'
1114import { useSqlEditorStateSnapshot } from 'state/sql-editor'
1215import { useSqlEditor } from '../SQLEditor'
1316import { sqlAiDisclaimerComment } from '../SQLEditor.constants'
@@ -20,16 +23,17 @@ export type UtilityTabResultsProps = {
2023}
2124
2225const UtilityTabResults = ( { id, isExecuting } : UtilityTabResultsProps ) => {
23- const { ui } = useStore ( )
24- const organization = useSelectedOrganization ( )
26+ const { ref } = useParams ( )
2527 const snap = useSqlEditorStateSnapshot ( )
26- const { mutateAsync : debugSql , isLoading : isDebugSqlLoading } = useSqlDebugMutation ( )
27- const { setDebugSolution, setAiInput, setSqlDiff, sqlDiff, setSelectedDiffType } = useSqlEditor ( )
28- const selectedOrganization = useSelectedOrganization ( )
28+ const state = useDatabaseSelectorStateSnapshot ( )
2929 const selectedProject = useSelectedProject ( )
30- const isOptedInToAI = selectedOrganization ?. opt_in_tags ?. includes ( OPT_IN_TAGS . AI_SQL ) ?? false
31- const [ hasEnabledAISchema ] = useLocalStorageQuery ( LOCAL_STORAGE_KEYS . SQL_EDITOR_AI_SCHEMA , true )
30+ const organization = useSelectedOrganization ( )
31+
32+ const { sqlDiff, setDebugSolution, setAiInput, setSqlDiff, setSelectedDiffType } = useSqlEditor ( )
33+ const { mutateAsync : debugSql , isLoading : isDebugSqlLoading } = useSqlDebugMutation ( )
3234
35+ const isOptedInToAI = organization ?. opt_in_tags ?. includes ( OPT_IN_TAGS . AI_SQL ) ?? false
36+ const [ hasEnabledAISchema ] = useLocalStorageQuery ( LOCAL_STORAGE_KEYS . SQL_EDITOR_AI_SCHEMA , true )
3337 const includeSchemaMetadata = ( isOptedInToAI || ! IS_PLATFORM ) && hasEnabledAISchema
3438
3539 const { data } = useEntityDefinitionsQuery (
@@ -67,6 +71,9 @@ const UtilityTabResults = ({ id, isExecuting }: UtilityTabResultsProps) => {
6771 const formattedError = ( result . error ?. formattedError ?. split ( '\n' ) ?? [ ] ) . filter (
6872 ( x : string ) => x . length > 0
6973 )
74+ const readReplicaError =
75+ state . selectedDatabaseId !== ref &&
76+ result . error . message . includes ( 'in a read-only transaction' )
7077
7178 return (
7279 < div className = "bg-table-header-light [[data-theme*=dark]_&]:bg-table-header-dark" >
@@ -105,50 +112,67 @@ const UtilityTabResults = ({ id, isExecuting }: UtilityTabResultsProps) => {
105112 </ pre >
106113 ) )
107114 ) : (
108- < p className = "font-mono text-sm" > { result . error ?. message } </ p >
115+ < >
116+ < p className = "font-mono text-sm" > Error: { result . error ?. message } </ p >
117+ { readReplicaError && (
118+ < p className = "text-sm text-foreground-light" >
119+ Note: Read replicas are for read only queries. Run write queries on the
120+ primary database instead.
121+ </ p >
122+ ) }
123+ </ >
109124 ) }
110125 </ div >
111126 ) }
112- { ! hasHipaaAddon && (
113- < Button
114- icon = {
115- < div className = "scale-75" >
116- < AiIconAnimation className = "w-3 h-3" loading = { isDebugSqlLoading } />
117- </ div >
118- }
119- disabled = { ! ! sqlDiff || isDebugSqlLoading }
120- onClick = { async ( ) => {
121- try {
122- const { solution, sql } = await debugSql ( {
123- sql : snippet . snippet . content . sql . replace ( sqlAiDisclaimerComment , '' ) . trim ( ) ,
124- errorMessage : result . error . message ,
125- entityDefinitions,
126- } )
127-
128- const formattedSql = format ( sql , {
129- language : 'postgresql' ,
130- keywordCase : 'lower' ,
131- } )
132- setAiInput ( '' )
133- setDebugSolution ( solution )
134- setSqlDiff ( {
135- original : snippet . snippet . content . sql ,
136- modified : formattedSql ,
137- } )
138- setSelectedDiffType ( DiffType . Modification )
139- } catch ( error : unknown ) {
140- if ( isError ( error ) ) {
141- ui . setNotification ( {
142- category : 'error' ,
143- message : `Failed to debug: ${ error . message } ` ,
127+
128+ < div className = "flex items-center gap-x-2" >
129+ { readReplicaError && (
130+ < Button
131+ className = "py-2"
132+ type = "default"
133+ onClick = { ( ) => {
134+ state . setSelectedDatabaseId ( ref )
135+ snap . resetResult ( id )
136+ } }
137+ >
138+ Switch to primary database
139+ </ Button >
140+ ) }
141+ { ! hasHipaaAddon && (
142+ < Button
143+ icon = { < AiIconAnimation className = "scale-75 w-3 h-3" loading = { isDebugSqlLoading } /> }
144+ disabled = { ! ! sqlDiff || isDebugSqlLoading }
145+ onClick = { async ( ) => {
146+ try {
147+ const { solution, sql } = await debugSql ( {
148+ sql : snippet . snippet . content . sql . replace ( sqlAiDisclaimerComment , '' ) . trim ( ) ,
149+ errorMessage : result . error . message ,
150+ entityDefinitions,
144151 } )
152+
153+ const formattedSql =
154+ sqlAiDisclaimerComment +
155+ '\n\n' +
156+ format ( sql , {
157+ language : 'postgresql' ,
158+ keywordCase : 'lower' ,
159+ } )
160+ setAiInput ( '' )
161+ setDebugSolution ( solution )
162+ setSqlDiff ( {
163+ original : snippet . snippet . content . sql ,
164+ modified : formattedSql ,
165+ } )
166+ setSelectedDiffType ( DiffType . Modification )
167+ } catch ( error : unknown ) {
168+ if ( isError ( error ) ) toast . error ( `Failed to debug: ${ error . message } ` )
145169 }
146- }
147- } }
148- >
149- Debug with Supabase AI
150- </ Button >
151- ) }
170+ } }
171+ >
172+ Debug with Supabase AI
173+ </ Button >
174+ ) }
175+ </ div >
152176 </ div >
153177 </ div >
154178 )
0 commit comments