Skip to content

Commit 6c1257b

Browse files
committed
fix: default_value can be literals or expressions
Literals: 'hello', 123 Expressions: NULL, NOW()
1 parent bd481d4 commit 6c1257b

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/api/columns.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const addColumnSqlize = ({
102102
name,
103103
type,
104104
default_value,
105+
default_value_format = 'literal',
105106
is_identity = false,
106107
is_nullable = true,
107108
is_primary_key = false,
@@ -113,6 +114,7 @@ const addColumnSqlize = ({
113114
name: string
114115
type: string
115116
default_value?: any
117+
default_value_format?: 'expression' | 'literal'
116118
is_identity?: boolean
117119
is_nullable?: boolean
118120
is_primary_key?: boolean
@@ -122,10 +124,10 @@ const addColumnSqlize = ({
122124
let defaultValueSql: string
123125
if (default_value === undefined) {
124126
defaultValueSql = ''
125-
} else if (typeof default_value === 'string') {
126-
defaultValueSql = `DEFAULT ${literal(default_value)}`
127-
} else {
127+
} else if (default_value_format === 'expression') {
128128
defaultValueSql = `DEFAULT ${default_value}`
129+
} else {
130+
defaultValueSql = `DEFAULT ${literal(default_value)}`
129131
}
130132
const isIdentitySql = is_identity ? 'GENERATED BY DEFAULT AS IDENTITY' : ''
131133
const isNullableSql = is_nullable ? 'NULL' : 'NOT NULL'
@@ -167,6 +169,7 @@ const alterColumnSqlize = ({
167169
type,
168170
drop_default = false,
169171
default_value,
172+
default_value_format = 'literal',
170173
is_nullable,
171174
comment,
172175
}: {
@@ -177,6 +180,7 @@ const alterColumnSqlize = ({
177180
type?: string
178181
drop_default?: boolean
179182
default_value?: any
183+
default_value_format?: 'expression' | 'literal'
180184
is_nullable?: boolean
181185
comment?: string
182186
}) => {
@@ -206,8 +210,10 @@ const alterColumnSqlize = ({
206210
oldName
207211
)
208212
} else if (default_value !== undefined) {
213+
let defaultValue =
214+
default_value_format === 'expression' ? default_value : literal(default_value)
209215
defaultValueSql = format(
210-
`ALTER TABLE %I.%I ALTER COLUMN %I SET DEFAULT ${default_value};`,
216+
`ALTER TABLE %I.%I ALTER COLUMN %I SET DEFAULT ${defaultValue};`,
211217
schema,
212218
table,
213219
oldName

test/integration/index.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,23 +313,24 @@ describe('/tables', async () => {
313313
(column) =>
314314
column.id === `${newTable.id}.1` && column.name === 'foo bar' && column.format === 'int2'
315315
)
316-
assert.equal(newColumn.default_value, 42)
316+
assert.equal(newColumn.default_value, "'42'::smallint")
317317
assert.equal(newColumn.is_nullable, false)
318318
assert.equal(newColumn.comment, 'foo')
319319

320320
await axios.delete(`${URL}/columns/${newTable.id}.1`)
321321
await axios.delete(`${URL}/tables/${newTable.id}`)
322322
})
323-
it('/columns default_value for type text', async () => {
323+
it('/columns default_value with expressions', async () => {
324324
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'a' })
325325
const { data: newColumn } = await axios.post(`${URL}/columns`, {
326326
table_id: newTable.id,
327327
name: 'a',
328-
type: 'text',
329-
default_value: 'a',
328+
type: 'timestamptz',
329+
default_value: 'NOW()',
330+
default_value_format: 'expression',
330331
})
331332

332-
assert.equal(newColumn.default_value, `'a'::text`)
333+
assert.equal(newColumn.default_value, 'now()')
333334

334335
await axios.delete(`${URL}/columns/${newTable.id}.1`)
335336
await axios.delete(`${URL}/tables/${newTable.id}`)

0 commit comments

Comments
 (0)