Files
supabase/apps/studio/components/interfaces/SQLEditor/useSqlEditorExecution.ts
Joshen Lim 71d58cba7f Joshenlim/fe 4401 re sql editor silently points to the primary instead of (#50513)
## Context

Fixes the following 2 issues with the database selection in the SQL
Editor
- An errant `useEffect` was resetting the `selectedDatabaseId` back to
the primary every time the `databases` list from `useReadReplicasQuery`
changed reference (not just on first load).
- `QuerySourceMenu` kept showing "Read Replica" even after selection had
reverted
- Was using local storage value as the `identifier` for
`DatabaseParametersSubMenu`, when it should use the valtio store as the
source of truth

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Improvements**
- The SQL Editor now remembers the last selected database between
sessions.
- Your saved database selection is restored when available; otherwise,
the project’s primary database is selected automatically.
- Query source settings now stay synchronized with the database
currently selected in the SQL Editor.

- **Bug Fixes**
- Background database refreshes no longer unexpectedly reset your
selected read replica to the primary database.
- Database selection now waits for saved preferences to load, preventing
a brief incorrect selection.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-17 12:34:13 +00:00

167 lines
5.1 KiB
TypeScript

import { type SafeSqlFragment } from '@supabase/pg-meta'
import { useQueryClient } from '@tanstack/react-query'
import { IS_PLATFORM, useParams } from 'common'
import { useCallback, useState } from 'react'
import { toast } from 'sonner'
import type { PotentialIssues } from './SQLEditor.types'
import {
analyzeQueryIssues,
buildExecuteParams,
hasBlockingIssues,
resolveConnectionString,
shouldAutoGenerateTitle,
} from './SQLEditor.utils'
import { useSQLEditorContext } from './SQLEditorContext'
import { useDatabaseEventTriggersQuery } from '@/data/database-event-triggers/database-event-triggers-query'
import { 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 { useOrgAiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { useTrack } from '@/lib/telemetry/track'
import { useDatabaseSelectorStateSnapshot } from '@/state/database-selector'
import { useGetImpersonatedRoleState } from '@/state/role-impersonation-state'
import { useSqlEditorSessionSnapshot } from '@/state/sql-editor/sql-editor-session-state'
import { getSqlEditorV2StateSnapshot } from '@/state/sql-editor/sql-editor-state'
type UseSqlEditorExecutionArgs = {
id: string
isDiffOpen: boolean
hasSelection: boolean
setAiTitle: (id: string, sql: string) => void
}
export function useSqlEditorExecution({
id,
isDiffOpen,
hasSelection,
setAiTitle,
}: UseSqlEditorExecutionArgs) {
const { ref } = useParams()
const { editor, clearPendingRunRefocus, refocusEditorAfterRunIfNeeded } = useSQLEditorContext()
const { data: project } = useSelectedProjectQuery()
const queryClient = useQueryClient()
const track = useTrack()
const sessionSnap = useSqlEditorSessionSnapshot()
const limit = sessionSnap.limit
const databaseSelectorState = useDatabaseSelectorStateSnapshot()
const getImpersonatedRoleState = useGetImpersonatedRoleState()
const { aiOptInLevel } = useOrgAiOptInLevel()
const { data: databases } = useReadReplicasQuery(
{ projectRef: ref },
{ enabled: isValidConnString(project?.connectionString) }
)
const { data: eventTriggers } = useDatabaseEventTriggersQuery(
{ projectRef: project?.ref, connectionString: project?.connectionString },
{ enabled: isValidConnString(project?.connectionString) }
)
const [potentialIssues, setPotentialIssues] = useState<PotentialIssues>()
const { mutate: execute, isPending: isExecuting } = useExecuteSqlMutation({
onSuccess(data, vars) {
if (id) {
sessionSnap.addResult(id, data.result, vars.autoLimit)
}
// revalidate lint query
queryClient.invalidateQueries({ queryKey: lintKeys.lint(ref) })
refocusEditorAfterRunIfNeeded()
},
onError(error, vars) {
if (id) {
editor.highlightErrorLine(error, hasSelection)
sessionSnap.addResultError(id, error, vars.autoLimit)
}
refocusEditorAfterRunIfNeeded()
},
})
const executeQuery = useCallback(
async (sql: SafeSqlFragment, force: boolean = false) => {
if (isDiffOpen) {
clearPendingRunRefocus()
return
}
if (!editor.isReady() || isExecuting || project === undefined) {
clearPendingRunRefocus()
return
}
const issues = analyzeQueryIssues(sql, eventTriggers)
if (hasBlockingIssues(issues, force)) {
setPotentialIssues(issues)
return
}
// use the latest state for the title-generation check
const snippet = getSqlEditorV2StateSnapshot().snippets[id]
if (
shouldAutoGenerateTitle({
aiOptInLevel,
snippetName: snippet?.snippet.name,
isPlatform: IS_PLATFORM,
})
) {
// Intentionally don't await title gen (lazy)
setAiTitle(id, sql)
}
editor.clearHighlights()
const impersonatedRoleState = getImpersonatedRoleState()
const connectionString = resolveConnectionString(
databases,
databaseSelectorState.selectedDatabaseId
)
if (!isValidConnString(connectionString)) {
clearPendingRunRefocus()
return toast.error('Unable to run query: Connection string is missing')
}
execute({
...buildExecuteParams({
sql,
limit,
connectionString,
projectRef: project.ref,
impersonatedRoleState,
}),
handleError: (error) => {
throw error
},
})
track('sql_editor_query_run_button_clicked', { source: 'database' })
},
[
editor,
clearPendingRunRefocus,
isDiffOpen,
id,
isExecuting,
project,
aiOptInLevel,
execute,
getImpersonatedRoleState,
setAiTitle,
databaseSelectorState.selectedDatabaseId,
databases,
eventTriggers,
limit,
track,
]
)
const resetPotentialIssues = useCallback(() => setPotentialIssues(undefined), [])
return { executeQuery, isExecuting, potentialIssues, resetPotentialIssues }
}