Files
YouduWiki/backend/migration/fns/0004_update_node_status_unreleased.go
2026-05-21 19:52:45 +08:00

37 lines
1.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package fns
import (
"gorm.io/gorm"
"github.com/chaitin/panda-wiki/domain"
"github.com/chaitin/panda-wiki/log"
)
type MigrationUpdateNodeStatusUnreleased struct {
Name string
logger *log.Logger
}
func NewMigrationUpdateNodeStatusUnreleased(logger *log.Logger) *MigrationUpdateNodeStatusUnreleased {
return &MigrationUpdateNodeStatusUnreleased{
Name: "0004_update_node_status_unreleased",
logger: logger,
}
}
func (m *MigrationUpdateNodeStatusUnreleased) Execute(tx *gorm.DB) error {
// 将所有 status=1 (Draft) 且从未发布过的节点更新为 status=0 (Unreleased)
// 判断条件node_releases 表中不存在该 node_id 的记录
result := tx.Model(&domain.Node{}).
Where("status = ?", domain.NodeStatusDraft).
Where("id NOT IN (SELECT DISTINCT node_id FROM node_releases)").
Update("status", domain.NodeStatusUnreleased)
if result.Error != nil {
return result.Error
}
m.logger.Info("migration update node status unreleased", log.Int64("affected_rows", result.RowsAffected))
return nil
}