mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +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>
99 lines
3.0 KiB
TypeScript
99 lines
3.0 KiB
TypeScript
import { ident, literal, safeSql } from '@supabase/pg-meta/src/pg-format'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { databaseQueuesKeys } from './keys'
|
|
import {
|
|
isQueueNameValid,
|
|
pgmqQueueTable,
|
|
} from '@/components/interfaces/Integrations/Queues/Queues.utils'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import { invalidateTableMetadata } from '@/data/tables/table-metadata-invalidation'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type DatabaseQueueCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
name: string
|
|
type: 'basic' | 'partitioned' | 'unlogged'
|
|
enableRls: boolean
|
|
configuration?: {
|
|
partitionInterval?: number
|
|
retentionInterval?: number
|
|
}
|
|
}
|
|
|
|
export async function createDatabaseQueue({
|
|
projectRef,
|
|
connectionString,
|
|
name,
|
|
type,
|
|
enableRls,
|
|
configuration,
|
|
}: DatabaseQueueCreateVariables) {
|
|
if (!isQueueNameValid(name)) {
|
|
throw new Error(
|
|
'Invalid queue name: must contain only alphanumeric characters, underscores, and hyphens'
|
|
)
|
|
}
|
|
|
|
const { partitionInterval, retentionInterval } = configuration ?? {}
|
|
|
|
const createFragment =
|
|
type === 'partitioned'
|
|
? safeSql`select from pgmq.create_partitioned(${literal(name)}, ${literal(partitionInterval)}, ${literal(retentionInterval)});`
|
|
: type === 'unlogged'
|
|
? safeSql`SELECT pgmq.create_unlogged(${literal(name)});`
|
|
: safeSql`SELECT pgmq.create(${literal(name)});`
|
|
|
|
const rlsFragment = enableRls
|
|
? safeSql` alter table ${ident('pgmq')}.${ident(pgmqQueueTable(name))} enable row level security;`
|
|
: safeSql``
|
|
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql: safeSql`${createFragment}${rlsFragment}`,
|
|
queryKey: databaseQueuesKeys.create(),
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabaseQueueCreateData = Awaited<ReturnType<typeof createDatabaseQueue>>
|
|
|
|
export const useDatabaseQueueCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabaseQueueCreateData, ResponseError, DatabaseQueueCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabaseQueueCreateData, ResponseError, DatabaseQueueCreateVariables>({
|
|
mutationFn: (vars) => createDatabaseQueue(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef, name } = variables
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: databaseQueuesKeys.list(projectRef) }),
|
|
invalidateTableMetadata(queryClient, {
|
|
projectRef,
|
|
schema: 'pgmq',
|
|
tableName: pgmqQueueTable(name),
|
|
}),
|
|
])
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database queue: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|