graph/db/migration1: fix defer commit/rollback in test tx executor
The defer closure checked a local err variable for commit/rollback decisions, but err remained nil after a successful BeginTx. When txBody failed, the error was returned directly without assigning to err, so the defer always committed instead of rolling back. Additionally, since err was not a named return value, the defer's Commit error assignment was silently swallowed. Replace the error-prone defer pattern with explicit rollback on txBody failure and a direct Commit return.
This commit is contained in:
1 file changed
+7
-8
@@ -31,16 +31,15 @@ func (t *testBatchedSQLQueries) ExecTx(ctx context.Context,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
} else {
|
||||
err = tx.Commit()
|
||||
}
|
||||
}()
|
||||
|
||||
reset()
|
||||
queries := sqlc.New(tx)
|
||||
|
||||
return txBody(queries)
|
||||
if err := txBody(queries); err != nil {
|
||||
_ = tx.Rollback()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
Reference in new issue
Block a user