Skip to content

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

Support temp enable db writes (#18249)

Joshen Lim317a177b5d50
diff --git a/studio/components/interfaces/Settings/Database/DatabaseSettings/ConfirmDisableReadOnlyModal.tsx b/studio/components/interfaces/Settings/Database/DatabaseSettings/ConfirmDisableReadOnlyModal.tsx
new file mode 100644
index 00000000000..1ed5977e7d0
--- /dev/null
+++ b/studio/components/interfaces/Settings/Database/DatabaseSettings/ConfirmDisableReadOnlyModal.tsx
@@ -0,0 +1,53 @@
+import { useParams } from 'common'
+import toast from 'react-hot-toast'
+import { Modal } from 'ui'
+
+import { useDisableReadOnlyModeMutation } from 'data/config/project-temp-disable-read-only-mutation'
+
+interface ConfirmDisableReadOnlyModeModalProps {
+ visible: boolean
+ onClose: () => void
+}
+
+const ConfirmDisableReadOnlyModeModal = ({
+ visible,
+ onClose,
+}: ConfirmDisableReadOnlyModeModalProps) => {
+ const { ref } = useParams()
+ const { mutate: disableReadOnlyMode, isLoading } = useDisableReadOnlyModeMutation({
+ onSuccess: () => {
+ toast.success('Successfully disabled read-only mode for 15 minutes')
+ onClose()
+ },
+ })
+
+ return (
+ <Modal
+ alignFooter="right"
+ visible={visible}
+ onCancel={onClose}
+ loading={isLoading}
+ confirmText="Disable read-only mode"
+ header="Confirm to temporarily disable read-only mode"
+ onConfirm={() => {
+ if (!ref) return console.error('Project ref is required')
+ disableReadOnlyMode({ projectRef: ref })
+ }}
+ >
+ <Modal.Content className="py-4 space-y-2">
+ <p className="text-sm">
+ This will temporarily allow writes to your database for the{' '}
+ <span className="text-amber-900">next 15 minutes</span>, during which you can reduce your
+ database size. After deleting data, you should run a vacuum to reclaim as much space as
+ possible.
+ </p>
+ <p className="text-sm">
+ If your database size has not been sufficiently reduced after 15 minutes, read-only mode
+ will be toggled back on. Otherwise, it will stay disabled.
+ </p>
+ </Modal.Content>
+ </Modal>
+ )
+}
+
+export default ConfirmDisableReadOnlyModeModal
diff --git a/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.tsx b/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.tsx
index 5b2d41c4241..8497b407207 100644
--- a/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.tsx
+++ b/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.tsx
@@ -1,19 +1,44 @@
import { useParams, useTelemetryProps } from 'common'
+import Link from 'next/link'
import { useRouter } from 'next/router'
-import { Input, Tabs } from 'ui'
+import { useState } from 'react'
+import {
+ AlertDescription_Shadcn_,
+ AlertTitle_Shadcn_,
+ Alert_Shadcn_,
+ Button,
+ IconAlertTriangle,
+ IconExternalLink,
+ Input,
+ Tabs,
+} from 'ui'
import Panel from 'components/ui/Panel'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import { useProjectSettingsQuery } from 'data/config/project-settings-query'
+import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query'
+import { useResourceWarningsQuery } from 'data/usage/resource-warnings-query'
+import { useSelectedOrganization } from 'hooks'
import { pluckObjectFields } from 'lib/helpers'
import Telemetry from 'lib/telemetry'
+import ConfirmDisableReadOnlyModeModal from './ConfirmDisableReadOnlyModal'
import ResetDbPassword from './ResetDbPassword'
const DatabaseSettings = () => {
const router = useRouter()
const { ref: projectRef } = useParams()
const telemetryProps = useTelemetryProps()
+ const selectedOrganization = useSelectedOrganization()
+ const [showConfirmationModal, setShowConfirmationModal] = useState(false)
+
const { data, isLoading, isError } = useProjectSettingsQuery({ projectRef })
+ const { data: subscription } = useProjectSubscriptionV2Query({ projectRef })
+ const { data: resourceWarnings } = useResourceWarningsQuery()
+
+ const isOrgBilling = !!selectedOrganization?.subscription_id
+ const isReadOnlyMode =
+ (resourceWarnings ?? [])?.find((warning) => warning.project === projectRef)
+ ?.is_readonly_mode_enabled ?? false
if (isError) {
return (
@@ -131,199 +156,271 @@ const DatabaseSettings = () => {
`-U ${connectionInfo.db_user}`
return (
- <div className="space-y-10">
- <section className="space-y-6">
- <h3 className="text-foreground mb-2 text-xl">Database Settings</h3>
- <Panel
- title={
- <h5 key="panel-title" className="mb-0">
- Connection info
- </h5>
- }
- className="!m-0"
- >
- <Panel.Content className="space-y-6">
- <Input
- className="input-mono"
- layout="horizontal"
- readOnly
- copy
- disabled
- value={connectionInfo.db_host}
- label="Host"
- onCopy={() => {
- handleCopy('Host')
- }}
- />
-
- <Input
- className="input-mono"
- layout="horizontal"
- readOnly
- copy
- disabled
- value={connectionInfo.db_name}
- label="Database name"
- />
-
- <Input
- className="input-mono"
- layout="horizontal"
- readOnly
- copy
- disabled
- value={connectionInfo.db_port.toString()}
- label="Port"
- />
-
- <Input
- layout="horizontal"
- className="input-mono table-input-cell text-base"
- readOnly
- copy
- disabled
- value={connectionInfo.db_user}
- label="User"
- />
-
- <Input
- className="input-mono"
- layout="horizontal"
- disabled
- readOnly
- value={'[The password you provided when you created this project]'}
- label="Password"
- />
- </Panel.Content>
- </Panel>
- </section>
-
- <ResetDbPassword />
-
- <section className="space-y-6">
- <Panel
- title={
- <h5 key="panel-title" className="mb-0">
- Connection string
- </h5>
- }
- className="!m-0"
- >
- <Panel.Content>
- <Tabs type="underlined" size="small">
- <Tabs.Panel id="psql" label="PSQL">
- <Input
- copy
- readOnly
- disabled
- value={psqlConnString}
- onCopy={() => {
- handleCopy('PSQL')
- }}
- />
- </Tabs.Panel>
-
- <Tabs.Panel id="uri" label="URI">
- <Input
- copy
- readOnly
- disabled
- value={uriConnString}
- onCopy={() => {
- handleCopy('URI')
- }}
- />
- </Tabs.Panel>
-
- <Tabs.Panel id="golang" label="Golang">
- <Input
- copy
- readOnly
- disabled
- value={golangConnString}
- onCopy={() => {
- handleCopy('Golang')
- }}
- />
- </Tabs.Panel>
-
- <Tabs.Panel id="jdbc" label="JDBC">
- <Input
- copy
- readOnly
- disabled
- value={
- `jdbc:postgresql://${
- connectionInfo.db_host
- }:${connectionInfo.db_port.toString()}` +
- `/${connectionInfo.db_name}?user=${connectionInfo.db_user}&password=[YOUR-PASSWORD]`
- }
- onCopy={() => {
- handleCopy('JDBC')
- }}
- />
- </Tabs.Panel>
-
- <Tabs.Panel id="dotnet" label=".NET">
- <Input
- copy
- readOnly
- disabled
- value={
- `User Id=${connectionInfo.db_user};Password=[YOUR-PASSWORD];` +
- `Server=${connectionInfo.db_host};Port=${connectionInfo.db_port.toString()};` +
- `Database=${connectionInfo.db_name}`
- }
- onCopy={() => {
- handleCopy('.NET')
- }}
- />
- </Tabs.Panel>
-
- <Tabs.Panel id="nodejs" label="Nodejs">
- <Input
- copy
- readOnly
- disabled
- value={uriConnString}
- onCopy={() => {
- handleCopy('Nodejs')
- }}
- />
- </Tabs.Panel>
-
- <Tabs.Panel id="php" label="PHP">
- <Input
- copy
- readOnly
- disabled
- value={golangConnString}
- onCopy={() => {
- handleCopy('PHP')
- }}
- />
- </Tabs.Panel>
-
- <Tabs.Panel id="python" label="Python">
- <Input
- copy
- readOnly
- disabled
- value={
- `user=${connectionInfo.db_user} password=[YOUR-PASSWORD]` +
- ` host=${connectionInfo.db_host} port=${connectionInfo.db_port.toString()}` +
- ` database=${connectionInfo.db_name}`
- }
- onCopy={() => {
- handleCopy('Python')
- }}
- />
- </Tabs.Panel>
- </Tabs>
- </Panel.Content>
- </Panel>
- </section>
- </div>
+ <>
+ <div className="space-y-10">
+ <section className="space-y-6">
+ <h3 className="text-foreground mb-2 text-xl">Database Settings</h3>
+
+ {isReadOnlyMode && (
+ <Alert_Shadcn_ variant="destructive">
+ <IconAlertTriangle />
+ <AlertTitle_Shadcn_>
+ Project is in read-only mode and database is no longer accepting write requests
+ </AlertTitle_Shadcn_>
+ <AlertDescription_Shadcn_>
+ You have reached 95% of your project's disk space, and read-only mode has been
+ enabled to preserve your database's stability and prevent your project from
+ exceeding its current billing plan. To resolve this, you may:
+ <ul className="list-disc pl-6 mt-1">
+ <li>
+ Temporarily disable read-only mode to free up space and reduce your database
+ size
+ </li>
+ {subscription?.plan.id === 'free' ? (
+ <li>
+ <Link
+ href={
+ isOrgBilling
+ ? `/org/${selectedOrganization.slug}/billing?panel=subscriptionPlan`
+ : `/project/${projectRef}/settings/billing/subscription?panel=subscriptionPlan`
+ }
+ >
+ <a className="text underline">Upgrade to the Pro plan</a>
+ </Link>{' '}
+ to increase your database size limit to 8GB.
+ </li>
+ ) : subscription?.plan.id === 'pro' && subscription?.usage_billing_enabled ? (
+ <li>
+ <Link
+ href={
+ isOrgBilling
+ ? `/org/${selectedOrganization.slug}/billing?panel=subscriptionPlan`
+ : `/project/${projectRef}/settings/billing/subscription?panel=subscriptionPlan`
+ }
+ >
+ <a className="text-foreground underline">Disable your Spend Cap</a>
+ </Link>{' '}
+ to allow your project to auto-scale and expand beyond the 8GB database size
+ limit
+ </li>
+ ) : null}
+ </ul>
+ </AlertDescription_Shadcn_>
+ <div className="mt-4 flex items-center space-x-2">
+ <Button type="default" onClick={() => setShowConfirmationModal(true)}>
+ Disable read-only mode
+ </Button>
+ <Link
+ passHref
+ href="https://supabase.com/docs/guides/platform/database-size#disabling-read-only-mode"
+ >
+ <a target="_blank" rel="noreferrer">
+ <Button type="default" icon={<IconExternalLink />}>
+ Learn more
+ </Button>
+ </a>
+ </Link>
+ </div>
+ </Alert_Shadcn_>
+ )}
+
+ <Panel
+ title={
+ <h5 key="panel-title" className="mb-0">
+ Connection info
+ </h5>
+ }
+ className="!m-0"
+ >
+ <Panel.Content className="space-y-6">
+ <Input
+ className="input-mono"
+ layout="horizontal"
+ readOnly
+ copy
+ disabled
+ value={connectionInfo.db_host}
+ label="Host"
+ onCopy={() => {
+ handleCopy('Host')
+ }}
+ />
+
+ <Input
+ className="input-mono"
+ layout="horizontal"
+ readOnly
+ copy
+ disabled
+ value={connectionInfo.db_name}
+ label="Database name"
+ />
+
+ <Input
+ className="input-mono"
+ layout="horizontal"
+ readOnly
+ copy
+ disabled
+ value={connectionInfo.db_port.toString()}
+ label="Port"
+ />
+
+ <Input
+ layout="horizontal"
+ className="input-mono table-input-cell text-base"
+ readOnly
+ copy
+ disabled
+ value={connectionInfo.db_user}
+ label="User"
+ />
+
+ <Input
+ className="input-mono"
+ layout="horizontal"
+ disabled
+ readOnly
+ value={'[The password you provided when you created this project]'}
+ label="Password"
+ />
+ </Panel.Content>
+ </Panel>
+ </section>
+
+ <ResetDbPassword />
+
+ <section className="space-y-6">
+ <Panel
+ title={
+ <h5 key="panel-title" className="mb-0">
+ Connection string
+ </h5>
+ }
+ className="!m-0"
+ >
+ <Panel.Content>
+ <Tabs type="underlined" size="small">
+ <Tabs.Panel id="psql" label="PSQL">
+ <Input
+ copy
+ readOnly
+ disabled
+ value={psqlConnString}
+ onCopy={() => {
+ handleCopy('PSQL')
+ }}
+ />
+ </Tabs.Panel>
+
+ <Tabs.Panel id="uri" label="URI">
+ <Input
+ copy
+ readOnly
+ disabled
+ value={uriConnString}
+ onCopy={() => {
+ handleCopy('URI')
+ }}
+ />
+ </Tabs.Panel>
+
+ <Tabs.Panel id="golang" label="Golang">
+ <Input
+ copy
+ readOnly
+ disabled
+ value={golangConnString}
+ onCopy={() => {
+ handleCopy('Golang')
+ }}
+ />
+ </Tabs.Panel>
+
+ <Tabs.Panel id="jdbc" label="JDBC">
+ <Input
+ copy
+ readOnly
+ disabled
+ value={
+ `jdbc:postgresql://${
+ connectionInfo.db_host
+ }:${connectionInfo.db_port.toString()}` +
+ `/${connectionInfo.db_name}?user=${connectionInfo.db_user}&password=[YOUR-PASSWORD]`
+ }
+ onCopy={() => {
+ handleCopy('JDBC')
+ }}
+ />
+ </Tabs.Panel>
+
+ <Tabs.Panel id="dotnet" label=".NET">
+ <Input
+ copy
+ readOnly
+ disabled
+ value={
+ `User Id=${connectionInfo.db_user};Password=[YOUR-PASSWORD];` +
+ `Server=${
+ connectionInfo.db_host
+ };Port=${connectionInfo.db_port.toString()};` +
+ `Database=${connectionInfo.db_name}`
+ }
+ onCopy={() => {
+ handleCopy('.NET')
+ }}
+ />
+ </Tabs.Panel>
+
+ <Tabs.Panel id="nodejs" label="Nodejs">
+ <Input
+ copy
+ readOnly
+ disabled
+ value={uriConnString}
+ onCopy={() => {
+ handleCopy('Nodejs')
+ }}
+ />
+ </Tabs.Panel>
+
+ <Tabs.Panel id="php" label="PHP">
+ <Input
+ copy
+ readOnly
+ disabled
+ value={golangConnString}
+ onCopy={() => {
+ handleCopy('PHP')
+ }}
+ />
+ </Tabs.Panel>
+
+ <Tabs.Panel id="python" label="Python">
+ <Input
+ copy
+ readOnly
+ disabled
+ value={
+ `user=${connectionInfo.db_user} password=[YOUR-PASSWORD]` +
+ ` host=${connectionInfo.db_host} port=${connectionInfo.db_port.toString()}` +
+ ` database=${connectionInfo.db_name}`
+ }
+ onCopy={() => {
+ handleCopy('Python')
+ }}
+ />
+ </Tabs.Panel>
+ </Tabs>
+ </Panel.Content>
+ </Panel>
+ </section>
+ </div>
+ <ConfirmDisableReadOnlyModeModal
+ visible={showConfirmationModal}
+ onClose={() => setShowConfirmationModal(false)}
+ />
+ </>
)
}
diff --git a/studio/components/ui/ResourceExhaustionWarningBanner/ResourceExhaustionWarningBanner.constants.ts b/studio/components/ui/ResourceExhaustionWarningBanner/ResourceExhaustionWarningBanner.constants.ts
index 5ed9d61b5f9..fd5f679b2b6 100644
--- a/studio/components/ui/ResourceExhaustionWarningBanner/ResourceExhaustionWarningBanner.constants.ts
+++ b/studio/components/ui/ResourceExhaustionWarningBanner/ResourceExhaustionWarningBanner.constants.ts
@@ -25,8 +25,8 @@ export const RESOURCE_WARNING_MESSAGES = {
},
},
docsUrl: 'https://supabase.com/docs/guides/platform/database-size#disabling-read-only-mode',
- buttonText: undefined,
- metric: undefined,
+ buttonText: 'View database settings',
+ metric: 'read_only',
},
disk_io_exhaustion: {
bannerContent: {
diff --git a/studio/components/ui/ResourceExhaustionWarningBanner/ResourceExhaustionWarningBanner.tsx b/studio/components/ui/ResourceExhaustionWarningBanner/ResourceExhaustionWarningBanner.tsx
index 37df8e4bb7a..99ca6389430 100644
--- a/studio/components/ui/ResourceExhaustionWarningBanner/ResourceExhaustionWarningBanner.tsx
+++ b/studio/components/ui/ResourceExhaustionWarningBanner/ResourceExhaustionWarningBanner.tsx
@@ -82,7 +82,7 @@ const ResourceExhaustionWarningBanner = () => {
? undefined
: metric === null
? '/project/[ref]/settings/[infra-path]'
- : metric === 'disk_space'
+ : metric === 'disk_space' || metric === 'read_only'
? '/project/[ref]/settings/database'
: `/project/[ref]/settings/[infra-path]#${metric}`
)
@@ -99,7 +99,9 @@ const ResourceExhaustionWarningBanner = () => {
if (
activeWarnings.length === 0 ||
((router.pathname.endsWith('/usage') || router.pathname.endsWith('/infrastructure')) &&
- !activeWarnings.includes('is_readonly_mode_enabled'))
+ !activeWarnings.includes('is_readonly_mode_enabled')) ||
+ (activeWarnings.includes('is_readonly_mode_enabled') &&
+ router.pathname.endsWith('settings/database'))
)
return null
diff --git a/studio/data/config/project-read-only-query.ts b/studio/data/config/project-read-only-query.ts
deleted file mode 100644
index 151b0ff12e2..00000000000
--- a/studio/data/config/project-read-only-query.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import { UseQueryOptions } from '@tanstack/react-query'
-import { useCallback } from 'react'
-import { ExecuteSqlData, useExecuteSqlPrefetch, useExecuteSqlQuery } from '../sql/execute-sql-query'
-
-// TODO: temporary solution to check if project is in read only mode
-// until we get an api endpoint for this
-
-export const getProjectReadOnlySql = () => {
- const sql = /* SQL */ `
- show default_transaction_read_only;
- `
-
- return sql
-}
-
-export type ProjectReadOnlyVariables = {
- projectRef?: string
- connectionString?: string
-}
-
-export type ProjectReadOnlyData = boolean
-export type ProjectReadOnlyError = unknown
-
-export const useProjectReadOnlyQuery = (
- { projectRef, connectionString }: ProjectReadOnlyVariables,
- options: Omit<
- UseQueryOptions<ExecuteSqlData, ProjectReadOnlyError, ProjectReadOnlyData>,
- 'select'
- > = {}
-) =>
- useExecuteSqlQuery(
- {
- projectRef,
- connectionString,
- sql: getProjectReadOnlySql(),
- queryKey: ['project-read-only'],
- },
- {
- select(data) {
- return data.result[0]?.default_transaction_read_only === 'on'
- },
- enabled: typeof projectRef !== 'undefined' && typeof connectionString !== 'undefined',
- retry: (failureCount) => {
- return failureCount < 3
- },
- ...options,
- }
- )
diff --git a/studio/data/config/project-temp-disable-read-only-mutation.ts b/studio/data/config/project-temp-disable-read-only-mutation.ts
new file mode 100644
index 00000000000..769fce31388
--- /dev/null
+++ b/studio/data/config/project-temp-disable-read-only-mutation.ts
@@ -0,0 +1,49 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { post } from 'data/fetchers'
+import { usageKeys } from 'data/usage/keys'
+import { ResponseError } from 'types'
+
+export type TempDisableReadOnlyModeVariables = {
+ projectRef: string
+}
+
+export async function tempDisableReadOnlyMode({ projectRef }: TempDisableReadOnlyModeVariables) {
+ const { data, error } = await post('/v1/projects/{ref}/readonly/temporary-disable', {
+ params: { path: { ref: projectRef } },
+ })
+
+ if (error) throw error
+ return data
+}
+
+type DisableReadOnlyModeData = Awaited<ReturnType<typeof tempDisableReadOnlyMode>>
+
+export const useDisableReadOnlyModeMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<DisableReadOnlyModeData, ResponseError, TempDisableReadOnlyModeVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+ return useMutation<DisableReadOnlyModeData, ResponseError, TempDisableReadOnlyModeVariables>(
+ (vars) => tempDisableReadOnlyMode(vars),
+ {
+ async onSuccess(data, variables, context) {
+ setTimeout(() => queryClient.invalidateQueries(usageKeys.resourceWarnings()), 2000)
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to disable read only mode: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}