refactor(数据库): 迁移持久层到 Ent 并清理 GORM
将仓储层/基础设施改为 Ent + 原生 SQL 执行路径,并移除 AutoMigrate 与 GORM 依赖。 重构内容包括: - 仓储层改用 Ent/SQL(含 usage_log/account 等复杂查询),统一错误映射 - 基础设施与 setup 初始化切换为 Ent + SQL migrations - 集成测试与 fixtures 迁移到 Ent 事务模型 - 清理遗留 GORM 模型/依赖,补充迁移与文档说明 - 增加根目录 Makefile 便于前后端编译 测试: - go test -tags unit ./... - go test -tags integration ./...
This commit is contained in:
113
backend/ent/schema/user_subscription.go
Normal file
113
backend/ent/schema/user_subscription.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/ent/schema/mixins"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
)
|
||||
|
||||
// UserSubscription holds the schema definition for the UserSubscription entity.
|
||||
type UserSubscription struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (UserSubscription) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Annotation{Table: "user_subscriptions"},
|
||||
}
|
||||
}
|
||||
|
||||
func (UserSubscription) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
mixins.TimeMixin{},
|
||||
}
|
||||
}
|
||||
|
||||
func (UserSubscription) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int64("user_id"),
|
||||
field.Int64("group_id"),
|
||||
|
||||
field.Time("starts_at").
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.Time("expires_at").
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.String("status").
|
||||
MaxLen(20).
|
||||
Default(service.SubscriptionStatusActive),
|
||||
|
||||
field.Time("daily_window_start").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.Time("weekly_window_start").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.Time("monthly_window_start").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
|
||||
field.Float("daily_usage_usd").
|
||||
SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}).
|
||||
Default(0),
|
||||
field.Float("weekly_usage_usd").
|
||||
SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}).
|
||||
Default(0),
|
||||
field.Float("monthly_usage_usd").
|
||||
SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}).
|
||||
Default(0),
|
||||
|
||||
field.Int64("assigned_by").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Time("assigned_at").
|
||||
Default(time.Now).
|
||||
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
||||
field.String("notes").
|
||||
Optional().
|
||||
Nillable().
|
||||
SchemaType(map[string]string{dialect.Postgres: "text"}),
|
||||
}
|
||||
}
|
||||
|
||||
func (UserSubscription) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("user", User.Type).
|
||||
Ref("subscriptions").
|
||||
Field("user_id").
|
||||
Unique().
|
||||
Required(),
|
||||
edge.From("group", Group.Type).
|
||||
Ref("subscriptions").
|
||||
Field("group_id").
|
||||
Unique().
|
||||
Required(),
|
||||
edge.From("assigned_by_user", User.Type).
|
||||
Ref("assigned_subscriptions").
|
||||
Field("assigned_by").
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
func (UserSubscription) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("user_id"),
|
||||
index.Fields("group_id"),
|
||||
index.Fields("status"),
|
||||
index.Fields("expires_at"),
|
||||
index.Fields("assigned_by"),
|
||||
index.Fields("user_id", "group_id").Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user