Files
Joshen Lim db0e6b761b Joshenlim/fe 4304 bring database connections out of feature preview (#50107)
## Context

As per PR title - we're bringing Database Connections out of feature
preview and it'll live on the dashboard by default 🙂
Also deprecating the existing Ongoing queries panel which Database
Connections now supercedes.

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

* **New Features**
* Database Connections is now available without feature-preview
activation.
* The SQL editor’s “View running queries” option now links directly to
Database Connections.

* **Bug Fixes**
* Query cancellation and session termination now refresh database
activity data.

* **Removed**
* Removed the in-editor ongoing queries panel and its termination
controls.
* Removed the Database Connections promotional banner, preview
messaging, settings, and related telemetry.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-08 17:26:24 +08:00

66 lines
1.9 KiB
TypeScript

import { getCancelQuerySQL } from '@supabase/pg-meta'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { databaseKeys } from '@/data/database/keys'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import { ResponseError, type UseCustomMutationOptions } from '@/types'
type QueryCancelVariables = {
pid: number
/** Pass the pid's last-known backend_start to guard against a reused pid matching an unrelated session */
backendStart?: string
projectRef?: string
connectionString?: string | null
}
async function cancelQuery({
pid,
backendStart,
projectRef,
connectionString,
}: QueryCancelVariables) {
const sql = getCancelQuerySQL({ pid, backendStart })
const { result } = await executeSql({
projectRef,
connectionString,
sql,
queryKey: ['cancel-query'],
})
if (backendStart !== undefined && result.length === 0) {
throw new ResponseError(
`Session (PID: ${pid}) has already changed since this list was loaded. Refresh and try again.`
)
}
return result
}
type QueryCancelData = Awaited<ReturnType<typeof cancelQuery>>
export const useQueryCancelMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<QueryCancelData, ResponseError, QueryCancelVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<QueryCancelData, ResponseError, QueryCancelVariables>({
mutationFn: (vars) => cancelQuery(vars),
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries({ queryKey: databaseKeys.databaseActivity(projectRef) })
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to cancel query: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}