mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03:00
## Summary
Third PR in the SafeSql migration stack. Flips the input/output types on
`pgMeta.functions/policies/triggers`'s `.create/.update/.remove` to use
`SafeSqlFragment`, and updates every Studio consumer atomically.
### pg-meta
- `pgMeta.functions/policies/triggers` `.create/.update/.remove` now
return `{ sql: SafeSqlFragment }` and accept branded input parameters
(`PGFunctionCreate`, `PGSavedFunction`, `PolicyCreate/UpdateParams`,
`PGTriggerCreate` with branded condition).
- `QueryModifier.toSql()` returns `SafeSqlFragment`.
### Studio consumers updated to the new branded API
- `data/database-functions/*` (query, create/update/delete mutations)
- `data/database-policies/*` (create, update mutations)
- `data/database-triggers/database-trigger-update-transaction-mutation`
- `components/Database/Triggers/TriggerSheet`
- `components/Database/Functions/CreateFunction`
- `components/Auth/Policies/PolicyEditorPanel`
These consumers land atomically with the pg-meta API change because the
input-type strictness flip (string → `SafeSqlFragment` for SQL fields)
forces every call site to update together.
## Stack
- 1/7: #45897 (merged)
- 2/7: #45903 (merged)
- 3/7: this PR
- 4/7–7/7: upcoming
## Test plan
- [x] `pnpm typecheck` passes
- [x] `pnpm --filter @supabase/pg-meta test` passes
- [x] Dev-server smoke test: function editor, policy editor, trigger
sheet
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Refactor**
* Strengthened SQL safety across policy, function, and trigger workflows
by converting raw SQL strings to typed SQL fragments and safer
composition
* Updated editor behavior to handle policy conditions/checks as typed
SQL fragments with improved initialization and template handling
* Aligned query and modifier interfaces to return typed SQL fragments
for safer composition
* **Tests**
* Updated tests to use typed SQL fragments and synchronous builders
where applicable
<!-- review_stack_entry_start -->
[](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45990)
<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
69 lines
2.0 KiB
TypeScript
69 lines
2.0 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-query'
|
|
import type { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type DatabaseFunctionsVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
const pgMetaFunctionsList = pgMeta.functions.list()
|
|
|
|
export async function getDatabaseFunctions(
|
|
{ projectRef, connectionString }: DatabaseFunctionsVariables,
|
|
signal?: AbortSignal,
|
|
headersInit?: HeadersInit
|
|
) {
|
|
let headers = new Headers(headersInit)
|
|
|
|
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>(
|
|
{ projectRef, connectionString }: DatabaseFunctionsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<DatabaseFunctionsData, DatabaseFunctionsError, TData> = {}
|
|
) =>
|
|
useQuery<DatabaseFunctionsData, DatabaseFunctionsError, TData>({
|
|
queryKey: databaseKeys.databaseFunctions(projectRef),
|
|
queryFn: ({ signal }) => getDatabaseFunctions({ projectRef, connectionString }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|