fix(仓储): 修复查询关闭错误并迁移集成测试

修复 rows.Close 失败时的错误返回逻辑
迁移网关路由集成测试到 ent 事务基建
补齐仓储接口变更对应的测试桩方法
新增 backend/Makefile 统一测试命令
测试: GOTOOLCHAIN=go1.24.11 go test ./...
测试: golangci-lint run ./... --timeout=5m
测试: make test-integration
This commit is contained in:
yangjianbo
2025-12-30 16:41:45 +08:00
parent b6fec590a7
commit aacbc98aec
15 changed files with 123 additions and 110 deletions

View File

@@ -235,9 +235,8 @@ func (r *groupRepository) DeleteCascade(ctx context.Context, id int64) ([]int64,
defer func() { _ = tx.Rollback() }()
exec = tx.Client()
txClient = exec
} else {
// 已处于外部事务中ErrTxStarted复用当前 client 参与同一事务。
}
// err 为 dbent.ErrTxStarted 时,复用当前 client 参与同一事务。
// Lock the group row to avoid concurrent writes while we cascade.
// 这里使用 exec.QueryContext 手动扫描,确保同一事务内加锁并能区分“未找到”与其他错误。
@@ -330,8 +329,8 @@ func (r *groupRepository) DeleteCascade(ctx context.Context, id int64) ([]int64,
return affectedUserIDs, nil
}
func (r *groupRepository) loadAccountCounts(ctx context.Context, groupIDs []int64) (map[int64]int64, error) {
counts := make(map[int64]int64, len(groupIDs))
func (r *groupRepository) loadAccountCounts(ctx context.Context, groupIDs []int64) (counts map[int64]int64, err error) {
counts = make(map[int64]int64, len(groupIDs))
if len(groupIDs) == 0 {
return counts, nil
}
@@ -344,23 +343,24 @@ func (r *groupRepository) loadAccountCounts(ctx context.Context, groupIDs []int6
if err != nil {
return nil, err
}
defer rows.Close()
defer func() {
if closeErr := rows.Close(); closeErr != nil && err == nil {
err = closeErr
counts = nil
}
}()
for rows.Next() {
var groupID int64
var count int64
if err := rows.Scan(&groupID, &count); err != nil {
if err = rows.Scan(&groupID, &count); err != nil {
return nil, err
}
counts[groupID] = count
}
if err := rows.Err(); err != nil {
if err = rows.Err(); err != nil {
return nil, err
}
return counts, nil
}
func errorsIsNoRows(err error) bool {
return err == sql.ErrNoRows
}