mirror of
https://github.com/supabase/supabase.git
synced 2026-05-23 01:39:34 +08:00
* fix(studio): early fail query when x-connection-encrypted is invalid * fix(studio): uniformize readDatabase and projectDetails connString handling * chore: update api types * chore: add connectionString null option * fix: only enforce x-connection-encrypted on platform * chore: refactor connString check in a single point * chore: fix guard logic * chore: fix pgMetaGuard * chore: fix types
50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import type { ResponseError } from 'types'
|
|
import { sqlKeys } from './keys'
|
|
|
|
export type QueryAbortVariables = {
|
|
pid: number
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export async function abortQuery({ pid, projectRef, connectionString }: QueryAbortVariables) {
|
|
const sql = /* SQL */ `select pg_terminate_backend(${pid})`
|
|
const { result } = await executeSql({ projectRef, connectionString, sql })
|
|
return result
|
|
}
|
|
|
|
type QueryAbortData = Awaited<ReturnType<typeof abortQuery>>
|
|
|
|
export const useQueryAbortMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<QueryAbortData, ResponseError, QueryAbortVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
return useMutation<QueryAbortData, ResponseError, QueryAbortVariables>(
|
|
(vars) => abortQuery(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries(sqlKeys.ongoingQueries(projectRef))
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to abort query: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|