Skip to content

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

Fix database tables page not updating when creating/deleting tables (#20625)

Joshen Lim078a694ed492
diff --git a/apps/studio/components/interfaces/Database/Tables/TableList.tsx b/apps/studio/components/interfaces/Database/Tables/TableList.tsx
index 89d9087f8c3..ca560f5f201 100644
--- a/apps/studio/components/interfaces/Database/Tables/TableList.tsx
+++ b/apps/studio/components/interfaces/Database/Tables/TableList.tsx
@@ -158,196 +158,193 @@ const TableList = ({
{isError && <AlertError error={error} subject="Failed to retrieve tables" />}
- {isSuccess &&
- (tables.length === 0 ? (
- <NoSearchResults />
- ) : (
- <div className="my-4 w-full">
- <Table
- head={[
- <Table.th key="name">Name</Table.th>,
- <Table.th key="description" className="hidden lg:table-cell">
- Description
- </Table.th>,
- <Table.th key="rows" className="hidden text-right xl:table-cell">
- Rows (Estimated)
- </Table.th>,
- <Table.th key="size" className="hidden text-right xl:table-cell">
- Size (Estimated)
- </Table.th>,
- <Table.th key="realtime" className="hidden xl:table-cell text-center">
- Realtime Enabled
- </Table.th>,
- <Table.th key="buttons"></Table.th>,
- ]}
- body={
- <>
- {tables.length === 0 && filterString.length === 0 && (
- <Table.tr key={snap.selectedSchemaName}>
- <Table.td colSpan={6}>
- <p className="text-sm text-foreground">No tables created yet</p>
- <p className="text-sm text-foreground-light">
- There are no tables found in the schema "{snap.selectedSchemaName}"
- </p>
+ {isSuccess && (
+ <div className="my-4 w-full">
+ <Table
+ head={[
+ <Table.th key="name">Name</Table.th>,
+ <Table.th key="description" className="hidden lg:table-cell">
+ Description
+ </Table.th>,
+ <Table.th key="rows" className="hidden text-right xl:table-cell">
+ Rows (Estimated)
+ </Table.th>,
+ <Table.th key="size" className="hidden text-right xl:table-cell">
+ Size (Estimated)
+ </Table.th>,
+ <Table.th key="realtime" className="hidden xl:table-cell text-center">
+ Realtime Enabled
+ </Table.th>,
+ <Table.th key="buttons"></Table.th>,
+ ]}
+ body={
+ <>
+ {tables.length === 0 && filterString.length === 0 && (
+ <Table.tr key={snap.selectedSchemaName}>
+ <Table.td colSpan={6}>
+ <p className="text-sm text-foreground">No tables created yet</p>
+ <p className="text-sm text-foreground-light">
+ There are no tables found in the schema "{snap.selectedSchemaName}"
+ </p>
+ </Table.td>
+ </Table.tr>
+ )}
+ {tables.length === 0 && filterString.length > 0 && (
+ <Table.tr key={snap.selectedSchemaName}>
+ <Table.td colSpan={6}>
+ <p className="text-sm text-foreground">No results found</p>
+ <p className="text-sm text-foreground-light">
+ Your search for "{filterString}" did not return any results
+ </p>
+ </Table.td>
+ </Table.tr>
+ )}
+ {tables.length > 0 &&
+ tables.map((x: any, i: any) => (
+ <Table.tr key={x.id}>
+ <Table.td>
+ {/* only show tooltips if required, to reduce noise */}
+ {x.name.length > 20 ? (
+ <Tooltip.Root delayDuration={0} disableHoverableContent={true}>
+ <Tooltip.Trigger
+ asChild
+ className="max-w-[95%] overflow-hidden text-ellipsis whitespace-nowrap"
+ >
+ <p>{x.name}</p>
+ </Tooltip.Trigger>
+ <Tooltip.Portal>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
+ 'border border-scale-200',
+ ].join(' ')}
+ >
+ <span className="text-xs text-foreground">{x.name}</span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Portal>
+ </Tooltip.Root>
+ ) : (
+ <p>{x.name}</p>
+ )}
</Table.td>
- </Table.tr>
- )}
- {tables.length === 0 && filterString.length > 0 && (
- <Table.tr key={snap.selectedSchemaName}>
- <Table.td colSpan={6}>
- <p className="text-sm text-foreground">No results found</p>
- <p className="text-sm text-foreground-light">
- Your search for "{filterString}" did not return any results
- </p>
+ <Table.td className="hidden max-w-sm truncate lg:table-cell break-all whitespace-normal">
+ {x.comment !== null ? (
+ <p title={x.comment}>{x.comment}</p>
+ ) : (
+ <p className="text-border-stronger">No description</p>
+ )}
</Table.td>
- </Table.tr>
- )}
- {tables.length > 0 &&
- tables.map((x: any, i: any) => (
- <Table.tr key={x.id}>
- <Table.td>
- {/* only show tooltips if required, to reduce noise */}
- {x.name.length > 20 ? (
- <Tooltip.Root delayDuration={0} disableHoverableContent={true}>
- <Tooltip.Trigger
- asChild
- className="max-w-[95%] overflow-hidden text-ellipsis whitespace-nowrap"
- >
- <p>{x.name}</p>
- </Tooltip.Trigger>
- <Tooltip.Portal>
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'border border-scale-200',
- ].join(' ')}
- >
- <span className="text-xs text-foreground">{x.name}</span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- </Tooltip.Root>
- ) : (
- <p>{x.name}</p>
- )}
- </Table.td>
- <Table.td className="hidden max-w-sm truncate lg:table-cell break-all whitespace-normal">
- {x.comment !== null ? (
- <p title={x.comment}>{x.comment}</p>
- ) : (
- <p className="text-border-stronger">No description</p>
- )}
- </Table.td>
- <Table.td className="hidden text-right xl:table-cell">
- {(x.live_rows_estimate ?? x.live_row_count).toLocaleString()}
- </Table.td>
- <Table.td className="hidden text-right xl:table-cell">
- <code className="text-sm">{x.size}</code>
- </Table.td>
- <Table.td className="hidden xl:table-cell text-center">
- {(realtimePublication?.tables ?? []).find(
- (table: any) => table.id === x.id
- ) ? (
- <div className="flex justify-center">
- <IconCheck strokeWidth={2} className="text-brand" />
- </div>
- ) : (
- <div className="flex justify-center">
- <IconX strokeWidth={2} className="text-foreground-lighter" />
- </div>
- )}
- </Table.td>
- <Table.td>
- <div className="flex justify-end gap-2">
- <Button
- type="default"
- iconRight={<IconColumns />}
- className="whitespace-nowrap hover:border-gray-500"
- style={{ paddingTop: 3, paddingBottom: 3 }}
- onClick={() => onOpenTable(x)}
- >
- {x.columns?.length} columns
- </Button>
+ <Table.td className="hidden text-right xl:table-cell">
+ {(x.live_rows_estimate ?? x.live_row_count).toLocaleString()}
+ </Table.td>
+ <Table.td className="hidden text-right xl:table-cell">
+ <code className="text-sm">{x.size}</code>
+ </Table.td>
+ <Table.td className="hidden xl:table-cell text-center">
+ {(realtimePublication?.tables ?? []).find(
+ (table: any) => table.id === x.id
+ ) ? (
+ <div className="flex justify-center">
+ <IconCheck strokeWidth={2} className="text-brand" />
+ </div>
+ ) : (
+ <div className="flex justify-center">
+ <IconX strokeWidth={2} className="text-foreground-lighter" />
+ </div>
+ )}
+ </Table.td>
+ <Table.td>
+ <div className="flex justify-end gap-2">
+ <Button
+ type="default"
+ iconRight={<IconColumns />}
+ className="whitespace-nowrap hover:border-gray-500"
+ style={{ paddingTop: 3, paddingBottom: 3 }}
+ onClick={() => onOpenTable(x)}
+ >
+ {x.columns?.length} columns
+ </Button>
- {!isLocked && (
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <Button type="default" className="px-1">
- <IconMoreVertical />
- </Button>
- </DropdownMenuTrigger>
- <DropdownMenuContent side="bottom" align="end" className="w-32">
- <DropdownMenuItem
- disabled={!canUpdateTables}
- onClick={() => onEditTable(x)}
- >
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger className="flex items-center space-x-2">
- <IconEdit size="tiny" />
- <p>Edit table</p>
- </Tooltip.Trigger>
- {!canUpdateTables && (
- <Tooltip.Portal>
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-alternative py-1 px-2 leading-none shadow',
- 'border border-background',
- ].join(' ')}
- >
- <span className="text-xs text-foreground">
- Additional permissions required to edit table
- </span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- )}
- </Tooltip.Root>
- </DropdownMenuItem>
+ {!isLocked && (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button type="default" className="px-1">
+ <IconMoreVertical />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent side="bottom" align="end" className="w-32">
+ <DropdownMenuItem
+ disabled={!canUpdateTables}
+ onClick={() => onEditTable(x)}
+ >
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger className="flex items-center space-x-2">
+ <IconEdit size="tiny" />
+ <p>Edit table</p>
+ </Tooltip.Trigger>
+ {!canUpdateTables && (
+ <Tooltip.Portal>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-alternative py-1 px-2 leading-none shadow',
+ 'border border-background',
+ ].join(' ')}
+ >
+ <span className="text-xs text-foreground">
+ Additional permissions required to edit table
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Portal>
+ )}
+ </Tooltip.Root>
+ </DropdownMenuItem>
- <DropdownMenuItem
- disabled={!canUpdateTables || isLocked}
- onClick={() => onDeleteTable(x)}
- >
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger className="flex items-center space-x-2">
- <IconTrash stroke="red" size="tiny" />
- <p>Delete table</p>
- </Tooltip.Trigger>
- {!canUpdateTables && (
- <Tooltip.Portal>
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-alternative py-1 px-2 leading-none shadow',
- 'border border-background',
- ].join(' ')}
- >
- <span className="text-xs text-foreground">
- Additional permissions required to delete table
- </span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- )}
- </Tooltip.Root>
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
- )}
- </div>
- </Table.td>
- </Table.tr>
- ))}
- </>
- }
- />
- </div>
- ))}
+ <DropdownMenuItem
+ disabled={!canUpdateTables || isLocked}
+ onClick={() => onDeleteTable(x)}
+ >
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger className="flex items-center space-x-2">
+ <IconTrash stroke="red" size="tiny" />
+ <p>Delete table</p>
+ </Tooltip.Trigger>
+ {!canUpdateTables && (
+ <Tooltip.Portal>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-alternative py-1 px-2 leading-none shadow',
+ 'border border-background',
+ ].join(' ')}
+ >
+ <span className="text-xs text-foreground">
+ Additional permissions required to delete table
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Portal>
+ )}
+ </Tooltip.Root>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )}
+ </div>
+ </Table.td>
+ </Table.tr>
+ ))}
+ </>
+ }
+ />
+ </div>
+ )}
</div>
)
}
diff --git a/apps/studio/components/interfaces/TableGridEditor/DeleteConfirmationDialogs.tsx b/apps/studio/components/interfaces/TableGridEditor/DeleteConfirmationDialogs.tsx
index deff5890385..2e0469f3c06 100644
--- a/apps/studio/components/interfaces/TableGridEditor/DeleteConfirmationDialogs.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/DeleteConfirmationDialogs.tsx
@@ -19,16 +19,19 @@ import { TableLike } from 'hooks/misc/useTable'
import { noop } from 'lib/void'
import { useGetImpersonatedRole } from 'state/role-impersonation-state'
import { useTableEditorStateSnapshot } from 'state/table-editor'
+import { tableKeys } from 'data/tables/keys'
export type DeleteConfirmationDialogsProps = {
projectRef?: string
selectedTable?: TableLike
+ includeColumns?: boolean
onAfterDeleteTable?: (tables: TableLike[]) => void
}
const DeleteConfirmationDialogs = ({
projectRef,
selectedTable,
+ includeColumns = false,
onAfterDeleteTable = noop,
}: DeleteConfirmationDialogsProps) => {
const { meta } = useStore()
@@ -163,6 +166,9 @@ const DeleteConfirmationDialogs = ({
const tables = await getTables(snap.selectedSchemaName)
await Promise.all([
+ queryClient.invalidateQueries(
+ tableKeys.list(project?.ref, selectedTableToDelete.schema, includeColumns)
+ ),
queryClient.invalidateQueries(entityTypeKeys.list(projectRef)),
// invalidate all views from this schema
snap.selectedSchemaName
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
index 6860c23dbcd..1748379fdbd 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
@@ -36,6 +36,7 @@ import { ImportContent } from './TableEditor/TableEditor.types'
export interface SidePanelEditorProps {
editable?: boolean
selectedTable?: PostgresTable
+ includeColumns?: boolean // This is mainly used for invalidating useTablesQuery
// Because the panel is shared between grid editor and database pages
// Both require different responses upon success of these events
@@ -45,6 +46,7 @@ export interface SidePanelEditorProps {
const SidePanelEditor = ({
editable = true,
selectedTable,
+ includeColumns = false,
onTableCreated = noop,
}: SidePanelEditorProps) => {
const snap = useTableEditorStateSnapshot()
@@ -268,7 +270,7 @@ const SidePanelEditor = ({
if (!project) return console.error('Project is required')
let realtimePublication = (publications ?? []).find((pub) => pub.name === 'supabase_realtime')
const publicTables = await queryClient.fetchQuery({
- queryKey: tableKeys.list(project.ref, 'public'),
+ queryKey: tableKeys.list(project.ref, 'public', includeColumns),
queryFn: ({ signal }) =>
getTables(
{ projectRef: project.ref, connectionString: project.connectionString, schema: 'public' },
@@ -369,7 +371,7 @@ const SidePanelEditor = ({
if (isRealtimeEnabled) await updateTableRealtime(table, isRealtimeEnabled)
await Promise.all([
- queryClient.invalidateQueries(tableKeys.list(project?.ref, table.schema)),
+ queryClient.invalidateQueries(tableKeys.list(project?.ref, table.schema, includeColumns)),
queryClient.invalidateQueries(entityTypeKeys.list(project?.ref)),
])
@@ -390,7 +392,7 @@ const SidePanelEditor = ({
if (isRealtimeEnabled) await updateTableRealtime(table, true)
await Promise.all([
- queryClient.invalidateQueries(tableKeys.list(project?.ref, table.schema)),
+ queryClient.invalidateQueries(tableKeys.list(project?.ref, table.schema, includeColumns)),
queryClient.invalidateQueries(entityTypeKeys.list(project?.ref)),
])
diff --git a/apps/studio/pages/project/[ref]/database/tables.tsx b/apps/studio/pages/project/[ref]/database/tables.tsx
index ca627652cb4..623a2bfa4b7 100644
--- a/apps/studio/pages/project/[ref]/database/tables.tsx
+++ b/apps/studio/pages/project/[ref]/database/tables.tsx
@@ -51,8 +51,12 @@ const DatabaseTables: NextPageWithLayout = () => {
</ScaffoldSection>
</ScaffoldContainer>
- <DeleteConfirmationDialogs projectRef={projectRef} selectedTable={selectedTableToEdit} />
- <SidePanelEditor selectedTable={selectedTable || selectedTableToEdit} />
+ <DeleteConfirmationDialogs
+ includeColumns
+ projectRef={projectRef}
+ selectedTable={selectedTableToEdit}
+ />
+ <SidePanelEditor includeColumns selectedTable={selectedTable || selectedTableToEdit} />
</>
)
}