@@ -6,11 +6,11 @@ import { PostgresMetaResult, PostgresColumn } from './types'
6
6
7
7
export default class PostgresMetaColumns {
8
8
query : ( sql : string ) => Promise < PostgresMetaResult < any > >
9
- metaTable : PostgresMetaTables
9
+ metaTables : PostgresMetaTables
10
10
11
11
constructor ( query : ( sql : string ) => Promise < PostgresMetaResult < any > > ) {
12
12
this . query = query
13
- this . metaTable = new PostgresMetaTables ( query )
13
+ this . metaTables = new PostgresMetaTables ( query )
14
14
}
15
15
16
16
async list ( { includeSystemSchemas = false } = { } ) : Promise < PostgresMetaResult < PostgresColumn [ ] > > {
@@ -102,7 +102,7 @@ export default class PostgresMetaColumns {
102
102
is_unique ?: boolean
103
103
comment ?: string
104
104
} ) : Promise < PostgresMetaResult < PostgresColumn > > {
105
- const { data, error } = await this . metaTable . retrieve ( { id : table_id } )
105
+ const { data, error } = await this . metaTables . retrieve ( { id : table_id } )
106
106
if ( error ) {
107
107
return { data : null , error }
108
108
}
@@ -176,28 +176,30 @@ COMMIT;`
176
176
const nameSql =
177
177
name === undefined || name === old ! . name
178
178
? ''
179
- : `ALTER TABLE ${ old ! . schema } .${ old ! . table } RENAME COLUMN ${ old ! . name } TO ${ name } ;`
179
+ : `ALTER TABLE ${ ident ( old ! . schema ) } .${ ident ( old ! . table ) } RENAME COLUMN ${ ident (
180
+ old ! . name
181
+ ) } TO ${ ident ( name ) } ;`
180
182
// We use USING to allow implicit conversion of incompatible types (e.g. int4 -> text).
181
183
const typeSql =
182
184
type === undefined
183
185
? ''
184
- : `ALTER TABLE ${ old ! . schema } .${ old ! . table } ALTER COLUMN ${
186
+ : `ALTER TABLE ${ ident ( old ! . schema ) } .${ ident ( old ! . table ) } ALTER COLUMN ${ ident (
185
187
old ! . name
186
- } SET DATA TYPE ${ type } USING ${ old ! . name } ::${ type } ;`
188
+ ) } SET DATA TYPE ${ type } USING ${ ident ( old ! . name ) } ::${ type } ;`
187
189
188
190
let defaultValueSql : string
189
191
if ( drop_default ) {
190
- defaultValueSql = `ALTER TABLE ${ old ! . schema } .${ old ! . table } ALTER COLUMN ${
191
- old ! . name
192
- } DROP DEFAULT;`
192
+ defaultValueSql = `ALTER TABLE ${ ident ( old ! . schema ) } .${ ident (
193
+ old ! . table
194
+ ) } ALTER COLUMN ${ ident ( old ! . name ) } DROP DEFAULT;`
193
195
} else if ( default_value === undefined ) {
194
196
defaultValueSql = ''
195
197
} else {
196
198
const defaultValue =
197
199
default_value_format === 'expression' ? default_value : literal ( default_value )
198
- defaultValueSql = `ALTER TABLE ${ old ! . schema } .${ old ! . table } ALTER COLUMN ${
199
- old ! . name
200
- } SET DEFAULT ${ defaultValue } ;`
200
+ defaultValueSql = `ALTER TABLE ${ ident ( old ! . schema ) } .${ ident (
201
+ old ! . table
202
+ ) } ALTER COLUMN ${ ident ( old ! . name ) } SET DEFAULT ${ defaultValue } ;`
201
203
}
202
204
// What identitySql does vary depending on the old and new values of
203
205
// is_identity and identity_generation.
@@ -209,32 +211,38 @@ COMMIT;`
209
211
// | false | - | add identity | drop if exists |
210
212
let identitySql = `ALTER TABLE ${ ident ( old ! . schema ) } .${ ident ( old ! . table ) } ALTER COLUMN ${ ident (
211
213
old ! . name
212
- ) } ; `
214
+ ) } `
213
215
if ( is_identity === false ) {
214
- identitySql += 'DROP IDENTITY IF EXISTS;'
216
+ identitySql += ' DROP IDENTITY IF EXISTS;'
215
217
} else if ( old ! . is_identity === true ) {
216
218
if ( identity_generation === undefined ) {
217
219
identitySql = ''
218
220
} else {
219
- identitySql += `SET GENERATED ${ identity_generation } ;`
221
+ identitySql += ` SET GENERATED ${ identity_generation } ;`
220
222
}
221
223
} else if ( is_identity === undefined ) {
222
224
identitySql = ''
223
225
} else {
224
- identitySql += `ADD GENERATED ${ identity_generation } AS IDENTITY;`
226
+ identitySql += ` ADD GENERATED ${ identity_generation } AS IDENTITY;`
225
227
}
226
228
let isNullableSql : string
227
229
if ( is_nullable === undefined ) {
228
230
isNullableSql = ''
229
231
} else {
230
232
isNullableSql = is_nullable
231
- ? `ALTER TABLE ${ old ! . schema } .${ old ! . table } ALTER COLUMN ${ old ! . name } DROP NOT NULL;`
232
- : `ALTER TABLE ${ old ! . schema } .${ old ! . table } ALTER COLUMN ${ old ! . name } SET NOT NULL;`
233
+ ? `ALTER TABLE ${ ident ( old ! . schema ) } .${ ident ( old ! . table ) } ALTER COLUMN ${ ident (
234
+ old ! . name
235
+ ) } DROP NOT NULL;`
236
+ : `ALTER TABLE ${ ident ( old ! . schema ) } .${ ident ( old ! . table ) } ALTER COLUMN ${ ident (
237
+ old ! . name
238
+ ) } SET NOT NULL;`
233
239
}
234
240
const commentSql =
235
241
comment === undefined
236
242
? ''
237
- : `COMMENT ON COLUMN ${ old ! . schema } .${ old ! . table } .${ old ! . name } IS ${ comment } ;`
243
+ : `COMMENT ON COLUMN ${ ident ( old ! . schema ) } .${ ident ( old ! . table ) } .${ ident (
244
+ old ! . name
245
+ ) } IS ${ literal ( comment ) } ;`
238
246
239
247
// nameSql must be last.
240
248
// defaultValueSql must be after typeSql.
@@ -263,7 +271,9 @@ COMMIT;`
263
271
if ( error ) {
264
272
return { data : null , error }
265
273
}
266
- const sql = `ALTER TABLE ${ column ! . schema } .${ column ! . table } DROP COLUMN ${ column ! . name } ;`
274
+ const sql = `ALTER TABLE ${ ident ( column ! . schema ) } .${ ident ( column ! . table ) } DROP COLUMN ${ ident (
275
+ column ! . name
276
+ ) } ;`
267
277
{
268
278
const { error } = await this . query ( sql )
269
279
if ( error ) {
0 commit comments