mirror of
https://github.com/supabase/supabase.git
synced 2026-06-28 03:19:09 -04:00
097f220c5c
## Context Dashboard currently doesn't have any support for managing stored procedures. In the event that the security advisor surfaces a warning about a stored procedure, users hence run into a dead-end as there's currently no way to self-remediate via the dashboard ## Changes involved We're hence adding support for managing stored procedures within Database Functions <img width="1082" height="546" alt="image" src="https://github.com/user-attachments/assets/2598a5fe-e58f-4e8a-ad2f-9cb6d0eb2f53" /> Creating a function now shows a dropdown to select the type <img width="500" alt="image" src="https://github.com/user-attachments/assets/acc9249d-7b25-4416-aae8-89c630e1c62b" /> In which if stored procedure is selected, the following fields will be hidden since they're irrelevant for stored procedures - Return type - Behaviour (Under advanced settings) Some other minor UI changes as well: - Field inputs are re-ordered a little, opting to group "Schema" and "Name" into one section, followed by "Type" and "Return type" - Opting to show "Return type" when editing a function but disabled - Add schema filter for fetching database functions to reduce unnecessary load on the database ## To test - [ ] Can create, update, delete, read stored procedures via database functions page <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary - **New Features** - Added PostgreSQL **procedure** support alongside functions, including a **Type** selector in the create/edit flow. - Updated Functions UI with a new **Type** column and procedure-aware return/argument details. - **Improvements** - Refreshed create/edit headers and language help text for clearer context. - Improved argument parsing/display, including better handling of procedure argument modes. - **Bug Fixes** - Corrected routine-type handling during function/procedure delete and update SQL operations. - **Tests** - Updated unit snapshots and end-to-end UI flows/labels for the new “New function” control. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import type { PGFunctionUpdate } from '@supabase/pg-meta/src/pg-meta-functions'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
import { z } from 'zod'
|
|
|
|
import type { SavedDatabaseFunction } from './database-functions-query'
|
|
import { databaseKeys } from '@/data/database/keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabaseFunctionUpdateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
func: SavedDatabaseFunction
|
|
payload: PGFunctionUpdate
|
|
}
|
|
|
|
export async function updateDatabaseFunction({
|
|
projectRef,
|
|
connectionString,
|
|
func,
|
|
payload,
|
|
}: DatabaseFunctionUpdateVariables) {
|
|
const { sql, zod } = pgMeta.functions.update(func, payload)
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['functions', 'update', func.id.toString()],
|
|
})
|
|
|
|
return result as z.infer<typeof zod>
|
|
}
|
|
|
|
type DatabaseFunctionUpdateData = Awaited<ReturnType<typeof updateDatabaseFunction>>
|
|
|
|
export const useDatabaseFunctionUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
DatabaseFunctionUpdateData,
|
|
ResponseError,
|
|
DatabaseFunctionUpdateVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseFunctionUpdateData, ResponseError, DatabaseFunctionUpdateVariables>({
|
|
mutationFn: (vars) => updateDatabaseFunction(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseKeys.databaseFunctions(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update database function: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|