mirror of
https://github.com/supabase/supabase.git
synced 2026-10-10 20:05: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.
86 lines
2.7 KiB
TypeScript
86 lines
2.7 KiB
TypeScript
import { ident, 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 { tableKeys } from '@/data/tables/keys'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabaseQueueCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
name: string
|
|
type: 'basic' | 'partitioned' | 'unlogged'
|
|
enableRls: boolean
|
|
configuration?: {
|
|
partitionInterval?: number
|
|
retentionInterval?: number
|
|
}
|
|
}
|
|
|
|
export async function createDatabaseQueue({
|
|
projectRef,
|
|
connectionString,
|
|
name,
|
|
type,
|
|
enableRls,
|
|
configuration,
|
|
}: DatabaseQueueCreateVariables) {
|
|
if (!isQueueNameValid(name)) {
|
|
throw new Error(
|
|
'Invalid queue name: must contain only alphanumeric characters, underscores, and hyphens'
|
|
)
|
|
}
|
|
|
|
const { partitionInterval, retentionInterval } = configuration ?? {}
|
|
|
|
const query =
|
|
type === 'partitioned'
|
|
? `select from pgmq.create_partitioned(${literal(name)}, ${literal(partitionInterval)}, ${literal(retentionInterval)});`
|
|
: type === 'unlogged'
|
|
? `SELECT pgmq.create_unlogged(${literal(name)});`
|
|
: `SELECT pgmq.create(${literal(name)});`
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql: `${query} ${enableRls ? `alter table pgmq.${ident('q_' + name)} enable row level security;` : ''}`.trim(),
|
|
queryKey: databaseQueuesKeys.create(),
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseQueueCreateData = Awaited<ReturnType<typeof createDatabaseQueue>>
|
|
|
|
export const useDatabaseQueueCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabaseQueueCreateData, ResponseError, DatabaseQueueCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseQueueCreateData, ResponseError, DatabaseQueueCreateVariables>({
|
|
mutationFn: (vars) => createDatabaseQueue(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseQueuesKeys.list(projectRef) })
|
|
queryClient.invalidateQueries({ queryKey: tableKeys.list(projectRef, 'pgmq') })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database queue: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|