fix: handle case not found when update#66
Conversation
Summary by CodeRabbit
WalkthroughDependency 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
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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
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 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 unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
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
recordmay not reflect DB-side defaults/triggers or updated fields without an explicit reload. If you’re on Postgres, consider adding RETURNING sorecordis populated afterUpdates.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.ErrRecordNotFoundis 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.
⛔ Files ignored due to path filters (1)
go.sumis 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.
No description provided.