fix(数据库): 修复默认分组缺失与迁移锁阻塞

通过迁移补种默认 groups 记录,避免新装空分组
迁移锁改为 try lock + 重试并加入超时
写入 usage_logs 时保留 rate_multiplier=0 语义
测试: go test ./...
This commit is contained in:
yangjianbo
2025-12-29 10:43:46 +08:00
parent 3d617de577
commit 3a7d3387e0
5 changed files with 29 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ package infrastructure
import (
"context"
"database/sql"
"time"
"github.com/Wei-Shaw/sub2api/ent"
"github.com/Wei-Shaw/sub2api/internal/config"
@@ -54,7 +55,9 @@ func InitEnt(cfg *config.Config) (*ent.Client, *sql.DB, error) {
// 确保数据库 schema 已准备就绪。
// SQL 迁移文件是 schema 的权威来源source of truth
// 这种方式比 Ent 的自动迁移更可控,支持复杂的迁移场景。
if err := applyMigrationsFS(context.Background(), drv.DB(), migrations.FS); err != nil {
migrationCtx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
if err := applyMigrationsFS(migrationCtx, drv.DB(), migrations.FS); err != nil {
_ = drv.Close() // 迁移失败时关闭驱动,避免资源泄露
return nil, nil, err
}