-
Notifications
You must be signed in to change notification settings - Fork 44
fix: handle driver.ErrSkip to avoid duplicate hooks execution with MySQL driver #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 PR addresses the duplicate hook execution issue when using the MySQL driver with InterpolateParams=false by implementing special handling for driver.ErrSkip.
- Refactored hook execution functions to avoid duplicate invocation
- Updated tests to verify that each hook is executed exactly once per operation
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| sqlhooks_test.go | Updated tests to count hook executions instead of simple booleans. |
| sqlhooks.go | Refactored ExecContext and QueryContext to use helper functions that handle driver.ErrSkip. |
…SQL driver When InterpolateParams=false is set in MySQL driver, it returns driver.ErrSkip which causes the SQL package to fall back to prepared statements, resulting in hooks being executed twice. This change handles driver.ErrSkip internally to ensure hooks are only executed once per logical operation.
|
@qustavo
|
|
Thanks for your contribution! |
|
Thanks for your review! After examining the MySQL driver code (*mysqlConn.interpolateParams), I can confirm that it returns
My fix handles all these cases:
To summarize, this change incorporates the sql package's handling of This approach is general and works for all |
Fix duplicate hooks execution with MySQL driver when InterpolateParams=false
Problem
When the MySQL driver is configured with
InterpolateParams=false(default value), it returnsdriver.ErrSkipwhich causes the SQL package to fall back to using prepared statements. This results in hooks being executed twice for a single logical operation:This double execution of hooks is unexpected and can lead to issues such as duplicate logging, metrics, or other side effects.
Solution
This change handles
driver.ErrSkipinternally by adding special handling in the hook execution flow, ensuring that hooks are only executed once per logical operation, even when the MySQL driver returnsdriver.ErrSkip.Implementation
execWithHooksandqueryWithHookshelper functions for cleaner code organizationdriver.ErrSkipto prevent duplicate hook executionsTest Changes
Related Issues
This fixes a long-standing issue that has been reported multiple times: