Commit ec87f39d authored by shaw's avatar shaw
Browse files

feat: 从 gorm 迁移到 ent (#92)

## 主要变更

- 将 ORM 从 GORM 迁移到 Ent
- 使用 SQL 文件迁移替代 GORM AutoMigrate
- 新增迁移运行器支持分布式锁和校验和验证
- 优化 Repository 层查询,新增轻量级存在性检查方法
- 新增完整的单元测试覆盖删除操作

## 迁移优势

- 类型安全与编译期校验
- 关系建模更清晰(Edge/Through)
- 查询一致性更好
- 迁移可控(SQL 文件作为唯一事实来源)
- 可维护性提升

## 新增迁移文件

- 005_schema_parity.sql: 字段对齐
- 006_fix_invalid_subscription_expires_at.sql: 修复过期时间
- 007_add_user_allowed_groups.sql: 用户允许分组表
- 008_seed_default_group.sql: 默认分组种子
- 009_fix_usage_logs_cache_columns.sql: 缓存列修复
parents fb883f00 3d296d88
This diff is collapsed.
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"github.com/Wei-Shaw/sub2api/ent/accountgroup"
"github.com/Wei-Shaw/sub2api/ent/predicate"
)
// AccountGroupDelete is the builder for deleting a AccountGroup entity.
type AccountGroupDelete struct {
config
hooks []Hook
mutation *AccountGroupMutation
}
// Where appends a list predicates to the AccountGroupDelete builder.
func (_d *AccountGroupDelete) Where(ps ...predicate.AccountGroup) *AccountGroupDelete {
_d.mutation.Where(ps...)
return _d
}
// Exec executes the deletion query and returns how many vertices were deleted.
func (_d *AccountGroupDelete) Exec(ctx context.Context) (int, error) {
return withHooks(ctx, _d.sqlExec, _d.mutation, _d.hooks)
}
// ExecX is like Exec, but panics if an error occurs.
func (_d *AccountGroupDelete) ExecX(ctx context.Context) int {
n, err := _d.Exec(ctx)
if err != nil {
panic(err)
}
return n
}
func (_d *AccountGroupDelete) sqlExec(ctx context.Context) (int, error) {
_spec := sqlgraph.NewDeleteSpec(accountgroup.Table, nil)
if ps := _d.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
affected, err := sqlgraph.DeleteNodes(ctx, _d.driver, _spec)
if err != nil && sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
_d.mutation.done = true
return affected, err
}
// AccountGroupDeleteOne is the builder for deleting a single AccountGroup entity.
type AccountGroupDeleteOne struct {
_d *AccountGroupDelete
}
// Where appends a list predicates to the AccountGroupDelete builder.
func (_d *AccountGroupDeleteOne) Where(ps ...predicate.AccountGroup) *AccountGroupDeleteOne {
_d._d.mutation.Where(ps...)
return _d
}
// Exec executes the deletion query.
func (_d *AccountGroupDeleteOne) Exec(ctx context.Context) error {
n, err := _d._d.Exec(ctx)
switch {
case err != nil:
return err
case n == 0:
return &NotFoundError{accountgroup.Label}
default:
return nil
}
}
// ExecX is like Exec, but panics if an error occurs.
func (_d *AccountGroupDeleteOne) ExecX(ctx context.Context) {
if err := _d.Exec(ctx); err != nil {
panic(err)
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
package ent
import "entgo.io/ent/dialect"
// Driver 暴露底层 driver,供需要 raw SQL 的集成层使用。
func (c *Client) Driver() dialect.Driver {
return c.driver
}
This diff is collapsed.
This diff is collapsed.
package ent
// 启用 sql/execquery 以生成 ExecContext/QueryContext 的透传接口,便于事务内执行原生 SQL。
//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature sql/upsert,intercept,sql/execquery --idtype int64 ./schema
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment