Skip to content

fix: [#738] The Orm Creating event can be triggered when the query with the Model method#1166

Merged
hwbrzzl merged 2 commits intov1.16.xfrom
bowen/#738-1
Aug 19, 2025
Merged

fix: [#738] The Orm Creating event can be triggered when the query with the Model method#1166
hwbrzzl merged 2 commits intov1.16.xfrom
bowen/#738-1

Conversation

@hwbrzzl
Copy link
Contributor

@hwbrzzl hwbrzzl commented Aug 18, 2025

📑 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.Ignore helper 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:

  • Introduced the errors.Ignore helper function and replaced direct calls to Close() and similar cleanup methods with errors.Ignore in many places (e.g., file, network, and response closing operations) to prevent unhandled errors during resource cleanup. This affects files such as database/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:

  • Refactored conditional expressions for clarity, such as replacing !(a && b) with !a || !b in database/gorm/event.go and 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]
  • Improved switch statement usage to use direct value matching rather than boolean expressions, making the code easier to read and maintain, particularly in mail/mail.go and log/utils.go. [1] [2] [3]

Test and Assertion Improvements:

  • Updated test assertions from assert.Nil to assert.NoError for better clarity and reliability, and improved temporary file cleanup in test files. [1] [2] [3] [4]

Database Event Handling Enhancements:

  • Added helper functions isSlice and hasID to database/gorm/query.go and 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:

  • Used assert.Equal with updated expected values in tests to reflect code changes.
  • Used _ = os.Setenv(...) to ignore errors from environment variable setting in build commands.
  • Minor improvements to string formatting and indentation logic for better output formatting and code clarity. [1] [2]

These changes collectively improve error resilience, code clarity, and maintainability throughout the project.

✅ Checks

  • Added test cases for my code

@codecov
Copy link

codecov bot commented Aug 18, 2025

Codecov Report

❌ Patch coverage is 62.50000% with 15 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (v1.16.x@c236b42). Learn more about missing BASE report.

Files with missing lines Patch % Lines
mail/mail.go 43.75% 9 Missing ⚠️
console/console/build_command.go 0.00% 3 Missing ⚠️
errors/errors.go 0.00% 2 Missing ⚠️
support/file/file.go 66.66% 1 Missing ⚠️
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.
📢 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.

@hwbrzzl hwbrzzl marked this pull request as ready for review August 18, 2025 11:47
Copilot AI review requested due to automatic review settings August 18, 2025 11:47
@hwbrzzl hwbrzzl requested a review from a team as a code owner August 18, 2025 11:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.Ignore helper 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@almas-x defer func seems to be easier?

defer func () {
  _ = f.Close()
}()

Copy link
Member

@devhaozi devhaozi left a comment

Choose a reason for hiding this comment

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

LGTM

return errors.Unwrap(err)
}

func Ignore(fn func() error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about func Ignore(fns ...func()error)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If so, I prefer defer func directly. 😂

Copy link
Contributor

Choose a reason for hiding this comment

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

The main point is using defer to handle closure functions just to ignore errors, which makes the code look noisy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, we can keep Ignore(fn func() error) for now.

@hwbrzzl hwbrzzl merged commit 9390d55 into v1.16.x Aug 19, 2025
15 checks passed
@hwbrzzl hwbrzzl deleted the bowen/#738-1 branch August 19, 2025 01:43
hwbrzzl added a commit that referenced this pull request Aug 20, 2025
…th the Model method (#1166)

* fix: [#738] The Orm Creating event can be triggered when the query with the Model method

* fix tests
hwbrzzl added a commit that referenced this pull request Aug 20, 2025
…th the Model method (#1167)

* 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

* optimize
hwbrzzl added a commit that referenced this pull request Sep 19, 2025
* 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>
hwbrzzl added a commit that referenced this pull request Oct 26, 2025
* 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>
hwbrzzl added a commit that referenced this pull request Oct 31, 2025
* 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>
hwbrzzl added a commit that referenced this pull request Oct 31, 2025
* 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>
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