diff --git a/internal/diff/constraint.go b/internal/diff/constraint.go index 1f3de2b7..2897e409 100644 --- a/internal/diff/constraint.go +++ b/internal/diff/constraint.go @@ -166,7 +166,12 @@ func constraintsEqual(old, new *ir.Constraint) bool { if old.CheckClause != new.CheckClause { return false } - if old.NoInherit != new.NoInherit { + // NoInherit (connoinherit) is only meaningful for CHECK constraints where + // it controls the NO INHERIT modifier. For PK/UNIQUE/FK/EXCLUDE constraints, + // PostgreSQL sets connoinherit inconsistently (e.g. true for a standalone + // constraint vs false for one created via PARTITION OF), so comparing it + // would cause false diffs on partition children (#495). + if old.Type == ir.ConstraintTypeCheck && old.NoInherit != new.NoInherit { return false } if old.ExclusionDefinition != new.ExclusionDefinition { diff --git a/testdata/diff/create_table/issue_495_partition_constraint_convergence/diff.sql b/testdata/diff/create_table/issue_495_partition_constraint_convergence/diff.sql new file mode 100644 index 00000000..e69de29b diff --git a/testdata/diff/create_table/issue_495_partition_constraint_convergence/new.sql b/testdata/diff/create_table/issue_495_partition_constraint_convergence/new.sql new file mode 100644 index 00000000..56f2a4f9 --- /dev/null +++ b/testdata/diff/create_table/issue_495_partition_constraint_convergence/new.sql @@ -0,0 +1,8 @@ +CREATE TABLE public.p ( + band_id integer NOT NULL, + geom_id integer NOT NULL, + CONSTRAINT p_band_id_geom_id_key UNIQUE (band_id, geom_id) +) PARTITION BY LIST (band_id); + +CREATE TABLE public.p_12 PARTITION OF public.p FOR VALUES IN (12); +CREATE TABLE public.p_13 PARTITION OF public.p FOR VALUES IN (13); diff --git a/testdata/diff/create_table/issue_495_partition_constraint_convergence/old.sql b/testdata/diff/create_table/issue_495_partition_constraint_convergence/old.sql new file mode 100644 index 00000000..66b880f4 --- /dev/null +++ b/testdata/diff/create_table/issue_495_partition_constraint_convergence/old.sql @@ -0,0 +1,22 @@ +-- This represents what the target database looks like after pgschema apply: +-- partition children created as standalone tables with explicit constraints, +-- then attached to the parent table. +CREATE TABLE public.p ( + band_id integer NOT NULL, + geom_id integer NOT NULL, + CONSTRAINT p_band_id_geom_id_key UNIQUE (band_id, geom_id) +) PARTITION BY LIST (band_id); + +CREATE TABLE public.p_12 ( + band_id integer NOT NULL, + geom_id integer NOT NULL, + CONSTRAINT p_12_band_id_geom_id_key UNIQUE (band_id, geom_id) +); +ALTER TABLE public.p ATTACH PARTITION public.p_12 FOR VALUES IN (12); + +CREATE TABLE public.p_13 ( + band_id integer NOT NULL, + geom_id integer NOT NULL, + CONSTRAINT p_13_band_id_geom_id_key UNIQUE (band_id, geom_id) +); +ALTER TABLE public.p ATTACH PARTITION public.p_13 FOR VALUES IN (13); diff --git a/testdata/diff/create_table/issue_495_partition_constraint_convergence/plan.json b/testdata/diff/create_table/issue_495_partition_constraint_convergence/plan.json new file mode 100644 index 00000000..e67dc6bd --- /dev/null +++ b/testdata/diff/create_table/issue_495_partition_constraint_convergence/plan.json @@ -0,0 +1,9 @@ +{ + "version": "1.0.0", + "pgschema_version": "1.11.1", + "created_at": "1970-01-01T00:00:00Z", + "source_fingerprint": { + "hash": "b52a7d3e192793cd8af86dbf6b990056fa88b14303a29a10bb55048423a811c0" + }, + "groups": null +} diff --git a/testdata/diff/create_table/issue_495_partition_constraint_convergence/plan.sql b/testdata/diff/create_table/issue_495_partition_constraint_convergence/plan.sql new file mode 100644 index 00000000..e69de29b diff --git a/testdata/diff/create_table/issue_495_partition_constraint_convergence/plan.txt b/testdata/diff/create_table/issue_495_partition_constraint_convergence/plan.txt new file mode 100644 index 00000000..241994af --- /dev/null +++ b/testdata/diff/create_table/issue_495_partition_constraint_convergence/plan.txt @@ -0,0 +1 @@ +No changes detected.