Files
supabase/apps/studio/data/database/migration-upsert-mutation.ts
Saxon Fletcher ea38c6d153 Review and merge branch (#36795)
* allow creating branching without git

* update branching modals

* add account connections

* edit branch

* copy

* update copy

* enable branch modal changes

* add gitless branching flag

* update account connections

* merge page

* merge experiment

* update merge

* update pull requests empty state

* use diff query

* branch diffing

* diff query

* Clean up

* refinements to gitless branching

* branching merge and status

* link

* branch function diffing

* update styling

* refactor

* remove hook

* error handling

* move

* remove enable branching modal

* re-add github linker

* abstract away enable and disable

* toggle fixes

* update logic to lean on connection status

* update form logic

* sheet layout

* gitless flag

* style and workflow updates

* fix side panel size

* fix duplicate onerror

* copy changes

* refetch

* merge mutation copy

* remove import

* add cost

* allow connection details on create

* initial queries

* push button

* merge cleanup

* Fix TS issues

* Fix TS issues

* Couple of clean ups

* Revert hardcode in useFlag

* Fix TS

* layout issues and github check

* refactor

* refactor to use new field

* cleanup

* style

* failed merge

* error positioning

* refactoring merge

* workflow refactor

* hook move

* clarification with github integration

* replace branch dropdown button

* update repo picker

* updates

* remove modal

* fix small nits

* change defaults

* clean up

* disable if not gitless and no connection

* clean up

* always show workflow run id

* optimistic

* fix branch query

* fix issues

* fetch edge diff

* confirm merge

* update edge functions key

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
Co-authored-by: Alaister Young <a@alaisteryoung.com>
2025-07-04 14:57:59 +10:00

71 lines
1.8 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, put } from 'data/fetchers'
import type { ResponseError } from 'types'
import { databaseKeys } from './keys'
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<
UseMutationOptions<MigrationUpsertData, ResponseError, MigrationUpsertVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<MigrationUpsertData, ResponseError, MigrationUpsertVariables>(
(vars) => upsertMigration(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries(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,
}
)
}