refactor(mcp-go): Profile 套雪花id+软删规约,补集成测试(还清 DB 规约欠债)
项目级 DB 规约(雪花字符串 id + created_at/updated_at + 软删索引)此前只在网关落地, mcp-go 的 sundynix_user_profile(Profile) 仍是旧复合主键 (user_id,key)、无 id/时间戳—— 本次对齐,全库统一。 - 新增 BaseModel + NewID(snowflake node=2,与网关 node=1 区分,避免共享 PG 中 id 冲突)。 - Profile 嵌 BaseModel:雪花 id 主键 + (user_id,key) 唯一索引。 - Upsert 改 OnConflict((user_id,key)),冲突覆盖 value 且保留原 id/created_at。 - 一次性迁移 migrateLegacyProfile:检测旧表(无 id 列)→ 备份偏好 → drop → 按新规约重建 → 回灌。 - 加 mcp-go 首个集成测试(store_test.go,gated on MEMORY_TEST_DSN): · TestBaseModelID 纯单测(id 非空/不重/BeforeCreate 补id不覆盖); · Integration:迁移后字段齐全 + Upsert 同键覆盖不新增 + id 稳定 + Get 渲染; · LegacyMigration:旧复合主键表 + 1 条偏好 → 迁移后 id 列存在、数据保留补新 id。 实测(docker postgres):三测全过,迁移日志确认旧偏好回灌保留 ✓。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -8,17 +8,46 @@ import (
|
||||
"log"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/snowflake"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// sfNode 是本服务的雪花 ID 生成器。node=2 与网关(node=1)区分,避免共享 PG 中 ID 冲突。
|
||||
var sfNode *snowflake.Node
|
||||
|
||||
func init() { sfNode, _ = snowflake.NewNode(2) }
|
||||
|
||||
// NewID 生成字符串型雪花 ID(项目级 DB 规约:主键统一用它)。
|
||||
func NewID() string { return sfNode.Generate().String() }
|
||||
|
||||
// BaseModel 是所有 DB 映射结构体的基础字段(项目级规约):
|
||||
// 字符串雪花 ID 主键 + 创建/更新时间 + GORM 软删(带索引)。与网关 store.BaseModel 同规约。
|
||||
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。
|
||||
func (b *BaseModel) BeforeCreate(*gorm.DB) error {
|
||||
if b.ID == "" {
|
||||
b.ID = NewID()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Profile 是用户常驻画像的一条 key/value 偏好(always-on memory)。
|
||||
// 复合主键 (user_id, key):同一用户同一键 upsert 覆盖。
|
||||
// 套用 BaseModel 规约:雪花 ID 主键;(user_id, key) 唯一索引 —— 同一用户同一键 upsert 覆盖。
|
||||
type Profile struct {
|
||||
UserID string `gorm:"primaryKey;column:user_id;size:64"`
|
||||
Key string `gorm:"primaryKey;size:64"`
|
||||
BaseModel
|
||||
UserID string `gorm:"column:user_id;size:64;uniqueIndex:idx_profile_uk"`
|
||||
Key string `gorm:"size:64;uniqueIndex:idx_profile_uk"`
|
||||
Value string `gorm:"type:text"`
|
||||
}
|
||||
|
||||
@@ -37,14 +66,46 @@ func Open(dsn string) *Store {
|
||||
log.Printf("[memory] postgres 不可用,记忆降级(召回为空): %v", err)
|
||||
return &Store{}
|
||||
}
|
||||
// 一次性迁移:旧表用复合主键 (user_id,key) 无 id/时间戳,与雪花规约不兼容。
|
||||
migrateLegacyProfile(db)
|
||||
if err := db.AutoMigrate(&Profile{}); err != nil {
|
||||
log.Printf("[memory] AutoMigrate 失败,记忆降级: %v", err)
|
||||
return &Store{}
|
||||
}
|
||||
log.Println("[memory] postgres connected, migrated sundynix_user_profile")
|
||||
log.Println("[memory] postgres connected, migrated sundynix_user_profile (雪花 id + 软删 规约)")
|
||||
return &Store{db: db}
|
||||
}
|
||||
|
||||
// migrateLegacyProfile 检测旧 Profile 表(无 id 列)则备份偏好 → 重建为雪花规约 → 回灌。
|
||||
func migrateLegacyProfile(db *gorm.DB) {
|
||||
m := db.Migrator()
|
||||
if !m.HasTable("sundynix_user_profile") {
|
||||
return // 全新库,AutoMigrate 直接建新表
|
||||
}
|
||||
if m.HasColumn(&Profile{}, "id") {
|
||||
return // 已是新规约
|
||||
}
|
||||
log.Println("[memory] 检测到旧 Profile 表(复合主键无 id),迁移到雪花规约(保留偏好)")
|
||||
var saved []struct {
|
||||
UserID string `gorm:"column:user_id"`
|
||||
Key string `gorm:"column:key"`
|
||||
Value string `gorm:"column:value"`
|
||||
}
|
||||
db.Raw(`SELECT user_id, "key", value FROM sundynix_user_profile`).Scan(&saved)
|
||||
if err := m.DropTable("sundynix_user_profile"); err != nil {
|
||||
log.Printf("[memory] 迁移 drop 旧表失败: %v", err)
|
||||
return
|
||||
}
|
||||
if err := db.AutoMigrate(&Profile{}); err != nil {
|
||||
log.Printf("[memory] 迁移建新表失败: %v", err)
|
||||
return
|
||||
}
|
||||
for _, r := range saved {
|
||||
_ = db.Create(&Profile{UserID: r.UserID, Key: r.Key, Value: r.Value}).Error
|
||||
}
|
||||
log.Printf("[memory] 已回灌 %d 条偏好(新雪花 id)", len(saved))
|
||||
}
|
||||
|
||||
// Get 返回某用户的画像,渲染为可直接注入 prompt 的多行文本(按 key 排序,稳定输出)。
|
||||
func (s *Store) Get(ctx context.Context, userID string) (string, error) {
|
||||
if s.db == nil || userID == "" {
|
||||
@@ -62,12 +123,15 @@ func (s *Store) Get(ctx context.Context, userID string) (string, error) {
|
||||
return strings.TrimRight(b.String(), "\n"), nil
|
||||
}
|
||||
|
||||
// Upsert 写入/更新一条画像偏好(key 冲突即覆盖 value)。
|
||||
// Upsert 写入/更新一条画像偏好((user_id,key) 冲突即覆盖 value,保留原 id)。
|
||||
func (s *Store) Upsert(ctx context.Context, userID, key, value string) error {
|
||||
if s.db == nil {
|
||||
return fmt.Errorf("memory store disabled")
|
||||
}
|
||||
return s.db.WithContext(ctx).Save(&Profile{UserID: userID, Key: key, Value: value}).Error
|
||||
return s.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "user_id"}, {Name: "key"}},
|
||||
DoUpdates: clause.Assignments(map[string]any{"value": value, "updated_at": time.Now()}),
|
||||
}).Create(&Profile{UserID: userID, Key: key, Value: value}).Error
|
||||
}
|
||||
|
||||
// Close 释放连接。
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// TestBaseModelID 验证雪花 ID 规约:NewID 非空且不重复;BeforeCreate 补 ID、已有则保留。
|
||||
func TestBaseModelID(t *testing.T) {
|
||||
a, b := NewID(), NewID()
|
||||
if a == "" || b == "" {
|
||||
t.Fatal("NewID 不应为空")
|
||||
}
|
||||
if a == b {
|
||||
t.Errorf("两次 NewID 应不同: %s", a)
|
||||
}
|
||||
var m BaseModel
|
||||
if err := m.BeforeCreate(nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m.ID == "" {
|
||||
t.Error("BeforeCreate 应补 ID")
|
||||
}
|
||||
m.ID = "fixed"
|
||||
_ = m.BeforeCreate(nil)
|
||||
if m.ID != "fixed" {
|
||||
t.Error("BeforeCreate 不应覆盖已有 ID")
|
||||
}
|
||||
}
|
||||
|
||||
// TestProfileStore_Integration 跑真实 Postgres(设 MEMORY_TEST_DSN 才执行):
|
||||
// 验证迁移后表含雪花规约字段、Upsert (user_id,key) 冲突覆盖而非新增、Get 渲染。
|
||||
func TestProfileStore_Integration(t *testing.T) {
|
||||
dsn := os.Getenv("MEMORY_TEST_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("设 MEMORY_TEST_DSN 启用 Postgres 集成测试")
|
||||
}
|
||||
raw, err := gorm.Open(postgres.New(postgres.Config{DSN: dsn}), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
if err != nil {
|
||||
t.Fatalf("连接 Postgres: %v", err)
|
||||
}
|
||||
_ = raw.Migrator().DropTable("sundynix_user_profile") // 干净起点
|
||||
|
||||
s := Open(dsn) // 触发迁移 + AutoMigrate
|
||||
if s.db == nil {
|
||||
t.Fatal("Store 不应降级")
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
// 新规约字段齐全。
|
||||
for _, col := range []string{"id", "created_at", "updated_at", "deleted_at"} {
|
||||
if !raw.Migrator().HasColumn(&Profile{}, col) {
|
||||
t.Errorf("表应含规约字段 %s", col)
|
||||
}
|
||||
}
|
||||
|
||||
// Upsert 两次同 (user_id,key) → 覆盖,不新增;id 稳定。
|
||||
if err := s.Upsert(ctx, "u1", "城市", "北京"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var first Profile
|
||||
raw.Where("user_id = ? AND key = ?", "u1", "城市").First(&first)
|
||||
if first.ID == "" || first.CreatedAt.IsZero() {
|
||||
t.Error("行应有雪花 id 与创建时间")
|
||||
}
|
||||
if err := s.Upsert(ctx, "u1", "城市", "上海"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var cnt int64
|
||||
raw.Model(&Profile{}).Where("user_id = ?", "u1").Count(&cnt)
|
||||
if cnt != 1 {
|
||||
t.Errorf("同键 upsert 应覆盖,期望 1 行,得 %d", cnt)
|
||||
}
|
||||
var after Profile
|
||||
raw.Where("user_id = ? AND key = ?", "u1", "城市").First(&after)
|
||||
if after.Value != "上海" || after.ID != first.ID {
|
||||
t.Errorf("应覆盖 value 且保留 id: value=%s id=%s/%s", after.Value, after.ID, first.ID)
|
||||
}
|
||||
|
||||
// Get 渲染多行(按 key 排序)。
|
||||
_ = s.Upsert(ctx, "u1", "爱好", "围棋")
|
||||
got, _ := s.Get(ctx, "u1")
|
||||
if got == "" || got != "- 城市:上海\n- 爱好:围棋" {
|
||||
t.Errorf("Get 渲染不符: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestProfileStore_LegacyMigration 验证旧复合主键表(无 id)→ 雪花规约表的迁移保留数据。
|
||||
func TestProfileStore_LegacyMigration(t *testing.T) {
|
||||
dsn := os.Getenv("MEMORY_TEST_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("设 MEMORY_TEST_DSN 启用 Postgres 集成测试")
|
||||
}
|
||||
raw, err := gorm.Open(postgres.New(postgres.Config{DSN: dsn}), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
if err != nil {
|
||||
t.Fatalf("连接 Postgres: %v", err)
|
||||
}
|
||||
// 造一个旧 schema 表(复合主键、无 id/时间戳)并塞一条偏好。
|
||||
raw.Migrator().DropTable("sundynix_user_profile")
|
||||
if err := raw.Exec(`CREATE TABLE sundynix_user_profile (user_id varchar(64), "key" varchar(64), value text, PRIMARY KEY(user_id, "key"))`).Error; err != nil {
|
||||
t.Fatalf("建旧表: %v", err)
|
||||
}
|
||||
raw.Exec(`INSERT INTO sundynix_user_profile (user_id, "key", value) VALUES ('legacy', '语言', 'Go')`)
|
||||
|
||||
s := Open(dsn) // 应触发 migrateLegacyProfile:备份→重建→回灌
|
||||
if s.db == nil {
|
||||
t.Fatal("Store 不应降级")
|
||||
}
|
||||
if !raw.Migrator().HasColumn(&Profile{}, "id") {
|
||||
t.Error("迁移后应有 id 列")
|
||||
}
|
||||
var p Profile
|
||||
if err := raw.Where("user_id = ? AND key = ?", "legacy", "语言").First(&p).Error; err != nil {
|
||||
t.Fatalf("迁移后旧数据应保留: %v", err)
|
||||
}
|
||||
if p.Value != "Go" || p.ID == "" {
|
||||
t.Errorf("旧偏好应保留且补新 id: value=%s id=%s", p.Value, p.ID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user