Skip to content

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

Finalize clean up logic for deleting analytics buckets (#40461)

Joshen Lim975986f33fb2
diff --git a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/AnalyticsBucketDetails.utils.ts b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/AnalyticsBucketDetails.utils.ts
index e52f59273ea..871195c02d8 100644
--- a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/AnalyticsBucketDetails.utils.ts
+++ b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/AnalyticsBucketDetails.utils.ts
@@ -22,3 +22,7 @@ export const getNamespaceTableNameFromPostgresTableName = (table: {
}) => {
return `${snakeCase(`${table.schema}.${table.name}`)}_changelog`
}
+
+export const getAnalyticsBucketsDestinationName = (bucketId: string) => {
+ return `${snakeCase(bucketId)}_destination`
+}
diff --git a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/ConnectTablesDialog.tsx b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/ConnectTablesDialog.tsx
index db8302bf601..afa0b2d2154 100644
--- a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/ConnectTablesDialog.tsx
+++ b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/ConnectTablesDialog.tsx
@@ -1,6 +1,5 @@
import { zodResolver } from '@hookform/resolvers/zod'
import { AnimatePresence, motion } from 'framer-motion'
-import { snakeCase } from 'lodash'
import { Loader2, Plus } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { SubmitHandler, useForm } from 'react-hook-form'
@@ -41,7 +40,10 @@ import {
import { Admonition } from 'ui-patterns'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { MultiSelector } from 'ui-patterns/multi-select'
-import { getAnalyticsBucketPublicationName } from './AnalyticsBucketDetails.utils'
+import {
+ getAnalyticsBucketPublicationName,
+ getAnalyticsBucketsDestinationName,
+} from './AnalyticsBucketDetails.utils'
import { useAnalyticsBucketAssociatedEntities } from './useAnalyticsBucketAssociatedEntities'
import { useAnalyticsBucketWrapperInstance } from './useAnalyticsBucketWrapperInstance'
@@ -237,7 +239,7 @@ export const ConnectTablesDialogContent = ({
s3SecretAccessKey,
s3Region,
}
- const destinationName = `${snakeCase(bucketId)}_destination`
+ const destinationName = getAnalyticsBucketsDestinationName(bucketId)
const { pipeline_id: pipelineId } = await createDestinationPipeline({
projectRef,
destinationName,
diff --git a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/useAnalyticsBucketAssociatedEntities.tsx b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/useAnalyticsBucketAssociatedEntities.tsx
index 41a3c6aa334..f864a6862a0 100644
--- a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/useAnalyticsBucketAssociatedEntities.tsx
+++ b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/useAnalyticsBucketAssociatedEntities.tsx
@@ -1,20 +1,20 @@
import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { WrapperMeta } from 'components/interfaces/Integrations/Wrappers/Wrappers.types'
+import { useDeleteDestinationPipelineMutation } from 'data/etl/delete-destination-pipeline-mutation'
+import { useReplicationDestinationsQuery } from 'data/etl/destinations-query'
import { useReplicationPipelinesQuery } from 'data/etl/pipelines-query'
-import {
- ReplicationPublication,
- useReplicationPublicationsQuery,
-} from 'data/etl/publications-query'
+import { useDeletePublicationMutation } from 'data/etl/publication-delete-mutation'
+import { useReplicationPublicationsQuery } from 'data/etl/publications-query'
import { useReplicationSourcesQuery } from 'data/etl/sources-query'
import { useFDWDeleteMutation } from 'data/fdw/fdw-delete-mutation'
-import { FDW } from 'data/fdw/fdws-query'
import { useS3AccessKeyDeleteMutation } from 'data/storage/s3-access-key-delete-mutation'
-import { S3AccessKey, useStorageCredentialsQuery } from 'data/storage/s3-access-key-query'
+import { useStorageCredentialsQuery } from 'data/storage/s3-access-key-query'
import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'
+import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import {
getAnalyticsBucketPublicationName,
getAnalyticsBucketS3KeyName,
+ getAnalyticsBucketsDestinationName,
} from './AnalyticsBucketDetails.utils'
import { useAnalyticsBucketWrapperInstance } from './useAnalyticsBucketWrapperInstance'
@@ -58,44 +58,62 @@ export const useAnalyticsBucketAssociatedEntities = (
(p) => p.name === getAnalyticsBucketPublicationName(bucketId ?? '')
)
+ const { data: destinationsData } = useReplicationDestinationsQuery({ projectRef })
+ const destinations = destinationsData?.destinations ?? []
+ const destination = destinations.find(
+ (x) => x.name === getAnalyticsBucketsDestinationName(bucketId ?? '')
+ )
+
const { data: pipelines } = useReplicationPipelinesQuery({ projectRef })
const pipeline = pipelines?.pipelines.find((x) => x.config.publication_name === publication?.name)
- return { icebergWrapper, icebergWrapperMeta, s3AccessKey, sourceId, publication, pipeline }
+ return {
+ icebergWrapper,
+ icebergWrapperMeta,
+ s3AccessKey,
+ sourceId,
+ publication,
+ pipeline,
+ destination,
+ }
}
-export const useAnalyticsBucketDeleteCleanUp = () => {
+export const useAnalyticsBucketDeleteCleanUp = ({
+ projectRef,
+ bucketId,
+}: {
+ projectRef?: string
+ bucketId?: string
+}) => {
+ const { data: project } = useSelectedProjectQuery()
+ const {
+ icebergWrapper,
+ icebergWrapperMeta,
+ s3AccessKey,
+ publication,
+ sourceId,
+ pipeline,
+ destination,
+ } = useAnalyticsBucketAssociatedEntities({ projectRef, bucketId: bucketId })
+
+ // Default error handlers from all mutations will be silenced
const { mutateAsync: deleteFDW, isLoading: isDeletingWrapper } = useFDWDeleteMutation({
- // Silence default error handler toast
onError: () => {},
})
-
const { mutateAsync: deleteS3AccessKey, isLoading: isDeletingKey } = useS3AccessKeyDeleteMutation(
- {
- // Silence default error handler toast
- onError: () => {},
- }
+ { onError: () => {} }
)
+ const { mutateAsync: deletePublication, isLoading: isDeletingPublication } =
+ useDeletePublicationMutation({ onError: () => {} })
+ const { mutateAsync: deletePipeline, isLoading: isDeletingPipeline } =
+ useDeleteDestinationPipelineMutation({ onError: () => {} })
- const isDeleting = isDeletingWrapper || isDeletingKey
+ const isDeleting =
+ isDeletingWrapper || isDeletingKey || isDeletingPublication || isDeletingPipeline
+
+ const mutateAsync = async () => {
+ const connectionString = project?.connectionString
- const mutateAsync = async ({
- bucketId,
- projectRef,
- connectionString,
- icebergWrapper,
- icebergWrapperMeta,
- s3AccessKey,
- publication,
- }: {
- bucketId?: string
- projectRef?: string
- connectionString?: string
- icebergWrapper?: FDW
- icebergWrapperMeta?: WrapperMeta
- s3AccessKey?: S3AccessKey
- publication?: ReplicationPublication
- }) => {
if (!!icebergWrapper && !!icebergWrapperMeta) {
try {
await deleteFDW({
@@ -121,9 +139,23 @@ export const useAnalyticsBucketDeleteCleanUp = () => {
console.warn(`Unable to find and delete corresponding S3 access key for ${bucketId}`)
}
- if (!!publication) {
+ if (!!pipeline && !!destination) {
+ try {
+ await deletePipeline({
+ projectRef,
+ destinationId: destination?.id,
+ pipelineId: pipeline.id,
+ })
+ } catch (error: any) {
+ console.error(`Failed to delete replication pipeline for: ${bucketId}`, error.message)
+ }
+ } else {
+ console.warn(`Unable to find and delete replication pipeline for ${bucketId}`)
+ }
+
+ if (!!publication && !!sourceId) {
try {
- // [TODO] Delete the publication
+ await deletePublication({ projectRef, sourceId, publicationName: publication.name })
} catch (error: any) {
console.error(`Failed to delete replication publication for: ${bucketId}`, error.message)
}
diff --git a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/DeleteAnalyticsBucketModal.tsx b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/DeleteAnalyticsBucketModal.tsx
index 2c3d539e947..1367552aba0 100644
--- a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/DeleteAnalyticsBucketModal.tsx
+++ b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/DeleteAnalyticsBucketModal.tsx
@@ -4,10 +4,7 @@ import { useParams } from 'common'
import { useAnalyticsBucketDeleteMutation } from 'data/storage/analytics-bucket-delete-mutation'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import TextConfirmModal from 'ui-patterns/Dialogs/TextConfirmModal'
-import {
- useAnalyticsBucketAssociatedEntities,
- useAnalyticsBucketDeleteCleanUp,
-} from './AnalyticsBucketDetails/useAnalyticsBucketAssociatedEntities'
+import { useAnalyticsBucketDeleteCleanUp } from './AnalyticsBucketDetails/useAnalyticsBucketAssociatedEntities'
export interface DeleteAnalyticsBucketModalProps {
visible: boolean
@@ -25,26 +22,13 @@ export const DeleteAnalyticsBucketModal = ({
const { ref: projectRef } = useParams()
const { data: project } = useSelectedProjectQuery()
- const { icebergWrapper, icebergWrapperMeta, s3AccessKey, publication } =
- useAnalyticsBucketAssociatedEntities({ projectRef, bucketId: bucketId })
-
const { mutateAsync: deleteAnalyticsBucketCleanUp, isLoading: isCleaningUpAnalyticsBucket } =
- useAnalyticsBucketDeleteCleanUp()
+ useAnalyticsBucketDeleteCleanUp({ projectRef, bucketId })
const { mutate: deleteAnalyticsBucket, isLoading: isDeletingAnalyticsBucket } =
useAnalyticsBucketDeleteMutation({
onSuccess: async () => {
- if (project?.connectionString) {
- await deleteAnalyticsBucketCleanUp({
- projectRef,
- connectionString: project.connectionString,
- bucketId: bucketId,
- icebergWrapper,
- icebergWrapperMeta,
- s3AccessKey,
- publication,
- })
- }
+ if (project?.connectionString) await deleteAnalyticsBucketCleanUp()
toast.success(`Successfully deleted analytics bucket ${bucketId}`)
onClose()
onSuccess?.()
diff --git a/apps/studio/data/etl/delete-destination-pipeline-mutation.ts b/apps/studio/data/etl/delete-destination-pipeline-mutation.ts
index 5df1384074a..5ea5dfd66fb 100644
--- a/apps/studio/data/etl/delete-destination-pipeline-mutation.ts
+++ b/apps/studio/data/etl/delete-destination-pipeline-mutation.ts
@@ -6,7 +6,7 @@ import type { ResponseError, UseCustomMutationOptions } from 'types'
import { replicationKeys } from './keys'
export type DeleteDestinationPipelineParams = {
- projectRef: string
+ projectRef?: string
destinationId: number
pipelineId: number
}
diff --git a/apps/studio/data/etl/publication-delete-mutation.ts b/apps/studio/data/etl/publication-delete-mutation.ts
index ffae14965d0..2457c0a3628 100644
--- a/apps/studio/data/etl/publication-delete-mutation.ts
+++ b/apps/studio/data/etl/publication-delete-mutation.ts
@@ -6,7 +6,7 @@ import type { ResponseError, UseCustomMutationOptions } from 'types'
import { replicationKeys } from './keys'
export type DeletePublicationParams = {
- projectRef: string
+ projectRef?: string
sourceId: number
publicationName: string
}