Skip to content

fix: handle case not found when update#66

Merged
ren0503 merged 1 commit into
masterfrom
fix/ren/65-fix-found-when-update
Aug 16, 2025
Merged

fix: handle case not found when update#66
ren0503 merged 1 commit into
masterfrom
fix/ren/65-fix-found-when-update

Conversation

@ren0503

@ren0503 ren0503 commented Aug 16, 2025

Copy link
Copy Markdown
Contributor

No description provided.

@coderabbitai

coderabbitai Bot commented Aug 16, 2025

Copy link
Copy Markdown

Summary by CodeRabbit

  • Bug Fixes

    • Update operations now return a clear “not found” error when the target record doesn’t exist, preventing runtime failures and improving reliability.
  • Chores

    • Upgraded several dependencies to newer patch/minor versions for improved stability, compatibility, and performance. No changes to toolchain or Go version.
  • Tests

    • No changes required; public APIs remain unchanged, ensuring backward compatibility.

Walkthrough

Dependency versions updated in go.mod. In mutation.go’s UpdateOne, a nil check was added after record retrieval to return gorm.ErrRecordNotFound when no record exists; remaining update logic unchanged.

Changes

Cohort / File(s) Summary
Dependency upgrades
go.mod
Bump versions: tinhtinh/v2 v2.1.4→v2.3.0; gorm v1.30.0→v1.30.1; golang.org/x/sync v0.15.0→v0.16.0; golang.org/x/text v0.26.0→v0.27.0. No toolchain/go version changes.
ORM update nil-check
mutation.go
UpdateOne adds if record == nil { return nil, gorm.ErrRecordNotFound } after fetch; rest of update flow unchanged.

Sequence Diagram(s)

sequenceDiagram
  actor Client
  participant Repo as Repository
  participant DB as GORM DB

  Client->>Repo: UpdateOne(input)
  Repo->>DB: Find record by criteria
  DB-->>Repo: record or nil

  alt Record not found
    Repo-->>Client: error gorm.ErrRecordNotFound
  else Record found
    Repo->>Repo: MapOne(record)
    Repo->>DB: Model(record).Updates(input)
    DB-->>Repo: result / error
    Repo-->>Client: updated record / error
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A whisk of whiskers, code hops light,
I nudge go.mod to newer height.
A nil appears? I thump—“Not found!”
And skip the fields that don’t abound.
With tidy paws, I merge and run,
Version bumps done—now back to fun. 🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.2.2)

Error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/product/migration-guide for migration instructions
The command is terminated due to an error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/product/migration-guide for migration instructions

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/ren/65-fix-found-when-update

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ren0503 ren0503 linked an issue Aug 16, 2025 that may be closed by this pull request
@ren0503 ren0503 added this to the SQL ORM Release v2.3.2 milestone Aug 16, 2025

@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: 0

🔭 Outside diff range comments (1)
mutation.go (1)

93-105: Mirror not-found handling in Increment/Decrement to avoid silent no-ops.

If the record doesn’t exist, these methods may update 0 rows without error. Add the same not-found guard you introduced in UpdateOne.

 func (repo *Repository[M]) Increment(id any, field string, value int) error {
   record, err := repo.FindOne(map[string]interface{}{"id": id}, FindOneOptions{})
 
   if err != nil {
     return err
   }
+  if record == nil {
+    return gorm.ErrRecordNotFound
+  }
 
   result := repo.DB.Model(record).Update(field, gorm.Expr(field+" + ?", value))
   if result.Error != nil {
     return result.Error
   }
   return nil
 }
 
 func (repo *Repository[M]) Decrement(id any, field string, value int) error {
   record, err := repo.FindOne(map[string]interface{}{"id": id}, FindOneOptions{})
   if err != nil {
     return err
   }
+  if record == nil {
+    return gorm.ErrRecordNotFound
+  }
 
   result := repo.DB.Model(record).Update(field, gorm.Expr(field+" - ?", value))
   if result.Error != nil {
     return result.Error
   }
   return nil
 }

Also applies to: 107-118

🧹 Nitpick comments (2)
mutation.go (2)

31-36: Optional: Return updated values using RETURNING (Postgres) to keep record in-sync.

Currently, the returned record may not reflect DB-side defaults/triggers or updated fields without an explicit reload. If you’re on Postgres, consider adding RETURNING so record is populated after Updates.

Apply within this range:

-    result := repo.DB.Model(record).Updates(input)
+    result := repo.DB.Model(record).Clauses(clause.Returning{}).Updates(input)

Outside this range, update imports accordingly:

import (
  "gorm.io/gorm"
  "gorm.io/gorm/clause"
)

23-37: Add a unit test for the not-found branch in UpdateOne.

A small test to assert gorm.ErrRecordNotFound is returned will prevent regressions and documents the new behavior.

I can draft a table-driven test covering found/not-found cases if helpful.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 9b7884d and fad20f5.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (2)
  • go.mod (2 hunks)
  • mutation.go (1 hunks)
🔇 Additional comments (2)
go.mod (1)

10-10: Dependency bumps look good (patch/minor upgrades).

These are safe, non-breaking version increments. No API changes expected. Proceed with CI to validate runtime compatibility.

Also applies to: 12-12, 27-27, 28-28

mutation.go (1)

28-30: Correctly return ErrRecordNotFound when no record is found.

This aligns UpdateOne with DeleteOne and prevents operating on a nil record. Good fix.

@ren0503 ren0503 merged commit 972a969 into master Aug 16, 2025
4 checks passed
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.

Not Handler Case Not Found When Update Model

1 participant