Commit 1d3f9ab
Fix critical transaction bug in WithTransact function (#28)
## Summary
This PR fixes a critical transaction handling bug in the `WithTransact`
function that could lead to data corruption during migrations.
## Problem
The original implementation had several serious flaws:
1. **Wrong error variable**: The defer block checked `err` from
`Begin()`, not from the function parameter `f()`
2. **Silent failures**: When `f()` returned an error, the transaction
would be **committed** instead of rolled back
3. **Lost commit errors**: Commit failures were not properly returned to
the caller
4. **No context support**: Used `Begin()` instead of `BeginTx(ctx, nil)`
## Solution
The fix ensures proper transaction handling by:
- ✅ Capturing the function error explicitly: `err = f(tx)`
- ✅ Rolling back on function errors with proper error wrapping
- ✅ Handling commit errors and attempting rollback for connection
cleanup
- ✅ Adding context support with `BeginTx(ctx, nil)`
- ✅ Adding nil callback guard for defensive programming
- ✅ Preserving panic recovery behavior
## Impact
**Before**: Transactions with errors would be committed → **Data
corruption risk**
**After**: Transactions with errors are properly rolled back → **Data
integrity preserved**
## Testing
- Added comprehensive test suite documenting the bug and verifying the
fix
- Oracle review confirms the implementation follows Go best practices
- All existing tests continue to pass
## Code Review
This fix was reviewed by the Oracle AI system and incorporates all
recommended improvements including:
- Context propagation
- Connection cleanup on commit failure
- Proper error wrapping
- Nil callback validation
Extracted from the transaction improvements in PR #27, focusing
specifically on the critical bug fix.
---------
Co-authored-by: Amp <amp@ampcode.com>1 parent 37815ff commit 1d3f9ab
2 files changed
+69
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
28 | 70 | | |
29 | 71 | | |
30 | 72 | | |
| |||
58 | 100 | | |
59 | 101 | | |
60 | 102 | | |
61 | | - | |
62 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
63 | 111 | | |
64 | 112 | | |
65 | 113 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
83 | 87 | | |
84 | 88 | | |
85 | 89 | | |
86 | 90 | | |
87 | 91 | | |
88 | 92 | | |
89 | | - | |
| 93 | + | |
90 | 94 | | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
99 | 111 | | |
100 | 112 | | |
101 | 113 | | |
| |||
0 commit comments