Files
supabase/apps/studio/data/tables/table-create-mutation.ts
T
Joshen Lim aba4e147eb Joshen/fe 3613 database tables query should have schema filter wherever appropriate (#46935)
## Context

There's certain areas in the dashboard where we're calling
`useTablesQuery` without a schema filter, in which case the dashboard
then fires a query against the project's database to fetch _all_ tables
across _all_ schemas - this could easily be a heavy query if there's a
large number of relations in the project's database.

Am hence opting to either add a schema filter if appropriate, or
otherwise opt to use the infinite loading behaviour

## Changes involved
- Add schema filter to `useTablesQuery` in database triggers and
publications
- Use infinite loading for tables in Cmd K for "Run query on table" and
"Search database tables"

## To test
- [x] Verify that database triggers + publications still function as
expected
- [x] Verify that CMD K "Run query on table" and "Search database
tables" still function as expected (including search)

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

* **New Features**
* Implemented debounced infinite-scrolling table search in the command
menu and SQL editor command flow.
* Added a schema selector dropdown to publications management for easier
navigation.
* **Improvements**
  * Removed the “Schema” column from the publications tables UI.
* Updated search guidance and table-picker status (counts/loading)
during infinite browsing.
  * Trigger table listings now follow the selected schema context.
* Refined command menu list height and improved the database-tables
placeholder text.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-06-16 15:45:39 +08:00

76 lines
2.1 KiB
TypeScript

import pgMeta from '@supabase/pg-meta'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { tableKeys } from './keys'
import { privilegeKeys } from '@/data/privileges/keys'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import type { ResponseError, UseCustomMutationOptions } from '@/types'
export type CreateTableBody = {
name: string
schema?: string
comment?: string | null
}
export type TableCreateVariables = {
projectRef: string
connectionString?: string | null
// the schema is required field
payload: CreateTableBody & { schema: string }
}
export async function createTable({ projectRef, connectionString, payload }: TableCreateVariables) {
const { sql } = pgMeta.tables.create(payload)
const { result } = await executeSql<void>({
projectRef,
connectionString,
sql,
queryKey: ['table', 'create'],
})
return result
}
type TableCreateData = Awaited<ReturnType<typeof createTable>>
export const useTableCreateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<TableCreateData, ResponseError, TableCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<TableCreateData, ResponseError, TableCreateVariables>({
mutationFn: (vars) => createTable(vars),
async onSuccess(data, variables, context) {
const { projectRef, payload } = variables
await Promise.all([
queryClient.invalidateQueries({
queryKey: tableKeys.list(projectRef, payload.schema, { includeColumns: true }),
}),
queryClient.invalidateQueries({
queryKey: tableKeys.list(projectRef, payload.schema, { includeColumns: false }),
}),
queryClient.invalidateQueries({
queryKey: privilegeKeys.tablePrivilegesList(projectRef),
}),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to create database table: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}