1
1
import { Router } from 'express'
2
+ import format from 'pg-format'
2
3
import SQL from 'sql-template-strings'
3
4
import { RunQuery } from '../lib/connectionPool'
4
5
import sql = require( '../lib/sql' )
@@ -126,17 +127,22 @@ const addColumnSqlize = ({
126
127
const commentSql =
127
128
comment === undefined
128
129
? ''
129
- : ` COMMENT ON COLUMN " ${ schema } "." ${ table } "." ${ name } " IS ' ${ comment } ';`
130
+ : format ( ' COMMENT ON COLUMN %I.%I.%I IS %L;' , schema , table , name , comment )
130
131
131
- return `
132
- ALTER TABLE "${ schema } "."${ table } " ADD COLUMN "${ name } " "${ type } "
132
+ return format (
133
+ `
134
+ ALTER TABLE %I.%I ADD COLUMN %I %I
133
135
${ defaultValueSql }
134
136
${ isIdentitySql }
135
137
${ isNullableSql }
136
138
${ isPrimaryKeySql }
137
139
${ isUniqueSql } ;
138
-
139
- ${ commentSql } `
140
+ ${ commentSql } `,
141
+ schema ,
142
+ table ,
143
+ name ,
144
+ type
145
+ )
140
146
}
141
147
const getColumnSqlize = ( tableId : number , name : string ) => {
142
148
return SQL `` . append ( columns ) . append ( SQL ` WHERE c.oid = ${ tableId } AND column_name = ${ name } ` )
@@ -168,30 +174,48 @@ const alterColumnSqlize = ({
168
174
comment ?: string
169
175
} ) => {
170
176
const nameSql =
171
- typeof name === ' undefined' || name === oldName
177
+ name === undefined || name === oldName
172
178
? ''
173
- : ` ALTER TABLE " ${ schema } "." ${ table } " RENAME COLUMN " ${ oldName } " TO " ${ name } ";`
179
+ : format ( ' ALTER TABLE %I.%I RENAME COLUMN %I TO %I;' , schema , table , oldName , name )
174
180
// We use USING to allow implicit conversion of incompatible types (e.g. int4 -> text).
175
181
const typeSql =
176
182
type === undefined
177
183
? ''
178
- : `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " SET DATA TYPE "${ type } " USING "${ oldName } "::"${ type } ";`
184
+ : format (
185
+ 'ALTER TABLE %I.%I ALTER COLUMN %I SET DATA TYPE %I USING %I::%I;' ,
186
+ schema ,
187
+ table ,
188
+ oldName ,
189
+ type ,
190
+ oldName ,
191
+ type
192
+ )
179
193
let defaultValueSql = ''
180
194
if ( drop_default ) {
181
- defaultValueSql = `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " DROP DEFAULT;`
195
+ defaultValueSql = format (
196
+ 'ALTER TABLE %I.%I ALTER COLUMN %I DROP DEFAULT;' ,
197
+ schema ,
198
+ table ,
199
+ oldName
200
+ )
182
201
} else if ( default_value !== undefined ) {
183
- defaultValueSql = `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " SET DEFAULT ${ default_value } ;`
202
+ defaultValueSql = format (
203
+ `ALTER TABLE %I.%I ALTER COLUMN %I SET DEFAULT ${ default_value } ;` ,
204
+ schema ,
205
+ table ,
206
+ oldName
207
+ )
184
208
}
185
209
let isNullableSql = ''
186
210
if ( is_nullable !== undefined ) {
187
211
isNullableSql = is_nullable
188
- ? ` ALTER TABLE " ${ schema } "." ${ table } " ALTER COLUMN " ${ oldName } " DROP NOT NULL;`
189
- : ` ALTER TABLE " ${ schema } "." ${ table } " ALTER COLUMN " ${ oldName } " SET NOT NULL;`
212
+ ? format ( ' ALTER TABLE %I.%I ALTER COLUMN %I DROP NOT NULL;' , schema , table , oldName )
213
+ : format ( ' ALTER TABLE %I.%I ALTER COLUMN %I SET NOT NULL;' , schema , table , oldName )
190
214
}
191
215
const commentSql =
192
216
comment === undefined
193
217
? ''
194
- : ` COMMENT ON COLUMN " ${ schema } "." ${ table } "." ${ oldName } " IS ' ${ comment } ';`
218
+ : format ( ' COMMENT ON COLUMN %I.%I.%I IS %L;' , schema , table , oldName , comment )
195
219
196
220
// nameSql must be last.
197
221
return `
@@ -204,7 +228,7 @@ BEGIN;
204
228
COMMIT;`
205
229
}
206
230
const dropColumnSqlize = ( schema : string , table : string , name : string ) => {
207
- return ` ALTER TABLE " ${ schema } "." ${ table } " DROP COLUMN " ${ name } "`
231
+ return format ( ' ALTER TABLE %I.%I DROP COLUMN %I' , schema , table , name )
208
232
}
209
233
const removeSystemSchemas = ( data : Tables . Column [ ] ) => {
210
234
return data . filter ( ( x ) => ! DEFAULT_SYSTEM_SCHEMAS . includes ( x . schema ) )
0 commit comments