mirror of
https://github.com/supabase/supabase.git
synced 2026-10-10 11:55:05 +03:00
Follow-up to #44451 which added `literal()` escaping to 4 queue message files. The remaining 5 files in the same directory still use raw string interpolation. The create mutation was the biggest gap -- no `literal()` and no `isQueueNameValid` at all. It could also interpolate `undefined` into SQL when partition config is missing. Applied the same pattern from #44451 to all 5 files: import `literal`, wrap interpolated values. For the metrics query and create mutation, also used `ident()` for table name references. ## Summary by CodeRabbit * **Refactor** * Improved internal SQL query construction for database queue operations to enhance code reliability and maintainability.
66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
import { literal } 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-query'
|
|
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: `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,
|
|
})
|
|
}
|