mirror of
https://github.com/supabase/supabase.git
synced 2026-10-10 20:05:06 +03:00
## Summary - Converts ~27 `executeSql` call sites in `apps/studio/data/**` to build SQL through `safeSql` / `ident` / `literal` / `keyword` / `joinSqlFragments` instead of raw template-string interpolation. - Tightens the `useDatabaseCronJobCreateMutation` and `useDatabaseEventTriggerCreateMutation` `sql`/`query` parameter types from `string` to `SafeSqlFragment` (callers already produce one). - Updates `getDeleteEnumeratedTypeSQL` in `packages/pg-meta` to return `SafeSqlFragment`. - Fixes a bug noticed while testing where Queues integration does not correctly handle queues with uppercase names. ## Pages to manually test - Integrations > Cron Jobs - Integrations > Queues - Database > Triggers > Event Triggers - Database > Indexes - Reports > Query Performance - Storage <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Bug Fixes** * Queue lookups now correctly handle case-insensitive queue names. * Queue table references are now properly managed and consistently applied throughout the queue management interface. * Improved queue name display normalization in the user interface. * **Chores** * Enhanced SQL query safety across the database layer through parameterized query construction and safer templating approaches. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import { ident, joinSqlFragments, keyword, safeSql } 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 columnList = joinSqlFragments(
|
|
columns.map((column) => ident(column)),
|
|
', '
|
|
)
|
|
const sql = safeSql`CREATE INDEX ON ${ident(schema)}.${ident(entity)} USING ${keyword(type)} (${columnList});`
|
|
|
|
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,
|
|
})
|
|
}
|