mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
Resolves FE-4744. Part of the grants rollout; builds on the read/write SQL variants from #53185. ## What - **Data layer:** the database functions, triggers, database webhooks, and event triggers queries now take a `read` grant (exposed as `queryOptions` factories). All 8 create/update/delete mutations take a `write` grant and mark their SQL with `asWriteSql`. - **Guards:** the hand-written FGA checks in the functions, triggers, event triggers, and database webhooks lists, the functions page, the triggers layout/route, and the webhooks integration tab now derive from grants, with denied and error states. - **Other consumers** updated to pass grants: `CreateFunction`, `TriggerSheet`, `EditHookPanel`, `DeleteHookModal`, `ChooseFunctionForm`, `FunctionSelector`, `AutoEnableRLSNotice`, and SQL editor definition/autocomplete hooks. ## Behavior changes - **Webhooks integration tab:** was gated on `database_webhooks_config_read`; now gated on `database_read`, which is what the backend checks for the pg-meta query it uses. Both permissions are granted to the same built-in roles, so there is no change for built-in roles. A permissions-load failure now shows an error alert instead of the content. - **`AutoEnableRLSNotice`:** hidden unless the user has read access, since it can't tell whether the trigger already exists without it. ## Out of scope - User-authored SQL execution in `QueryEditor` / `useSqlEditorExecution` stays `unguarded.migrating` (FE-4750). ## Testing - New tests: query factories and write mutations (`data/database-sql-grants.test.ts`), webhooks tab granted/denied/error, functions list read/write granted/denied/error. - Typecheck, Prettier, lint ratchet, and Studio vitest suites for affected areas pass.
82 lines
2.5 KiB
TypeScript
82 lines
2.5 KiB
TypeScript
import {
|
|
asWriteSql,
|
|
getDatabaseTriggerUpdateSQL,
|
|
type PGTrigger,
|
|
type PGTriggerCreate,
|
|
type SafeSqlFragment,
|
|
} from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { databaseTriggerKeys } from './keys'
|
|
import { executeSql, type PgMetaWriteGrant } from '@/data/sql/execute-sql-mutation'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
// [Joshen] Writing this query within FE as the PATCH endpoint from pg-meta only supports updating
|
|
// trigger name and enabled mode. So we'll delete and create the trigger, within a single transaction
|
|
// Copying the SQL from https://github.com/supabase/postgres-meta/blob/master/src/lib/PostgresMetaTriggers.ts
|
|
|
|
export type DatabaseTriggerUpdateVariables = {
|
|
projectRef: string
|
|
grant: PgMetaWriteGrant
|
|
connectionString?: string | null
|
|
originalTrigger: PGTrigger
|
|
updatedTrigger: Omit<PGTriggerCreate, 'events'> &
|
|
Pick<PGTrigger, 'enabled_mode'> & { events: Array<SafeSqlFragment> }
|
|
}
|
|
|
|
export async function updateDatabaseTrigger({
|
|
projectRef,
|
|
grant,
|
|
connectionString,
|
|
originalTrigger,
|
|
updatedTrigger,
|
|
}: DatabaseTriggerUpdateVariables) {
|
|
const sql = getDatabaseTriggerUpdateSQL({ originalTrigger, updatedTrigger })
|
|
await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sqlOrigin: 'studio',
|
|
queryName: 'trigger-update-transaction',
|
|
sql: asWriteSql(sql),
|
|
queryKey: ['trigger', 'update', originalTrigger.id],
|
|
grant,
|
|
})
|
|
return updatedTrigger
|
|
}
|
|
|
|
type DatabaseTriggerUpdateTxnData = Awaited<ReturnType<typeof updateDatabaseTrigger>>
|
|
|
|
export const useDatabaseTriggerUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
DatabaseTriggerUpdateTxnData,
|
|
StudioError,
|
|
DatabaseTriggerUpdateVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseTriggerUpdateTxnData, StudioError, DatabaseTriggerUpdateVariables>({
|
|
mutationFn: (vars) => updateDatabaseTrigger(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseTriggerKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update database trigger: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|