去报告它绕过事务内部使用的数据库句柄的linter吧
概览
去找个报告它在交易中用过数据库句柄的linter。 回调接收事务时为 tx。r.db 是交易来自的池,因此审计日志写在事务外部。 代码会编译。一个没有失败的测试也通过了。txbypass 从源代码中找到了它。 每份报告结尾都会标明其规则名称。这个名称就是 //txbypass:ignore 所用的。 固定一个版本,运行 go vet 或 golangci-lint,或者从归档中安装 Mise Use 在项目的 Mise.toml 中钉住版本,所以每次检出和 CI 运行同一个版本。添加 -g 来为你机器上的每个项目安装。 通过Go Veet,它用和你其他Vet检查一样的包裹加载方式运行,并缓存: 来自发布档案,已与已发布的校验码进行核对:
概述(AI 翻译)
TXBYPASS
去找个报告它在交易中用过数据库句柄的linter。
概述
回调以 tx 的形式接收该事务。r.db 是交易来自的池,因此审计日志写入事务外部。
代码:出了什么问题
写入能经回滚存活。它从未包含在交易中
写入可以无限等待。它运行在另一个连接上,需要该事务持有的锁。对于一个连接池,它会等待连接本身
读取无法看到事务的行。它们尚未提交
代码会编译。一个没有失败的测试也通过了。txbypass 从源代码中找到了它。
每条规则的报告都以规则名称结尾。这个名称就是 //txbypass:ignore 所采用的。关于指令本身的报告不命名规则,且无忽略会使其静音。
安装
| 方法 | 命令 | 需求 |
|---|---|---|
| mise (推荐) | mise use "github:mpyw/[email protected]" |
没反应。安装预构建的二进制文件 |
go tool |
go get -tool github.com/mpyw/txbypass/cmd/txbypass@latest |
走1.27+ |
go install |
'去安装GitHub吧。 |
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
Go linter that reports a database handle used inside a transaction it opened.
Overview
package app
import (
"context"
"gorm.io/gorm"
)
func (r *OrderRepo) Place(
ctx context.Context,
o *Order,
) error {
return r.db.WithContext(ctx).Transaction(
func(tx *gorm.DB) error {
if err := tx.Create(o).Error; err != nil {
return err
}
log := &AuditLog{OrderID: o.ID}
return r.db.Create(log).Error
},
)
}
$ txbypass ./...
order.go:19:11: r.db bypasses the transaction opened with Transaction; use tx instead [outer-handle]
The callback receives the transaction as tx. r.db is the pool the transaction came from, so the audit log is written outside the transaction.
The code compiles. A test where nothing fails passes too. txbypass finds it from the source.
Every report of a rule ends with the rule’s name. That name is what //txbypass:ignore takes. A report about a directive itself names no rule, and no ignore silences it.
Install
| Method | Command | Needs |
|---|---|---|
| mise (recommended) | mise use "github:mpyw/[email protected]" |
Nothing. Installs the prebuilt binary |
go tool |
go get -tool github.com/mpyw/txbypass/cmd/txbypass@latest |
Go 1.26+ |
go install |
go install github.com/mpyw/txbypass/cmd/txbypass@latest |
Go 1.26+ |
| Release archive | See below | Nothing |
txbypass ./...
推荐工具
换一个关键词,或者移除筛选条件。
安装
npx skillfish add mpyw/txbypass