fix(studio): sort Postgres versions newest first in the version selector (#53278)

## 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"
/>
This commit is contained in:
Drew McLellan authored and GitHub committed 2026-11-12 13:53:15 +00:00
1 parent f2af7ea280
commit ce5c68198c
3 files changed
+61 -2

No files matched your search

@@ -12,7 +12,7 @@ import {
} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { smartRegionToExactRegion } from './ProjectCreation.utils'
import { smartRegionToExactRegion, sortPostgresVersionsNewestFirst } from './ProjectCreation.utils'
import { useProjectCreationPostgresVersionsQuery } from '@/data/config/project-creation-postgres-versions-query'
import { useProjectUnpausePostgresVersionsQuery } from '@/data/config/project-unpause-postgres-versions-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
@@ -108,7 +108,7 @@ export const PostgresVersionSelector = ({
type === 'create'
? (createVersions?.available_versions ?? [])
: (unpauseVersions?.available_versions ?? [])
const availableVersions = versions.sort((a, b) => a.version.localeCompare(b.version)).reverse()
const availableVersions = sortPostgresVersionsNewestFirst(versions)
const postgresVersionSelection = useWatch({
control: form.control,
name: 'postgresVersionSelection',
@@ -13,6 +13,7 @@ import {
resolveDefaultDbRegion,
resolvePostgresVersion,
smartRegionToExactRegion,
sortPostgresVersionsNewestFirst,
} from './ProjectCreation.utils'
import type { PostgresEngine, ReleaseChannel } from '@/data/projects/new-project.constants'
@@ -320,3 +321,48 @@ describe('resolvePostgresVersion', () => {
).toEqual({})
})
})
describe('sortPostgresVersionsNewestFirst', () => {
const toVersions = (versions: string[]) =>
versions.map((version) => ({ version: `supabase-postgres-${version}` }))
const toNumbers = (versions: { version: string }[]) =>
versions.map((x) => x.version.replace('supabase-postgres-', ''))
it('compares segments numerically, not alphabetically', () => {
const sorted = sortPostgresVersionsNewestFirst(
toVersions([
'17.6.1.164',
'17.6.0.062',
'17.11.0.002',
'17.11.0.001',
'15.19.0.002',
'15.14.1.164',
])
)
expect(toNumbers(sorted)).toEqual([
'17.11.0.002',
'17.11.0.001',
'17.6.1.164',
'17.6.0.062',
'15.19.0.002',
'15.14.1.164',
])
})
it('interleaves OrioleDB versions by version number', () => {
const sorted = sortPostgresVersionsNewestFirst(
toVersions(['17.6.0.062-orioledb', '17.11.0.002', '17.6.1.164'])
)
expect(toNumbers(sorted)).toEqual(['17.11.0.002', '17.6.1.164', '17.6.0.062-orioledb'])
})
it('does not mutate the input', () => {
const input = toVersions(['15.6.1.139', '17.6.1.164'])
const sorted = sortPostgresVersionsNewestFirst(input)
expect(sorted).not.toBe(input)
expect(toNumbers(input)).toEqual(['15.6.1.139', '17.6.1.164'])
})
})
@@ -173,6 +173,19 @@ export function getRegionSelectionType({
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