mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +08:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Bug fix ## What is the current behavior? Fixes stale table metadata after saving edits from the table editor drawer. Previously, metadata-only table changes, such as column updates, primary key/foreign key changes, table renames, or schema moves, could leave cached table data stale. This affected the Database tables list, schema visualizer, and reopening the edit drawer. Fixes #47540 ## What is the new behavior? Table metadata caches are now invalidated consistently after table create, update, delete, column delete, and queue table creation flows. The Database tables list, schema visualizer, table editor drawer, table definitions, constraints, foreign keys, table columns, rows, and lint data now refresh correctly after relevant table metadata changes. ## Additional context https://github.com/user-attachments/assets/de849710-8d7b-4d8b-af3b-5232154e996b <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Table metadata now refreshes consistently after table creation, updates, duplication, and deletion. * **Bug Fixes** * Improved synchronization for table lists, lint results, constraints, and row counts after changes. * Renaming or moving tables now refreshes both the previous and updated locations. * Column and queue changes now trigger the appropriate table metadata updates. * **Tests** * Added coverage for metadata refresh behavior across edits, moves, optional lint updates, and row counts. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Ali Waseem <waseema393@gmail.com>
76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { privilegeKeys } from '@/data/privileges/keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import { invalidateTableMetadata } from '@/data/tables/table-metadata-invalidation'
|
|
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([
|
|
invalidateTableMetadata(queryClient, {
|
|
projectRef,
|
|
schema: payload.schema,
|
|
tableName: payload.name,
|
|
includeLint: true,
|
|
}),
|
|
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,
|
|
})
|
|
}
|