Skip to content

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

adds new update fdw mutation

Alaister Young58a18cfe950f
diff --git a/studio/components/interfaces/Database/Wrappers/EditWrapper.tsx b/studio/components/interfaces/Database/Wrappers/EditWrapper.tsx
index 2ba5ba6c170..7fc186b1552 100644
--- a/studio/components/interfaces/Database/Wrappers/EditWrapper.tsx
+++ b/studio/components/interfaces/Database/Wrappers/EditWrapper.tsx
@@ -18,8 +18,7 @@ import { PermissionAction } from '@supabase/shared-types/out/constants'
import { VaultSecret } from 'types'
import { checkPermissions, useParams, useStore } from 'hooks'
import { useFDWsQuery } from 'data/fdw/fdws-query'
-import { useFDWCreateMutation } from 'data/fdw/fdw-create-mutation'
-import { useFDWDeleteMutation } from 'data/fdw/fdw-delete-mutation'
+import { useFDWUpdateMutation } from 'data/fdw/fdw-update-mutation'
import InputField from './InputField'
import { WRAPPERS } from './Wrappers.constants'
@@ -55,8 +54,7 @@ const EditWrapper = () => {
const wrapper = wrappers.find((w) => Number(w.id) === Number(id))
const wrapperMeta = WRAPPERS.find((w) => w.handlerName === wrapper?.handler)
- const { mutateAsync: createFDW } = useFDWCreateMutation()
- const { mutateAsync: deleteFDW } = useFDWDeleteMutation()
+ const { mutateAsync: updateFDW } = useFDWUpdateMutation()
const [wrapperTables, setWrapperTables] = useState<any[]>([])
const [isEditingTable, setIsEditingTable] = useState(false)
@@ -83,7 +81,7 @@ const EditWrapper = () => {
if (isLoading) {
return (
- <div className="w-full h-full flex items-center justify-center">
+ <div className="flex items-center justify-center w-full h-full">
<Loading />
</div>
)
@@ -92,8 +90,8 @@ const EditWrapper = () => {
if (wrapper === undefined || wrapperMeta === undefined) {
if (isSaving) {
return (
- <div className="w-full h-full flex flex-col items-center justify-center space-y-4">
- <div className="space-x-4 flex items-center">
+ <div className="flex flex-col items-center justify-center w-full h-full space-y-4">
+ <div className="flex items-center space-x-4">
<IconLoader className="animate-spin" size={16} />
<p className="text-sm">Updating wrapper</p>
</div>
@@ -101,10 +99,10 @@ const EditWrapper = () => {
)
} else {
return (
- <div className="w-full h-full flex flex-col items-center justify-center space-y-4">
+ <div className="flex flex-col items-center justify-center w-full h-full space-y-4">
<div className="space-y-2 flex flex-col items-center w-[400px]">
<p>Unknown wrapper</p>
- <p className="text-sm text-scale-1000 text-center">
+ <p className="text-sm text-center text-scale-1000">
The wrapper ID {id} cannot be found in your project. Head back to select another
wrapper.
</p>
@@ -147,17 +145,11 @@ const EditWrapper = () => {
setIsSaving(true)
setSubmitting(true)
try {
- await deleteFDW({
- projectRef: project?.ref,
- connectionString: project?.connectionString,
- wrapper,
- wrapperMeta,
- })
-
- await createFDW({
+ await updateFDW({
projectRef: project?.ref,
connectionString: project?.connectionString,
wrapper: wrapperMeta,
+ wrapperMeta,
formState: { ...values, server_name: `${wrapper_name}_server` },
tables: wrapperTables,
})
@@ -252,7 +244,7 @@ const EditWrapper = () => {
<FormPanel
disabled={!canCreateWrapper}
footer={
- <div className="flex py-4 px-8">
+ <div className="flex px-8 py-4">
<FormActions
form={formId}
isSubmitting={isSubmitting}
@@ -364,7 +356,7 @@ const EditWrapper = () => {
</div>
)}
{wrapperTables.length === 0 && formErrors.tables && (
- <p className="text-sm text-red-900 text-right">{formErrors.tables}</p>
+ <p className="text-sm text-right text-red-900">{formErrors.tables}</p>
)}
</FormSectionContent>
</FormSection>
diff --git a/studio/data/fdw/fdw-create-mutation.ts b/studio/data/fdw/fdw-create-mutation.ts
index 4d56cf7d863..1cc30aaf0a1 100644
--- a/studio/data/fdw/fdw-create-mutation.ts
+++ b/studio/data/fdw/fdw-create-mutation.ts
@@ -2,6 +2,7 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react
import { AvailableColumn, Wrapper } from 'components/interfaces/Database/Wrappers/Wrappers.types'
import { executeSql } from 'data/sql/execute-sql-query'
import { sqlKeys } from 'data/sql/keys'
+import { wrapWithTransaction } from 'data/sql/utils/transaction'
import { useStore } from 'hooks'
export type FDWCreateVariables = {
@@ -14,7 +15,7 @@ export type FDWCreateVariables = {
tables: any[]
}
-export function getFDWCreateSql({
+export function getCreateFDWSql({
wrapper,
formState,
tables,
@@ -112,8 +113,6 @@ export function getFDWCreateSql({
.join('\n\n')
const sql = /* SQL */ `
- begin;
-
${newSchemasSql}
${createWrapperSql}
@@ -123,8 +122,6 @@ export function getFDWCreateSql({
${createServerSql}
${createTablesSql}
-
- commit;
`
return sql
@@ -137,7 +134,7 @@ export async function createFDW({
formState,
tables,
}: FDWCreateVariables) {
- const sql = getFDWCreateSql({ wrapper, formState, tables })
+ const sql = wrapWithTransaction(getCreateFDWSql({ wrapper, formState, tables }))
const { result } = await executeSql({ projectRef, connectionString, sql })
diff --git a/studio/data/fdw/fdw-delete-mutation.ts b/studio/data/fdw/fdw-delete-mutation.ts
index 4e7b98db373..c3d4940fc31 100644
--- a/studio/data/fdw/fdw-delete-mutation.ts
+++ b/studio/data/fdw/fdw-delete-mutation.ts
@@ -2,6 +2,7 @@ import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react
import { Wrapper } from 'components/interfaces/Database/Wrappers/Wrappers.types'
import { executeSql } from 'data/sql/execute-sql-query'
import { sqlKeys } from 'data/sql/keys'
+import { wrapWithTransaction } from 'data/sql/utils/transaction'
import { useStore } from 'hooks'
export type FDWDeleteVariables = {
@@ -30,13 +31,9 @@ export const getDeleteFDWSql = ({
const deleteEncryptedSecretsSql = deleteEncryptedSecretsSqlArray.join('\n')
const sql = /* SQL */ `
- begin;
-
drop foreign data wrapper if exists ${wrapper.name} cascade;
${deleteEncryptedSecretsSql}
-
- commit;
`
return sql
@@ -52,7 +49,7 @@ export async function deleteFDW({
throw new Error('projectRef is required')
}
- const sql = getDeleteFDWSql({ wrapper, wrapperMeta })
+ const sql = wrapWithTransaction(getDeleteFDWSql({ wrapper, wrapperMeta }))
const { result } = await executeSql({ projectRef, connectionString, sql })
diff --git a/studio/data/fdw/fdw-update-mutation.ts b/studio/data/fdw/fdw-update-mutation.ts
new file mode 100644
index 00000000000..d3e2fe130da
--- /dev/null
+++ b/studio/data/fdw/fdw-update-mutation.ts
@@ -0,0 +1,84 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { Wrapper } from 'components/interfaces/Database/Wrappers/Wrappers.types'
+import { executeSql } from 'data/sql/execute-sql-query'
+import { sqlKeys } from 'data/sql/keys'
+import { wrapWithTransaction } from 'data/sql/utils/transaction'
+import { useStore } from 'hooks'
+import { getCreateFDWSql } from './fdw-create-mutation'
+import { getDeleteFDWSql } from './fdw-delete-mutation'
+
+export type FDWUpdateVariables = {
+ projectRef?: string
+ connectionString?: string
+ wrapper: Wrapper
+ wrapperMeta: Wrapper
+ formState: {
+ [k: string]: string
+ }
+ tables: any[]
+}
+
+export const getUpdateFDWSql = ({
+ wrapper,
+ wrapperMeta,
+ formState,
+ tables,
+}: Pick<FDWUpdateVariables, 'wrapper' | 'wrapperMeta' | 'formState' | 'tables'>) => {
+ const deleteWrapperSql = getDeleteFDWSql({ wrapper, wrapperMeta })
+ const createWrapperSql = getCreateFDWSql({
+ wrapper: wrapperMeta,
+ formState,
+ tables,
+ })
+
+ const sql = /* SQL */ `
+ ${deleteWrapperSql}
+
+ ${createWrapperSql}
+ `
+
+ return sql
+}
+
+export async function updateFDW({
+ projectRef,
+ connectionString,
+ wrapper,
+ wrapperMeta,
+ formState,
+ tables,
+}: FDWUpdateVariables) {
+ if (!projectRef) {
+ throw new Error('projectRef is required')
+ }
+
+ const sql = wrapWithTransaction(getUpdateFDWSql({ wrapper, wrapperMeta, formState, tables }))
+
+ const { result } = await executeSql({ projectRef, connectionString, sql })
+
+ return result
+}
+
+type FDWUpdateData = Awaited<ReturnType<typeof updateFDW>>
+
+export const useFDWUpdateMutation = ({
+ onSuccess,
+ ...options
+}: Omit<UseMutationOptions<FDWUpdateData, unknown, FDWUpdateVariables>, 'mutationFn'> = {}) => {
+ const queryClient = useQueryClient()
+ const { vault } = useStore()
+
+ return useMutation<FDWUpdateData, unknown, FDWUpdateVariables>((vars) => updateFDW(vars), {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+
+ await Promise.all([
+ queryClient.invalidateQueries(sqlKeys.query(projectRef, ['fdws'])),
+ vault.load(),
+ ])
+
+ await onSuccess?.(data, variables, context)
+ },
+ ...options,
+ })
+}
diff --git a/studio/data/sql/utils/transaction.ts b/studio/data/sql/utils/transaction.ts
new file mode 100644
index 00000000000..36fa24b2571
--- /dev/null
+++ b/studio/data/sql/utils/transaction.ts
@@ -0,0 +1,9 @@
+export function wrapWithTransaction(sql: string) {
+ return /* SQL */ `
+ begin;
+
+ ${sql}
+
+ commit;
+ `
+}