Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/dump/dump_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ func TestDumpCommand_Issue345ArrayCast(t *testing.T) {
runExactMatchTest(t, "issue_345_array_cast")
}

func TestDumpCommand_Issue396CheckConstraintIsNotNull(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_396_check_constraint_is_not_null")
}

func TestDumpCommand_Issue191FunctionProcedureOverload(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
Expand Down
15 changes: 12 additions & 3 deletions ir/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,18 @@ func (i *Inspector) buildConstraints(ctx context.Context, schema *IR, targetSche
// Handle check constraints
if cType == ConstraintTypeCheck {
if checkClause := i.safeInterfaceToString(constraint.CheckClause); checkClause != "" && checkClause != "<nil>" {
// Skip system-generated NOT NULL constraints as they're redundant with column definitions
if strings.Contains(checkClause, "IS NOT NULL") {
continue
// Skip simple NOT NULL check constraints (e.g., "CHECK ((value IS NOT NULL))")
// as they're redundant with column definitions. Only skip if the entire
// expression is just "identifier IS NOT NULL".
inner := strings.TrimPrefix(strings.TrimSpace(checkClause), "CHECK ")
inner = strings.TrimSpace(inner)
for len(inner) > 2 && inner[0] == '(' && inner[len(inner)-1] == ')' && isBalancedParentheses(inner[1:len(inner)-1]) {
inner = strings.TrimSpace(inner[1 : len(inner)-1])
}
if prefix, ok := strings.CutSuffix(inner, " IS NOT NULL"); ok {
Comment on lines +524 to +528
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NOT NULL CHECK skipping logic is still too permissive: removing all parentheses and then only checking for whitespace means expressions like CHECK (lower(col) IS NOT NULL) or CHECK (foo(bar) IS NOT NULL) become lowercol IS NOT NULL / foobar IS NOT NULL and will be incorrectly treated as a “simple identifier IS NOT NULL”, causing legitimate CHECK constraints to be omitted again. Consider stripping only the outer CHECK wrapper/parentheses (balanced), then matching the remaining expression against a strict identifier regex (quoted or unquoted) + IS NOT NULL, rather than deleting all parentheses in the expression.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This concern applied to the first commit but is already fixed in the current code. The pushed version uses isBalancedParentheses to peel only matched outer parentheses — it does not remove all parens. After peeling, CHECK ((lower(col) IS NOT NULL)) becomes lower(col) IS NOT NULL, the prefix lower(col) contains a space, and the constraint is correctly preserved.

if !strings.Contains(strings.TrimSpace(prefix), " ") {
continue
}
}

// Use CheckClause as-is from PostgreSQL's pg_get_constraintdef(c.oid, true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "issue_396_check_constraint_is_not_null",
"description": "Table-level CHECK constraints containing IS NOT NULL in complex expressions are silently omitted from schema dump",
"source": "https://github.com/pgplex/pgschema/issues/396",
"notes": [
"The inspector incorrectly skips CHECK constraints that contain 'IS NOT NULL' anywhere in the expression",
"Only simple NOT NULL check constraints should be skipped, not complex expressions using IS NOT NULL"
]
}
40 changes: 40 additions & 0 deletions testdata/dump/issue_396_check_constraint_is_not_null/pgdump.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: test_table; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.test_table (
id integer NOT NULL,
status text NOT NULL,
reason text,
actor_id uuid
);

--
-- Name: test_table test_table_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.test_table
ADD CONSTRAINT test_table_pkey PRIMARY KEY (id);

--
-- Name: test_table test_table_status_check; Type: CHECK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE public.test_table
ADD CONSTRAINT test_table_status_check CHECK (((status = 'active'::text) OR ((status = 'cancelled'::text) AND (reason IS NOT NULL)) OR ((status = 'revoked'::text) AND (actor_id IS NOT NULL))));

--
-- PostgreSQL database dump complete
--
21 changes: 21 additions & 0 deletions testdata/dump/issue_396_check_constraint_is_not_null/pgschema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--
-- pgschema database dump
--

-- Dumped from database version PostgreSQL 18.0
-- Dumped by pgschema version 1.9.0


--
-- Name: test_table; Type: TABLE; Schema: -; Owner: -
--

CREATE TABLE IF NOT EXISTS test_table (
id integer,
status text NOT NULL,
reason text,
actor_id uuid,
CONSTRAINT test_table_pkey PRIMARY KEY (id),
CONSTRAINT test_table_status_check CHECK (status = 'active'::text OR status = 'cancelled'::text AND reason IS NOT NULL OR status = 'revoked'::text AND actor_id IS NOT NULL)
);

19 changes: 19 additions & 0 deletions testdata/dump/issue_396_check_constraint_is_not_null/raw.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--
-- Test case for GitHub issue #396: Table-level CHECK constraints omitted from schema dump
--
-- CHECK constraints containing IS NOT NULL in complex expressions
-- are silently dropped because the inspector filters out any constraint
-- with "IS NOT NULL" in its expression, not just simple NOT NULL constraints.
--

CREATE TABLE test_table (
id int PRIMARY KEY,
status text NOT NULL,
reason text,
actor_id uuid,
CONSTRAINT test_table_status_check CHECK (
(status = 'active')
OR (status = 'cancelled' AND reason IS NOT NULL)
OR (status = 'revoked' AND actor_id IS NOT NULL)
)
);