-
Notifications
You must be signed in to change notification settings - Fork 41
fix: table-level CHECK constraints with IS NOT NULL silently omitted from dump (#396) #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+108
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
testdata/dump/issue_396_check_constraint_is_not_null/manifest.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
40
testdata/dump/issue_396_check_constraint_is_not_null/pgdump.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
21
testdata/dump/issue_396_check_constraint_is_not_null/pgschema.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
19
testdata/dump/issue_396_check_constraint_is_not_null/raw.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| ) | ||
| ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)orCHECK (foo(bar) IS NOT NULL)becomelowercol IS NOT NULL/foobar IS NOT NULLand 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.There was a problem hiding this comment.
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
isBalancedParenthesesto peel only matched outer parentheses — it does not remove all parens. After peeling,CHECK ((lower(col) IS NOT NULL))becomeslower(col) IS NOT NULL, the prefixlower(col)contains a space, and the constraint is correctly preserved.