fix: [#738] The Orm Creating event can be triggered when the query with the Model method#1166
fix: [#738] The Orm Creating event can be triggered when the query with the Model method#1166
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## v1.16.x #1166 +/- ##
==========================================
Coverage ? 66.91%
==========================================
Files ? 215
Lines ? 14081
Branches ? 0
==========================================
Hits ? 9422
Misses ? 4282
Partials ? 377 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
This pull request addresses an issue where ORM events were being triggered inappropriately when performing bulk operations or operations on slices/collections. The fix improves event system robustness by preventing events from firing for slices or objects without IDs, and replaces direct resource cleanup calls with an error-ignoring helper throughout the codebase.
- Added helper functions to determine when to skip event firing for bulk operations
- Introduced
errors.Ignorehelper for consistent resource cleanup error handling - Improved code readability by simplifying conditional logic and switch statements
Reviewed Changes
Copilot reviewed 28 out of 29 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| database/gorm/query.go | Added helper functions and event skipping logic for bulk operations |
| tests/query_test.go | Added comprehensive test cases to verify events are not triggered for slice operations |
| tests/models.go | Added test assertions to catch inappropriate event triggering |
| errors/errors.go | Added new Ignore helper function for resource cleanup |
| Multiple files | Replaced direct resource cleanup calls with errors.Ignore wrapper |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| return err | ||
| } | ||
| defer f.Close() | ||
| defer errors.Ignore(f.Close) |
There was a problem hiding this comment.
@almas-x defer func seems to be easier?
defer func () {
_ = f.Close()
}()
| return errors.Unwrap(err) | ||
| } | ||
|
|
||
| func Ignore(fn func() error) { |
There was a problem hiding this comment.
How about func Ignore(fns ...func()error)?
There was a problem hiding this comment.
If so, I prefer defer func directly. 😂
There was a problem hiding this comment.
The main point is using defer to handle closure functions just to ignore errors, which makes the code look noisy.
There was a problem hiding this comment.
Sure, we can keep Ignore(fn func() error) for now.
* correctly set cc and bcc headers (#1144) * correct the error return from SendMailJob handle (#1147) * fix: [#743] package make command generates correct code (#1151) (#1152) (cherry picked from commit 93dc8a3) * fix: [#749] The path is incorrect when publishing package files (#1157) * chore: optimize assertions for package installation (#1160) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting (#1162) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting * optimize * fix: [#738] The Orm Creating event can be triggered when the query with the Model method (#1166) * fix: [#738] The Orm Creating event can be triggered when the query with the Model method * fix tests * upgrade: v1.16.1 * fix: [#762] handle panic when using transaction (#1183) * fix: [#762] handle panic when using transaction * v1.16.2 * optimize * fix lint * fix: [#768] facades.DB will panic when migrating a new column (#1185) * fix: [#768] facades.DB will panic when migrating a new column * optimize * optimize * feat: [#770] Add a SelectRaw function for the ORM (#1186) * fix: [#770] Add a SelectRaw function for the ORM * fix: [#770] Add a SelectRaw function for the ORM * fix ci * upgrade: v1.16.3 * fix typo --------- Co-authored-by: krishan kumar <84431594+kkumar-gcc@users.noreply.github.com> Co-authored-by: ALMAS <almas.cc@icloud.com>
* correctly set cc and bcc headers (#1144) * correct the error return from SendMailJob handle (#1147) * fix: [#743] package make command generates correct code (#1151) (#1152) (cherry picked from commit 93dc8a3) * fix: [#749] The path is incorrect when publishing package files (#1157) * chore: optimize assertions for package installation (#1160) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting (#1162) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting * optimize * fix: [#738] The Orm Creating event can be triggered when the query with the Model method (#1166) * fix: [#738] The Orm Creating event can be triggered when the query with the Model method * fix tests * upgrade: v1.16.1 * fix: [#762] handle panic when using transaction (#1183) * fix: [#762] handle panic when using transaction * v1.16.2 * optimize * fix lint * fix: [#768] facades.DB will panic when migrating a new column (#1185) * fix: [#768] facades.DB will panic when migrating a new column * optimize * optimize * feat: [#770] Add a SelectRaw function for the ORM (#1186) * fix: [#770] Add a SelectRaw function for the ORM * fix: [#770] Add a SelectRaw function for the ORM * fix ci * upgrade: v1.16.3 * fix: comand cannot be run concurrently (#1243) * fix: comand cannot be run concurrently * fix ci * fix ci * optimize * optimize * optimize * optimize * optimize global options * fix ci * upgrade v1.16.4 * optimize * optimize --------- Co-authored-by: krishan kumar <84431594+kkumar-gcc@users.noreply.github.com> Co-authored-by: ALMAS <almas.cc@icloud.com>
* correctly set cc and bcc headers (#1144) * correct the error return from SendMailJob handle (#1147) * fix: [#743] package make command generates correct code (#1151) (#1152) (cherry picked from commit 93dc8a3) * fix: [#749] The path is incorrect when publishing package files (#1157) * chore: optimize assertions for package installation (#1160) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting (#1162) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting * optimize * fix: [#738] The Orm Creating event can be triggered when the query with the Model method (#1166) * fix: [#738] The Orm Creating event can be triggered when the query with the Model method * fix tests * upgrade: v1.16.1 * fix: [#762] handle panic when using transaction (#1183) * fix: [#762] handle panic when using transaction * v1.16.2 * optimize * fix lint * fix: [#768] facades.DB will panic when migrating a new column (#1185) * fix: [#768] facades.DB will panic when migrating a new column * optimize * optimize * feat: [#770] Add a SelectRaw function for the ORM (#1186) * fix: [#770] Add a SelectRaw function for the ORM * fix: [#770] Add a SelectRaw function for the ORM * fix ci * upgrade: v1.16.3 * fix: comand cannot be run concurrently (#1243) * fix: comand cannot be run concurrently * fix ci * fix ci * optimize * optimize * optimize * optimize * optimize global options * fix ci * upgrade v1.16.4 * fix: [#807] queue.Shutdown doesn't stop the queue as expected (#1252) * fix: [#807] queue.Shutdown doesn't stop the queue as expected * optimize * upgrade: v1.16.5 * fix * Update queue/worker_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * optimize --------- Co-authored-by: krishan kumar <84431594+kkumar-gcc@users.noreply.github.com> Co-authored-by: ALMAS <almas.cc@icloud.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* correctly set cc and bcc headers (#1144) * correct the error return from SendMailJob handle (#1147) * fix: [#743] package make command generates correct code (#1151) (#1152) (cherry picked from commit 93dc8a3) * fix: [#749] The path is incorrect when publishing package files (#1157) * chore: optimize assertions for package installation (#1160) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting (#1162) * fix: The configuredServiceProviders, publishes and publishGroups are not reset when Booting * optimize * fix: [#738] The Orm Creating event can be triggered when the query with the Model method (#1166) * fix: [#738] The Orm Creating event can be triggered when the query with the Model method * fix tests * upgrade: v1.16.1 * fix: [#762] handle panic when using transaction (#1183) * fix: [#762] handle panic when using transaction * v1.16.2 * optimize * fix lint * fix: [#768] facades.DB will panic when migrating a new column (#1185) * fix: [#768] facades.DB will panic when migrating a new column * optimize * optimize * feat: [#770] Add a SelectRaw function for the ORM (#1186) * fix: [#770] Add a SelectRaw function for the ORM * fix: [#770] Add a SelectRaw function for the ORM * fix ci * upgrade: v1.16.3 * fix: comand cannot be run concurrently (#1243) * fix: comand cannot be run concurrently * fix ci * fix ci * optimize * optimize * optimize * optimize * optimize global options * fix ci * upgrade v1.16.4 * fix: [#807] queue.Shutdown doesn't stop the queue as expected (#1252) * fix: [#807] queue.Shutdown doesn't stop the queue as expected * optimize * upgrade: v1.16.5 * fix * Update queue/worker_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * optimize --------- Co-authored-by: krishan kumar <84431594+kkumar-gcc@users.noreply.github.com> Co-authored-by: ALMAS <almas.cc@icloud.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
📑 Description
Closes goravel/goravel#738
This pull request introduces several improvements focused on error handling, resource management, and code readability across the codebase. The most significant changes are the adoption of a new
errors.Ignorehelper for safely ignoring errors from resource cleanup operations, refactoring conditional logic for clarity, and improving switch statement usage. These updates enhance code reliability and maintainability, especially around file and network resource handling.Error Handling and Resource Management:
errors.Ignorehelper function and replaced direct calls toClose()and similar cleanup methods witherrors.Ignorein many places (e.g., file, network, and response closing operations) to prevent unhandled errors during resource cleanup. This affects files such asdatabase/gorm/query.go,filesystem/local.go,support/file/file.go,support/http/body.go,testing/file/file.go,testing/http/test_response.go,tests/container.go, and more. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]Code Readability and Logic Simplification:
!(a && b)with!a || !bindatabase/gorm/event.goand simplifying switch statements to use direct value matching instead of boolean expressions in several locations (mail/mail.go,log/utils.go). [1] [2] [3] [4] [5]mail/mail.goandlog/utils.go. [1] [2] [3]Test and Assertion Improvements:
assert.Niltoassert.NoErrorfor better clarity and reliability, and improved temporary file cleanup in test files. [1] [2] [3] [4]Database Event Handling Enhancements:
isSliceandhasIDtodatabase/gorm/query.goand updated event methods to skip event firing for slices or objects without IDs, improving event system robustness. [1] [2] [3] [4] [5] [6] [7]Miscellaneous Improvements:
assert.Equalwith updated expected values in tests to reflect code changes._ = os.Setenv(...)to ignore errors from environment variable setting in build commands.These changes collectively improve error resilience, code clarity, and maintainability throughout the project.
✅ Checks