Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

delete fdw

Alaister Young8ec82cbad7f8
diff --git a/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx b/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx
index 243ba078e1e..f59ea1bfb3d 100644
--- a/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx
+++ b/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx
@@ -7,21 +7,24 @@ import { confirmAlert } from 'components/to-be-cleaned/ModalsDeprecated/ConfirmM
import { checkPermissions, useStore } from 'hooks'
import { Wrapper } from './types'
import WrapperEditor from './WrapperEditor'
+import { useFDWDeleteMutation } from 'data/fdw/fdw-delete-mutation'
+import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
export type WrapperCardProps = {
wrapper: Wrapper
+ enabled?: boolean
}
-const WrapperCard = ({ wrapper }: WrapperCardProps) => {
+const WrapperCard = ({ wrapper, enabled = false }: WrapperCardProps) => {
+ const { project } = useProjectContext()
const { ui } = useStore()
- const isOn = false
- const [loading, setLoading] = useState(false)
-
const canUpdateWrappers = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'wrappers')
const [isEditorVisible, setIsEditorVisible] = useState(false)
+ const { mutateAsync: deleteFDW, isLoading } = useFDWDeleteMutation()
+
async function enableWrapper() {
setIsEditorVisible(true)
}
@@ -29,29 +32,19 @@ const WrapperCard = ({ wrapper }: WrapperCardProps) => {
async function disableWrapper() {
confirmAlert({
title: 'Confirm to disable wrapper',
- message: `Are you sure you want to turn OFF "${wrapper.name}" wrapper?`,
+ message: `Are you sure you want to turn OFF "${wrapper.name}" wrapper? This will also delete ALL tables created with this wrapper.`,
onAsyncConfirm: async () => {
try {
- setLoading(true)
- // const response: any = await meta.wrappers.del(wrapper.name)
- // if (response.error) {
- // throw response.error
- // } else {
- // ui.setNotification({
- // category: 'success',
- // message: `${wrapper.name.toUpperCase()} is off.`,
- // })
- // }
+ await deleteFDW({
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ name: wrapper.name,
+ })
} catch (error: any) {
ui.setNotification({
category: 'error',
- message: `Toggle ${wrapper.name.toUpperCase()} failed: ${error.message}`,
+ message: `Disabling ${wrapper.name} failed: ${error.message}`,
})
- } finally {
- // Need to reload them because the delete function
- // removes the wrapper from the store
- // meta.wrappers.load()
- setLoading(false)
}
},
})
@@ -77,14 +70,14 @@ const WrapperCard = ({ wrapper }: WrapperCardProps) => {
>
{wrapper.label}
</h3>
- {loading ? (
+ {isLoading ? (
<IconLoader className="animate-spin" size={16} />
) : (
<Toggle
size="tiny"
- checked={isOn}
+ checked={enabled}
disabled={!canUpdateWrappers}
- onChange={() => (isOn ? disableWrapper() : enableWrapper())}
+ onChange={() => (enabled ? disableWrapper() : enableWrapper())}
/>
)}
</div>
diff --git a/studio/components/interfaces/Database/Wrappers/Wrappers.tsx b/studio/components/interfaces/Database/Wrappers/Wrappers.tsx
index 79a975b6a13..69ef2c83c80 100644
--- a/studio/components/interfaces/Database/Wrappers/Wrappers.tsx
+++ b/studio/components/interfaces/Database/Wrappers/Wrappers.tsx
@@ -28,7 +28,7 @@ const Wrappers = () => {
<h4 className="text-lg">Enabled wrappers</h4>
<div className="grid grid-cols-1 gap-6 mb-4 md:grid-cols-2 xl:grid-cols-3">
{enabledWrappers.map((wrapper) => (
- <WrapperCard key={wrapper.name} wrapper={wrapper} />
+ <WrapperCard key={wrapper.name} wrapper={wrapper} enabled={true} />
))}
</div>
</div>
@@ -39,7 +39,7 @@ const Wrappers = () => {
<h4 className="text-lg">Available wrappers</h4>
<div className="grid grid-cols-1 gap-6 mb-4 md:grid-cols-2 xl:grid-cols-3">
{disabledWrappers.map((wrapper) => (
- <WrapperCard key={wrapper.name} wrapper={wrapper} />
+ <WrapperCard key={wrapper.name} wrapper={wrapper} enabled={false} />
))}
</div>
</div>
diff --git a/studio/data/custom-domains/custom-domains-delete-mutation.ts b/studio/data/custom-domains/custom-domains-delete-mutation.ts
index 014ce571e4d..50e992f4831 100644
--- a/studio/data/custom-domains/custom-domains-delete-mutation.ts
+++ b/studio/data/custom-domains/custom-domains-delete-mutation.ts
@@ -1,6 +1,5 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { delete_ } from 'lib/common/fetch'
-import { getCustomDomains } from './custom-domains-query'
import { customDomainKeys } from './keys'
export type CustomDomainDeleteVariables = {
diff --git a/studio/data/fdw/fdw-create-mutation.ts b/studio/data/fdw/fdw-create-mutation.ts
index 65fb878e20c..5cd08b6d7c6 100644
--- a/studio/data/fdw/fdw-create-mutation.ts
+++ b/studio/data/fdw/fdw-create-mutation.ts
@@ -138,7 +138,9 @@ export const useFDWCreateMutation = ({
return useMutation<FDWCreateData, unknown, FDWCreateVariables>((vars) => createFDW(vars), {
async onSuccess(data, variables, context) {
- await queryClient.invalidateQueries(sqlKeys.query(variables.projectRef, ['fdws']))
+ const { projectRef } = variables
+
+ await queryClient.invalidateQueries(sqlKeys.query(projectRef, ['fdws']))
await onSuccess?.(data, variables, context)
},
diff --git a/studio/data/fdw/fdw-delete-mutation.ts b/studio/data/fdw/fdw-delete-mutation.ts
new file mode 100644
index 00000000000..854555343d4
--- /dev/null
+++ b/studio/data/fdw/fdw-delete-mutation.ts
@@ -0,0 +1,49 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { executeSql } from 'data/sql/execute-sql-query'
+import { sqlKeys } from 'data/sql/keys'
+
+export type FDWDeleteVariables = {
+ projectRef?: string
+ connectionString?: string
+ name: string
+}
+
+export const getDeleteFDWSql = ({ name }: Pick<FDWDeleteVariables, 'name'>) => {
+ const sql = /* SQL */ `
+ drop foreign data wrapper if exists ${name} cascade;
+ `
+
+ return sql
+}
+
+export async function deleteFDW({ projectRef, connectionString, name }: FDWDeleteVariables) {
+ if (!projectRef) {
+ throw new Error('projectRef is required')
+ }
+
+ const sql = getDeleteFDWSql({ name })
+
+ const { result } = await executeSql({ projectRef, connectionString, sql })
+
+ return result
+}
+
+type FDWDeleteData = Awaited<ReturnType<typeof deleteFDW>>
+
+export const useFDWDeleteMutation = ({
+ onSuccess,
+ ...options
+}: Omit<UseMutationOptions<FDWDeleteData, unknown, FDWDeleteVariables>, 'mutationFn'> = {}) => {
+ const queryClient = useQueryClient()
+
+ return useMutation<FDWDeleteData, unknown, FDWDeleteVariables>((vars) => deleteFDW(vars), {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+
+ await queryClient.invalidateQueries(sqlKeys.query(projectRef, ['fdws']))
+
+ await onSuccess?.(data, variables, context)
+ },
+ ...options,
+ })
+}