Skip to content

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

feat: transfer project to another org

Kevin Grüneberge0a6dbab7675
diff --git a/studio/components/interfaces/Settings/General/TransferProjectPanel/TransferProjectButton.tsx b/studio/components/interfaces/Settings/General/TransferProjectPanel/TransferProjectButton.tsx
new file mode 100644
index 00000000000..3aee3566e35
--- /dev/null
+++ b/studio/components/interfaces/Settings/General/TransferProjectPanel/TransferProjectButton.tsx
@@ -0,0 +1,348 @@
+import { FC, useEffect, useMemo, useState } from 'react'
+import {
+ Alert,
+ Button,
+ IconLoader,
+ IconShield,
+ IconTool,
+ IconUsers,
+ Listbox,
+ Loading,
+ Modal,
+} from 'ui'
+import * as Tooltip from '@radix-ui/react-tooltip'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+
+import { useStore, useSelectedProject, useCheckPermissions } from 'hooks'
+import { useProjectTransferPreviewQuery } from 'data/projects/project-transfer-preview-query'
+import { useProjectTransferMutation } from 'data/projects/project-transfer-mutation'
+import { useOrganizationsQuery } from 'data/organizations/organizations-query'
+import Link from 'next/link'
+
+const TransferProjectButton: FC<{}> = () => {
+ const { ui } = useStore()
+
+ const project = useSelectedProject()
+ const projectRef = project?.ref
+ const projectOrgId = project?.organization_id
+ const { data: allOrganizations } = useOrganizationsQuery()
+
+ const organizations = useMemo(
+ () =>
+ (allOrganizations || [])
+ .filter((it) => it.id !== projectOrgId)
+ // Only orgs with org-level subscription
+ .filter((it) => it.subscription_id),
+ [allOrganizations, projectOrgId]
+ )
+
+ const [isOpen, setIsOpen] = useState(false)
+ const [isLoading, setIsLoading] = useState(false)
+
+ const [selectedOrg, setSelectedOrg] = useState()
+
+ const { error: transferError, mutateAsync: transferProject } = useProjectTransferMutation()
+
+ const {
+ data: transferPreviewData,
+ error: transferPreviewError,
+ isLoading: transferPreviewIsLoading,
+ remove,
+ } = useProjectTransferPreviewQuery(
+ { projectRef, targetOrganizationSlug: selectedOrg },
+ { enabled: !isLoading && isOpen }
+ )
+
+ useEffect(() => {
+ if (isOpen) {
+ // reset state
+ setSelectedOrg(undefined)
+ setIsLoading(false)
+ } else {
+ // Invalidate cache
+ remove()
+ }
+ }, [isOpen])
+
+ const canTransferProject = useCheckPermissions(PermissionAction.UPDATE, 'projects')
+
+ const toggle = () => {
+ setIsOpen(!isOpen)
+ }
+
+ async function handleTransferProject() {
+ if (project === undefined) return
+ if (selectedOrg === undefined) return
+
+ setIsLoading(true)
+
+ try {
+ await transferProject({ projectRef, targetOrganizationSlug: selectedOrg })
+ ui.setNotification({
+ category: 'success',
+
+ duration: 5000,
+ message: `Successfully transferred project ${project?.name}.`,
+ })
+ setIsOpen(false)
+ } catch {
+ } finally {
+ setIsLoading(false)
+ }
+ }
+
+ return (
+ <>
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button onClick={toggle} type="default" disabled={!canTransferProject}>
+ Transfer project
+ </Button>
+ </Tooltip.Trigger>
+ {!canTransferProject && (
+ <Tooltip.Portal>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow', // background
+ 'border border-scale-200 ', //border
+ ].join(' ')}
+ >
+ <span className="text-xs text-scale-1200">
+ You need additional permissions to delete this project
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Portal>
+ )}
+ </Tooltip.Root>
+
+ <Modal
+ closable
+ onCancel={() => toggle()}
+ visible={isOpen}
+ loading={isLoading}
+ size={'xlarge'}
+ header={`Transfer project ${project?.name}`}
+ customFooter={
+ <div className="flex items-center space-x-2 justify-end">
+ <Button type="default" onClick={() => setIsOpen(false)}>
+ Cancel
+ </Button>
+ <Button
+ onClick={() => handleTransferProject()}
+ disabled={
+ !transferPreviewData || !transferPreviewData.valid || isLoading || !selectedOrg
+ }
+ >
+ Transfer Project
+ </Button>
+ </div>
+ }
+ >
+ <div className="space-y-4 py-4 text-scale-1100">
+ <Modal.Content>
+ <p className='text-sm'>
+ To transfer projects, the owner must be a member of both the source and target
+ organizations.
+ </p>
+
+ <p className="font-bold mt-6 text-sm">Transferring considerations:</p>
+
+ <ul className="mt-4 space-y-5 text-sm px-4">
+ <li className="flex gap-4">
+ <span className="shrink-0 mt-1">
+ <IconLoader />
+ </span>
+ <div>
+ <p className="font-bold">No downtime</p>
+ <p>There is no downtime or restrictions involved when transferring a project.</p>
+ </div>
+ </li>
+
+ <li className="flex gap-4">
+ <span className="shrink-0 mt-1">
+ <IconShield />
+ </span>
+ <div>
+ <p className="font-bold">Permissions</p>
+ <p>
+ Depending on your role in the target organization, your level of permissions may
+ change after transfer.
+ </p>
+ </div>
+ </li>
+
+ <li className="flex gap-4">
+ <span className="shrink-0 mt-1">
+ <IconTool w={14} className="flex-shrink-0" />
+ </span>
+ <div>
+ <p className="font-bold">Features</p>
+ <p>
+ Moving your project to an organization with a smaller subscription plan may
+ result in the loss of certain features (i.e. image transformations).
+ </p>
+ </div>
+ </li>
+ </ul>
+ </Modal.Content>
+ <Modal.Content>
+ {organizations.length > 0 && (
+ <div className="mt-8 mx-4 border-t pt-4 space-y-2">
+ <Listbox
+ label="Select Target Organization"
+ layout="vertical"
+ value={selectedOrg}
+ onChange={(slug) => setSelectedOrg(slug)}
+ placeholder="Select Organization"
+ >
+ <Listbox.Option disabled key="no-results" label="Select Organization" value="">
+ Select Organization
+ </Listbox.Option>
+ {organizations.map((x: any) => (
+ <Listbox.Option
+ key={x.id}
+ label={x.name}
+ value={x.slug}
+ addOnBefore={() => <IconUsers />}
+ >
+ {x.name}
+ </Listbox.Option>
+ ))}
+ </Listbox>
+ <p className="text-scale-1000 text-sm">
+ The target organization needs to use{' '}
+ <Link href="https://www.notion.so/supabase/Organization-Level-Billing-9c159d69375b4af095f0b67881276582?pvs=4">
+ <a target="_blank" rel="noreferrer" className="underline">
+ organization-level-billing
+ </a>
+ </Link>
+ . If you're looking to migrate an organization to the new billing, head to your{' '}
+ <Link href="/org/_/general">
+ <a target="_blank" rel="noreferrer" className="underline">
+ organizations settings
+ </a>
+ </Link>
+ .
+ </p>
+ </div>
+ )}
+ </Modal.Content>
+
+ <Loading active={selectedOrg !== undefined && transferPreviewIsLoading}>
+ <Modal.Content>
+ <div className="px-4">
+ {transferPreviewData && transferPreviewData.valid && (
+ <div className="text-sm text-scale-1100 p-4 bg-scale-400">
+ {transferPreviewData.source_subscription_plan !==
+ transferPreviewData.target_subscription_plan ? (
+ <div>
+ <p>
+ Your project is currently on the{' '}
+ {transferPreviewData.source_subscription_plan} plan, whereas the target
+ organization uses the {transferPreviewData.target_subscription_plan} plan.
+ </p>
+ </div>
+ ) : (
+ <div>
+ Your project and the target organization are both on the{' '}
+ {transferPreviewData.source_subscription_plan} subscription plan.
+ </div>
+ )}
+
+ <div className="my-4">
+ {transferPreviewData.credits_on_source_organization === 0 ? (
+ <span>
+ {' '}
+ Your current organization won't be granted any prorated credits.
+ </span>
+ ) : (
+ <span>
+ {' '}
+ Your current organization will be granted{' '}
+ <span className="text-brand-900">
+ ${transferPreviewData.credits_on_source_organization}
+ </span>{' '}
+ in credits as proration.
+ </span>
+ )}
+ {transferPreviewData.costs_on_target_organization === 0 ? (
+ <span>
+ {' '}
+ The target organization won't be charged any immediate upfront payment.
+ </span>
+ ) : (
+ <span>
+ {' '}
+ The target organization will be billed{' '}
+ <span className="text-brand-900">
+ ${transferPreviewData.costs_on_target_organization}
+ </span>{' '}
+ immediately to prorate for the remainder of the billing period.
+ </span>
+ )}
+ </div>
+ </div>
+ )}
+ {transferPreviewData && transferPreviewData.warnings.length > 0 && (
+ <Alert withIcon variant="warning" title="Warnings for project transfer" className='mt-3'>
+ <div className="space-y-1">
+ {transferPreviewData.warnings.map((warning) => (
+ <p key={warning.key}>{warning.message}</p>
+ ))}
+ </div>
+ </Alert>
+ )}
+ {transferPreviewData && transferPreviewData.errors.length > 0 && (
+ <Alert withIcon variant="danger" title="Project cannot be transferred">
+ <div className="space-y-1">
+ {transferPreviewData.errors.map((error) => (
+ <p key={error.key}>{error.message}</p>
+ ))}
+ </div>
+ {transferPreviewData.members_exceeding_free_project_limit.length > 0 && (
+ <div className="space-y-2">
+ <p className="text-sm text-scale-1100">
+ These members have reached their maximum limits for the number of active
+ Free plan projects within organizations where they are an administrator or
+ owner:
+ </p>
+ <ul className="pl-5 text-sm list-disc text-scale-1100">
+ {(transferPreviewData.members_exceeding_free_project_limit || []).map(
+ (member, idx: number) => (
+ <li key={`member-${idx}`}>
+ {member.name} (Limit: {member.limit} free projects)
+ </li>
+ )
+ )}
+ </ul>
+ <p className="text-sm text-scale-1100">
+ These members will need to either delete, pause, or upgrade one or more of
+ their projects before you can downgrade this project.
+ </p>
+ </div>
+ )}
+ </Alert>
+ )}
+ {transferPreviewError && !transferError && (
+ <Alert withIcon variant="danger" title="Project cannot be transferred">
+ <p>{transferPreviewError.message}</p>
+ </Alert>
+ )}
+ {transferError && (
+ <Alert withIcon variant="danger" title="Project cannot be transferred">
+ <p>{transferError.message}</p>
+ </Alert>
+ )}
+ </div>
+ </Modal.Content>
+ </Loading>
+ </div>
+ </Modal>
+ </>
+ )
+}
+
+export default TransferProjectButton
diff --git a/studio/components/interfaces/Settings/General/TransferProjectPanel/TransferProjectPanel.tsx b/studio/components/interfaces/Settings/General/TransferProjectPanel/TransferProjectPanel.tsx
new file mode 100644
index 00000000000..ff8ce40428d
--- /dev/null
+++ b/studio/components/interfaces/Settings/General/TransferProjectPanel/TransferProjectPanel.tsx
@@ -0,0 +1,46 @@
+import { FC } from 'react'
+
+import { useSelectedProject } from 'hooks'
+import Panel from 'components/ui/Panel'
+
+import TransferProjectButton from './TransferProjectButton'
+import { FormHeader } from 'components/ui/Forms'
+import { IconTruck } from 'ui'
+
+interface Props {}
+
+const TransferProjectPanel: FC<Props> = ({}) => {
+ const project = useSelectedProject()
+
+ if (project === undefined) return <></>
+
+ return (
+ <section>
+ <FormHeader
+ title="Transfer Project"
+ description="Transfer your project to a different organization with no downtime."
+ />
+ <Panel>
+ <Panel.Content>
+ <div className="flex justify-between items-center gap-8">
+ <div className="flex space-x-4">
+ <IconTruck className="mt-1" />
+ <div className="space-y-1 xl:max-w-lg">
+ <p className="text-sm">Transfer project to another organization</p>
+ <p className="text-sm text-scale-1100">
+ To transfer projects, the owner must be a member of both the source and target
+ organizations.
+ </p>
+ </div>
+ </div>
+ <div>
+ <TransferProjectButton />
+ </div>
+ </div>
+ </Panel.Content>
+ </Panel>
+ </section>
+ )
+}
+
+export default TransferProjectPanel
diff --git a/studio/components/interfaces/Settings/General/index.ts b/studio/components/interfaces/Settings/General/index.ts
index 8ff5b36ea73..ecd3345f01c 100644
--- a/studio/components/interfaces/Settings/General/index.ts
+++ b/studio/components/interfaces/Settings/General/index.ts
@@ -3,5 +3,15 @@ import Infrastructure from './Infrastructure/Infrastructure'
import CustomDomainConfig from './CustomDomainConfig/CustomDomainConfig'
import DeleteProjectPanel from './DeleteProjectPanel/DeleteProjectPanel'
import DeleteProjectButton from './DeleteProjectPanel/DeleteProjectButton'
+import TransferProjectButton from './TransferProjectPanel/TransferProjectButton'
+import TransferProjectPanel from './TransferProjectPanel/TransferProjectPanel'
-export { General, Infrastructure, CustomDomainConfig, DeleteProjectPanel, DeleteProjectButton }
+export {
+ General,
+ Infrastructure,
+ CustomDomainConfig,
+ DeleteProjectPanel,
+ DeleteProjectButton,
+ TransferProjectPanel,
+ TransferProjectButton,
+}
diff --git a/studio/components/ui/UpgradeToPro.tsx b/studio/components/ui/UpgradeToPro.tsx
index e1771980505..fe0a055afa9 100644
--- a/studio/components/ui/UpgradeToPro.tsx
+++ b/studio/components/ui/UpgradeToPro.tsx
@@ -26,7 +26,6 @@ const UpgradeToPro: FC<Props> = ({ icon, primaryText, projectRef, secondaryText
'stripe.subscriptions'
)
const projectUpdateDisabled = useFlag('disableProjectCreationAndUpdate')
- const isEnterprise = tier === PRICING_TIER_PRODUCT_IDS.ENTERPRISE
return (
<div
@@ -48,11 +47,11 @@ const UpgradeToPro: FC<Props> = ({ icon, primaryText, projectRef, secondaryText
<Tooltip.Root delayDuration={0}>
<Tooltip.Trigger>
<Button type="primary" disabled={!canUpdateSubscription || projectUpdateDisabled}>
- <Link href={`/project/${ref}/settings/billing/subscription?panel=subscriptionPlan`}>
+ <Link href={`/project/${ref}/settings/billing/subscription?panel=${tier === PRICING_TIER_PRODUCT_IDS.FREE ? 'subscriptionPlan' : 'customDomain'}`}>
<a>
{tier === PRICING_TIER_PRODUCT_IDS.FREE
? 'Upgrade to Pro'
- : 'Modify subscription'}
+ : 'Enable Custom Domain'}
</a>
</Link>
</Button>
diff --git a/studio/data/projects/keys.ts b/studio/data/projects/keys.ts
index 0726abeaf9e..8e3e5c10db0 100644
--- a/studio/data/projects/keys.ts
+++ b/studio/data/projects/keys.ts
@@ -1,4 +1,10 @@
export const projectKeys = {
list: () => ['all-projects'] as const,
detail: (projectRef: string | undefined) => ['project', projectRef, 'detail'] as const,
+ projectTransfer: (projectRef: string | undefined, targetOrganizationSlug: string | undefined) =>
+ ['projects', 'transfer', projectRef, targetOrganizationSlug] as const,
+ projectTransferPreview: (
+ projectRef: string | undefined,
+ targetOrganizationSlug: string | undefined
+ ) => ['projects', 'transfer', projectRef, targetOrganizationSlug, 'preview'] as const,
}
diff --git a/studio/data/projects/project-transfer-mutation.ts b/studio/data/projects/project-transfer-mutation.ts
new file mode 100644
index 00000000000..45cfe5abdcc
--- /dev/null
+++ b/studio/data/projects/project-transfer-mutation.ts
@@ -0,0 +1,60 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { post } from 'lib/common/fetch'
+import { API_URL } from 'lib/constants'
+import { projectKeys } from './keys'
+
+export type ProjectTransferVariables = {
+ projectRef?: string
+ targetOrganizationSlug?: string
+}
+
+type ProjectTransferError = {
+ message: string
+}
+
+export async function transferProject({
+ projectRef,
+ targetOrganizationSlug,
+}: ProjectTransferVariables) {
+ if (!projectRef) throw new Error('projectRef is required')
+ if (!targetOrganizationSlug) throw new Error('targetOrganizationSlug is required')
+
+ const payload: { target_organization_slug: string } = {
+ target_organization_slug: targetOrganizationSlug,
+ }
+
+ const response = await post(`${API_URL}/projects/${projectRef}/transfer`, payload)
+ if (response.error) throw response.error
+
+ return response
+}
+
+type ProjectTransferData = Awaited<ReturnType<typeof transferProject>>
+
+export const useProjectTransferMutation = ({
+ onSuccess,
+ ...options
+}: Omit<
+ UseMutationOptions<ProjectTransferData, ProjectTransferError, ProjectTransferVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+
+ return useMutation<ProjectTransferData, ProjectTransferError, ProjectTransferVariables>(
+ (vars) => transferProject(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef, targetOrganizationSlug } = variables
+ await Promise.all([
+ queryClient.invalidateQueries(
+ projectKeys.projectTransferPreview(projectRef, targetOrganizationSlug)
+ ),
+ queryClient.invalidateQueries(projectKeys.detail(projectRef)),
+ queryClient.invalidateQueries(projectKeys.list()),
+ ])
+ await onSuccess?.(data, variables, context)
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/data/projects/project-transfer-preview-query.ts b/studio/data/projects/project-transfer-preview-query.ts
new file mode 100644
index 00000000000..a5edae66caf
--- /dev/null
+++ b/studio/data/projects/project-transfer-preview-query.ts
@@ -0,0 +1,123 @@
+import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
+import { post } from 'lib/common/fetch'
+import { API_URL } from 'lib/constants'
+import { useCallback } from 'react'
+import { projectKeys } from './keys'
+
+export type ProjectTransferPreviewVariables = {
+ projectRef?: string
+ targetOrganizationSlug?: string
+}
+
+export type PlanId = 'free' | 'pro' | 'team' | 'enterprise'
+
+type MemberExceedingFreeProjectLimit = {
+ name: string
+ limit: number
+}
+
+type PreviewTransferInfo = {
+ key: string
+ message: string
+}
+
+export type PreviewProjectTransferResponse = {
+ valid: boolean
+
+ warnings: PreviewTransferInfo[]
+ errors: PreviewTransferInfo[]
+
+ members_exceeding_free_project_limit: MemberExceedingFreeProjectLimit[]
+
+ has_permissions_on_source_organization: boolean
+ has_access_to_target_organization: boolean
+
+ source_project_eligible: boolean
+
+ target_organization_eligible: boolean | null
+ target_organization_has_free_project_slots: boolean | null
+
+ credits_on_source_organization: number
+ costs_on_target_organization: number
+ charge_on_target_organization: number
+
+ source_subscription_plan: PlanId
+ target_subscription_plan: PlanId | null
+}
+
+export async function previewProjectTransfer(
+ { projectRef, targetOrganizationSlug }: ProjectTransferPreviewVariables,
+ signal?: AbortSignal
+) {
+ if (!projectRef) throw new Error('projectRef is required')
+ if (!targetOrganizationSlug) throw new Error('targetOrganizationSlug is required')
+
+ const response = await post(
+ `${API_URL}/projects/${projectRef}/transfer/preview`,
+ {
+ target_organization_slug: targetOrganizationSlug,
+ },
+ { signal }
+ )
+ if (response.error) throw response.error
+
+ return response as PreviewProjectTransferResponse
+}
+
+export type ProjectTransferPreviewData = Awaited<ReturnType<typeof previewProjectTransfer>>
+export type ProjectTransferPreviewError = {
+ message: string
+}
+
+export const useProjectTransferPreviewQuery = <TData = ProjectTransferPreviewData>(
+ { projectRef, targetOrganizationSlug }: ProjectTransferPreviewVariables,
+ {
+ enabled = true,
+ ...options
+ }: UseQueryOptions<ProjectTransferPreviewData, ProjectTransferPreviewError, TData> = {}
+) =>
+ useQuery<ProjectTransferPreviewData, ProjectTransferPreviewError, TData>(
+ projectKeys.projectTransferPreview(projectRef, targetOrganizationSlug),
+ ({ signal }) => previewProjectTransfer({ projectRef, targetOrganizationSlug }, signal),
+ {
+ enabled:
+ enabled &&
+ typeof projectRef !== 'undefined' &&
+ typeof targetOrganizationSlug !== 'undefined',
+ ...options,
+
+ retry: (failureCount, error) => {
+ // Don't retry on 400s
+ if (
+ typeof error === 'object' &&
+ error !== null &&
+ 'code' in error &&
+ (error as any).code === 400
+ ) {
+ return false
+ }
+
+ if (failureCount < 3) {
+ return true
+ }
+
+ return false
+ },
+ }
+ )
+
+export const useProjectTransferPreviewPrefetch = ({
+ projectRef,
+ targetOrganizationSlug,
+}: ProjectTransferPreviewVariables) => {
+ const client = useQueryClient()
+
+ return useCallback(() => {
+ if (projectRef && targetOrganizationSlug) {
+ client.prefetchQuery(
+ projectKeys.projectTransferPreview(projectRef, targetOrganizationSlug),
+ ({ signal }) => previewProjectTransfer({ projectRef, targetOrganizationSlug }, signal)
+ )
+ }
+ }, [projectRef, targetOrganizationSlug])
+}
diff --git a/studio/pages/project/[ref]/settings/general.tsx b/studio/pages/project/[ref]/settings/general.tsx
index a1981c1e607..9049ec621ac 100644
--- a/studio/pages/project/[ref]/settings/general.tsx
+++ b/studio/pages/project/[ref]/settings/general.tsx
@@ -7,15 +7,20 @@ import {
Infrastructure,
CustomDomainConfig,
DeleteProjectPanel,
+ TransferProjectPanel,
} from 'components/interfaces/Settings/General'
+import { useFlag } from 'hooks'
const ProjectSettings: NextPageWithLayout = () => {
+ const transferProjectEnabled = useFlag('transferProject')
+
// [Joshen] Opting for larger gap instead of gap-8 as compared to other pages for better grouping of content
return (
<div className="1xl:px-28 mx-auto flex flex-col gap-10 px-5 py-6 lg:px-16 xl:px-24 2xl:px-32 ">
<General />
<Infrastructure />
<CustomDomainConfig />
+ {transferProjectEnabled && <TransferProjectPanel />}
<DeleteProjectPanel />
</div>
)