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:
2026-10-30 23:48:43 +08:00
parent 07f3166ab7
commit 151faf25fa
9 changed files with 87 additions and 83 deletions

View File

@@ -11,6 +11,7 @@ import (
"testing"
"feedsystem_video_go/internal/middleware/rabbitmq"
"feedsystem_video_go/internal/notification"
amqp "github.com/rabbitmq/amqp091-go"
"gorm.io/driver/mysql"
@@ -25,10 +26,10 @@ import (
type fakeHub struct {
mu sync.Mutex
pushes []*Notification
pushes []*notification.Notification
}
func (h *fakeHub) Push(userID uint, n *Notification) {
func (h *fakeHub) Push(userID uint, n *notification.Notification) {
h.mu.Lock()
defer h.mu.Unlock()
h.pushes = append(h.pushes, n)
@@ -62,10 +63,10 @@ func newWorkerTestDB(t *testing.T) *gorm.DB {
if err != nil {
t.Fatalf("connect mysql: %v", err)
}
if err := db.Migrator().DropTable(&Notification{}, &testVideoRow{}); err != nil {
if err := db.Migrator().DropTable(&notification.Notification{}, &testVideoRow{}); err != nil {
t.Fatalf("drop tables: %v", err)
}
if err := db.AutoMigrate(&testVideoRow{}, &Notification{}); err != nil {
if err := db.AutoMigrate(&testVideoRow{}, &notification.Notification{}); err != nil {
t.Fatalf("migrate: %v", err)
}
return db
@@ -104,7 +105,7 @@ func TestNotificationWorker_DuplicateDeliveryWritesOneRow(t *testing.T) {
}
var count int64
db.Model(&Notification{}).Count(&count)
db.Model(&notification.Notification{}).Count(&count)
if count != 1 {
t.Fatalf("notifications 行数 = %d, want 1(同一意图键必须去重)", count)
}
@@ -132,7 +133,7 @@ func TestNotificationWorker_MissingEventIDDoesNotDedupe(t *testing.T) {
}
var count int64
db.Model(&Notification{}).Count(&count)
db.Model(&notification.Notification{}).Count(&count)
if count != 2 {
t.Fatalf("notifications 行数 = %d, want 2(无意图键时不判重)", count)
}
@@ -151,7 +152,7 @@ func TestNotificationWorker_SelfActionWritesNothing(t *testing.T) {
t.Fatalf("process: %v", err)
}
var count int64
db.Model(&Notification{}).Count(&count)
db.Model(&notification.Notification{}).Count(&count)
if count != 0 {
t.Fatalf("notifications 行数 = %d, want 0", count)
}
@@ -176,7 +177,7 @@ func TestNotificationWorker_RewrittenRoutingKeyStillWorks(t *testing.T) {
}
var count int64
db.Model(&Notification{}).Count(&count)
db.Model(&notification.Notification{}).Count(&count)
if count != 1 {
t.Fatalf("notifications 行数 = %d, want 1(routing key 被改写不应影响分派)", count)
}