Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

add copy table schema option to table dropdown (#35909)

Jordi Enric318280f2fe14
diff --git a/apps/studio/components/layouts/TableEditorLayout/EntityListItem.tsx b/apps/studio/components/layouts/TableEditorLayout/EntityListItem.tsx
index d4d56add08e..e0cd30dde26 100644
--- a/apps/studio/components/layouts/TableEditorLayout/EntityListItem.tsx
+++ b/apps/studio/components/layouts/TableEditorLayout/EntityListItem.tsx
@@ -44,6 +44,9 @@ import {
TreeViewItemVariant,
} from 'ui'
import { useProjectContext } from '../ProjectLayout/ProjectContext'
+import { useTableDefinitionQuery } from 'data/database/table-definition-query'
+import { formatSql } from 'lib/formatSql'
+
export interface EntityListItemProps {
id: number | string
projectRef: string
@@ -51,6 +54,11 @@ export interface EntityListItemProps {
isActive?: boolean
}
+// [jordi] Used to determine the entity is a table and not a view or other unsupported entity type
+function isTableLikeEntityListItem(entity: { type?: string }) {
+ return entity?.type === ENTITY_TYPE.TABLE || entity?.type === ENTITY_TYPE.PARTITIONED_TABLE
+}
+
const EntityListItem: ItemRenderer<Entity, EntityListItemProps> = ({
id,
projectRef,
@@ -62,6 +70,15 @@ const EntityListItem: ItemRenderer<Entity, EntityListItemProps> = ({
const snap = useTableEditorStateSnapshot()
const { selectedSchema } = useQuerySchemaState()
+ const { data: tableDefinition, isLoading: isTableDefinitionLoading } = useTableDefinitionQuery(
+ {
+ id: entity.id,
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ },
+ { enabled: isTableLikeEntityListItem(entity) }
+ )
+
// For tabs preview flag logic
const isTableEditorTabsEnabled = useIsTableEditorTabsEnabled()
const tabId = createTabId(entity.type, { id: entity.id })
@@ -286,6 +303,30 @@ const EntityListItem: ItemRenderer<Entity, EntityListItemProps> = ({
<span>Copy name</span>
</DropdownMenuItem>
+ {isTableLikeEntityListItem(entity) && (
+ <DropdownMenuItem
+ key="copy-schema"
+ className="space-x-2"
+ disabled={isTableDefinitionLoading || !tableDefinition}
+ onClick={async (e) => {
+ e.stopPropagation()
+ if (!tableDefinition) return
+ try {
+ const formatted = formatSql(tableDefinition)
+ await copyToClipboard(formatted)
+ toast.success('Table schema copied to clipboard', { id: 'copy-schema' })
+ } catch (err: any) {
+ toast.error('Failed to copy schema: ' + (err.message || err), {
+ id: 'copy-schema',
+ })
+ }
+ }}
+ >
+ <Clipboard size={12} />
+ <span>Copy table schema</span>
+ </DropdownMenuItem>
+ )}
+
{entity.type === ENTITY_TYPE.TABLE && (
<>
<DropdownMenuSeparator />