Add test for inherited CHECK constraint drop
authorAlvaro Herrera <[email protected]>
Thu, 24 Aug 2023 14:51:43 +0000 (16:51 +0200)
committerAlvaro Herrera <[email protected]>
Thu, 24 Aug 2023 14:51:43 +0000 (16:51 +0200)
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
src/test/regress/sql/inherit.sql

index a7fbeed9eb9d989aac07b5ba385b61b0d8866287..a8283b77103498ea9cbbb44dfbe089f0626293d4 100644 (file)
@@ -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);
index 215d58e80d39834a94f193b4651a33172575d4e2..0ce83f16ba73fc78120886920ff7e7aba9905372 100644 (file)
@@ -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);