Skip to content

chore(ent): enable sql/upsert feature flag - #1200

Merged
kalbasit merged 1 commit into
mainfrom
chore-ent-enable-sql-upsert
May 22, 2026
Merged

kalbasit merged 1 commit into
mainfrom
chore-ent-enable-sql-upsert

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

§11.3 requires expressing PutNarInfo's UPSERT semantics
(INSERT ... ON CONFLICT (hash) DO UPDATE ... WHERE url IS NULL)
in Ent's fluent API. The OnConflict / UpsertOne / UpsertBulk
builders are only emitted when the sql/upsert feature flag
is on, so this commit turns it on and regenerates.

What changed:

  • ent/generate.go: add --feature sql/upsert to the codegen
    invocation. Subsequent go generate ./ent invocations now
    always produce OnConflict-capable builders; CI's
    git diff --exit-code ./ent/ check stays meaningful because
    the regenerated tree is committed here.
  • Every <entity>_create.go file gains an OnConflict method,
    a Conflict type that exposes per-field
    Update/UpdateNewValues/Ignore handlers, and the corresponding
    Upsert / UpsertOne / UpsertBulk types. Pure additions; no
    existing API changes.

No call-site code uses OnConflict yet — §11.3's caller
migrations land in the next commit. Adding the feature flag in
its own commit keeps the §11.3 conversion diff focused on the
behavioural change, not the ~4k lines of mechanical codegen.

go build ./... succeeds, golangci-lint run ./... returns
zero issues, no other commit in the stack is affected.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@dosubot dosubot Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. go Pull requests that update go code labels May 21, 2026
@kalbasit

kalbasit commented May 21, 2026 •

Copy link
Copy Markdown
Owner Author

This change is part of the following stack:

Change managed by git-spice.

This was referenced May 21, 2026
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from f9615e7 to cbc78ef Compare May 22, 2026 05:59
@kalbasit
kalbasit force-pushed the feat-cache-getnarinfo-ent-finish branch from 75e0ad4 to b8748eb Compare May 22, 2026 06:47
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from cbc78ef to e9c62db Compare May 22, 2026 06:47
@kalbasit
kalbasit force-pushed the feat-cache-getnarinfo-ent-finish branch from b8748eb to 15cfae6 Compare May 22, 2026 07:06
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from e9c62db to b49741f Compare May 22, 2026 07:06
@kalbasit
kalbasit force-pushed the feat-cache-getnarinfo-ent-finish branch from 15cfae6 to fca78b0 Compare May 22, 2026 18:07
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from b49741f to b5e2e6c Compare May 22, 2026 18:07
@coderabbitai

coderabbitai Bot commented May 22, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Rate limit exceeded

@kalbasit has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 6 minutes and 13 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4fd4bacf-620a-421e-96c6-1a4ec96fece8

📥 Commits

Reviewing files that changed from the base of the PR and between 96e4a3d and 088f9bb.

📒 Files selected for processing (10)
  • ent/chunk_create.go
  • ent/configentry_create.go
  • ent/generate.go
  • ent/narfile_create.go
  • ent/narfilechunk_create.go
  • ent/narinfo_create.go
  • ent/narinfonarfile_create.go
  • ent/narinforeference_create.go
  • ent/narinfosignature_create.go
  • ent/pinnedclosure_create.go
📝 Walkthrough

Walkthrough

Adds SQL upsert (ON CONFLICT) support across generated Ent create builders by enabling code generation with the sql/upsert feature, adding conflict option storage and OnConflict entry points, and providing Upsert helper types for single-row and bulk insert conflict resolution.

Changes

SQL upsert support for entity builders

