refactor(notification): 通知实体抽成独立包
`video` 与 `worker` 各有一份 `notifications` 表的实体。 两份定义会漂移,所以合并到 `internal/notification`。 新增 `entity.go` 保存唯一一份列标签。 `worker`、`ssehub`、`comment_repo`、`db` 与两个集成测试改为引用新包。 `db` 包不再 import `worker`。 去重键仍是 `*string`,因此 NULL 才能共存。 验证:go test -count=1 ./... 全绿;集成测试三包全绿。
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"feedsystem_video_go/internal/notification"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
@@ -119,17 +121,17 @@ func (r *CommentRepository) GetByID(ctx context.Context, id uint) (*Comment, err
|
||||
|
||||
// CreateMentions 幂等写入提及通知:DedupeKey 相同则静默跳过。
|
||||
//
|
||||
// 显式写 CreatedAt 而不依赖 autoCreateTime:这条路径写的是"列投影"而不是
|
||||
// worker 包的实体,autoCreateTime 只在 GORM 认为字段是零值时才生效——一旦
|
||||
// 有人在调用前给 CreatedAt 赋了零值以外的值,两种来源就会打架(P2-16 ①)。
|
||||
// 显式写 CreatedAt 而不依赖 autoCreateTime:这一批行共享同一个时间戳,便于
|
||||
// 按"同一次评论"检索;autoCreateTime 只在字段为零值时才生效,依赖它会让
|
||||
// 同一批写入拿到毫秒级不同的时间(P2-16 ①)。
|
||||
func (r *CommentRepository) CreateMentions(ctx context.Context, mentions []Mention) error {
|
||||
if len(mentions) == 0 {
|
||||
return nil
|
||||
}
|
||||
now := time.Now()
|
||||
rows := make([]NotificationRow, 0, len(mentions))
|
||||
rows := make([]notification.Notification, 0, len(mentions))
|
||||
for _, m := range mentions {
|
||||
rows = append(rows, NotificationRow{
|
||||
rows = append(rows, notification.Notification{
|
||||
RecipientID: m.RecipientID,
|
||||
SenderID: m.SenderID,
|
||||
Type: "mention",
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"feedsystem_video_go/internal/notification"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
@@ -49,10 +51,10 @@ func newTestDB(t *testing.T) *gorm.DB {
|
||||
if err != nil {
|
||||
t.Fatalf("connect mysql: %v", err)
|
||||
}
|
||||
if err := db.Migrator().DropTable(&Comment{}, &Video{}, &NotificationRow{}, &testAccount{}); err != nil {
|
||||
if err := db.Migrator().DropTable(&Comment{}, &Video{}, ¬ification.Notification{}, &testAccount{}); err != nil {
|
||||
t.Fatalf("drop tables: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&testAccount{}, &Video{}, &Comment{}, &NotificationRow{}); err != nil {
|
||||
if err := db.AutoMigrate(&testAccount{}, &Video{}, &Comment{}, ¬ification.Notification{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
return db
|
||||
@@ -208,7 +210,7 @@ func TestP0_4_MentionNotificationDedupe(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var rows []NotificationRow
|
||||
var rows []notification.Notification
|
||||
if err := db.Find(&rows).Error; err != nil {
|
||||
t.Fatalf("查询 notifications: %v", err)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package video
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Mention 是一条"提及通知"的写入意图。
|
||||
@@ -19,31 +18,6 @@ type Mention struct {
|
||||
DedupeKey string
|
||||
}
|
||||
|
||||
// NotificationRow 是 notifications 表在 video 包里的投影。
|
||||
//
|
||||
// 为什么不直接 import worker.Notification:worker 包 import 了 video
|
||||
// (CommentWorker 依赖 CommentStore),反向 import 会成环。这张投影只需要
|
||||
// "写一行通知"所需的字段,schema 的权威定义仍在 worker.Notification,
|
||||
// 因此下面每个列标签都必须与它逐字一致——不一致时 AutoMigrate 会试图改动
|
||||
// 生产列类型,那是本方案最不希望引入的意外。
|
||||
type NotificationRow struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
RecipientID uint `gorm:"index;not null"`
|
||||
SenderID uint `gorm:"not null"`
|
||||
Type string `gorm:"type:varchar(50);not null"`
|
||||
TargetID uint `gorm:"column:target_id"`
|
||||
Content string `gorm:"type:varchar(255)"`
|
||||
IsRead bool `gorm:"default:false"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
|
||||
// DedupeKey 与 worker.Notification.DedupeKey 指向同一个唯一索引
|
||||
// uk_notifications_dedupe。为 nil 时不参与判重(MySQL 不约束 NULL),
|
||||
// 这正是"没有意图键的旧路径"仍然能写入的原因。
|
||||
DedupeKey *string `gorm:"column:dedupe_key;type:varchar(128);uniqueIndex:uk_notifications_dedupe"`
|
||||
}
|
||||
|
||||
func (NotificationRow) TableName() string { return "notifications" }
|
||||
|
||||
// CommentStore 是 CommentService 与 CommentWorker 依赖的窄接口。
|
||||
//
|
||||
// 抽接口不是为了"面向接口编程",而是为了给"判重与写入必须发生在同一条 SQL
|
||||
|
||||
Reference in New Issue
Block a user