Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/lib/PostgresMetaColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,16 @@ COMMIT;`
// TODO: make this more robust - use type_id or type_schema + type_name instead
// of just type.
const typeIdent = (type: string) => {
return type.endsWith('[]')
? `${ident(type.slice(0, -2))}[]`
: type.includes('.')
? type
: ident(type)
const isArray = type.endsWith('[]')
const base = isArray ? type.slice(0, -2) : type
if (!base.includes('.')) {
return isArray ? `${ident(base)}[]` : ident(base)
}
if (isArray) {
return `${base
.split('.')
.map((part) => ident(part))
.join('.')}[]`
}
return base
}
33 changes: 33 additions & 0 deletions test/lib/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,3 +1017,36 @@ test('column with fully-qualified type', async () => {

await pgMeta.query(`drop table public.t; drop schema s cascade;`)
})

test('column with schema-qualified array type', async () => {
await pgMeta.query(
`drop table if exists public.t_schema_array; drop schema if exists s_schema_array cascade;`
)
await pgMeta.query(
`create table public.t_schema_array(); create schema s_schema_array; create type s_schema_array.my_type as enum ('a');`
)

try {
const table = await pgMeta.tables.retrieve({
schema: 'public',
name: 't_schema_array',
})
const created = await pgMeta.columns.create({
table_id: table.data!.id,
name: 'c',
type: 's_schema_array.my_type[]',
})
expect(created.error).toBeNull()
expect(created.data).toMatchObject({
name: 'c',
format: '_my_type',
data_type: 'ARRAY',
schema: 'public',
table: 't_schema_array',
})
} finally {
await pgMeta.query(
`drop table if exists public.t_schema_array; drop schema if exists s_schema_array cascade;`
)
}
})