4fd44380aa
所有数据库映射结构体收敛到统一基类,清理混乱。
- store/base.go:BaseModel{ID string(雪花,bwmarrin/snowflake) PK, CreatedAt, UpdatedAt,
DeletedAt gorm软删index} + BeforeCreate 生成 id + NewID()。
- 全模型嵌入 BaseModel:User/Task/LLMModel/KB/Doc/Agent(去掉各自 ID uint/CreatedAt);
Task 业务 id(task_xxx)挪到 TaskID 唯一列,主键统一雪花。
- 模型 id uint→string:admin :id 路由、SetActiveModel/DeleteModel/SaveModel、modelBody.ID。
- 一次性迁移 migrateLegacyIntIDs:检测旧整型 id(AutoMigrate 不改主键类型)→ 备份
sundynix_model 行(唯一不可再生的 API 密钥)→ 删旧表 → 按新规约重建 → 回灌模型(新雪花 id)。
其它表(User/Task/KB/Doc/Agent)为可重建测试数据,重置。
- Doc 预埋 Size/Preview/ObjectKey 字段,DocLink 表(为后续 B/C)。
验证:重启 gateway → 日志"回灌 2 条模型配置";PG sundynix_model.id=varchar、有
created_at/updated_at/deleted_at;DeepSeek/百炼 密钥保留(keylen 35);admin 列表返回
雪花 string id + 脱敏 key;健康五灯全绿。gateway build 通过。
注:mcp-go 的 sundynix_user_profile(Profile) 模型尚未套同规约,待跟进对齐。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package store
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/bwmarrin/snowflake"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// 雪花 ID 生成器(单节点;多实例部署应按机器分配 node id)。
|
|
var sfNode *snowflake.Node
|
|
|
|
func init() {
|
|
n, err := snowflake.NewNode(1)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
sfNode = n
|
|
}
|
|
|
|
// NewID 生成字符串型雪花 ID(全库主键统一用它)。
|
|
func NewID() string { return sfNode.Generate().String() }
|
|
|
|
// BaseModel 是所有数据库映射结构体的基础字段:
|
|
// 字符串雪花主键 + 创建/更新时间 + GORM 软删除索引(deleted_at)。
|
|
// 约定:每个模型都嵌入它,不再各自声明 ID/CreatedAt/UpdatedAt。
|
|
type BaseModel struct {
|
|
ID string `gorm:"primaryKey;size:24" json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
}
|
|
|
|
// BeforeCreate 为空 ID 的新记录分配雪花 ID(嵌入后各模型自动具备)。
|
|
func (b *BaseModel) BeforeCreate(*gorm.DB) error {
|
|
if b.ID == "" {
|
|
b.ID = NewID()
|
|
}
|
|
return nil
|
|
}
|