Skip to content

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

Ensure that backup is always available for download in restoring, restore failed or pause failed state (#29361)

Joshen Lim743bcb7e3cd8
diff --git a/apps/studio/components/layouts/ProjectLayout/PauseFailedState.tsx b/apps/studio/components/layouts/ProjectLayout/PauseFailedState.tsx
index 1fa1b12c642..88eb1049fe9 100644
--- a/apps/studio/components/layouts/ProjectLayout/PauseFailedState.tsx
+++ b/apps/studio/components/layouts/ProjectLayout/PauseFailedState.tsx
@@ -1,40 +1,112 @@
+import { Download, MoreVertical, Trash } from 'lucide-react'
import Link from 'next/link'
-import { Button, CriticalIcon } from 'ui'
+import { useState } from 'react'
+import { useParams } from 'common'
+import DeleteProjectModal from 'components/interfaces/Settings/General/DeleteProjectPanel/DeleteProjectModal'
+import { ButtonTooltip } from 'components/ui/ButtonTooltip'
+import { useBackupDownloadMutation } from 'data/database/backup-download-mutation'
+import { useDownloadableBackupQuery } from 'data/database/backup-query'
+import {
+ Button,
+ CriticalIcon,
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
+} from 'ui'
import { useProjectContext } from './ProjectContext'
const PauseFailedState = () => {
+ const { ref } = useParams()
const { project } = useProjectContext()
+ const [visible, setVisible] = useState(false)
+
+ const { data } = useDownloadableBackupQuery({ projectRef: ref })
+ const backups = data?.backups ?? []
+
+ const { mutate: downloadBackup, isLoading: isDownloading } = useBackupDownloadMutation({
+ onSuccess: (res) => {
+ const { fileUrl } = res
+
+ // Trigger browser download by create,trigger and remove tempLink
+ const tempLink = document.createElement('a')
+ tempLink.href = fileUrl
+ document.body.appendChild(tempLink)
+ tempLink.click()
+ document.body.removeChild(tempLink)
+ },
+ })
+
+ const onClickDownloadBackup = () => {
+ if (!ref) return console.error('Project ref is required')
+ if (backups.length === 0) return console.error('No available backups to download')
+ downloadBackup({ ref, backup: backups[0] })
+ }
return (
- <div className="flex items-center justify-center h-full">
- <div className="bg-surface-100 border border-overlay rounded-md w-3/4 lg:w-1/2">
- <div className="space-y-6 pt-6">
- <div className="flex px-8 space-x-8">
- <div className="mt-1">
- <CriticalIcon className="w-5 h-5" />
+ <>
+ <div className="flex items-center justify-center h-full">
+ <div className="bg-surface-100 border border-overlay rounded-md w-3/4 lg:w-1/2">
+ <div className="space-y-6 pt-6">
+ <div className="flex px-8 space-x-8">
+ <div className="mt-1">
+ <CriticalIcon className="w-5 h-5" />
+ </div>
+ <div className="space-y-1">
+ <p>Something went wrong while pausing your project</p>
+ <p className="text-sm text-foreground-light">
+ Your project's data is intact, but your project is inaccessible due to the failure
+ while pausing. Please contact support for assistance.
+ </p>
+ </div>
</div>
- <div className="space-y-1">
- <p>Something went wrong while pausing your project</p>
- <p className="text-sm text-foreground-light">
- Your project's data is intact, but your project is inaccessible due to the failure
- while pausing. Please contact support for assistance.
- </p>
- </div>
- </div>
- <div className="border-t border-overlay flex items-center justify-end py-4 px-8">
- <Button asChild type="default">
- <Link
- href={`/support/new?category=Database_unresponsive&ref=${project?.ref}&subject=Restoration%20failed%20for%20project`}
+ <div className="border-t border-overlay flex items-center justify-end gap-x-2 py-4 px-8">
+ <Button asChild type="default">
+ <Link
+ href={`/support/new?category=Database_unresponsive&ref=${project?.ref}&subject=Restoration%20failed%20for%20project`}
+ >
+ Contact support
+ </Link>
+ </Button>
+ <ButtonTooltip
+ type="default"
+ icon={<Download />}
+ loading={isDownloading}
+ disabled={backups.length === 0}
+ tooltip={{ content: { side: 'bottom', text: 'No available backups to download' } }}
+ onClick={onClickDownloadBackup}
>
- Contact support
- </Link>
- </Button>
+ Download backup
+ </ButtonTooltip>
+ <DropdownMenu>
+ <DropdownMenuTrigger>
+ <Button type="default" className="px-1.5" icon={<MoreVertical />} />
+ </DropdownMenuTrigger>
+ <DropdownMenuContent className="w-72" align="end">
+ <DropdownMenuItem
+ onClick={() => setVisible(true)}
+ className="items-start gap-x-2"
+ >
+ <div className="translate-y-0.5">
+ <Trash size={14} />
+ </div>
+ <div className="">
+ <p>Delete project</p>
+ <p className="text-foreground-lighter">
+ Project cannot be restored once it is deleted
+ </p>
+ </div>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
</div>
</div>
</div>
- </div>
+ <DeleteProjectModal visible={visible} onClose={() => setVisible(false)} />
+ </>
)
}
diff --git a/apps/studio/components/layouts/ProjectLayout/PausingState.tsx b/apps/studio/components/layouts/ProjectLayout/PausingState.tsx
index 5b1b14a57f0..1c4c55c0720 100644
--- a/apps/studio/components/layouts/ProjectLayout/PausingState.tsx
+++ b/apps/studio/components/layouts/ProjectLayout/PausingState.tsx
@@ -53,7 +53,7 @@ const PausingState = ({ project }: PausingStateProps) => {
</div>
</div>
<div className="mx-auto mt-8 mb-16 w-full max-w-7xl">
- <div className="mx-6 flex h-[500px] items-center justify-center rounded border border-overlay bg-surface-100 p-8">
+ <div className="flex h-[500px] items-center justify-center rounded border border-overlay bg-surface-100 p-8">
<div className="grid w-[380px] gap-4">
<div className="relative mx-auto max-w-[300px]">
<div className="absolute flex h-full w-full items-center justify-center">
@@ -62,7 +62,7 @@ const PausingState = ({ project }: PausingStateProps) => {
<Circle className="text-foreground-lighter" size={50} strokeWidth={1.5} />
</div>
<p className="text-center">Pausing {project.name}</p>
- <p className="mt-4 text-center text-sm text-foreground-light">
+ <p className="text-center text-sm text-foreground-light">
You may restore your project anytime thereafter, and your data will be restored to
when it was initially paused.
</p>
diff --git a/apps/studio/components/layouts/ProjectLayout/RestoreFailedState.tsx b/apps/studio/components/layouts/ProjectLayout/RestoreFailedState.tsx
index 8c08328cb7c..8b416e8a6a0 100644
--- a/apps/studio/components/layouts/ProjectLayout/RestoreFailedState.tsx
+++ b/apps/studio/components/layouts/ProjectLayout/RestoreFailedState.tsx
@@ -1,8 +1,12 @@
-import { MoreVertical, Trash } from 'lucide-react'
+import { Download, MoreVertical, Trash } from 'lucide-react'
import Link from 'next/link'
import { useState } from 'react'
+import { useParams } from 'common'
import DeleteProjectModal from 'components/interfaces/Settings/General/DeleteProjectPanel/DeleteProjectModal'
+import { ButtonTooltip } from 'components/ui/ButtonTooltip'
+import { useBackupDownloadMutation } from 'data/database/backup-download-mutation'
+import { useDownloadableBackupQuery } from 'data/database/backup-query'
import {
Button,
CriticalIcon,
@@ -14,9 +18,32 @@ import {
import { useProjectContext } from './ProjectContext'
const RestoreFailedState = () => {
+ const { ref } = useParams()
const { project } = useProjectContext()
const [visible, setVisible] = useState(false)
+ const { data } = useDownloadableBackupQuery({ projectRef: ref })
+ const backups = data?.backups ?? []
+
+ const { mutate: downloadBackup, isLoading: isDownloading } = useBackupDownloadMutation({
+ onSuccess: (res) => {
+ const { fileUrl } = res
+
+ // Trigger browser download by create,trigger and remove tempLink
+ const tempLink = document.createElement('a')
+ tempLink.href = fileUrl
+ document.body.appendChild(tempLink)
+ tempLink.click()
+ document.body.removeChild(tempLink)
+ },
+ })
+
+ const onClickDownloadBackup = () => {
+ if (!ref) return console.error('Project ref is required')
+ if (backups.length === 0) return console.error('No available backups to download')
+ downloadBackup({ ref, backup: backups[0] })
+ }
+
return (
<>
<div className="flex items-center justify-center h-full">
@@ -43,6 +70,16 @@ const RestoreFailedState = () => {
Contact support
</Link>
</Button>
+ <ButtonTooltip
+ type="default"
+ icon={<Download />}
+ loading={isDownloading}
+ disabled={backups.length === 0}
+ tooltip={{ content: { side: 'bottom', text: 'No available backups to download' } }}
+ onClick={onClickDownloadBackup}
+ >
+ Download backup
+ </ButtonTooltip>
<DropdownMenu>
<DropdownMenuTrigger>
<Button type="default" className="px-1.5" icon={<MoreVertical />} />
diff --git a/apps/studio/components/layouts/ProjectLayout/RestoringState.tsx b/apps/studio/components/layouts/ProjectLayout/RestoringState.tsx
index cbaf05fe45e..d2d5825f67e 100644
--- a/apps/studio/components/layouts/ProjectLayout/RestoringState.tsx
+++ b/apps/studio/components/layouts/ProjectLayout/RestoringState.tsx
@@ -1,14 +1,18 @@
import { useQueryClient } from '@tanstack/react-query'
-import { useParams } from 'common'
+import { CheckCircle, Download, Loader } from 'lucide-react'
+import Link from 'next/link'
import { useEffect, useRef, useState } from 'react'
-import { Button } from 'ui'
+import { useParams } from 'common'
+import { ButtonTooltip } from 'components/ui/ButtonTooltip'
+import { useBackupDownloadMutation } from 'data/database/backup-download-mutation'
+import { useDownloadableBackupQuery } from 'data/database/backup-query'
import { projectKeys } from 'data/projects/keys'
import { invalidateProjectDetailsQuery } from 'data/projects/project-detail-query'
import { getWithTimeout } from 'lib/common/fetch'
import { API_URL, PROJECT_STATUS } from 'lib/constants'
+import { Button } from 'ui'
import { useProjectContext } from './ProjectContext'
-import { CheckCircle, Loader } from 'lucide-react'
const RestoringState = () => {
const { ref } = useParams()
@@ -19,10 +23,27 @@ const RestoringState = () => {
const [loading, setLoading] = useState(false)
const [isCompleted, setIsCompleted] = useState(false)
- useEffect(() => {
- checkServerInterval.current = window.setInterval(checkServer, 4000)
- return () => clearInterval(checkServerInterval.current)
- }, [])
+ const { data } = useDownloadableBackupQuery({ projectRef: ref })
+ const backups = data?.backups ?? []
+
+ const { mutate: downloadBackup, isLoading: isDownloading } = useBackupDownloadMutation({
+ onSuccess: (res) => {
+ const { fileUrl } = res
+
+ // Trigger browser download by create,trigger and remove tempLink
+ const tempLink = document.createElement('a')
+ tempLink.href = fileUrl
+ document.body.appendChild(tempLink)
+ tempLink.click()
+ document.body.removeChild(tempLink)
+ },
+ })
+
+ const onClickDownloadBackup = () => {
+ if (!ref) return console.error('Project ref is required')
+ if (backups.length === 0) return console.error('No available backups to download')
+ downloadBackup({ ref, backup: backups[0] })
+ }
async function checkServer() {
if (!project) return
@@ -48,6 +69,11 @@ const RestoringState = () => {
if (ref) await invalidateProjectDetailsQuery(queryClient, ref)
}
+ useEffect(() => {
+ checkServerInterval.current = window.setInterval(checkServer, 4000)
+ return () => clearInterval(checkServerInterval.current)
+ }, [])
+
return (
<div className="flex items-center justify-center h-full">
<div className="bg-surface-100 border border-overlay rounded-md w-3/4 lg:w-1/2">
@@ -86,6 +112,25 @@ const RestoringState = () => {
</div>
</div>
)}
+ <div className="border-t border-overlay flex items-center justify-end py-4 px-8 gap-x-2">
+ <Button asChild type="default">
+ <Link
+ href={`/support/new?category=Database_unresponsive&ref=${project?.ref}&subject=Restoration%20failed%20for%20project`}
+ >
+ Contact support
+ </Link>
+ </Button>
+ <ButtonTooltip
+ type="default"
+ icon={<Download />}
+ loading={isDownloading}
+ disabled={backups.length === 0}
+ tooltip={{ content: { side: 'bottom', text: 'No available backups to download' } }}
+ onClick={onClickDownloadBackup}
+ >
+ Download backup
+ </ButtonTooltip>
+ </div>
</div>
</div>
)
diff --git a/apps/studio/data/database/backup-query.ts b/apps/studio/data/database/backup-query.ts
new file mode 100644
index 00000000000..b4f6430dfa6
--- /dev/null
+++ b/apps/studio/data/database/backup-query.ts
@@ -0,0 +1,40 @@
+import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+
+import { get, handleError } from 'data/fetchers'
+import type { ResponseError } from 'types'
+import { databaseKeys } from './keys'
+
+export type DownloadableBackupVariables = {
+ projectRef?: string
+}
+
+export async function getDownloadableBackup(
+ { projectRef }: DownloadableBackupVariables,
+ signal?: AbortSignal
+) {
+ if (!projectRef) throw new Error('Project ref is required')
+
+ const { data, error } = await get(`/platform/database/{ref}/backups/downloadable-backups`, {
+ params: { path: { ref: projectRef } },
+ signal,
+ })
+
+ if (error) handleError(error)
+ return data
+}
+
+export type DownloadableBackupData = Awaited<ReturnType<typeof getDownloadableBackup>>
+export type DownloadableBackupError = ResponseError
+
+export const useDownloadableBackupQuery = <TData = DownloadableBackupData>(
+ { projectRef }: DownloadableBackupVariables,
+ {
+ enabled = true,
+ ...options
+ }: UseQueryOptions<DownloadableBackupData, DownloadableBackupError, TData> = {}
+) =>
+ useQuery<DownloadableBackupData, DownloadableBackupError, TData>(
+ databaseKeys.backups(projectRef),
+ ({ signal }) => getDownloadableBackup({ projectRef }, signal),
+ { enabled: enabled && typeof projectRef !== 'undefined', ...options }
+ )