Files
supabase/apps/studio/data/fdw/fdw-drop-foreign-table-mutation.ts
Joshen Lim 1baaded0bb Consolidate execute-sql-query into execute-sql-mutation (#46944)
## 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
2026-06-16 00:07:16 +08:00

66 lines
2.1 KiB
TypeScript

import { getDropForeignTableSql } from '@supabase/pg-meta'
import { wrapWithTransaction } from '@supabase/pg-meta/src/query'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { fdwKeys } from './keys'
import { entityTypeKeys } from '@/data/entity-types/keys'
import { foreignTableKeys } from '@/data/foreign-tables/keys'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import type { ResponseError, UseCustomMutationOptions } from '@/types'
export type FDWDropForeignTableVariables = {
projectRef?: string
connectionString?: string | null
schemaName: string
tableName: string
}
export async function dropForeignTable({
projectRef,
connectionString,
...rest
}: FDWDropForeignTableVariables) {
const sql = wrapWithTransaction(
getDropForeignTableSql({ schema: rest.schemaName, table: rest.tableName })
)
const { result } = await executeSql({ projectRef, connectionString, sql })
return result
}
type DropForeignTableData = Awaited<ReturnType<typeof dropForeignTable>>
export const useFDWDropForeignTableMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<DropForeignTableData, ResponseError, FDWDropForeignTableVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<DropForeignTableData, ResponseError, FDWDropForeignTableVariables>({
mutationFn: (vars) => dropForeignTable(vars),
async onSuccess(data, variables, context) {
const { projectRef } = variables
await Promise.all([
queryClient.invalidateQueries({ queryKey: fdwKeys.list(projectRef), refetchType: 'all' }),
queryClient.invalidateQueries({ queryKey: entityTypeKeys.list(projectRef) }),
queryClient.invalidateQueries({ queryKey: foreignTableKeys.list(projectRef) }),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to drop foreign table for foreign data wrapper: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}