fix: 默认板块仅在空库播种,改名后重启不再复活

This commit is contained in:
2026-09-27 04:42:46 +08:00
parent 372bc29b41
commit 7d8c79f61f
2 changed files with 52 additions and 7 deletions

View File

@@ -382,20 +382,23 @@ func backfillBoardDefaults(db *gorm.DB) {
}
}
// seedDefaultBoards 写入默认板块
// seedDefaultBoards 仅在板块表为空(全新站点首启)时写入默认板块。
// 曾按「名字是否存在」补种:站长改名板块后每次重启都会复活一套默认板块
// (真实事故:0.2.1 重启后默认 4 板块重现)。表里已有任何未删除板块即视为已初始化。
func seedDefaultBoards(db *gorm.DB) {
var count int64
db.Model(&Board{}).Count(&count)
if count > 0 {
return
}
defaults := []Board{
{Name: "综合讨论", Description: "什么都可以聊", Icon: "message-circle", SortOrder: 1, PostPolicy: BoardPostPolicyAll, Visible: true},
{Name: "技术分享", Description: "分享技术心得与问题", Icon: "code", SortOrder: 2, PostPolicy: BoardPostPolicyAll, Visible: true},
{Name: "问答求助", Description: "提问与解答", Icon: "help-circle", SortOrder: 3, PostPolicy: BoardPostPolicyAll, Visible: true},
{Name: "闲聊灌水", Description: "轻松闲聊", Icon: "coffee", SortOrder: 4, PostPolicy: BoardPostPolicyAll, Visible: true},
}
for _, b := range defaults {
var count int64
db.Model(&Board{}).Where("name = ?", b.Name).Count(&count)
if count == 0 {
_ = db.Create(&b).Error
}
for i := range defaults {
_ = db.Create(&defaults[i]).Error
}
}