Files
supabase/apps/studio/components/interfaces/TableGridEditor/TableDefinition.tsx
T
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

124 lines
3.4 KiB
TypeScript

import Editor from '@monaco-editor/react'
import { useParams } from 'common'
import { useTheme } from 'next-themes'
import Link from 'next/link'
import { useMemo, useRef } from 'react'
import { Button } from 'ui'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import { Footer } from '@/components/grid/components/footer/Footer'
import { useTableDefinitionQuery } from '@/data/database/table-definition-query'
import { useViewDefinitionQuery } from '@/data/database/view-definition-query'
import { Entity, isTableLike, isViewLike } from '@/data/table-editor/table-editor-types'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { formatSql } from '@/lib/formatSql'
import { timeout } from '@/lib/helpers'
export interface TableDefinitionProps {
entity?: Entity
}
export const TableDefinition = ({ entity }: TableDefinitionProps) => {
const { ref } = useParams()
const editorRef = useRef(null)
const monacoRef = useRef(null)
const { resolvedTheme } = useTheme()
const { data: project } = useSelectedProjectQuery()
const viewResult = useViewDefinitionQuery(
{
id: entity?.id,
includeCreateStatement: true,
projectRef: project?.ref,
connectionString: project?.connectionString,
},
{
enabled: isViewLike(entity),
}
)
const tableResult = useTableDefinitionQuery(
{
id: entity?.id,
projectRef: project?.ref,
connectionString: project?.connectionString,
},
{
enabled: isTableLike(entity),
}
)
const { data: definition, isLoading } = isViewLike(entity) ? viewResult : tableResult
const formattedDefinition = useMemo(
() => (definition ? formatSql(definition) : undefined),
[definition]
)
const handleEditorOnMount = async (editor: any, monaco: any) => {
editorRef.current = editor
monacoRef.current = monaco
// add margin above first line
editor.changeViewZones((accessor: any) => {
accessor.addZone({
afterLineNumber: 0,
heightInPx: 4,
domNode: document.createElement('div'),
})
})
// when editor did mount, it will need a delay before focus() works properly
await timeout(500)
editor?.focus()
}
if (isLoading) {
return (
<div className="h-full grid">
<div className="p-4">
<GenericSkeletonLoader />
</div>
<div className="mt-auto">
<Footer />
</div>
</div>
)
}
return (
<>
<div className="flex-grow overflow-y-auto border-t border-muted relative">
<Button asChild type="default" className="absolute top-2 right-5 z-10">
<Link
href={`/project/${ref}/sql/new?content=${encodeURIComponent(
formattedDefinition ?? ''
)}`}
>
Open in SQL Editor
</Link>
</Button>
<Editor
className="monaco-editor"
theme={resolvedTheme?.includes('dark') ? 'vs-dark' : 'vs'}
onMount={handleEditorOnMount}
defaultLanguage="pgsql"
value={formattedDefinition}
path={''}
options={{
domReadOnly: true,
readOnly: true,
tabSize: 2,
fontSize: 13,
minimap: { enabled: false },
wordWrap: 'on',
fixedOverflowWidgets: true,
}}
/>
</div>
<Footer />
</>
)
}