-
Notifications
You must be signed in to change notification settings - Fork 42
Preserve UNIQUE NULLS NOT DISTINCT
#413
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
tianzhou
merged 4 commits into
pgplex:main
from
ivgiuliani:fix/unique-nulls-not-distinct-dropped
Apr 29, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7c32f96
test: pin UNIQUE NULLS NOT DISTINCT modifier dropped from constraints
ivgiuliani 74eb1b8
chore: regenerate sqlc bindings for UNIQUE NULLS NOT DISTINCT
ivgiuliani dc88527
fix: preserve UNIQUE NULLS NOT DISTINCT through round-trip
ivgiuliani 2cb4f78
test: add dump fixture for UNIQUE NULLS NOT DISTINCT round-trip
ivgiuliani 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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
testdata/diff/create_table/add_unique_constraint_nulls_not_distinct/diff.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,2 @@ | ||
| ALTER TABLE pgschema_repro_nulls | ||
| ADD CONSTRAINT pgschema_repro_nulls_uniq UNIQUE NULLS NOT DISTINCT (a, b); |
11 changes: 11 additions & 0 deletions
11
testdata/diff/create_table/add_unique_constraint_nulls_not_distinct/new.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,11 @@ | ||
| -- Regression for: UNIQUE NULLS NOT DISTINCT modifier dropped from constraints | ||
| -- Add a table-level UNIQUE constraint with the NULLS NOT DISTINCT modifier | ||
| -- (PostgreSQL 15+). Without the fix, the inspector loses the modifier and the | ||
| -- generated migration emits a plain UNIQUE (a, b) — silently changing the | ||
| -- semantics of the constraint and breaking INSERT ... ON CONFLICT flows that | ||
| -- rely on NULLs colliding. | ||
| CREATE TABLE public.pgschema_repro_nulls ( | ||
| a integer, | ||
| b integer, | ||
| CONSTRAINT pgschema_repro_nulls_uniq UNIQUE NULLS NOT DISTINCT (a, b) | ||
| ); | ||
6 changes: 6 additions & 0 deletions
6
testdata/diff/create_table/add_unique_constraint_nulls_not_distinct/old.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,6 @@ | ||
| -- Regression for: UNIQUE NULLS NOT DISTINCT modifier dropped from constraints | ||
| -- The starting state has no UNIQUE constraint at all; we add one in new.sql. | ||
| CREATE TABLE public.pgschema_repro_nulls ( | ||
| a integer, | ||
| b integer | ||
| ); |
20 changes: 20 additions & 0 deletions
20
testdata/diff/create_table/add_unique_constraint_nulls_not_distinct/plan.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,20 @@ | ||
| { | ||
| "version": "1.0.0", | ||
| "pgschema_version": "1.9.0", | ||
| "created_at": "1970-01-01T00:00:00Z", | ||
| "source_fingerprint": { | ||
| "hash": "70a4465367d5d40d0149eadc73c423f9eb954838b6602f00ca3496b264baf2e9" | ||
| }, | ||
| "groups": [ | ||
| { | ||
| "steps": [ | ||
| { | ||
| "sql": "ALTER TABLE pgschema_repro_nulls\nADD CONSTRAINT pgschema_repro_nulls_uniq UNIQUE NULLS NOT DISTINCT (a, b);", | ||
| "type": "table.constraint", | ||
| "operation": "create", | ||
| "path": "public.pgschema_repro_nulls.pgschema_repro_nulls_uniq" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
2 changes: 2 additions & 0 deletions
2
testdata/diff/create_table/add_unique_constraint_nulls_not_distinct/plan.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,2 @@ | ||
| ALTER TABLE pgschema_repro_nulls | ||
| ADD CONSTRAINT pgschema_repro_nulls_uniq UNIQUE NULLS NOT DISTINCT (a, b); |
14 changes: 14 additions & 0 deletions
14
testdata/diff/create_table/add_unique_constraint_nulls_not_distinct/plan.txt
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,14 @@ | ||
| Plan: 1 to modify. | ||
|
|
||
| Summary by type: | ||
| tables: 1 to modify | ||
|
|
||
| Tables: | ||
| ~ pgschema_repro_nulls | ||
| + pgschema_repro_nulls_uniq (constraint) | ||
|
|
||
| DDL to be executed: | ||
| -------------------------------------------------- | ||
|
|
||
| ALTER TABLE pgschema_repro_nulls | ||
| ADD CONSTRAINT pgschema_repro_nulls_uniq UNIQUE NULLS NOT DISTINCT (a, b); |
9 changes: 9 additions & 0 deletions
9
testdata/dump/issue_412_unique_nulls_not_distinct/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_412_unique_nulls_not_distinct", | ||
| "description": "pgschema dump silently drops the NULLS NOT DISTINCT modifier from UNIQUE table constraints", | ||
| "source": "https://github.com/pgplex/pgschema/issues/412", | ||
| "notes": [ | ||
| "NULLS NOT DISTINCT was introduced in PostgreSQL 15; the underlying pg_index.indnullsnotdistinct column does not exist on PG14.", | ||
| "The inspector exposes the modifier on UNIQUE constraints by joining pg_index via conindid and reading indnullsnotdistinct defensively (to_jsonb), which collapses to false on PG14." | ||
| ] | ||
| } |
32 changes: 32 additions & 0 deletions
32
testdata/dump/issue_412_unique_nulls_not_distinct/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,32 @@ | ||
| -- | ||
| -- PostgreSQL database dump | ||
| -- | ||
|
|
||
| SET statement_timeout = 0; | ||
| SET lock_timeout = 0; | ||
| -- SET transaction_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: pgschema_repro_nulls; Type: TABLE; Schema: public; Owner: - | ||
| -- | ||
|
|
||
| CREATE TABLE public.pgschema_repro_nulls ( | ||
| a integer, | ||
| b integer | ||
| ); | ||
|
|
||
| -- | ||
| -- Name: pgschema_repro_nulls pgschema_repro_nulls_uniq; Type: CONSTRAINT; Schema: public; Owner: - | ||
| -- | ||
|
|
||
| ALTER TABLE ONLY public.pgschema_repro_nulls | ||
| ADD CONSTRAINT pgschema_repro_nulls_uniq UNIQUE NULLS NOT DISTINCT (a, b); | ||
|
|
||
| -- | ||
| -- PostgreSQL database dump complete | ||
| -- |
18 changes: 18 additions & 0 deletions
18
testdata/dump/issue_412_unique_nulls_not_distinct/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,18 @@ | ||
| -- | ||
| -- pgschema database dump | ||
| -- | ||
|
|
||
| -- Dumped from database version PostgreSQL 18.0 | ||
| -- Dumped by pgschema version 1.9.0 | ||
|
|
||
|
|
||
| -- | ||
| -- Name: pgschema_repro_nulls; Type: TABLE; Schema: -; Owner: - | ||
| -- | ||
|
|
||
| CREATE TABLE IF NOT EXISTS pgschema_repro_nulls ( | ||
| a integer, | ||
| b integer, | ||
| CONSTRAINT pgschema_repro_nulls_uniq UNIQUE NULLS NOT DISTINCT (a, b) | ||
| ); | ||
|
|
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,14 @@ | ||
| -- | ||
| -- Test case for GitHub issue #412: UNIQUE NULLS NOT DISTINCT dropped from dump | ||
| -- | ||
| -- The NULLS NOT DISTINCT modifier (PostgreSQL 15+) makes NULL-bearing tuples | ||
| -- collide for uniqueness purposes, which is the opposite of the SQL default. | ||
| -- pgschema dump used to silently drop the modifier, emitting a plain | ||
| -- UNIQUE (...) constraint and quietly changing semantics. | ||
| -- | ||
|
|
||
| CREATE TABLE pgschema_repro_nulls ( | ||
| a integer, | ||
| b integer, | ||
| CONSTRAINT pgschema_repro_nulls_uniq UNIQUE NULLS NOT DISTINCT (a, b) | ||
| ); |
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
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 PR description says
pgschema dumpwas the primary regression surface, yet no dump test (testdata/dump/) is added to verify that thedumpcommand now emitsNULLS NOT DISTINCTcorrectly when inspecting a live database. The diff test covers the planning path, but a dump test (raw.sql+pgschema.sqlpair) would directly confirm the inspector → formatter round-trip and prevent regressions on the original reported bug.