mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 16:09:06 +00:00
## Problem The internal-only Postgres version selector sorted versions by raw string comparison, so `17.6.1.164` appeared above `17.11.0.002` and the list looked randomly ordered. The sort also ran in place on the array returned by React Query, which can be the cached data. Resolves FE-4603. ## Solution - Add `sortPostgresVersionsNewestFirst` in `ProjectCreation.utils.ts`. It compares version segments numerically, returns a new array, and does not mutate its input. - Use it in `PostgresVersionSelector` in place of `.sort().reverse()`. - OrioleDB versions are not grouped; they are ordered by version number like the rest. - Side effect: the default selection (the first GA version in the list) is now the newest GA version. - Unit tests cover numeric ordering and non-mutation. Before: <img width="447" height="184" alt="Screenshot 2026-11-11 at 16 00 45" src="https://github.com/user-attachments/assets/219f26a1-1475-40ce-a0dc-49480005194f" /> After: <img width="448" height="196" alt="Screenshot 2026-11-11 at 15 58 32" src="https://github.com/user-attachments/assets/24a99b91-22ff-4667-bf96-0046a426d95b" />
234 lines
8.3 KiB
TypeScript
234 lines
8.3 KiB
TypeScript
import type { CloudProvider, Region } from 'shared-data'
|
|
import { AWS_REGIONS } from 'shared-data'
|
|
import { SMART_REGION_TO_EXACT_REGION_MAP } from 'shared-data/regions'
|
|
|
|
import type { OrganizationAvailableRegionsData } from '@/data/organizations/organization-available-regions-query'
|
|
import {
|
|
DesiredInstanceSize,
|
|
instanceSizeSpecs,
|
|
PostgresEngine,
|
|
ReleaseChannel,
|
|
} from '@/data/projects/new-project.constants'
|
|
|
|
/** Form value of the "Auto" option in the region selector, which maps to `{ code: 'auto', type: 'auto' }` */
|
|
export const AUTO_REGION_VALUE = 'best_available'
|
|
|
|
export function smartRegionToExactRegion(smartOrExactRegion: string) {
|
|
// available-versions doesn't accept 'auto', so look versions up against the Americas region for Auto
|
|
const smartRegion = smartOrExactRegion === AUTO_REGION_VALUE ? 'Americas' : smartOrExactRegion
|
|
return SMART_REGION_TO_EXACT_REGION_MAP.get(smartRegion) ?? smartRegion
|
|
}
|
|
|
|
export function getAvailableRegions(
|
|
cloudProvider: CloudProvider,
|
|
environment = process.env.NEXT_PUBLIC_ENVIRONMENT
|
|
): Region {
|
|
switch (cloudProvider) {
|
|
case 'AWS':
|
|
case 'AWS_K8S':
|
|
return AWS_REGIONS
|
|
case 'AWS_NIMBUS':
|
|
if (environment !== 'prod') {
|
|
// Only allow Southeast Asia for Nimbus (local/staging)
|
|
return {
|
|
SOUTHEAST_ASIA: AWS_REGIONS.SOUTHEAST_ASIA,
|
|
}
|
|
}
|
|
|
|
// Only allow US East for Nimbus (prod)
|
|
return {
|
|
EAST_US: AWS_REGIONS.EAST_US,
|
|
}
|
|
default:
|
|
throw new Error('Invalid cloud provider')
|
|
}
|
|
}
|
|
|
|
type ResolveDefaultDbRegionArgs = {
|
|
cloudProvider: CloudProvider
|
|
isHighAvailabilityRestricted: boolean
|
|
highAvailabilityRegionName: string | undefined
|
|
isSmartRegionEnabled: boolean
|
|
/** Whether the Auto option is offered, in which case it is preferred over the recommended region */
|
|
isAutoRegionAvailable?: boolean
|
|
recommendedSmartRegion: string | undefined
|
|
/** Used when there's no recommended smart region */
|
|
firstAvailableSmartRegion?: string
|
|
autoDefaultRegion: string | undefined
|
|
fixedDefaultRegion: string
|
|
environment?: string
|
|
}
|
|
|
|
export function resolveDefaultDbRegion({
|
|
cloudProvider,
|
|
isHighAvailabilityRestricted,
|
|
highAvailabilityRegionName,
|
|
isSmartRegionEnabled,
|
|
isAutoRegionAvailable = false,
|
|
recommendedSmartRegion,
|
|
firstAvailableSmartRegion,
|
|
autoDefaultRegion,
|
|
fixedDefaultRegion,
|
|
environment = process.env.NEXT_PUBLIC_ENVIRONMENT,
|
|
}: ResolveDefaultDbRegionArgs): string | undefined {
|
|
if (isHighAvailabilityRestricted) return highAvailabilityRegionName
|
|
if (isSmartRegionEnabled && isAutoRegionAvailable) return AUTO_REGION_VALUE
|
|
if (isSmartRegionEnabled) return recommendedSmartRegion ?? firstAvailableSmartRegion
|
|
|
|
// The geolocated default is only usable if the provider actually offers that region
|
|
// (e.g. AWS_NIMBUS is restricted to a single region)
|
|
const isAutoDefaultRegionAvailable = Object.entries(
|
|
getAvailableRegions(cloudProvider, environment)
|
|
).some(([, region]) => region.displayName === autoDefaultRegion)
|
|
|
|
return isAutoDefaultRegionAvailable && autoDefaultRegion !== undefined
|
|
? autoDefaultRegion
|
|
: fixedDefaultRegion
|
|
}
|
|
|
|
/**
|
|
* When launching new projects, they only get assigned a compute size once successfully launched,
|
|
* this might assume wrong compute size, but only for projects being rapidly launched after one another on non-default compute sizes.
|
|
*
|
|
* Needs to be in the API in the future [kevin]
|
|
*/
|
|
export const monthlyInstancePrice = (instance: string | undefined): number => {
|
|
return instanceSizeSpecs[instance as DesiredInstanceSize]?.priceMonthly || 10
|
|
}
|
|
|
|
export const instanceLabel = (instance: string | undefined): string => {
|
|
return instanceSizeSpecs[instance as DesiredInstanceSize]?.label || 'Micro'
|
|
}
|
|
|
|
export const getHighAvailabilityRegionCode = (
|
|
environment = process.env.NEXT_PUBLIC_ENVIRONMENT
|
|
) => {
|
|
// Local dev stacks can run in any of the supported regions, so they're left unrestricted
|
|
if (environment === 'prod') return 'us-east-1'
|
|
if (environment === 'staging') return 'us-east-1'
|
|
if (environment === 'local') return 'eu-central-1'
|
|
return undefined
|
|
}
|
|
|
|
export const filterHighAvailabilityRegions = <T extends { code: string }>(
|
|
regions: T[],
|
|
highAvailability: boolean,
|
|
environment = process.env.NEXT_PUBLIC_ENVIRONMENT
|
|
) => {
|
|
const isLocal = environment === 'local'
|
|
const regionCode = getHighAvailabilityRegionCode(environment)
|
|
return highAvailability && !isLocal && regionCode !== undefined
|
|
? regions.filter((region) => region.code === regionCode)
|
|
: regions
|
|
}
|
|
|
|
export type FreeTierGeneralRegionExperimentVariant = 'test' | 'control'
|
|
|
|
type GetFreeTierGeneralRegionExperimentVariantArgs = {
|
|
isFreePlan: boolean
|
|
smartRegionEnabled: boolean
|
|
enrollmentFlag: boolean
|
|
selectionFlag: boolean
|
|
}
|
|
|
|
/**
|
|
* Org's arm in the freeTierGeneralRegionExperiment. The test arm only sees general regions.
|
|
* Returns undefined when the org isn't enrolled.
|
|
*/
|
|
export function getFreeTierGeneralRegionExperimentVariant({
|
|
isFreePlan,
|
|
smartRegionEnabled,
|
|
enrollmentFlag,
|
|
selectionFlag,
|
|
}: GetFreeTierGeneralRegionExperimentVariantArgs):
|
|
| FreeTierGeneralRegionExperimentVariant
|
|
| undefined {
|
|
const isInExperiment = isFreePlan && smartRegionEnabled && enrollmentFlag === true
|
|
if (!isInExperiment) return undefined
|
|
return selectionFlag === true ? 'test' : 'control'
|
|
}
|
|
|
|
export type AvailableRegions = NonNullable<OrganizationAvailableRegionsData>['all']
|
|
|
|
type GetRegionSelectionTypeArgs = {
|
|
dbRegion: string | undefined
|
|
availableRegions: AvailableRegions | undefined
|
|
}
|
|
|
|
/**
|
|
* Classifies the submitted region: the Auto (best available) option ('auto'), a smart group
|
|
* region (e.g. Americas, 'general') or an exact region (e.g. us-east-1, 'specific'). An untouched
|
|
* field reports whichever default it holds. Returns undefined if dbRegion matches neither list.
|
|
*/
|
|
export function getRegionSelectionType({
|
|
dbRegion,
|
|
availableRegions,
|
|
}: GetRegionSelectionTypeArgs): 'general' | 'auto' | 'specific' | undefined {
|
|
if (!dbRegion || !availableRegions) return undefined
|
|
if (dbRegion === AUTO_REGION_VALUE)
|
|
return availableRegions.auto !== undefined ? 'auto' : undefined
|
|
const isGeneral = availableRegions.smartGroup.some((region) => region.name === dbRegion)
|
|
const isSpecific = availableRegions.specific.some((region) => region.name === dbRegion)
|
|
if (!isGeneral && !isSpecific) return undefined
|
|
return isGeneral ? 'general' : 'specific'
|
|
}
|
|
|
|
/**
|
|
* Sorts Postgres versions newest first. Compares numerically per segment, so `17.11.0.002` sorts
|
|
* above `17.6.1.164`, which a plain string comparison gets wrong. Returns a new array rather than
|
|
* sorting in place, since the input is often React Query's cached data.
|
|
*/
|
|
export function sortPostgresVersionsNewestFirst<T extends { version: string }>(
|
|
versions: readonly T[]
|
|
): T[] {
|
|
return [...versions].sort((a, b) =>
|
|
b.version.localeCompare(a.version, undefined, { numeric: true })
|
|
)
|
|
}
|
|
|
|
export interface PostgresVersionDetails {
|
|
postgresEngine?: Exclude<PostgresEngine, '13' | '14'>
|
|
releaseChannel?: ReleaseChannel
|
|
}
|
|
|
|
export const extractPostgresVersionDetails = (value: string): PostgresVersionDetails => {
|
|
if (!value) {
|
|
return { postgresEngine: undefined, releaseChannel: undefined }
|
|
}
|
|
|
|
const [postgresEngine, releaseChannel] = value.split('|')
|
|
return { postgresEngine, releaseChannel } as PostgresVersionDetails
|
|
}
|
|
|
|
/**
|
|
* Picks the engine + release channel to submit on project creation. The selector's value is used
|
|
* only when it matches the selected Postgres Type in Advanced Config. Otherwise, OrioleDB falls
|
|
* back to the available OrioleDB image, and Postgres falls back to the API default
|
|
*/
|
|
export function resolvePostgresVersion({
|
|
postgresVersionSelection,
|
|
useOrioleDb,
|
|
availableOrioleVersion,
|
|
}: {
|
|
postgresVersionSelection: string
|
|
useOrioleDb: boolean
|
|
availableOrioleVersion?: {
|
|
postgres_engine: Exclude<PostgresEngine, '13' | '14'>
|
|
release_channel: ReleaseChannel
|
|
}
|
|
}): PostgresVersionDetails {
|
|
const selection = extractPostgresVersionDetails(postgresVersionSelection)
|
|
const isSelectionOriole = !!selection.postgresEngine?.endsWith('oriole')
|
|
|
|
if (useOrioleDb) {
|
|
if (isSelectionOriole) return selection
|
|
return {
|
|
postgresEngine: availableOrioleVersion?.postgres_engine,
|
|
releaseChannel: availableOrioleVersion?.release_channel,
|
|
}
|
|
}
|
|
|
|
if (isSelectionOriole) return {}
|
|
return selection
|
|
}
|