@@ -4,9 +4,6 @@ import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
44import { columnsSql } from './sql'
55import { PostgresMetaResult , PostgresColumn } from './types'
66
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"[]`.
107export default class PostgresMetaColumns {
118 query : ( sql : string ) => Promise < PostgresMetaResult < any > >
129 metaTables : PostgresMetaTables
@@ -163,7 +160,7 @@ export default class PostgresMetaColumns {
163160
164161 const sql = `
165162BEGIN;
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 ) }
167164 ${ defaultValueClause }
168165 ${ isNullableClause }
169166 ${ isPrimaryKeyClause }
@@ -223,7 +220,7 @@ COMMIT;`
223220 ? ''
224221 : `ALTER TABLE ${ ident ( old ! . schema ) } .${ ident ( old ! . table ) } ALTER COLUMN ${ ident (
225222 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 ) } ;`
227224
228225 let defaultValueSql : string
229226 if ( drop_default ) {
@@ -349,3 +346,7 @@ COMMIT;`
349346 return { data : column ! , error : null }
350347 }
351348}
349+
350+ const typeIdent = ( type : string ) => {
351+ return type . endsWith ( '[]' ) ? `${ ident ( type . slice ( 0 , - 2 ) ) } []` : ident ( type )
352+ }
0 commit comments