Files
supabase/apps/studio/data/database/view-definition-query.ts
Vaibhav fdf8732727 fix: Include security_invoker in definition (#44936)
## TL;DR

the table editor definition panel was showing incomplete SQL for views
with `WITH (security_invoker = true)`
ignoring the reloption and making it easy to accidentally strip it when
recreating the view

## prob

When viewing a security invoker view in the Table Editor, the Definition
panel only showed `CREATE VIEW ... AS ...`
 without the `WITH (security_invoker = true)` clause

which caused two issues:

1. the displayed SQL was incomplete and didn't match the actual view
definition
2. users copying the SQL to recreate the view would unintentionally lose
the security_invoker setting


## ex:

| Before | After |
|--------|-------|
| `create view public.exposed_api as`<br>`select id, secret from
public.rls_protected_table;` | `create view public.exposed_api with
(security_invoker = true) as`<br>`select id, secret from
public.rls_protected_table;` |

## ref:
- closes https://github.com/supabase/supabase/issues/44934

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* View definitions now show the full CREATE statement (including
materialized views and WITH (...) options) and preserve security options
like security_invoker when viewed or opened in the SQL editor.

* **Tests**
* Added end-to-end test verifying security option preservation in view
definitions and when opening them in the SQL editor.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-04-16 07:03:54 -06:00

56 lines
1.7 KiB
TypeScript

import { getViewDefinitionSql } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { databaseKeys } from './keys'
import { executeSql, ExecuteSqlError } from '@/data/sql/execute-sql-query'
import { UseCustomQueryOptions } from '@/types'
type GetViewDefinitionArgs = {
id?: number
includeCreateStatement?: boolean
}
export type ViewDefinitionVariables = GetViewDefinitionArgs & {
projectRef?: string
connectionString?: string | null
}
export async function getViewDefinition(
{ projectRef, connectionString, id, includeCreateStatement }: ViewDefinitionVariables,
signal?: AbortSignal
) {
if (!id) throw new Error('View ID is required')
const sql = getViewDefinitionSql({ id, includeCreateStatement })
const { result } = await executeSql(
{
projectRef,
connectionString,
sql,
queryKey: ['view-definition', id, includeCreateStatement ?? false],
},
signal
)
return result[0].definition.trim()
}
export type ViewDefinitionData = string
export type ViewDefinitionError = ExecuteSqlError
export const useViewDefinitionQuery = <TData = ViewDefinitionData>(
{ projectRef, connectionString, id, includeCreateStatement }: ViewDefinitionVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<ViewDefinitionData, ViewDefinitionError, TData> = {}
) =>
useQuery<ViewDefinitionData, ViewDefinitionError, TData>({
queryKey: databaseKeys.viewDefinition(projectRef, id, includeCreateStatement),
queryFn: ({ signal }) =>
getViewDefinition({ projectRef, connectionString, id, includeCreateStatement }, signal),
enabled:
enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined' && !isNaN(id),
...options,
})