mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 09:20:21 -04:00
69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { databaseKeys } from './keys'
|
|
import { handleError, put } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type MigrationUpsertVariables = {
|
|
projectRef: string
|
|
query: string
|
|
name?: string
|
|
idempotencyKey?: string
|
|
}
|
|
|
|
export async function upsertMigration({
|
|
projectRef,
|
|
query,
|
|
name,
|
|
idempotencyKey,
|
|
}: MigrationUpsertVariables) {
|
|
const headers: Record<string, string> = {}
|
|
if (idempotencyKey) {
|
|
headers['Idempotency-Key'] = idempotencyKey
|
|
}
|
|
|
|
const body: { query: string; name?: string } = { query }
|
|
if (name) {
|
|
body.name = name
|
|
}
|
|
|
|
const { data, error } = await put('/v1/projects/{ref}/database/migrations', {
|
|
params: { path: { ref: projectRef } },
|
|
body,
|
|
headers,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type MigrationUpsertData = Awaited<ReturnType<typeof upsertMigration>>
|
|
|
|
export const useMigrationUpsertMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<MigrationUpsertData, ResponseError, MigrationUpsertVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
return useMutation<MigrationUpsertData, ResponseError, MigrationUpsertVariables>({
|
|
mutationFn: (vars) => upsertMigration(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databaseKeys.migrations(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to upsert migration: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|