Skip to content

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

ColumnDefaultValue null value suggestion to apply if column is nullable (#50117)

Joshen Lim08224b40d39d
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx
index d05901e00b0..28941c4878e 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnDefaultValue.tsx
@@ -3,31 +3,50 @@ import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectVa
import { POSTGRES_DATA_TYPES } from '../SidePanelEditor.constants'
import type { ColumnField } from '../SidePanelEditor.types'
-import { typeExpressionSuggestions } from './ColumnEditor.constants'
+import { nullSuggestion, typeExpressionSuggestions } from './ColumnEditor.constants'
import type { Suggestion } from './ColumnEditor.types'
import InputWithSuggestions from './InputWithSuggestions'
import type { EnumeratedType } from '@/data/enumerated-types/enumerated-types-query'
+const getDefaultValueSuggestions = (format: string, isNullable: boolean): Suggestion[] => {
+ const nullableSuggestion: Suggestion[] = isNullable ? [nullSuggestion] : []
+ return nullableSuggestion.concat(typeExpressionSuggestions?.[format] ?? [])
+}
+
interface ColumnDefaultValueProps {
columnFields: ColumnField
enumTypes: EnumeratedType[]
+ className?: string
+ size?: 'small' | 'tiny' | 'medium' | 'large'
+ showLabel?: boolean
+ layout?: 'horizontal' | 'vertical'
+ 'data-testid'?: string
+ 'aria-label'?: string
onUpdateField: (changes: Partial<ColumnField>) => void
}
-const ColumnDefaultValue = ({
+export const ColumnDefaultValue = ({
columnFields,
enumTypes = [],
+ className,
+ size,
+ showLabel = true,
+ layout = 'vertical',
+ 'data-testid': dataTestId,
+ 'aria-label': ariaLabel,
onUpdateField = noop,
}: ColumnDefaultValueProps) => {
- const suggestions: Suggestion[] = typeExpressionSuggestions?.[columnFields.format] ?? []
+ const { format, isNullable, isIdentity } = columnFields
+ const suggestions: Suggestion[] = getDefaultValueSuggestions(format, isNullable)
+ // Identity columns have their default value assigned by Postgres, so it isn't user-editable
+ const disabled = format.includes('int') && isIdentity
// If selected column type is a user-defined enum, show a dropdown list of options
const isEnum: boolean =
- !POSTGRES_DATA_TYPES.includes(columnFields.format) &&
- enumTypes.some((type) => type.name === columnFields.format)
+ !POSTGRES_DATA_TYPES.includes(format) && enumTypes.some((type) => type.name === format)
if (isEnum) {
- const enumType = enumTypes.find((type) => type.name === columnFields.format)
+ const enumType = enumTypes.find((type) => type.name === format)
const enumValues = enumType?.enums ?? []
const originalDefaultValue = columnFields?.defaultValue ?? ''
const formattedValue = originalDefaultValue.includes('::')
@@ -37,18 +56,28 @@ const ColumnDefaultValue = ({
if (enumType !== undefined) {
return (
<>
- <label className="block text-foreground-light">Default Value</label>
+ {showLabel && (
+ <label htmlFor="select-editor" className="block text-foreground-light">
+ Default Value
+ </label>
+ )}
<Select
name="select-editor"
value={formattedValue}
onValueChange={(value) => onUpdateField({ defaultValue: value })}
>
- <SelectTrigger>
+ <SelectTrigger
+ size={size}
+ className={className}
+ data-testid={dataTestId}
+ aria-label={ariaLabel}
+ >
<SelectValue id="select-editor" placeholder="NULL" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
- <SelectItem value={null as any}>NULL</SelectItem>
+ {/* @ts-ignore: value being null is valid in this context */}
+ {isNullable && <SelectItem value={null}>NULL</SelectItem>}
{enumValues.map((value) => (
<SelectItem key={value} value={value}>
{value}
@@ -64,24 +93,31 @@ const ColumnDefaultValue = ({
return (
<InputWithSuggestions
- label="Default Value"
- layout="vertical"
- description="Can either be a literal or an expression. When using an expression wrap your expression in brackets, e.g. (gen_random_uuid())"
+ label={showLabel ? 'Default Value' : undefined}
+ layout={layout}
+ description={
+ showLabel
+ ? 'Can either be a literal or an expression. When using an expression wrap your expression in brackets, e.g. (gen_random_uuid())'
+ : undefined
+ }
placeholder={
typeof columnFields.defaultValue === 'string' && columnFields.defaultValue.length === 0
? 'EMPTY'
: 'NULL'
}
+ size={size}
+ disabled={disabled}
+ className={className}
+ data-testid={dataTestId}
+ aria-label={ariaLabel}
value={columnFields?.defaultValue ?? ''}
suggestions={suggestions}
suggestionsHeader="Suggested expressions"
suggestionsTooltip="Suggested expressions"
- onChange={(event: any) => onUpdateField({ defaultValue: event.target.value })}
+ onChange={(event) => onUpdateField({ defaultValue: event.target.value })}
onSelectSuggestion={(suggestion: Suggestion) =>
onUpdateField({ defaultValue: suggestion.value })
}
/>
)
}
-
-export default ColumnDefaultValue
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.constants.ts b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.constants.ts
index aafa68b750a..d0ecf56759b 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.constants.ts
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.constants.ts
@@ -1,6 +1,12 @@
import type { Suggestion } from './ColumnEditor.types'
import type { Dictionary } from '@/types'
+export const nullSuggestion: Suggestion = {
+ name: 'Set as NULL',
+ value: null,
+ description: 'Set the default value as NULL value',
+}
+
const defaultTimeBasedExpressions: Suggestion[] = [
{
name: 'now()',
@@ -15,11 +21,6 @@ const defaultTimeBasedExpressions: Suggestion[] = [
]
const defaultTextBasedValues: Suggestion[] = [
- {
- name: 'Set as NULL',
- value: null,
- description: 'Set the default value as NULL value',
- },
{
name: 'Set as empty string',
value: '',
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
index 4b646a22274..e3a8977fcfe 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
@@ -33,7 +33,7 @@ import type {
CreateColumnPayload,
UpdateColumnPayload,
} from '../SidePanelEditor.types'
-import ColumnDefaultValue from './ColumnDefaultValue'
+import { ColumnDefaultValue } from './ColumnDefaultValue'
import {
generateColumnField,
generateColumnFieldFromPGColumn,
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx
index cba1129e2ad..cf4b4ee0d79 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx
@@ -22,10 +22,8 @@ import {
} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
-import { typeExpressionSuggestions } from '../ColumnEditor/ColumnEditor.constants'
-import type { Suggestion } from '../ColumnEditor/ColumnEditor.types'
+import { ColumnDefaultValue } from '../ColumnEditor/ColumnDefaultValue'
import ColumnType from '../ColumnEditor/ColumnType'
-import InputWithSuggestions from '../ColumnEditor/InputWithSuggestions'
import { ForeignKey } from '../ForeignKeySelector/ForeignKeySelector.types'
import type { ColumnField } from '../SidePanelEditor.types'
import { checkIfRelationChanged } from './ForeignKeysManagement/ForeignKeysManagement.utils'
@@ -79,7 +77,6 @@ export const Column = ({
}: ColumnProps) => {
const { data: project } = useSelectedProjectQuery()
const [open, setOpen] = useState(false)
- const suggestions: Suggestion[] = typeExpressionSuggestions?.[column.format] ?? []
const settingsCount = [
column.isNullable ? 1 : 0,
@@ -318,27 +315,17 @@ export const Column = ({
</div>
<div className={`${isNewRecord ? 'w-[25%]' : 'w-[30%]'}`}>
<div className="w-[95%]">
- <InputWithSuggestions
- aria-label="Column default value"
- data-testid={`${column.name}-default-value`}
- placeholder={
- typeof column.defaultValue === 'string' && column.defaultValue.length === 0
- ? 'EMPTY'
- : 'NULL'
- }
+ <ColumnDefaultValue
+ columnFields={column}
+ enumTypes={enumTypes}
+ showLabel={false}
size="small"
- value={column.defaultValue ?? ''}
- disabled={column.format.includes('int') && column.isIdentity}
className={`rounded-sm lg:gap-0 ${
column.format.includes('int') && column.isIdentity ? 'opacity-50' : ''
}`}
- suggestions={suggestions}
- suggestionsHeader="Suggested expressions"
- suggestionsTooltip="Suggested expressions"
- onChange={(event) => onUpdateColumn({ defaultValue: event.target.value })}
- onSelectSuggestion={(suggestion: Suggestion) =>
- onUpdateColumn({ defaultValue: suggestion.value })
- }
+ data-testid={`${column.name}-default-value`}
+ aria-label="Column default value"
+ onUpdateField={onUpdateColumn}
/>
</div>
</div>