Files
supabase/apps/studio/data/database-columns/database-column-delete-mutation.ts
Ivan Vasilov ef651aa9ba chore: Migrate ColumnsStore (#20032)
* Add react-query mutations for columns APIs.

* Use the new delete column mutation.

* Remove the column store and replace all its methods with mutations from react-query.

* Fix type errors.

* Move some the meta store methods to be pure functions in sidepanel.utils.

* Move the createColumn and updateColumn out of the metaStore.

* Some refactors and fixes

* Shift query invalidation when deleting column to mutation file instead of component file

* reorder some code for my sanity

* remove some @ts-ignores

* remove more @ts-ignores

* Update apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts

* Fix ForeignKeyFormatter crashing client

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Alaister Young <a@alaisteryoung.com>
Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2024-01-10 15:20:18 +08:00

94 lines
3.3 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-hot-toast'
import { del } from 'data/fetchers'
import { ResponseError } from 'types'
import { sqlKeys } from 'data/sql/keys'
import { entityTypeKeys } from 'data/entity-types/keys'
import { TableLike } from 'hooks/misc/useTable'
import { tableKeys } from 'data/tables/keys'
import { viewKeys } from 'data/views/keys'
export type DatabaseColumnDeleteVariables = {
projectRef: string
connectionString?: string
id: string
cascade?: boolean
table?: TableLike
}
export async function deleteDatabaseColumn({
projectRef,
connectionString,
id,
cascade = false,
}: DatabaseColumnDeleteVariables) {
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
const { data, error } = await del('/platform/pg-meta/{ref}/columns', {
params: {
header: { 'x-connection-encrypted': connectionString! },
path: { ref: projectRef },
// cascade is expected to be a string 'true' or 'false'
query: { id, cascade: cascade.toString() },
},
headers,
})
if (error) throw error
return data
}
type DatabaseColumnDeleteData = Awaited<ReturnType<typeof deleteDatabaseColumn>>
export const useDatabaseColumnDeleteMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<DatabaseColumnDeleteData, ResponseError, DatabaseColumnDeleteVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<DatabaseColumnDeleteData, ResponseError, DatabaseColumnDeleteVariables>(
(vars) => deleteDatabaseColumn(vars),
{
async onSuccess(data, variables, context) {
const { projectRef, table } = variables
await Promise.all([
queryClient.invalidateQueries(sqlKeys.query(projectRef, ['foreign-key-constraints'])),
// refetch all entities in the sidebar because deleting a column may regenerate a view (and change its id)
queryClient.invalidateQueries(entityTypeKeys.list(projectRef)),
...(table !== undefined
? [
queryClient.invalidateQueries(tableKeys.table(projectRef, table.id)),
queryClient.invalidateQueries(
sqlKeys.query(projectRef, [table.schema, table.name])
),
queryClient.invalidateQueries(
sqlKeys.query(projectRef, ['table-definition', table.schema, table.name])
),
// invalidate all views from this schema, not sure if this is needed since you can't actually delete a column
// which has a view dependent on it
queryClient.invalidateQueries(viewKeys.listBySchema(projectRef, table.schema)),
// invalidate the view if there's a view with this id, not sure if this is needed because you can't delete a
// column from a view
queryClient.invalidateQueries(viewKeys.view(projectRef, table.id)),
]
: []),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to delete database column: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}