refactor(store): DB 规约统一 —— 雪花字符串 id + created/updated/deleted_at 软删
所有数据库映射结构体收敛到统一基类,清理混乱。
- 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>
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -18,7 +17,7 @@ import (
|
||||
// 表 sundynix_model 由 Gateway 持有;Dispatcher 经 NATS 取激活配置。
|
||||
|
||||
type modelBody struct {
|
||||
ID uint `json:"id"`
|
||||
ID string `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
Provider string `json:"provider"`
|
||||
BaseURL string `json:"base_url"`
|
||||
@@ -58,7 +57,7 @@ func (h *Handler) SaveModel(c *gin.Context) {
|
||||
if kind == "" {
|
||||
kind = contract.ConfigKindChat
|
||||
}
|
||||
m := &store.LLMModel{ID: b.ID, Kind: kind, Provider: provider, BaseURL: b.BaseURL, APIKey: b.APIKey, Model: b.Model}
|
||||
m := &store.LLMModel{BaseModel: store.BaseModel{ID: b.ID}, Kind: kind, Provider: provider, BaseURL: b.BaseURL, APIKey: b.APIKey, Model: b.Model}
|
||||
if err := h.db.SaveModel(c.Request.Context(), m); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -69,8 +68,8 @@ func (h *Handler) SaveModel(c *gin.Context) {
|
||||
|
||||
// SetActiveModel: POST /api/v1/admin/models/:id/active —— 设为激活并广播。
|
||||
func (h *Handler) SetActiveModel(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
if err := h.db.SetActiveModel(c.Request.Context(), uint(id)); err != nil {
|
||||
id := c.Param("id")
|
||||
if err := h.db.SetActiveModel(c.Request.Context(), id); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -80,8 +79,8 @@ func (h *Handler) SetActiveModel(c *gin.Context) {
|
||||
|
||||
// DeleteModel: DELETE /api/v1/admin/models/:id
|
||||
func (h *Handler) DeleteModel(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
if err := h.db.DeleteModel(c.Request.Context(), uint(id)); err != nil {
|
||||
id := c.Param("id")
|
||||
if err := h.db.DeleteModel(c.Request.Context(), id); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -98,7 +97,7 @@ func (h *Handler) TestModel(c *gin.Context) {
|
||||
}
|
||||
// 若传了已存的 id 但未带 key,用库里的真实 key。
|
||||
key := b.APIKey
|
||||
if key == "" && b.ID != 0 {
|
||||
if key == "" && b.ID != "" {
|
||||
if rows, _ := h.db.ListModels(c.Request.Context(), ""); rows != nil {
|
||||
for _, m := range rows {
|
||||
if m.ID == b.ID {
|
||||
|
||||
Reference in New Issue
Block a user