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:
yangjianbo
2025-12-29 10:03:27 +08:00
parent fd51ff6970
commit 3d617de577
149 changed files with 62892 additions and 3212 deletions

View File

@@ -0,0 +1,354 @@
// Code generated by ent, DO NOT EDIT.
package ent
import (
"fmt"
"strings"
"time"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"github.com/Wei-Shaw/sub2api/ent/group"
"github.com/Wei-Shaw/sub2api/ent/user"
"github.com/Wei-Shaw/sub2api/ent/usersubscription"
)
// UserSubscription is the model entity for the UserSubscription schema.
type UserSubscription struct {
config `json:"-"`
// ID of the ent.
ID int64 `json:"id,omitempty"`
// CreatedAt holds the value of the "created_at" field.
CreatedAt time.Time `json:"created_at,omitempty"`
// UpdatedAt holds the value of the "updated_at" field.
UpdatedAt time.Time `json:"updated_at,omitempty"`
// UserID holds the value of the "user_id" field.
UserID int64 `json:"user_id,omitempty"`
// GroupID holds the value of the "group_id" field.
GroupID int64 `json:"group_id,omitempty"`
// StartsAt holds the value of the "starts_at" field.
StartsAt time.Time `json:"starts_at,omitempty"`
// ExpiresAt holds the value of the "expires_at" field.
ExpiresAt time.Time `json:"expires_at,omitempty"`
// Status holds the value of the "status" field.
Status string `json:"status,omitempty"`
// DailyWindowStart holds the value of the "daily_window_start" field.
DailyWindowStart *time.Time `json:"daily_window_start,omitempty"`
// WeeklyWindowStart holds the value of the "weekly_window_start" field.
WeeklyWindowStart *time.Time `json:"weekly_window_start,omitempty"`
// MonthlyWindowStart holds the value of the "monthly_window_start" field.
MonthlyWindowStart *time.Time `json:"monthly_window_start,omitempty"`
// DailyUsageUsd holds the value of the "daily_usage_usd" field.
DailyUsageUsd float64 `json:"daily_usage_usd,omitempty"`
// WeeklyUsageUsd holds the value of the "weekly_usage_usd" field.
WeeklyUsageUsd float64 `json:"weekly_usage_usd,omitempty"`
// MonthlyUsageUsd holds the value of the "monthly_usage_usd" field.
MonthlyUsageUsd float64 `json:"monthly_usage_usd,omitempty"`
// AssignedBy holds the value of the "assigned_by" field.
AssignedBy *int64 `json:"assigned_by,omitempty"`
// AssignedAt holds the value of the "assigned_at" field.
AssignedAt time.Time `json:"assigned_at,omitempty"`
// Notes holds the value of the "notes" field.
Notes *string `json:"notes,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the UserSubscriptionQuery when eager-loading is set.
Edges UserSubscriptionEdges `json:"edges"`
selectValues sql.SelectValues
}
// UserSubscriptionEdges holds the relations/edges for other nodes in the graph.
type UserSubscriptionEdges struct {
// User holds the value of the user edge.
User *User `json:"user,omitempty"`
// Group holds the value of the group edge.
Group *Group `json:"group,omitempty"`
// AssignedByUser holds the value of the assigned_by_user edge.
AssignedByUser *User `json:"assigned_by_user,omitempty"`
// loadedTypes holds the information for reporting if a
// type was loaded (or requested) in eager-loading or not.
loadedTypes [3]bool
}
// UserOrErr returns the User value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserSubscriptionEdges) UserOrErr() (*User, error) {
if e.User != nil {
return e.User, nil
} else if e.loadedTypes[0] {
return nil, &NotFoundError{label: user.Label}
}
return nil, &NotLoadedError{edge: "user"}
}
// GroupOrErr returns the Group value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserSubscriptionEdges) GroupOrErr() (*Group, error) {
if e.Group != nil {
return e.Group, nil
} else if e.loadedTypes[1] {
return nil, &NotFoundError{label: group.Label}
}
return nil, &NotLoadedError{edge: "group"}
}
// AssignedByUserOrErr returns the AssignedByUser value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserSubscriptionEdges) AssignedByUserOrErr() (*User, error) {
if e.AssignedByUser != nil {
return e.AssignedByUser, nil
} else if e.loadedTypes[2] {
return nil, &NotFoundError{label: user.Label}
}
return nil, &NotLoadedError{edge: "assigned_by_user"}
}
// scanValues returns the types for scanning values from sql.Rows.
func (*UserSubscription) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case usersubscription.FieldDailyUsageUsd, usersubscription.FieldWeeklyUsageUsd, usersubscription.FieldMonthlyUsageUsd:
values[i] = new(sql.NullFloat64)
case usersubscription.FieldID, usersubscription.FieldUserID, usersubscription.FieldGroupID, usersubscription.FieldAssignedBy:
values[i] = new(sql.NullInt64)
case usersubscription.FieldStatus, usersubscription.FieldNotes:
values[i] = new(sql.NullString)
case usersubscription.FieldCreatedAt, usersubscription.FieldUpdatedAt, usersubscription.FieldStartsAt, usersubscription.FieldExpiresAt, usersubscription.FieldDailyWindowStart, usersubscription.FieldWeeklyWindowStart, usersubscription.FieldMonthlyWindowStart, usersubscription.FieldAssignedAt:
values[i] = new(sql.NullTime)
default:
values[i] = new(sql.UnknownType)
}
}
return values, nil
}
// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the UserSubscription fields.
func (_m *UserSubscription) assignValues(columns []string, values []any) error {
if m, n := len(values), len(columns); m < n {
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
}
for i := range columns {
switch columns[i] {
case usersubscription.FieldID:
value, ok := values[i].(*sql.NullInt64)
if !ok {
return fmt.Errorf("unexpected type %T for field id", value)
}
_m.ID = int64(value.Int64)
case usersubscription.FieldCreatedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field created_at", values[i])
} else if value.Valid {
_m.CreatedAt = value.Time
}
case usersubscription.FieldUpdatedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field updated_at", values[i])
} else if value.Valid {
_m.UpdatedAt = value.Time
}
case usersubscription.FieldUserID:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field user_id", values[i])
} else if value.Valid {
_m.UserID = value.Int64
}
case usersubscription.FieldGroupID:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field group_id", values[i])
} else if value.Valid {
_m.GroupID = value.Int64
}
case usersubscription.FieldStartsAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field starts_at", values[i])
} else if value.Valid {
_m.StartsAt = value.Time
}
case usersubscription.FieldExpiresAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field expires_at", values[i])
} else if value.Valid {
_m.ExpiresAt = value.Time
}
case usersubscription.FieldStatus:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field status", values[i])
} else if value.Valid {
_m.Status = value.String
}
case usersubscription.FieldDailyWindowStart:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field daily_window_start", values[i])
} else if value.Valid {
_m.DailyWindowStart = new(time.Time)
*_m.DailyWindowStart = value.Time
}
case usersubscription.FieldWeeklyWindowStart:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field weekly_window_start", values[i])
} else if value.Valid {
_m.WeeklyWindowStart = new(time.Time)
*_m.WeeklyWindowStart = value.Time
}
case usersubscription.FieldMonthlyWindowStart:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field monthly_window_start", values[i])
} else if value.Valid {
_m.MonthlyWindowStart = new(time.Time)
*_m.MonthlyWindowStart = value.Time
}
case usersubscription.FieldDailyUsageUsd:
if value, ok := values[i].(*sql.NullFloat64); !ok {
return fmt.Errorf("unexpected type %T for field daily_usage_usd", values[i])
} else if value.Valid {
_m.DailyUsageUsd = value.Float64
}
case usersubscription.FieldWeeklyUsageUsd:
if value, ok := values[i].(*sql.NullFloat64); !ok {
return fmt.Errorf("unexpected type %T for field weekly_usage_usd", values[i])
} else if value.Valid {
_m.WeeklyUsageUsd = value.Float64
}
case usersubscription.FieldMonthlyUsageUsd:
if value, ok := values[i].(*sql.NullFloat64); !ok {
return fmt.Errorf("unexpected type %T for field monthly_usage_usd", values[i])
} else if value.Valid {
_m.MonthlyUsageUsd = value.Float64
}
case usersubscription.FieldAssignedBy:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field assigned_by", values[i])
} else if value.Valid {
_m.AssignedBy = new(int64)
*_m.AssignedBy = value.Int64
}
case usersubscription.FieldAssignedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field assigned_at", values[i])
} else if value.Valid {
_m.AssignedAt = value.Time
}
case usersubscription.FieldNotes:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field notes", values[i])
} else if value.Valid {
_m.Notes = new(string)
*_m.Notes = value.String
}
default:
_m.selectValues.Set(columns[i], values[i])
}
}
return nil
}
// Value returns the ent.Value that was dynamically selected and assigned to the UserSubscription.
// This includes values selected through modifiers, order, etc.
func (_m *UserSubscription) Value(name string) (ent.Value, error) {
return _m.selectValues.Get(name)
}
// QueryUser queries the "user" edge of the UserSubscription entity.
func (_m *UserSubscription) QueryUser() *UserQuery {
return NewUserSubscriptionClient(_m.config).QueryUser(_m)
}
// QueryGroup queries the "group" edge of the UserSubscription entity.
func (_m *UserSubscription) QueryGroup() *GroupQuery {
return NewUserSubscriptionClient(_m.config).QueryGroup(_m)
}
// QueryAssignedByUser queries the "assigned_by_user" edge of the UserSubscription entity.
func (_m *UserSubscription) QueryAssignedByUser() *UserQuery {
return NewUserSubscriptionClient(_m.config).QueryAssignedByUser(_m)
}
// Update returns a builder for updating this UserSubscription.
// Note that you need to call UserSubscription.Unwrap() before calling this method if this UserSubscription
// was returned from a transaction, and the transaction was committed or rolled back.
func (_m *UserSubscription) Update() *UserSubscriptionUpdateOne {
return NewUserSubscriptionClient(_m.config).UpdateOne(_m)
}
// Unwrap unwraps the UserSubscription entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (_m *UserSubscription) Unwrap() *UserSubscription {
_tx, ok := _m.config.driver.(*txDriver)
if !ok {
panic("ent: UserSubscription is not a transactional entity")
}
_m.config.driver = _tx.drv
return _m
}
// String implements the fmt.Stringer.
func (_m *UserSubscription) String() string {
var builder strings.Builder
builder.WriteString("UserSubscription(")
builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
builder.WriteString("created_at=")
builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("updated_at=")
builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("user_id=")
builder.WriteString(fmt.Sprintf("%v", _m.UserID))
builder.WriteString(", ")
builder.WriteString("group_id=")
builder.WriteString(fmt.Sprintf("%v", _m.GroupID))
builder.WriteString(", ")
builder.WriteString("starts_at=")
builder.WriteString(_m.StartsAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("expires_at=")
builder.WriteString(_m.ExpiresAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("status=")
builder.WriteString(_m.Status)
builder.WriteString(", ")
if v := _m.DailyWindowStart; v != nil {
builder.WriteString("daily_window_start=")
builder.WriteString(v.Format(time.ANSIC))
}
builder.WriteString(", ")
if v := _m.WeeklyWindowStart; v != nil {
builder.WriteString("weekly_window_start=")
builder.WriteString(v.Format(time.ANSIC))
}
builder.WriteString(", ")
if v := _m.MonthlyWindowStart; v != nil {
builder.WriteString("monthly_window_start=")
builder.WriteString(v.Format(time.ANSIC))
}
builder.WriteString(", ")
builder.WriteString("daily_usage_usd=")
builder.WriteString(fmt.Sprintf("%v", _m.DailyUsageUsd))
builder.WriteString(", ")
builder.WriteString("weekly_usage_usd=")
builder.WriteString(fmt.Sprintf("%v", _m.WeeklyUsageUsd))
builder.WriteString(", ")
builder.WriteString("monthly_usage_usd=")
builder.WriteString(fmt.Sprintf("%v", _m.MonthlyUsageUsd))
builder.WriteString(", ")
if v := _m.AssignedBy; v != nil {
builder.WriteString("assigned_by=")
builder.WriteString(fmt.Sprintf("%v", *v))
}
builder.WriteString(", ")
builder.WriteString("assigned_at=")
builder.WriteString(_m.AssignedAt.Format(time.ANSIC))
builder.WriteString(", ")
if v := _m.Notes; v != nil {
builder.WriteString("notes=")
builder.WriteString(*v)
}
builder.WriteByte(')')
return builder.String()
}
// UserSubscriptions is a parsable slice of UserSubscription.
type UserSubscriptions []*UserSubscription