From 3b6c4aaef77444bf947fb9a05a62f8a2bc756298 Mon Sep 17 00:00:00 2001 From: Bobbie Soedirgo Date: Wed, 20 Mar 2024 13:49:37 +0700 Subject: [PATCH] fix: duplicated columns w/ multiple unique constraints --- src/lib/sql/columns.sql | 2 +- test/lib/columns.ts | 73 ++++++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/lib/sql/columns.sql b/src/lib/sql/columns.sql index 8866987b..ad01e22a 100644 --- a/src/lib/sql/columns.sql +++ b/src/lib/sql/columns.sql @@ -76,7 +76,7 @@ FROM ) ON t.typtype = 'd' AND t.typbasetype = bt.oid LEFT JOIN ( - SELECT + SELECT DISTINCT ON (table_id, ordinal_position) conrelid AS table_id, conkey[1] AS ordinal_position FROM pg_catalog.pg_constraint diff --git a/test/lib/columns.ts b/test/lib/columns.ts index 85767300..3fcac79f 100644 --- a/test/lib/columns.ts +++ b/test/lib/columns.ts @@ -934,6 +934,39 @@ test('column with multiple checks', async () => { await pgMeta.query(`drop table t`) }) +test('column with multiple unique constraints', async () => { + await pgMeta.query(`create table t(c int8 unique); alter table t add unique (c);`) + + const res = await pgMeta.columns.list() + const columns = res.data + ?.filter((c) => c.schema === 'public' && c.table === 't') + .map(({ id, table_id, ...c }) => c) + expect(columns).toMatchInlineSnapshot(` + [ + { + "check": null, + "comment": null, + "data_type": "bigint", + "default_value": null, + "enums": [], + "format": "int8", + "identity_generation": null, + "is_generated": false, + "is_identity": false, + "is_nullable": true, + "is_unique": true, + "is_updatable": true, + "name": "c", + "ordinal_position": 1, + "schema": "public", + "table": "t", + }, + ] + `) + + await pgMeta.query(`drop table t`) +}) + test('dropping column checks', async () => { await pgMeta.query(`create table public.t(c int8 check (c != 0))`) @@ -955,34 +988,30 @@ test('column with fully-qualified type', async () => { schema: 'public', name: 't', }) - const column = await pgMeta.columns.create({ + const { data } = await pgMeta.columns.create({ table_id: table.data!.id, name: 'c', type: 's.my_type', }) + const { id, table_id, ...column } = data! expect(column).toMatchInlineSnapshot(` { - "data": { - "check": null, - "comment": null, - "data_type": "USER-DEFINED", - "default_value": null, - "enums": [], - "format": "my_type", - "id": "16619.1", - "identity_generation": null, - "is_generated": false, - "is_identity": false, - "is_nullable": true, - "is_unique": false, - "is_updatable": true, - "name": "c", - "ordinal_position": 1, - "schema": "public", - "table": "t", - "table_id": 16619, - }, - "error": null, + "check": null, + "comment": null, + "data_type": "USER-DEFINED", + "default_value": null, + "enums": [], + "format": "my_type", + "identity_generation": null, + "is_generated": false, + "is_identity": false, + "is_nullable": true, + "is_unique": false, + "is_updatable": true, + "name": "c", + "ordinal_position": 1, + "schema": "public", + "table": "t", } `)