Files
supabase/apps/studio/data/materialized-views/materialized-view-delete-mutation.ts
Joshen Lim 4901f081e5 Migrate remaining requests to pg-meta API to use query endpoint (#47758)
## Context

Migrates the remaining API requests to the pg-meta endpoint to use the
query endpoint directly with the SQL from the pg-meta package. This
touches the following:
- policies
- publications
- triggers
- views
- materialized views
- types

## To test
Just need to verify that we're still fetching the data correctly on
these pages
- Database policies
- Database publications
- Database triggers
- Database tables (views + materialized views)
- Database types

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Improved and stabilized loading of database metadata (views, triggers,
RLS policies, publications, materialized views, and enum types),
including more reliable schema-scoped filtering.
* Updated policy loading behavior and related UI queries to consistently
use schema arrays, improving cache correctness and consistency.
* **Tests**
* Updated end-to-end test synchronization to wait for the correct
metadata responses using more specific request identifiers.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-09 17:09:03 +08:00

80 lines
2.3 KiB
TypeScript

import { ident, safeSql } from '@supabase/pg-meta/src/pg-format'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { materializedViewKeys } from './keys'
import { entityTypeKeys } from '@/data/entity-types/keys'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import { tableEditorKeys } from '@/data/table-editor/keys'
import type { ResponseError, UseCustomMutationOptions } from '@/types'
export type MaterializedViewDeleteVariables = {
projectRef: string
connectionString?: string | null
id: number
name: string
schema: string
cascade?: boolean
}
export async function deleteMaterializedView({
projectRef,
connectionString,
id,
name,
schema,
cascade = false,
}: MaterializedViewDeleteVariables) {
const sql = safeSql`DROP MATERIALIZED VIEW ${ident(schema)}.${ident(name)}${cascade ? safeSql` CASCADE` : safeSql``};`
const { result } = await executeSql<void>({
projectRef,
connectionString,
sql,
queryKey: ['materialized-view', 'delete', id],
})
return result
}
type MaterializedViewDeleteData = Awaited<ReturnType<typeof deleteMaterializedView>>
export const useMaterializedViewDeleteMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<
MaterializedViewDeleteData,
ResponseError,
MaterializedViewDeleteVariables
>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<MaterializedViewDeleteData, ResponseError, MaterializedViewDeleteVariables>({
mutationFn: (vars) => deleteMaterializedView(vars),
async onSuccess(data, variables, context) {
const { id, projectRef, schema } = variables
await Promise.all([
queryClient.invalidateQueries({ queryKey: tableEditorKeys.tableEditor(projectRef, id) }),
queryClient.invalidateQueries({
queryKey: materializedViewKeys.listBySchema(projectRef, [schema]),
}),
queryClient.invalidateQueries({ queryKey: entityTypeKeys.list(projectRef) }),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to delete materialized view: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}