@@ -4,9 +4,6 @@ import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
4
4
import { columnsSql } from './sql'
5
5
import { PostgresMetaResult , PostgresColumn } from './types'
6
6
7
- // TODO: Fix handling of `type` in `create()` and `update()`.
8
- // `type` on its own is not enough, e.g. `1::my type` should be `1::"my type"`.
9
- // `ident(type)` is not enough, e.g. `"int2[]"` should be `"int2"[]`.
10
7
export default class PostgresMetaColumns {
11
8
query : ( sql : string ) => Promise < PostgresMetaResult < any > >
12
9
metaTables : PostgresMetaTables
@@ -163,7 +160,7 @@ export default class PostgresMetaColumns {
163
160
164
161
const sql = `
165
162
BEGIN;
166
- ALTER TABLE ${ ident ( schema ) } .${ ident ( table ) } ADD COLUMN ${ ident ( name ) } ${ type }
163
+ ALTER TABLE ${ ident ( schema ) } .${ ident ( table ) } ADD COLUMN ${ ident ( name ) } ${ typeIdent ( type ) }
167
164
${ defaultValueClause }
168
165
${ isNullableClause }
169
166
${ isPrimaryKeyClause }
@@ -223,7 +220,7 @@ COMMIT;`
223
220
? ''
224
221
: `ALTER TABLE ${ ident ( old ! . schema ) } .${ ident ( old ! . table ) } ALTER COLUMN ${ ident (
225
222
old ! . name
226
- ) } SET DATA TYPE ${ ident ( type ) } USING ${ ident ( old ! . name ) } ::${ ident ( type ) } ;`
223
+ ) } SET DATA TYPE ${ typeIdent ( type ) } USING ${ ident ( old ! . name ) } ::${ typeIdent ( type ) } ;`
227
224
228
225
let defaultValueSql : string
229
226
if ( drop_default ) {
@@ -349,3 +346,7 @@ COMMIT;`
349
346
return { data : column ! , error : null }
350
347
}
351
348
}
349
+
350
+ const typeIdent = ( type : string ) => {
351
+ return type . endsWith ( '[]' ) ? `${ ident ( type . slice ( 0 , - 2 ) ) } []` : ident ( type )
352
+ }
0 commit comments