Skip to content

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

Fix: Dynamic texts for CHECK constraint placeholder (#29230)

Jason Chu6e456911c76e
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
index 3d717734a9f..bd5264360ea 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
@@ -35,6 +35,7 @@ import {
generateColumnFieldFromPostgresColumn,
generateCreateColumnPayload,
generateUpdateColumnPayload,
+ getPlaceholderText,
validateFields,
} from './ColumnEditor.utils'
import ColumnForeignKey from './ColumnForeignKey'
@@ -74,6 +75,9 @@ const ColumnEditor = ({
const [errors, setErrors] = useState<Dictionary<any>>({})
const [columnFields, setColumnFields] = useState<ColumnField>()
const [fkRelations, setFkRelations] = useState<ForeignKey[]>([])
+ const [placeholder, setPlaceholder] = useState(
+ getPlaceholderText(columnFields?.format, columnFields?.name)
+ )
const { data: types } = useEnumeratedTypesQuery({
projectRef: project?.ref,
@@ -129,6 +133,7 @@ const ColumnEditor = ({
}
const changedName = 'name' in changes && changes.name !== columnFields.name
+ const changedFormat = 'format' in changes && changes.format !== columnFields.format
if (
changedName &&
@@ -144,6 +149,12 @@ const ColumnEditor = ({
)
}
+ if (changedName || changedFormat) {
+ setPlaceholder(
+ getPlaceholderText(changes.format || columnFields.format, changes.name || columnFields.name)
+ )
+ }
+
const updatedColumnFields: ColumnField = { ...columnFields, ...changes }
setColumnFields(updatedColumnFields)
updateEditorDirty()
@@ -342,7 +353,7 @@ const ColumnEditor = ({
<Input
label="CHECK Constraint"
labelOptional="Optional"
- placeholder={`e.g length(${columnFields?.name || 'column_name'}) < 500`}
+ placeholder={placeholder}
type="text"
value={columnFields?.check ?? ''}
onChange={(event: any) => onUpdateField({ check: event.target.value })}
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
index 60319888c7a..8c71db7e8f3 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
@@ -4,7 +4,6 @@ import type { Dictionary } from 'types'
import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'
import type { ForeignKeyConstraint } from 'data/database/foreign-key-constraints-query'
-import type { PartitionedTable, Table } from 'data/table-editor/table-editor-types'
import { uuidv4 } from 'lib/helpers'
import {
ColumnField,
@@ -250,3 +249,50 @@ export const getForeignKeyCascadeAction = (action?: string) => {
return undefined
}
}
+
+export const getPlaceholderText = (format?: string, columnFieldName?: string) => {
+ const columnName = columnFieldName || 'column_name'
+
+ switch (format) {
+ case 'int2':
+ case 'int4':
+ case 'int8':
+ case 'numeric':
+ return `"${columnName}" > 0`
+
+ case 'float4':
+ case 'float8':
+ return `"${columnName}" > 0.0`
+
+ case 'text':
+ case 'varchar':
+ return `length("${columnName}") <= 50`
+
+ case 'json':
+ case 'jsonb':
+ return `jsonb_typeof("${columnName}"->'active') = 'boolean'`
+
+ case 'bool':
+ return `"${columnName}" in (true, false)`
+
+ case 'date':
+ return `"${columnName}" > '2024-01-01'`
+
+ case 'time':
+ return `"${columnName}" between '09:00:00' and '12:00:00'`
+
+ case 'timetz':
+ return `"${columnName}" at time zone 'UTC' between '09:00:00+00' and '17:00:00+00'`
+
+ case 'uuid':
+ return `"${columnName}" '00000000-0000-0000-0000-000000000000'`
+
+ case 'timestamp':
+ return `"${columnName}" > '2023-01-01 00:00' and "${columnName}" < '2025-01-01 00:00'`
+ case 'timestamptz':
+ return `"${columnName}" > '2023-01-01 00:00:00+00' and "${columnName}" < '2025-01-01 00:00:00+00'`
+
+ default:
+ return `length("${columnName}") < 500`
+ }
+}