From f75d7af8924007c735c02f8b9e9c14722c8d79e9 Mon Sep 17 00:00:00 2001 From: Oliver Rice Date: Wed, 10 Jul 2024 11:02:51 -0500 Subject: [PATCH 1/2] issue 542 checkin current (incorrect) behavior with partial unique indexes --- test/expected/issue_542_partial_unique.out | 116 +++++++++++++++++++++ test/sql/issue_542_partial_unique.sql | 54 ++++++++++ 2 files changed, 170 insertions(+) create mode 100644 test/expected/issue_542_partial_unique.out create mode 100644 test/sql/issue_542_partial_unique.sql diff --git a/test/expected/issue_542_partial_unique.out b/test/expected/issue_542_partial_unique.out new file mode 100644 index 00000000..60825026 --- /dev/null +++ b/test/expected/issue_542_partial_unique.out @@ -0,0 +1,116 @@ +begin; + create table public.works( + work_id int primary key + ); + create table public.readthroughs ( + readthrough_id int primary key, + work_id int not null references public.works(work_id), + status text not null + ); + select jsonb_pretty( + graphql.resolve($$ + { + __type(name: "Works") { + kind + fields { + name + type { + kind + name + } + } + } + } + $$) + ); + jsonb_pretty +------------------------------------------------------- + { + + "data": { + + "__type": { + + "kind": "OBJECT", + + "fields": [ + + { + + "name": "nodeId", + + "type": { + + "kind": "NON_NULL", + + "name": null + + } + + }, + + { + + "name": "workId", + + "type": { + + "kind": "NON_NULL", + + "name": null + + } + + }, + + { + + "name": "readthroughsCollection",+ + "type": { + + "kind": "NON_NULL", + + "name": null + + } + + } + + ] + + } + + } + + } +(1 row) + + /* Creating partial unique referencing status should NOT change the relationship with + the readthroughs to a non-null unique because its partial and other statuses may + have multiple associated readthroughs */ + create unique index idx_unique_in_progress_readthrough + on public.readthroughs (work_id) + where status in ('in_progress'); + select jsonb_pretty( + graphql.resolve($$ + { + __type(name: "Works") { + kind + fields { + name + type { + kind + name + } + } + } + } + $$) + ); + jsonb_pretty +--------------------------------------------- + { + + "data": { + + "__type": { + + "kind": "OBJECT", + + "fields": [ + + { + + "name": "nodeId", + + "type": { + + "kind": "NON_NULL",+ + "name": null + + } + + }, + + { + + "name": "workId", + + "type": { + + "kind": "NON_NULL",+ + "name": null + + } + + }, + + { + + "name": "work", + + "type": { + + "kind": "NON_NULL",+ + "name": null + + } + + } + + ] + + } + + } + + } +(1 row) + +rollback; diff --git a/test/sql/issue_542_partial_unique.sql b/test/sql/issue_542_partial_unique.sql new file mode 100644 index 00000000..a72d42d0 --- /dev/null +++ b/test/sql/issue_542_partial_unique.sql @@ -0,0 +1,54 @@ +begin; + + create table public.works( + work_id int primary key + ); + + create table public.readthroughs ( + readthrough_id int primary key, + work_id int not null references public.works(work_id), + status text not null + ); + + select jsonb_pretty( + graphql.resolve($$ + { + __type(name: "Works") { + kind + fields { + name + type { + kind + name + } + } + } + } + $$) + ); + + /* Creating partial unique referencing status should NOT change the relationship with + the readthroughs to a non-null unique because its partial and other statuses may + have multiple associated readthroughs */ + create unique index idx_unique_in_progress_readthrough + on public.readthroughs (work_id) + where status in ('in_progress'); + + select jsonb_pretty( + graphql.resolve($$ + { + __type(name: "Works") { + kind + fields { + name + type { + kind + name + } + } + } + } + $$) + ); + +rollback; From 452a5d1b64b52e18658caa798957db51efb18e12 Mon Sep 17 00:00:00 2001 From: Oliver Rice Date: Wed, 10 Jul 2024 11:06:17 -0500 Subject: [PATCH 2/2] exclude partial indexes from unique index check --- sql/load_sql_context.sql | 2 +- test/expected/issue_542_partial_unique.out | 62 +++++++++++----------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/sql/load_sql_context.sql b/sql/load_sql_context.sql index cd449ee8..15899200 100644 --- a/sql/load_sql_context.sql +++ b/sql/load_sql_context.sql @@ -280,7 +280,7 @@ select ), array[]::text[] ), - 'is_unique', pi.indisunique, + 'is_unique', pi.indisunique and pi.indpred is null, 'is_primary_key', pi.indisprimary ) ) diff --git a/test/expected/issue_542_partial_unique.out b/test/expected/issue_542_partial_unique.out index 60825026..9c6f128b 100644 --- a/test/expected/issue_542_partial_unique.out +++ b/test/expected/issue_542_partial_unique.out @@ -79,37 +79,37 @@ begin; } $$) ); - jsonb_pretty ---------------------------------------------- - { + - "data": { + - "__type": { + - "kind": "OBJECT", + - "fields": [ + - { + - "name": "nodeId", + - "type": { + - "kind": "NON_NULL",+ - "name": null + - } + - }, + - { + - "name": "workId", + - "type": { + - "kind": "NON_NULL",+ - "name": null + - } + - }, + - { + - "name": "work", + - "type": { + - "kind": "NON_NULL",+ - "name": null + - } + - } + - ] + - } + - } + + jsonb_pretty +------------------------------------------------------- + { + + "data": { + + "__type": { + + "kind": "OBJECT", + + "fields": [ + + { + + "name": "nodeId", + + "type": { + + "kind": "NON_NULL", + + "name": null + + } + + }, + + { + + "name": "workId", + + "type": { + + "kind": "NON_NULL", + + "name": null + + } + + }, + + { + + "name": "readthroughsCollection",+ + "type": { + + "kind": "NON_NULL", + + "name": null + + } + + } + + ] + + } + + } + } (1 row)