Skip to content

x/sqlbuilder/upserter: add PredicateClause to restrict updates - #48

Merged
jlevesy merged 1 commit into
masterfrom
am-predicated-upserter
Oct 22, 2026
Merged

jlevesy merged 1 commit into
masterfrom
am-predicated-upserter

Conversation

@AlexisMontagne

Copy link
Copy Markdown
Member

What does this PR do?

Allows callers to only update an existing row when a predicate holds, e.g. for optimistic concurrency checks. Rows that do not exist yet are still inserted.

This adds a Where to sqlbuilder.OnConflictClause, rendered as DO UPDATE SET ... WHERE, and exposes it on the upserter through Statement.PredicateClause. The Where is ignored for DO NOTHING.

What are the observable changes?

Feature: upserter.Statement accepts a PredicateClause. With a key constrained upsert, the generated statement becomes:

INSERT INTO foo(buz) VALUES ($1)
ON CONFLICT (buz) DO UPDATE SET bar = $2 WHERE biz = $3

Good PR checklist

  • Title makes sense
  • Is against the correct branch
  • Only addresses one issue
  • Properly assigned
  • Added/updated tests
  • Added/updated documentation
  • Properly labeled

Additional Notes

Rebased on master after Excluded landed; the only conflict was in insert_statement_test.go, where both sides added independent test cases and both were kept.

Allows callers to only update an existing row when a predicate holds,
e.g. for optimistic concurrency checks. Rows that do not exist yet are
still inserted. Backed by a new Where on OnConflictClause, rendered as
DO UPDATE SET ... WHERE.

@jlevesy jlevesy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@jlevesy
jlevesy merged commit d885f50 into master Oct 22, 2026
5 checks passed
@jlevesy
jlevesy deleted the am-predicated-upserter branch October 22, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants