From 3da13a6257bc08b1d402c83feb2a35450f988365 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Thu, 24 Aug 2023 16:51:43 +0200 Subject: [PATCH] Add test for inherited CHECK constraint drop This code is insufficiently covered by tests, so add a few small test cases to immortalize its behavior before it gets rewritten completely by the project to catalog NOT NULL constraints. --- src/test/regress/expected/inherit.out | 109 ++++++++++++++++++++++++++ src/test/regress/sql/inherit.sql | 39 +++++++++ 2 files changed, 148 insertions(+) diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out index a7fbeed9eb..a8283b7710 100644 --- a/src/test/regress/expected/inherit.out +++ b/src/test/regress/expected/inherit.out @@ -1283,6 +1283,115 @@ order by 1, 2; drop table p1 cascade; NOTICE: drop cascades to table p1_c1 +-- +-- Test DROP behavior of multiply-defined CHECK constraints +-- +create table p1(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p1_c1 (f1 int constraint f1_pos CHECK (f1 > 0)) inherits (p1); +NOTICE: merging column "f1" with inherited definition +NOTICE: merging constraint "f1_pos" with inherited definition +alter table p1_c1 drop constraint f1_pos; +ERROR: cannot drop inherited constraint "f1_pos" of relation "p1_c1" +alter table p1 drop constraint f1_pos; +\d p1_c1 + Table "public.p1_c1" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + f1 | integer | | | +Check constraints: + "f1_pos" CHECK (f1 > 0) +Inherits: p1 + +drop table p1 cascade; +NOTICE: drop cascades to table p1_c1 +create table p1(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p2(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p1p2_c1 (f1 int) inherits (p1, p2); +NOTICE: merging multiple inherited definitions of column "f1" +NOTICE: merging column "f1" with inherited definition +create table p1p2_c2 (f1 int constraint f1_pos CHECK (f1 > 0)) inherits (p1, p2); +NOTICE: merging multiple inherited definitions of column "f1" +NOTICE: merging column "f1" with inherited definition +NOTICE: merging constraint "f1_pos" with inherited definition +alter table p2 drop constraint f1_pos; +alter table p1 drop constraint f1_pos; +\d p1p2_c* + Table "public.p1p2_c1" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + f1 | integer | | | +Inherits: p1, + p2 + + Table "public.p1p2_c2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + f1 | integer | | | +Check constraints: + "f1_pos" CHECK (f1 > 0) +Inherits: p1, + p2 + +drop table p1, p2 cascade; +NOTICE: drop cascades to 2 other objects +DETAIL: drop cascades to table p1p2_c1 +drop cascades to table p1p2_c2 +create table p1(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p1_c1() inherits (p1); +create table p1_c2() inherits (p1); +create table p1_c1c2() inherits (p1_c1, p1_c2); +NOTICE: merging multiple inherited definitions of column "f1" +\d p1_c1c2 + Table "public.p1_c1c2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + f1 | integer | | | +Check constraints: + "f1_pos" CHECK (f1 > 0) +Inherits: p1_c1, + p1_c2 + +alter table p1 drop constraint f1_pos; +\d p1_c1c2 + Table "public.p1_c1c2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + f1 | integer | | | +Inherits: p1_c1, + p1_c2 + +drop table p1 cascade; +NOTICE: drop cascades to 3 other objects +DETAIL: drop cascades to table p1_c1 +drop cascades to table p1_c2 +drop cascades to table p1_c1c2 +create table p1(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p1_c1() inherits (p1); +create table p1_c2(constraint f1_pos CHECK (f1 > 0)) inherits (p1); +NOTICE: merging constraint "f1_pos" with inherited definition +create table p1_c1c2() inherits (p1_c1, p1_c2, p1); +NOTICE: merging multiple inherited definitions of column "f1" +NOTICE: merging multiple inherited definitions of column "f1" +alter table p1_c2 drop constraint f1_pos; +ERROR: cannot drop inherited constraint "f1_pos" of relation "p1_c2" +alter table p1 drop constraint f1_pos; +alter table p1_c1c2 drop constraint f1_pos; +ERROR: cannot drop inherited constraint "f1_pos" of relation "p1_c1c2" +alter table p1_c2 drop constraint f1_pos; +\d p1_c1c2 + Table "public.p1_c1c2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + f1 | integer | | | +Inherits: p1_c1, + p1_c2, + p1 + +drop table p1 cascade; +NOTICE: drop cascades to 3 other objects +DETAIL: drop cascades to table p1_c1 +drop cascades to table p1_c2 +drop cascades to table p1_c1c2 -- Test that a valid child can have not-valid parent, but not vice versa create table invalid_check_con(f1 int); create table invalid_check_con_child() inherits(invalid_check_con); diff --git a/src/test/regress/sql/inherit.sql b/src/test/regress/sql/inherit.sql index 215d58e80d..0ce83f16ba 100644 --- a/src/test/regress/sql/inherit.sql +++ b/src/test/regress/sql/inherit.sql @@ -443,6 +443,45 @@ order by 1, 2; drop table p1 cascade; +-- +-- Test DROP behavior of multiply-defined CHECK constraints +-- +create table p1(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p1_c1 (f1 int constraint f1_pos CHECK (f1 > 0)) inherits (p1); +alter table p1_c1 drop constraint f1_pos; +alter table p1 drop constraint f1_pos; +\d p1_c1 +drop table p1 cascade; + +create table p1(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p2(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p1p2_c1 (f1 int) inherits (p1, p2); +create table p1p2_c2 (f1 int constraint f1_pos CHECK (f1 > 0)) inherits (p1, p2); +alter table p2 drop constraint f1_pos; +alter table p1 drop constraint f1_pos; +\d p1p2_c* +drop table p1, p2 cascade; + +create table p1(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p1_c1() inherits (p1); +create table p1_c2() inherits (p1); +create table p1_c1c2() inherits (p1_c1, p1_c2); +\d p1_c1c2 +alter table p1 drop constraint f1_pos; +\d p1_c1c2 +drop table p1 cascade; + +create table p1(f1 int constraint f1_pos CHECK (f1 > 0)); +create table p1_c1() inherits (p1); +create table p1_c2(constraint f1_pos CHECK (f1 > 0)) inherits (p1); +create table p1_c1c2() inherits (p1_c1, p1_c2, p1); +alter table p1_c2 drop constraint f1_pos; +alter table p1 drop constraint f1_pos; +alter table p1_c1c2 drop constraint f1_pos; +alter table p1_c2 drop constraint f1_pos; +\d p1_c1c2 +drop table p1 cascade; + -- Test that a valid child can have not-valid parent, but not vice versa create table invalid_check_con(f1 int); create table invalid_check_con_child() inherits(invalid_check_con);