Skip to content

A Postgres table constraint written UNIQUE(a, b) is no longer read as a column - #28

Merged
avison9 merged 1 commit into
mainfrom
fix/postgres-glued-constraints
Nov 14, 2026
Merged

avison9 merged 1 commit into
mainfrom
fix/postgres-glued-constraints

Conversation

@avison9

@avison9 avison9 commented Nov 14, 2026

Copy link
Copy Markdown
Owner

What changes

The Postgres reader compares keywords up to a (. Postgres allows UNIQUE(name, deleteat) and PRIMARY KEY(id, channelid) with no space, and Mattermost's migrations use that form throughout. ddl.Words keeps a parenthesised group glued to the word before it, so:

  • UNIQUE(channelid, remoteid) was read as a column with that name, producing an info line for a column that does not exist;
  • PRIMARY KEY(id, channelid) became PRIMARY + KEY(id, channelid), so the primary key was lost. The replica-identity rule needs it;
  • a column-level CHECK(...) ended up in the column's type;
  • ALTER TABLE ... ADD UNIQUE(...) and ADD CHECK(...) added a column.

The helper keyword stays in the Postgres reader. ddl.HasPrefixFold is shared with the ClickHouse reader, which compares words like Kafka(...) whole.

Corpus and tests first

  • corpus/postgres-glued-constraints: Mattermost v10.11.0's 000006 (emoji) and 000037 (sharedchannelremotes) with the real names. On main it printed two info lines for public.emoji.UNIQUE(name, deleteat) and public.sharedchannelremotes.UNIQUE(channelid, remoteid). Now it prints ok.
  • TestAConstraintGluedToItsParenthesisIsNotAColumn covers the cases the output doesn't show: the primary key [id channelid], a column-level CHECK(, ADD UNIQUE(, ADD CHECK(, and a real ADD COLUMN that still works. It failed on main.

Verified

  • gofmt -l . is clean; go vet, go build and go test ./... pass.
  • All 280 Postgres migration files of Mattermost v10.11.0, with a probe connector that captures only id so that every other column is listed:
    • main: 572 info, 9 of them made-up (channels, emoji, PropertyGroups, roles, schemes, sharedchannelremotes, teams, usergroups x2);
    • this branch: 563 info, none made-up.
    • The diff of the two column lists is exactly those 9 lines, so no real column was lost.
  • RefuseRadar at origin/main: 0 error(s), 0 warning(s), 162 info, the same with both builds.

Found, not fixed here

Postgres folds unquoted identifiers to lower case, but the reader keeps CREATE TABLE IF NOT EXISTS PropertyGroups as written (Mattermost). That is its own change, with its own corpus entry.

… a column

Postgres needs no space between a constraint keyword and its
parenthesis, and Mattermost's migrations use that form throughout:
UNIQUE(name, deleteat), PRIMARY KEY(id, channelid). The word splitter
keeps a parenthesised group glued to the word before it, so the reader
saw a column named "UNIQUE(name, deleteat)" (an info line for a column
that does not exist) and a word "KEY(id, channelid)" that hid the primary
key. The same held for CHECK( on a column and for ALTER TABLE ... ADD
UNIQUE( / ADD CHECK(, which added a column.

Keywords in the Postgres reader are now compared up to a parenthesis.
The helper stays in this reader: the shared ddl.HasPrefixFold is also
used by the ClickHouse reader, where a word like Kafka(...) is compared
whole.

Measured on all 280 of Mattermost v10.11.0's Postgres migration files:
nine made-up columns before (channels, emoji, PropertyGroups, roles,
schemes, sharedchannelremotes, teams, usergroups twice), none after,
and no real column lost. RefuseRadar unchanged at 162 info.

Not done here: Postgres folds unquoted identifiers to lower case, and
the reader keeps CREATE TABLE PropertyGroups as written. Its own change.
@avison9 avison9 self-assigned this Nov 14, 2026
@avison9
avison9 merged commit 7ada842 into main Nov 14, 2026
2 checks passed
@avison9
avison9 deleted the fix/postgres-glued-constraints branch November 14, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant