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 11import { Router } from 'express'
2- import format from 'pg-format'
2+ import format , { literal } from 'pg-format'
33import SQL from 'sql-template-strings'
44import { RunQuery } from '../lib/connectionPool'
55import 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' : ''
Original file line number Diff line number Diff 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` , {
You can’t perform that action at this time.
0 commit comments