bindbox-game/scripts/add_is_granted_col.go

39 lines
903 B
Go

package main
import (
"bindbox-game/configs"
"bindbox-game/internal/repository/mysql"
"flag"
"fmt"
"os"
)
func main() {
flag.Parse()
configs.Init()
repo, err := mysql.New()
if err != nil {
fmt.Printf("DB Error: %v\n", err)
os.Exit(1)
}
db := repo.GetDbW()
// 添加 is_granted 字段
sql := "ALTER TABLE livestream_draw_logs ADD COLUMN is_granted TINYINT(1) DEFAULT 0 COMMENT '是否已发放奖品' AFTER created_at"
// 检查列是否存在
var count int64
db.Raw("SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'livestream_draw_logs' AND COLUMN_NAME = 'is_granted'").Scan(&count)
if count == 0 {
if err := db.Exec(sql).Error; err != nil {
fmt.Printf("Failed to add column: %v\n", err)
os.Exit(1)
}
fmt.Println("SUCCESS: Added is_granted column")
} else {
fmt.Println("Column is_granted already exists")
}
}