将账号、认证、管理员、分析、自动化、卡券、聊天、商品、关键词、设置和订单能力迁移到 application 与 adapter,删除 Server 内对应的旧仓储和业务服务实现,并收紧架构检查。 完善批量发布与订单刷新生命周期、凭证写回、账号任务补偿、订单 reconciliation、通知和敏感数据边界;补充正常、失败、取消、并发、越权及多方言回归测试,并修正 MySQL 延迟任务表的 InnoDB 配置。 修复空卡密响应为 null 时前端加载失败的问题,重建嵌入式前端资产;重写重构总计划,清除历史流水并明确阶段 1 至 10 各对应一次完整迭代和一个 PR。 验证:make check;make test-server-race;npm test/typecheck/comments:check/build;Docker SQLite/MySQL 8.4/PostgreSQL 17 TestMultiDB_*。
53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package adapter
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"xianyu-go/internal/db"
|
|
)
|
|
|
|
// TransactionRepository 将数据库事务生命周期适配为 Server 的最小执行端口。
|
|
type TransactionRepository struct {
|
|
// store 保存数据库聚合入口,仅在事务适配器内部使用。
|
|
store *db.Store
|
|
}
|
|
|
|
// NewTransactionRepository 创建数据库事务适配器。
|
|
func NewTransactionRepository(store *db.Store) *TransactionRepository {
|
|
return &TransactionRepository{store: store}
|
|
}
|
|
|
|
// WithTransaction 创建、提交或回滚一个数据库事务。
|
|
func (repository *TransactionRepository) WithTransaction(ctx context.Context, work func(*sql.Tx) error) error {
|
|
if repository == nil || repository.store == nil || repository.store.DB == nil {
|
|
return errors.New("事务 repository 未初始化")
|
|
}
|
|
if work == nil {
|
|
return errors.New("事务工作函数不能为空")
|
|
}
|
|
// tx 和 beginErr 保存事务创建结果及错误。
|
|
tx, beginErr := repository.store.DB.BeginTx(ctx, nil)
|
|
if beginErr != nil {
|
|
return beginErr
|
|
}
|
|
// committed 标识事务是否已经提交,避免提交失败后重复处理回滚。
|
|
committed := false
|
|
defer func() {
|
|
if !committed {
|
|
_ = tx.Rollback()
|
|
}
|
|
}()
|
|
// workErr 保存事务工作函数返回的业务错误。
|
|
if workErr := work(tx); workErr != nil {
|
|
return workErr
|
|
}
|
|
// commitErr 保存事务提交错误。
|
|
if commitErr := tx.Commit(); commitErr != nil {
|
|
return commitErr
|
|
}
|
|
committed = true
|
|
return nil
|
|
}
|