mirror of
https://github.com/supabase/supabase.git
synced 2026-10-15 06:09:08 +03:00
Fixes #44587 index-delete-mutation.ts and index-create-mutation.ts construct SQL using raw string interpolation for identifiers (schema, table, column names) without escaping double quotes. Uses ident() from @supabase/pg-meta/src/pg-format, same utility already used in the database-queues mutations. ## Summary by CodeRabbit * **Refactor** * Improved identifier quoting/escaping for creating and dropping database indexes to ensure consistent, safer index operations. --------- Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { ident } from '@supabase/pg-meta/src/pg-format'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { databaseIndexesKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-query'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabaseIndexCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
payload: {
|
|
schema: string
|
|
entity: string
|
|
type: string
|
|
columns: string[]
|
|
}
|
|
}
|
|
|
|
export async function createDatabaseIndex({
|
|
projectRef,
|
|
connectionString,
|
|
payload,
|
|
}: DatabaseIndexCreateVariables) {
|
|
const { schema, entity, type, columns } = payload
|
|
|
|
const sql = `
|
|
CREATE INDEX ON ${ident(schema)}.${ident(entity)} USING ${type} (${columns
|
|
.map((column) => ident(column))
|
|
.join(', ')});
|
|
`.trim()
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['indexes', schema],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseIndexCreateData = Awaited<ReturnType<typeof createDatabaseIndex>>
|
|
|
|
export const useDatabaseIndexCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabaseIndexCreateData, ResponseError, DatabaseIndexCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseIndexCreateData, ResponseError, DatabaseIndexCreateVariables>({
|
|
mutationFn: (vars) => createDatabaseIndex(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseIndexesKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database index: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|