fix(数据层): 修复数据完整性与仓储一致性问题

## 数据完整性修复 (fix-critical-data-integrity)
- 添加 error_translate.go 统一错误转换层
- 修复 nil 输入和 NotFound 错误处理
- 增强仓储层错误一致性

## 仓储一致性修复 (fix-high-repository-consistency)
- Group schema 添加 default_validity_days 字段
- Account schema 添加 proxy edge 关联
- 新增 UsageLog ent schema 定义
- 修复 UpdateBalance/UpdateConcurrency 受影响行数校验

## 数据卫生修复 (fix-medium-data-hygiene)
- UserSubscription 添加软删除支持 (SoftDeleteMixin)
- RedeemCode/Setting 添加硬删除策略文档
- account_groups/user_allowed_groups 的 created_at 声明 timestamptz
- 停止写入 legacy users.allowed_groups 列
- 新增迁移: 011-014 (索引优化、软删除、孤立数据审计、列清理)

## 测试补充
- 添加 UserSubscription 软删除测试
- 添加迁移回归测试
- 添加 NotFound 错误测试

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
yangjianbo
2025-12-31 14:11:57 +08:00
parent 820bb16ca7
commit 5906f9ab98
87 changed files with 15258 additions and 485 deletions

View File

@@ -13,6 +13,8 @@ import (
"entgo.io/ent/schema/field"
"github.com/Wei-Shaw/sub2api/ent/account"
"github.com/Wei-Shaw/sub2api/ent/group"
"github.com/Wei-Shaw/sub2api/ent/proxy"
"github.com/Wei-Shaw/sub2api/ent/usagelog"
)
// AccountCreate is the builder for creating a Account entity.
@@ -292,6 +294,26 @@ func (_c *AccountCreate) AddGroups(v ...*Group) *AccountCreate {
return _c.AddGroupIDs(ids...)
}
// SetProxy sets the "proxy" edge to the Proxy entity.
func (_c *AccountCreate) SetProxy(v *Proxy) *AccountCreate {
return _c.SetProxyID(v.ID)
}
// AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
func (_c *AccountCreate) AddUsageLogIDs(ids ...int64) *AccountCreate {
_c.mutation.AddUsageLogIDs(ids...)
return _c
}
// AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
func (_c *AccountCreate) AddUsageLogs(v ...*UsageLog) *AccountCreate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddUsageLogIDs(ids...)
}
// Mutation returns the AccountMutation object of the builder.
func (_c *AccountCreate) Mutation() *AccountMutation {
return _c.mutation
@@ -495,10 +517,6 @@ func (_c *AccountCreate) createSpec() (*Account, *sqlgraph.CreateSpec) {
_spec.SetField(account.FieldExtra, field.TypeJSON, value)
_node.Extra = value
}
if value, ok := _c.mutation.ProxyID(); ok {
_spec.SetField(account.FieldProxyID, field.TypeInt64, value)
_node.ProxyID = &value
}
if value, ok := _c.mutation.Concurrency(); ok {
_spec.SetField(account.FieldConcurrency, field.TypeInt, value)
_node.Concurrency = value
@@ -567,6 +585,39 @@ func (_c *AccountCreate) createSpec() (*Account, *sqlgraph.CreateSpec) {
edge.Target.Fields = specE.Fields
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.ProxyIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: account.ProxyTable,
Columns: []string{account.ProxyColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.ProxyID = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.UsageLogsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: account.UsageLogsTable,
Columns: []string{account.UsageLogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
@@ -721,12 +772,6 @@ func (u *AccountUpsert) UpdateProxyID() *AccountUpsert {
return u
}
// AddProxyID adds v to the "proxy_id" field.
func (u *AccountUpsert) AddProxyID(v int64) *AccountUpsert {
u.Add(account.FieldProxyID, v)
return u
}
// ClearProxyID clears the value of the "proxy_id" field.
func (u *AccountUpsert) ClearProxyID() *AccountUpsert {
u.SetNull(account.FieldProxyID)
@@ -1094,13 +1139,6 @@ func (u *AccountUpsertOne) SetProxyID(v int64) *AccountUpsertOne {
})
}
// AddProxyID adds v to the "proxy_id" field.
func (u *AccountUpsertOne) AddProxyID(v int64) *AccountUpsertOne {
return u.Update(func(s *AccountUpsert) {
s.AddProxyID(v)
})
}
// UpdateProxyID sets the "proxy_id" field to the value that was provided on create.
func (u *AccountUpsertOne) UpdateProxyID() *AccountUpsertOne {
return u.Update(func(s *AccountUpsert) {
@@ -1676,13 +1714,6 @@ func (u *AccountUpsertBulk) SetProxyID(v int64) *AccountUpsertBulk {
})
}
// AddProxyID adds v to the "proxy_id" field.
func (u *AccountUpsertBulk) AddProxyID(v int64) *AccountUpsertBulk {
return u.Update(func(s *AccountUpsert) {
s.AddProxyID(v)
})
}
// UpdateProxyID sets the "proxy_id" field to the value that was provided on create.
func (u *AccountUpsertBulk) UpdateProxyID() *AccountUpsertBulk {
return u.Update(func(s *AccountUpsert) {