Layer / File(s) Summary
Code generation setup with upsert feature
ent/generate.go
Declares ent as generated code and enables sql/upsert during generation via //go:generate directive.
Single-entity upsert support
ent/chunk_create.go, ent/configentry_create.go, ent/narfile_create.go, ent/narfilechunk_create.go, ent/narinfo_create.go, ent/narinfonarfile_create.go, ent/narinforeference_create.go, ent/narinfosignature_create.go, ent/pinnedclosure_create.go
Each Create builder adds the sql dialect import, stores conflict []sql.ConflictOption, wires it into the insert spec (_spec.OnConflict), and exposes OnConflict/OnConflictColumns that return UpsertOne builders. UpsertOne/Upsert helpers provide fluent conflict-resolution options (UpdateNewValues, Ignore, DoNothing, Update) and per-field update setters, plus execution helpers (Exec, ExecX, ID, IDX) that validate conflict options.
Bulk upsert support
ent/chunk_create.go, ent/configentry_create.go, ent/narfile_create.go, ent/narfilechunk_create.go, ent/narinfo_create.go, ent/narinfonarfile_create.go, ent/narinforeference_create.go, ent/narinfosignature_create.go, ent/pinnedclosure_create.go
Each CreateBulk builder stores bulk-level conflict []sql.ConflictOption, applies it to batch create specs (spec.OnConflict), and exposes OnConflict/OnConflictColumns returning UpsertBulk. UpsertBulk provides the same conflict-resolution and per-field update API, with Exec/ExecX validating bulk-level conflict configuration and rejecting per-item conflict settings.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

size:XL

Poem

🐇 I nibble through conflicts, gentle and spry,
New upsert paths stitched where inserts used to lie,
Single or bulk, the clauses now sing,
ON CONFLICT resolved — a joyous spring!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: enabling the sql/upsert feature flag in Ent code generation.
Description check ✅ Passed The description is well-related to the changeset, explaining the motivation (§11.3 requirements), what changed (feature flag enabled in generate.go, regenerated create files), and rationale (feature flag enables OnConflict builders).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@kalbasit
kalbasit force-pushed the feat-cache-getnarinfo-ent-finish branch from fca78b0 to 3ab6f68 Compare May 22, 2026 19:02
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from b5e2e6c to fc295e1 Compare May 22, 2026 19:02

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ent/pinnedclosure_create.go`:
- Around line 577-581: PinnedClosureUpsertBulk.ExecX currently calls
u.create.Exec(ctx) directly and therefore bypasses the validation logic in
PinnedClosureUpsertBulk.Exec (conflict option checks and per-builder OnConflict
misuse checks). Fix by delegating ExecX to the validated path: call u.Exec(ctx)
and panic on the returned error instead of calling u.create.Exec(ctx), ensuring
the existing OnConflict/conflict-options validation in
PinnedClosureUpsertBulk.Exec is executed.
- Around line 323-336: ExecX and ID are bypassing the upsert validation by
calling the underlying create builder directly; change ExecX to delegate to the
existing upsert-aware Exec(ctx) (panic on its error) and change ID to first
invoke u.Exec(ctx) to enforce the missing-options guard before calling the
underlying create save path (e.g., call u.create.Save(ctx) only after
u.Exec(ctx) succeeds) so the OnConflict option validation runs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 993a098b-62f5-4916-b490-63ce8ab0d4aa

📥 Commits

Reviewing files that changed from the base of the PR and between 3ab6f68 and fc295e1.

📒 Files selected for processing (10)
  • ent/chunk_create.go
  • ent/configentry_create.go
  • ent/generate.go
  • ent/narfile_create.go
  • ent/narfilechunk_create.go
  • ent/narinfo_create.go
  • ent/narinfonarfile_create.go
  • ent/narinforeference_create.go
  • ent/narinfosignature_create.go
  • ent/pinnedclosure_create.go

Comment on lines +323 to +336
func (u *PinnedClosureUpsertOne) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *PinnedClosureUpsertOne) ID(ctx context.Context) (id int, err error) {
node, err := u.create.Save(ctx)
if err != nil {
return id, err
}
return node.ID, nil
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

ExecX and ID bypass the upsert option validation path.

Line 324 and Line 331 call the underlying create builder directly, so the missing-options guard in Exec is skipped. This can silently run a non-upsert insert flow when OnConflict() is called without options.

Proposed fix
 func (u *PinnedClosureUpsertOne) ExecX(ctx context.Context) {
-	if err := u.create.Exec(ctx); err != nil {
+	if err := u.Exec(ctx); err != nil {
 		panic(err)
 	}
 }

 // Exec executes the UPSERT query and returns the inserted/updated ID.
 func (u *PinnedClosureUpsertOne) ID(ctx context.Context) (id int, err error) {
+	if len(u.create.conflict) == 0 {
+		return 0, errors.New("ent: missing options for PinnedClosureCreate.OnConflict")
+	}
 	node, err := u.create.Save(ctx)
 	if err != nil {
 		return id, err
 	}
 	return node.ID, nil
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ent/pinnedclosure_create.go` around lines 323 - 336, ExecX and ID are
bypassing the upsert validation by calling the underlying create builder
directly; change ExecX to delegate to the existing upsert-aware Exec(ctx) (panic
on its error) and change ID to first invoke u.Exec(ctx) to enforce the
missing-options guard before calling the underlying create save path (e.g., call
u.create.Save(ctx) only after u.Exec(ctx) succeeds) so the OnConflict option
validation runs.

