fix(orm): txOrm miss debug log

This commit is contained in:
Cr 2021-09-03 14:23:09 +08:00
parent 556e743c84
commit 0e9b2c27ea
No known key found for this signature in database
GPG Key ID: E6C6812B87229952
3 changed files with 62 additions and 1 deletions

View File

@ -65,7 +65,7 @@
- Fix 4734: do not reset id in Delete function. [4738](https://github.com/beego/beego/pull/4738) [4742](https://github.com/beego/beego/pull/4742) - Fix 4734: do not reset id in Delete function. [4738](https://github.com/beego/beego/pull/4738) [4742](https://github.com/beego/beego/pull/4742)
- Fix 4699: Remove Remove goyaml2 dependency. [4755](https://github.com/beego/beego/pull/4755) - Fix 4699: Remove Remove goyaml2 dependency. [4755](https://github.com/beego/beego/pull/4755)
- Fix 4698: Prompt error when config format is incorrect. [4757](https://github.com/beego/beego/pull/4757) - Fix 4698: Prompt error when config format is incorrect. [4757](https://github.com/beego/beego/pull/4757)
- Fix 4674: Tx Orm missing debug log [4756](https://github.com/beego/beego/pull/4756)
## Fix Sonar ## Fix Sonar
- [4677](https://github.com/beego/beego/pull/4677) - [4677](https://github.com/beego/beego/pull/4677)

View File

@ -536,6 +536,11 @@ func (o *orm) BeginWithCtxAndOpts(ctx context.Context, opts *sql.TxOptions) (TxO
db: &TxDB{tx: tx}, db: &TxDB{tx: tx},
}, },
} }
if Debug {
_txOrm.db = newDbQueryLog(o.alias, _txOrm.db)
}
var taskTxOrm TxOrmer = _txOrm var taskTxOrm TxOrmer = _txOrm
return taskTxOrm, nil return taskTxOrm, nil
} }

View File

@ -2886,3 +2886,59 @@ func TestContextCanceled(t *testing.T) {
_, err = qs.Filter("UserName", "slene").CountWithCtx(ctx) _, err = qs.Filter("UserName", "slene").CountWithCtx(ctx)
throwFail(t, AssertIs(err, context.Canceled)) throwFail(t, AssertIs(err, context.Canceled))
} }
func TestDebugLog(t *testing.T) {
txCommitFn := func() {
o := NewOrm()
o.DoTx(func(ctx context.Context, txOrm TxOrmer) (txerr error) {
_, txerr = txOrm.QueryTable(&User{}).Count()
return
})
}
txRollbackFn := func() {
o := NewOrm()
o.DoTx(func(ctx context.Context, txOrm TxOrmer) (txerr error) {
user := NewUser()
user.UserName = "slene"
user.Email = "vslene@gmail.com"
user.Password = "pass"
user.Status = 3
user.IsStaff = true
user.IsActive = true
txOrm.Insert(user)
txerr = fmt.Errorf("mock error")
return
})
}
Debug = true
output1 := captureDebugLogOutput(txCommitFn)
assert.Contains(t, output1, "START TRANSACTION")
assert.Contains(t, output1, "COMMIT")
output2 := captureDebugLogOutput(txRollbackFn)
assert.Contains(t, output2, "START TRANSACTION")
assert.Contains(t, output2, "ROLLBACK")
Debug = false
output1 = captureDebugLogOutput(txCommitFn)
assert.EqualValues(t, output1, "")
output2 = captureDebugLogOutput(txRollbackFn)
assert.EqualValues(t, output2, "")
}
func captureDebugLogOutput(f func()) string {
var buf bytes.Buffer
DebugLog.SetOutput(&buf)
defer func() {
DebugLog.SetOutput(os.Stderr)
}()
f()
return buf.String()
}