11import { Router } from 'express'
2+ import format from 'pg-format'
23import SQL from 'sql-template-strings'
34import { RunQuery } from '../lib/connectionPool'
45import sql = require( '../lib/sql' )
@@ -126,17 +127,22 @@ const addColumnSqlize = ({
126127 const commentSql =
127128 comment === undefined
128129 ? ''
129- : ` COMMENT ON COLUMN " ${ schema } "." ${ table } "." ${ name } " IS ' ${ comment } ';`
130+ : format ( ' COMMENT ON COLUMN %I.%I.%I IS %L;' , schema , table , name , comment )
130131
131- return `
132- ALTER TABLE "${ schema } "."${ table } " ADD COLUMN "${ name } " "${ type } "
132+ return format (
133+ `
134+ ALTER TABLE %I.%I ADD COLUMN %I %I
133135 ${ defaultValueSql }
134136 ${ isIdentitySql }
135137 ${ isNullableSql }
136138 ${ isPrimaryKeySql }
137139 ${ isUniqueSql } ;
138-
139- ${ commentSql } `
140+ ${ commentSql } `,
141+ schema ,
142+ table ,
143+ name ,
144+ type
145+ )
140146}
141147const getColumnSqlize = ( tableId : number , name : string ) => {
142148 return SQL `` . append ( columns ) . append ( SQL ` WHERE c.oid = ${ tableId } AND column_name = ${ name } ` )
@@ -168,30 +174,48 @@ const alterColumnSqlize = ({
168174 comment ?: string
169175} ) => {
170176 const nameSql =
171- typeof name === ' undefined' || name === oldName
177+ name === undefined || name === oldName
172178 ? ''
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 )
174180 // We use USING to allow implicit conversion of incompatible types (e.g. int4 -> text).
175181 const typeSql =
176182 type === undefined
177183 ? ''
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+ )
179193 let defaultValueSql = ''
180194 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+ )
182201 } 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+ )
184208 }
185209 let isNullableSql = ''
186210 if ( is_nullable !== undefined ) {
187211 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 )
190214 }
191215 const commentSql =
192216 comment === undefined
193217 ? ''
194- : ` COMMENT ON COLUMN " ${ schema } "." ${ table } "." ${ oldName } " IS ' ${ comment } ';`
218+ : format ( ' COMMENT ON COLUMN %I.%I.%I IS %L;' , schema , table , oldName , comment )
195219
196220 // nameSql must be last.
197221 return `
@@ -204,7 +228,7 @@ BEGIN;
204228COMMIT;`
205229}
206230const 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 )
208232}
209233const removeSystemSchemas = ( data : Tables . Column [ ] ) => {
210234 return data . filter ( ( x ) => ! DEFAULT_SYSTEM_SCHEMAS . includes ( x . schema ) )
0 commit comments