Comment on lines +577 to +581
func (u *PinnedClosureUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

PinnedClosureUpsertBulk.ExecX skips bulk upsert validation.

Line 578 calls u.create.Exec(ctx) directly, bypassing the validation in PinnedClosureUpsertBulk.Exec (missing conflict options and per-builder OnConflict misuse checks).

Proposed fix
 func (u *PinnedClosureUpsertBulk) ExecX(ctx context.Context) {
-	if err := u.create.Exec(ctx); err != nil {
+	if err := u.Exec(ctx); err != nil {
 		panic(err)
 	}
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (u *PinnedClosureUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}
func (u *PinnedClosureUpsertBulk) ExecX(ctx context.Context) {
if err := u.Exec(ctx); err != nil {
panic(err)
}
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ent/pinnedclosure_create.go` around lines 577 - 581,
PinnedClosureUpsertBulk.ExecX currently calls u.create.Exec(ctx) directly and
therefore bypasses the validation logic in PinnedClosureUpsertBulk.Exec
(conflict option checks and per-builder OnConflict misuse checks). Fix by
delegating ExecX to the validated path: call u.Exec(ctx) and panic on the
returned error instead of calling u.create.Exec(ctx), ensuring the existing
OnConflict/conflict-options validation in PinnedClosureUpsertBulk.Exec is
executed.

@kalbasit
kalbasit force-pushed the feat-cache-getnarinfo-ent-finish branch from 3ab6f68 to 9949687 Compare May 22, 2026 20:44
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from fc295e1 to 96e4a3d Compare May 22, 2026 20:44

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ent/chunk_create.go`:
- Around line 471-474: The ExecX methods are bypassing the validation logic in
Exec by calling u.create.Exec(ctx) directly; update ChunkUpsertOne.ExecX (and
ChunkUpsertBulk.ExecX) to invoke the validated method (call u.Exec(ctx) and
panic on the returned error) so conflict-option checks and per-builder conflict
misuse checks run the same path as Exec, rather than calling u.create.Exec(ctx)
directly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ffc4dd08-5bfb-44de-9a33-d4d4d570aa44

📥 Commits

Reviewing files that changed from the base of the PR and between fc295e1 and 96e4a3d.

📒 Files selected for processing (10)
  • ent/chunk_create.go
  • ent/configentry_create.go
  • ent/generate.go
  • ent/narfile_create.go
  • ent/narfilechunk_create.go
  • ent/narinfo_create.go
  • ent/narinfonarfile_create.go
  • ent/narinforeference_create.go
  • ent/narinfosignature_create.go
  • ent/pinnedclosure_create.go
✅ Files skipped from review due to trivial changes (6)
  • ent/generate.go
  • ent/narinfo_create.go
  • ent/narfilechunk_create.go
  • ent/narfile_create.go
  • ent/pinnedclosure_create.go
  • ent/narinforeference_create.go

Comment thread ent/chunk_create.go
Comment on lines +471 to +474
func (u *ChunkUpsertOne) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

ExecX bypasses upsert validation in Exec

ChunkUpsertOne.ExecX and ChunkUpsertBulk.ExecX call u.create.Exec(ctx) directly, so they skip the guardrails in Exec (missing conflict options and per-builder conflict misuse). This makes ExecX behavior diverge from “ExecX is like Exec”.

Suggested fix
 func (u *ChunkUpsertOne) ExecX(ctx context.Context) {
-	if err := u.create.Exec(ctx); err != nil {
+	if err := u.Exec(ctx); err != nil {
 		panic(err)
 	}
 }
@@
 func (u *ChunkUpsertBulk) ExecX(ctx context.Context) {
-	if err := u.create.Exec(ctx); err != nil {
+	if err := u.Exec(ctx); err != nil {
 		panic(err)
 	}
 }

Also applies to: 767-770

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ent/chunk_create.go` around lines 471 - 474, The ExecX methods are bypassing
the validation logic in Exec by calling u.create.Exec(ctx) directly; update
ChunkUpsertOne.ExecX (and ChunkUpsertBulk.ExecX) to invoke the validated method
(call u.Exec(ctx) and panic on the returned error) so conflict-option checks and
per-builder conflict misuse checks run the same path as Exec, rather than
calling u.create.Exec(ctx) directly.

@kalbasit
kalbasit force-pushed the feat-cache-getnarinfo-ent-finish branch from 9949687 to 8f24530 Compare May 22, 2026 20:51
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from 96e4a3d to ab5cee7 Compare May 22, 2026 20:51
@kalbasit
kalbasit force-pushed the feat-cache-getnarinfo-ent-finish branch from 8f24530 to 8d9fd40 Compare May 22, 2026 21:04
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from ab5cee7 to 0604b80 Compare May 22, 2026 21:04
@kalbasit
kalbasit force-pushed the feat-cache-getnarinfo-ent-finish branch from 8d9fd40 to ac82cc7 Compare May 22, 2026 21:16
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from 0604b80 to a326646 Compare May 22, 2026 21:16
Base automatically changed from feat-cache-getnarinfo-ent-finish to main May 22, 2026 21:29
§11.3 requires expressing PutNarInfo's UPSERT semantics
(`INSERT ... ON CONFLICT (hash) DO UPDATE ... WHERE url IS NULL`)
in Ent's fluent API. The OnConflict / UpsertOne / UpsertBulk
builders are only emitted when the `sql/upsert` feature flag
is on, so this commit turns it on and regenerates.

What changed:

- `ent/generate.go`: add `--feature sql/upsert` to the codegen
  invocation. Subsequent `go generate ./ent` invocations now
  always produce OnConflict-capable builders; CI's
  `git diff --exit-code ./ent/` check stays meaningful because
  the regenerated tree is committed here.
- Every `<entity>_create.go` file gains an OnConflict method,
  a Conflict type that exposes per-field
  Update/UpdateNewValues/Ignore handlers, and the corresponding
  Upsert / UpsertOne / UpsertBulk types. Pure additions; no
  existing API changes.

No call-site code uses OnConflict yet — §11.3's caller
migrations land in the next commit. Adding the feature flag in
its own commit keeps the §11.3 conversion diff focused on the
behavioural change, not the ~4k lines of mechanical codegen.

`go build ./...` succeeds, `golangci-lint run ./...` returns
zero issues, no other commit in the stack is affected.
@kalbasit
kalbasit force-pushed the chore-ent-enable-sql-upsert branch from a326646 to 088f9bb Compare May 22, 2026 21:29
@kalbasit
kalbasit enabled auto-merge (squash) May 22, 2026 21:30
@codecov

codecov Bot commented May 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 5.68%. Comparing base (c7b53ee) to head (088f9bb).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #1200   +/-   ##
=====================================
  Coverage   5.68%   5.68%           
=====================================
  Files          6       6           
  Lines        387     387           
=====================================
  Hits          22      22           
  Misses       361     361           
  Partials       4       4           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kalbasit
kalbasit merged commit 964dd78 into main May 22, 2026
16 checks passed
@kalbasit
kalbasit deleted the chore-ent-enable-sql-upsert branch May 22, 2026 21:41
@coderabbitai coderabbitai Bot mentioned this pull request May 25, 2026
7 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update go code merge-stack size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant