import { useQueryClient } from '@tanstack/react-query' import Link from 'next/link' import { useRouter } from 'next/router' import { useState } from 'react' import { AlertDescription_Shadcn_, AlertTitle_Shadcn_, Alert_Shadcn_, Button, Form, IconAlertCircle, IconAlertTriangle, Listbox, Modal, } from 'ui' import { useParams } from 'common/hooks' import { useProjectUpgradeEligibilityQuery } from 'data/config/project-upgrade-eligibility-query' import { useProjectUpgradeMutation } from 'data/projects/project-upgrade-mutation' import { setProjectStatus } from 'data/projects/projects-query' import { useStore } from 'hooks' import { PROJECT_STATUS } from 'lib/constants' const ProjectUpgradeAlert = () => { const router = useRouter() const { ui } = useStore() const { ref } = useParams() const queryClient = useQueryClient() const [showUpgradeModal, setShowUpgradeModal] = useState(false) const formId = 'project-upgrade-form' const { data } = useProjectUpgradeEligibilityQuery({ projectRef: ref }) const currentPgVersion = (data?.current_app_version ?? '').split('supabase-postgres-')[1] const latestPgVersion = (data?.latest_app_version ?? '').split('supabase-postgres-')[1] const durationEstimateHours = data?.duration_estimate_hours || 1 const legacyAuthCustomRoles = data?.legacy_auth_custom_roles || [] const initialValues = { version: data?.target_upgrade_versions?.[0]?.postgres_version ?? 0 } const { mutate: upgradeProject, isLoading: isUpgrading } = useProjectUpgradeMutation({ onSuccess: (res, variables) => { setProjectStatus(queryClient, variables.ref, PROJECT_STATUS.UPGRADING) ui.setNotification({ category: 'success', message: 'Upgrading project' }) router.push(`/project/${variables.ref}?upgradeInitiated=true`) }, }) const onConfirmUpgrade = async (values: any) => { if (!ref) return ui.setNotification({ category: 'error', message: 'Project ref not found' }) upgradeProject({ ref, target_version: values.version }) } return ( <> Your project can be upgraded to the latest version of Postgres

The latest version of Postgres ({latestPgVersion}) is available for your project.

setShowUpgradeModal(false)} header={

Upgrade Postgres

} >
{({ values }: { values: any }) => { const selectedVersion = (data?.target_upgrade_versions ?? []).find( (x) => x.postgres_version === values.version ) return ( <>

All services are going offline.

You will not be able to downgrade back to Postgres {currentPgVersion}.

Your project will be offline for up to {durationEstimateHours} hour {durationEstimateHours === 1 ? '' : 's'}

It is advised to upgrade at a time when there will be minimal impact for your application.

{(data?.potential_breaking_changes ?? []).length > 0 && ( Breaking changes

Your project will be upgraded across major versions of Postgres. This may involve breaking changes.

)} {legacyAuthCustomRoles.length > 0 && ( Custom Postgres roles will not work automatically after upgrade

You must run a series of commands after upgrading.

This is because new Postgres versions use scram-sha-256 authentication by default and do not support md5, as it has been deprecated.

Run the following commands after the upgrade:

{legacyAuthCustomRoles.map((role) => (
ALTER ROLE {role} WITH PASSWORD 'newpassword';
))}
)} {data?.target_upgrade_versions.map((version) => ( {version.postgres_version} ( {version.app_version.split('supabase-postgres-')[1]}) ))}
) }}
) } export default ProjectUpgradeAlert