mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
## Summary - The Connection management "Allocation strategy" select on the Auth > Performance page called its `onValueChange` handler's conversion logic with whatever value it was given, with no validation. If that handler ever fired with a value outside the `'percent' | 'connections'` enum, it would silently overwrite a correctly loaded config, converting it to the wrong absolute connection count and leaving the strategy dropdown in a blank/inconsistent state. - Extracted the percent/connections conversion into a pure, unit-tested `convertPoolSize()` helper (`PerformanceSettingsForm.utils.ts`) and added a guard so `onValueChange` ignores any value that isn't a recognized allocation unit. ## How to test 1. Under **Connection management**, switch **Allocation strategy** back and forth between "Absolute number of connections" and "Percent of max connections" — the value should convert correctly each time and the dropdown should never render blank. 2. Save, then hard-reload the page — the saved strategy and value should persist as shown. ## Test plan - [x] `PerformanceSettingsForm.utils.test.ts` — unit tests covering both conversion directions, clamping, and the invalid-value guard - [x] `PerformanceSettingsForm.test.tsx` — MSW-backed component test verifying persisted percent/absolute configs render correctly on load - [x] `pnpm test:studio` - [x] `pnpm typecheck` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Improvements** * Switching database pool allocation strategies now automatically converts values between percentage and connection-based units. * Values are rounded and constrained appropriately to remain within supported limits. * Allocation settings now handle invalid or zero values more safely. * **Tests** * Added coverage verifying persisted allocation strategies and pool-size conversion behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
28 lines
800 B
TypeScript
28 lines
800 B
TypeScript
export type AllocationUnit = 'percent' | 'connections'
|
|
|
|
export function isAllocationUnit(value: unknown): value is AllocationUnit {
|
|
return value === 'percent' || value === 'connections'
|
|
}
|
|
|
|
// Converts a pool size between units when the allocation strategy changes,
|
|
// preserving roughly the same effective connection budget.
|
|
export function convertPoolSize({
|
|
fromUnit,
|
|
toUnit,
|
|
currentValue,
|
|
maxConnectionLimit,
|
|
}: {
|
|
fromUnit: AllocationUnit
|
|
toUnit: AllocationUnit
|
|
currentValue: number
|
|
maxConnectionLimit: number
|
|
}): number {
|
|
if (fromUnit === toUnit) return currentValue
|
|
|
|
if (toUnit === 'percent') {
|
|
return Math.ceil((Math.min(maxConnectionLimit, currentValue) / maxConnectionLimit) * 100)
|
|
}
|
|
|
|
return Math.floor(maxConnectionLimit * (Math.min(100, currentValue) / 100))
|
|
}
|