Skip to content

Commit fab59a7

Browse files
committed
fix: create column with fully-qualified type
1 parent 9c61b75 commit fab59a7

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/lib/PostgresMetaColumns.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,12 @@ COMMIT;`
419419
}
420420
}
421421

422+
// TODO: make this more robust - use type_id or type_schema + type_name instead
423+
// of just type.
422424
const typeIdent = (type: string) => {
423-
return type.endsWith('[]') ? `${ident(type.slice(0, -2))}[]` : ident(type)
425+
return type.endsWith('[]')
426+
? `${ident(type.slice(0, -2))}[]`
427+
: type.includes('.')
428+
? type
429+
: ident(type)
424430
}

test/lib/columns.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,44 @@ test('dropping column checks', async () => {
947947

948948
await pgMeta.query(`drop table t`)
949949
})
950+
951+
test('column with fully-qualified type', async () => {
952+
await pgMeta.query(`create table public.t(); create schema s; create type s.my_type as enum ();`)
953+
954+
const table = await pgMeta.tables.retrieve({
955+
schema: 'public',
956+
name: 't',
957+
})
958+
const column = await pgMeta.columns.create({
959+
table_id: table.data!.id,
960+
name: 'c',
961+
type: 's.my_type',
962+
})
963+
expect(column).toMatchInlineSnapshot(`
964+
{
965+
"data": {
966+
"check": null,
967+
"comment": null,
968+
"data_type": "USER-DEFINED",
969+
"default_value": null,
970+
"enums": [],
971+
"format": "my_type",
972+
"id": "16619.1",
973+
"identity_generation": null,
974+
"is_generated": false,
975+
"is_identity": false,
976+
"is_nullable": true,
977+
"is_unique": false,
978+
"is_updatable": true,
979+
"name": "c",
980+
"ordinal_position": 1,
981+
"schema": "public",
982+
"table": "t",
983+
"table_id": 16619,
984+
},
985+
"error": null,
986+
}
987+
`)
988+
989+
await pgMeta.query(`drop table public.t; drop schema s cascade;`)
990+
})

0 commit comments

Comments
 (0)