mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 19:05:06 +03:00
## Context Just some clean up as I was going through stuff - `useExecuteSqlQuery` is deprecated and not used at all - As such `execute-sql-query` is technically irrelevant, the more relevant file is `execute-sql-mutation` - Hence opting to consolidate `execute-sql-query` into `execute-sql-mutation` - Also removing `ExecuteSqlError` since its just re-exporting the `ResponseError` type There's a lot of file changes but its essentially just updating the importing statements across the files
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { tableKeys } from './keys'
|
|
import { entityTypeKeys } from '@/data/entity-types/keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import { tableEditorKeys } from '@/data/table-editor/keys'
|
|
import { viewKeys } from '@/data/views/keys'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type TableDeleteVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
id: number
|
|
name: string
|
|
schema: string
|
|
cascade?: boolean
|
|
}
|
|
|
|
export async function deleteTable({
|
|
projectRef,
|
|
connectionString,
|
|
id,
|
|
name,
|
|
schema,
|
|
cascade = false,
|
|
}: TableDeleteVariables) {
|
|
const { sql } = pgMeta.tables.remove({ name, schema }, { cascade })
|
|
|
|
const { result } = await executeSql<void>({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['table', 'delete', id],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type TableDeleteData = Awaited<ReturnType<typeof deleteTable>>
|
|
|
|
export const useTableDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<TableDeleteData, ResponseError, TableDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<TableDeleteData, ResponseError, TableDeleteVariables>({
|
|
mutationFn: (vars) => deleteTable(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { id, projectRef, schema } = variables
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: tableEditorKeys.tableEditor(projectRef, id) }),
|
|
queryClient.invalidateQueries({ queryKey: tableKeys.list(projectRef, schema) }),
|
|
queryClient.invalidateQueries({
|
|
queryKey: tableKeys.infiniteListPrefix(projectRef, schema),
|
|
}),
|
|
queryClient.invalidateQueries({ queryKey: entityTypeKeys.list(projectRef) }),
|
|
// invalidate all views from this schema
|
|
queryClient.invalidateQueries({ queryKey: viewKeys.listBySchema(projectRef, schema) }),
|
|
])
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete database table: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|