mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
## Context Just some clean up as I was going through stuff - `useExecuteSqlQuery` is deprecated and not used at all - As such `execute-sql-query` is technically irrelevant, the more relevant file is `execute-sql-mutation` - Hence opting to consolidate `execute-sql-query` into `execute-sql-mutation` - Also removing `ExecuteSqlError` since its just re-exporting the `ResponseError` type There's a lot of file changes but its essentially just updating the importing statements across the files
66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
import { literal, safeSql } from '@supabase/pg-meta/src/pg-format'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { databaseQueuesKeys } from './keys'
|
|
import { isQueueNameValid } from '@/components/interfaces/Integrations/Queues/Queues.utils'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabaseQueueDeleteVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
queueName: string
|
|
}
|
|
|
|
export async function deleteDatabaseQueue({
|
|
projectRef,
|
|
connectionString,
|
|
queueName,
|
|
}: DatabaseQueueDeleteVariables) {
|
|
if (!isQueueNameValid(queueName)) {
|
|
throw new Error(
|
|
'Invalid queue name: must contain only alphanumeric characters, underscores, and hyphens'
|
|
)
|
|
}
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql: safeSql`select * from pgmq.drop_queue(${literal(queueName)});`,
|
|
queryKey: databaseQueuesKeys.delete(queueName),
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseQueueDeleteData = Awaited<ReturnType<typeof deleteDatabaseQueue>>
|
|
|
|
export const useDatabaseQueueDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabaseQueueDeleteData, ResponseError, DatabaseQueueDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseQueueDeleteData, ResponseError, DatabaseQueueDeleteVariables>({
|
|
mutationFn: (vars) => deleteDatabaseQueue(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseQueuesKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete database queue: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|