mirror of
https://github.com/supabase/supabase.git
synced 2026-06-28 11:33:52 -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 -->
74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import pgMeta, { type SafeSqlFragment } from '@supabase/pg-meta'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
import { z } from 'zod'
|
|
|
|
import { databaseKeys } from '@/data/database/keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type DatabaseFunctionsVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
schema?: string
|
|
}
|
|
|
|
export type DatabaseFunction = z.infer<typeof pgMeta.functions.pgFunctionZod>
|
|
export type SavedDatabaseFunction = Omit<
|
|
DatabaseFunction,
|
|
| 'complete_statement'
|
|
| 'argument_types'
|
|
| 'identity_argument_types'
|
|
| 'return_type'
|
|
| 'config_params'
|
|
> & {
|
|
complete_statement: SafeSqlFragment
|
|
argument_types: SafeSqlFragment
|
|
identity_argument_types: SafeSqlFragment
|
|
return_type: SafeSqlFragment
|
|
config_params: Record<string, SafeSqlFragment> | null
|
|
type: 'function' | 'procedure'
|
|
}
|
|
|
|
export async function getDatabaseFunctions(
|
|
{ projectRef, connectionString, schema }: DatabaseFunctionsVariables,
|
|
signal?: AbortSignal,
|
|
headersInit?: HeadersInit
|
|
) {
|
|
let headers = new Headers(headersInit)
|
|
|
|
const pgMetaFunctionsList = pgMeta.functions.list({
|
|
includedSchemas: schema ? [schema] : undefined,
|
|
})
|
|
const { result } = await executeSql(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sql: pgMetaFunctionsList.sql,
|
|
queryKey: ['database-functions'],
|
|
},
|
|
signal,
|
|
headers
|
|
)
|
|
|
|
return result as SavedDatabaseFunction[]
|
|
}
|
|
|
|
export type DatabaseFunctionsData = Awaited<ReturnType<typeof getDatabaseFunctions>>
|
|
export type DatabaseFunctionsError = ResponseError
|
|
|
|
export const useDatabaseFunctionsQuery = <TData = DatabaseFunctionsData>(
|
|
vars: DatabaseFunctionsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<DatabaseFunctionsData, DatabaseFunctionsError, TData> = {}
|
|
) => {
|
|
const { projectRef, schema } = vars
|
|
return useQuery<DatabaseFunctionsData, DatabaseFunctionsError, TData>({
|
|
queryKey: databaseKeys.databaseFunctions(projectRef, schema),
|
|
queryFn: ({ signal }) => getDatabaseFunctions(vars, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|
|
}
|