File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change 1
1
import { Router } from 'express'
2
- import format from 'pg-format'
2
+ import format , { literal } from 'pg-format'
3
3
import SQL from 'sql-template-strings'
4
4
import { RunQuery } from '../lib/connectionPool'
5
5
import sql = require( '../lib/sql' )
@@ -119,7 +119,14 @@ const addColumnSqlize = ({
119
119
is_unique ?: boolean
120
120
comment ?: string
121
121
} ) => {
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
+ }
123
130
const isIdentitySql = is_identity ? 'GENERATED BY DEFAULT AS IDENTITY' : ''
124
131
const isNullableSql = is_nullable ? 'NULL' : 'NOT NULL'
125
132
const isPrimaryKeySql = is_primary_key ? 'PRIMARY KEY' : ''
Original file line number Diff line number Diff line change @@ -308,6 +308,20 @@ describe('/tables', async () => {
308
308
await axios . delete ( `${ URL } /columns/${ newTable . id } .1` )
309
309
await axios . delete ( `${ URL } /tables/${ newTable . id } ` )
310
310
} )
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
+ } )
311
325
it ( 'PATCH /columns' , async ( ) => {
312
326
const { data : newTable } = await axios . post ( `${ URL } /tables` , { name : 'foo bar' } )
313
327
await axios . post ( `${ URL } /columns` , {
You can’t perform that action at this time.
0 commit comments