fix: prevent RowsAffected double-counting when triggers fire#363
Open
dlevy-msft-sql wants to merge 4 commits into
Open
fix: prevent RowsAffected double-counting when triggers fire#363dlevy-msft-sql wants to merge 4 commits into
dlevy-msft-sql wants to merge 4 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #363 +/- ##
===========================================
+ Coverage 80.62% 96.60% +15.98%
===========================================
Files 35 92 +57
Lines 6910 74360 +67450
===========================================
+ Hits 5571 71837 +66266
- Misses 1068 2188 +1120
- Partials 271 335 +64
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
e841b35 to
8ae789e
Compare
There was a problem hiding this comment.
Pull request overview
Fixes incorrect RowsAffected() values when DML statements fire AFTER triggers that emit their own rowcount tokens, by ensuring the final DONE token’s count is treated as authoritative.
Changes:
- Update
tokenProcessor.iterateResponse()to assign (not accumulate)DONE/DONEPROCrow counts. - Add unit tests validating rowcount assignment behavior in trigger-like and multi-statement token sequences.
- Add integration tests covering
RowsAffected()with triggers (NOCOUNT on/off) and multi-statement batch behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| token.go | Changes DONE rowcount handling to avoid double-counting when triggers emit additional rowcount tokens. |
| token_test.go | Adds unit tests asserting the new rowcount assignment semantics. |
| rowsaffected_test.go | Adds integration coverage for trigger scenarios and multi-statement batch RowsAffected() behavior. |
fdb9e28 to
f876715
Compare
Change doneInProcStruct row count handling from += to = in iterateResponse, mirroring the doneStruct fix. For the RPC/sp_executesql path, both trigger and outer statement counts arrive as DONEINPROC tokens; accumulation would inflate RowsAffected when triggers fire without SET NOCOUNT ON. Add TestRowCountDoneInProcOnlyRPCPath covering the DONEINPROC-only sequence where DONEPROC lacks the doneCount flag.
Move multi-row update test to before the NOCOUNT trigger switch so it exercises the original double-counting bug path where both trigger and outer statement DONEINPROC counts would accumulate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a DML statement (UPDATE/INSERT/DELETE) fires an AFTER trigger that does not use
SET NOCOUNT ON, SQL Server sends two TDS tokens with row counts:DONEINPROC(0xFD) - the trigger's DML row countDONE(0xFF) - the outer statement's actual row countThe driver's
iterateResponse()was accumulating (+=) both counts intorowCount, causingRowsAffected()to return the sum (e.g., 2 instead of 1 for a single-row update that fires a trigger inserting 1 audit row).This matches the behavior reported in #204 where
RowsAffected()returns incorrect values.Root Cause
In
token.go,iterateResponse()line 1200:Fix
Change
doneStructhandling from accumulation (+=) to assignment (=):The final
DONE/DONEPROCtoken carries the authoritative row count for the outermost statement.DONEINPROCtokens fromsp_executesqlstill work correctly because the subsequentDONEPROChasstatus=0(nodoneCountflag set), so it doesn't overwrite.Verified Scenarios
Tests
TestRowsAffectedWithTriggerintegration test covering all 4 scenariosTestAffectedRowscontinues to pass./msdsn,./internal/...,./integratedauth,./azuread)Fixes #204