Files
supabase/apps/studio/data/fdw/fdw-update-mutation.ts
Ivan Vasilov a88e89e4a8 feat: Add support for import foreign schema for wrappers (#36827)
* Add schema flag to the foreign-schema wrappers.

* Support the new mode in the create wrapper flow.

* Minor CSS tweaks.

* Re-add a missing line.

* Add source schema field.

* Make source and target schema part of the form state.

* Fix the edit mutation.

* Show a warninf for the feature if the wrappers extension is below 0.5.0.

* Bring the SchemaEditor side panel into 21st century and simplify it.

* Use the schema editor in the Create wrapper sheet to create a new schema.

* Minor fixes.

* Fix the badge border in WrapperRow.
2025-07-04 10:06:18 +02:00

102 lines
2.9 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import type { WrapperMeta } from 'components/interfaces/Integrations/Wrappers/Wrappers.types'
import { entityTypeKeys } from 'data/entity-types/keys'
import { foreignTableKeys } from 'data/foreign-tables/keys'
import { executeSql } from 'data/sql/execute-sql-query'
import { wrapWithTransaction } from 'data/sql/utils/transaction'
import { vaultSecretsKeys } from 'data/vault/keys'
import type { ResponseError } from 'types'
import { getCreateFDWSql } from './fdw-create-mutation'
import { getDeleteFDWSql } from './fdw-delete-mutation'
import { FDW } from './fdws-query'
import { fdwKeys } from './keys'
export type FDWUpdateVariables = {
projectRef?: string
connectionString?: string | null
wrapper: FDW
wrapperMeta: WrapperMeta
formState: {
[k: string]: string
}
tables: any[]
}
export const getUpdateFDWSql = ({
wrapper,
wrapperMeta,
formState,
tables,
}: Pick<FDWUpdateVariables, 'wrapper' | 'wrapperMeta' | 'formState' | 'tables'>) => {
const deleteWrapperSql = getDeleteFDWSql({ wrapper, wrapperMeta })
const createWrapperSql = getCreateFDWSql({
wrapperMeta,
formState,
tables,
mode: 'tables',
sourceSchema: '',
targetSchema: '',
})
const sql = /* SQL */ `
${deleteWrapperSql}
${createWrapperSql}
`
return sql
}
export async function updateFDW({
projectRef,
connectionString,
wrapper,
wrapperMeta,
formState,
tables,
}: FDWUpdateVariables) {
const sql = wrapWithTransaction(getUpdateFDWSql({ wrapper, wrapperMeta, formState, tables }))
const { result } = await executeSql({ projectRef, connectionString, sql })
return result
}
type FDWUpdateData = Awaited<ReturnType<typeof updateFDW>>
export const useFDWUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<FDWUpdateData, ResponseError, FDWUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<FDWUpdateData, ResponseError, FDWUpdateVariables>((vars) => updateFDW(vars), {
async onSuccess(data, variables, context) {
const { projectRef } = variables
await Promise.all([
queryClient.invalidateQueries(fdwKeys.list(projectRef), { refetchType: 'all' }),
queryClient.invalidateQueries(entityTypeKeys.list(projectRef)),
queryClient.invalidateQueries(foreignTableKeys.list(projectRef)),
queryClient.invalidateQueries(vaultSecretsKeys.list(projectRef)),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(
`Failed to update ${variables.wrapper.name} foreign data wrapper: ${data.message}`
)
} else {
onError(data, variables, context)
}
},
...options,
})
}