Skip to content

Commit c361a2e

Browse files
committed
fix: /columns default_value for text
In the SQL for default_value, the string literal should be quoted.
1 parent 26bafaf commit c361a2e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/api/columns.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Router } from 'express'
2-
import format from 'pg-format'
2+
import format, { literal } from 'pg-format'
33
import SQL from 'sql-template-strings'
44
import { RunQuery } from '../lib/connectionPool'
55
import sql = require('../lib/sql')
@@ -119,7 +119,14 @@ const addColumnSqlize = ({
119119
is_unique?: boolean
120120
comment?: string
121121
}) => {
122-
const defaultValueSql = default_value === undefined ? '' : `DEFAULT ${default_value}`
122+
let defaultValueSql: string
123+
if (default_value === undefined) {
124+
defaultValueSql = ''
125+
} else if (typeof default_value === 'string') {
126+
defaultValueSql = `DEFAULT ${literal(default_value)}`
127+
} else {
128+
defaultValueSql = `DEFAULT ${default_value}`
129+
}
123130
const isIdentitySql = is_identity ? 'GENERATED BY DEFAULT AS IDENTITY' : ''
124131
const isNullableSql = is_nullable ? 'NULL' : 'NOT NULL'
125132
const isPrimaryKeySql = is_primary_key ? 'PRIMARY KEY' : ''

test/integration/index.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,20 @@ describe('/tables', async () => {
308308
await axios.delete(`${URL}/columns/${newTable.id}.1`)
309309
await axios.delete(`${URL}/tables/${newTable.id}`)
310310
})
311+
it('/columns default_value for type text', async () => {
312+
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'a' })
313+
const { data: newColumn } = await axios.post(`${URL}/columns`, {
314+
table_id: newTable.id,
315+
name: 'a',
316+
type: 'text',
317+
default_value: 'a',
318+
})
319+
320+
assert.equal(newColumn.default_value, `'a'::text`)
321+
322+
await axios.delete(`${URL}/columns/${newTable.id}.1`)
323+
await axios.delete(`${URL}/tables/${newTable.id}`)
324+
})
311325
it('PATCH /columns', async () => {
312326
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'foo bar' })
313327
await axios.post(`${URL}/columns`, {

0 commit comments

Comments
 (0)