feat(mcp,acp): Repository.WithTx + acp.Repository.InTx

Cross-module transactional support for spec §6.1: ACP CreateSession must
atomically insert acp_sessions row + mcp_tokens row (FK NOT NULL).

- mcp.Repository.WithTx(tx) -> Repository (tx-bound, no pool)
- acp.Repository.InTx(ctx, fn) -> error (pgx.BeginFunc wrapper)
- acp.Repository.WithTx(tx) -> Repository (tx-bound)
- Update fakeRepos in test files to satisfy new interface methods

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-08 12:40:54 +08:00
parent 62d9a91ca5
commit be8d5e3a1b
5 changed files with 53 additions and 0 deletions
+10
View File
@@ -32,6 +32,9 @@ type Repository interface {
// 测试辅助:直接物理删(生产不暴露)
DeleteByID(ctx context.Context, id uuid.UUID) error
// WithTx 返回绑定到指定事务的 Repository。详见 pgRepo.WithTx 文档。
WithTx(tx pgx.Tx) Repository
}
// NewPostgresRepository 用 pgxpool 构造 Repository。
@@ -44,6 +47,13 @@ type pgRepo struct {
q *mcpsqlc.Queries
}
// WithTx 返回一个绑定到指定事务的 Repository 副本。Caller 在外部用 pgx.BeginFunc
// 或 acp.Repository.InTx 取得 tx;这里只负责生成一个把所有 sqlc 调用切到 tx 的轻
// 实例。tx-bound 实例不持有 pool(防止误用同一实例混 pool/tx 两路)。
func (r *pgRepo) WithTx(tx pgx.Tx) Repository {
return &pgRepo{pool: nil, q: r.q.WithTx(tx)}
}
// ===== 类型转换 helper =====
func toPgUUID(u uuid.UUID) pgtype.UUID {