mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 19:35:06 +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.
80 lines
2.2 KiB
TypeScript
80 lines
2.2 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 DatabaseQueueMessageReadVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
queueName: string
|
|
duration: number
|
|
messageId: number
|
|
}
|
|
|
|
export async function readDatabaseQueueMessage({
|
|
projectRef,
|
|
connectionString,
|
|
queueName,
|
|
messageId,
|
|
duration,
|
|
}: DatabaseQueueMessageReadVariables) {
|
|
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.set_vt(${literal(queueName)}, ${literal(messageId)}, ${literal(duration)})`,
|
|
queryKey: databaseQueuesKeys.create(),
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseQueueMessageReadData = Awaited<ReturnType<typeof readDatabaseQueueMessage>>
|
|
|
|
export const useDatabaseQueueMessageReadMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
DatabaseQueueMessageReadData,
|
|
ResponseError,
|
|
DatabaseQueueMessageReadVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<
|
|
DatabaseQueueMessageReadData,
|
|
ResponseError,
|
|
DatabaseQueueMessageReadVariables
|
|
>({
|
|
mutationFn: (vars) => readDatabaseQueueMessage(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef, queueName } = variables
|
|
await queryClient.invalidateQueries({
|
|
queryKey: databaseQueuesKeys.getMessagesInfinite(projectRef, queueName),
|
|
})
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to postpone database queue message: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|