| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- // +build ignore
- package main
- import (
- _ "app/commons/config"
- "app/commons/core"
- "app/commons/model/entity"
- "fmt"
- "gorm.io/gorm"
- )
- func main() {
- fmt.Println("=== 数据库迁移工具 ===")
- // 获取数据库连接
- db := core.MainDb()
- currentDB := db.Migrator().CurrentDatabase()
- fmt.Printf("当前数据库: %s\n", currentDB)
- // 定义要迁移的表
- tables := []interface{}{
- // Telegram 红包模块
- &entity.TgRedPacket{},
- &entity.TgRedPacketRecord{},
- &entity.TgUserBind{},
- &entity.TgRedPacketConfig{},
- }
- // 执行迁移
- for _, table := range tables {
- if err := migrateTable(db, table); err != nil {
- fmt.Printf("❌ 迁移失败: %v\n", err)
- return
- }
- }
- fmt.Println("✅ 数据库迁移完成")
- }
- func migrateTable(db *gorm.DB, model interface{}) error {
- tableName := getTableName(model)
- fmt.Printf("正在迁移表: %s\n", tableName)
- if err := db.AutoMigrate(model); err != nil {
- return err
- }
- fmt.Printf("✓ %s 迁移成功\n", tableName)
- return nil
- }
- func getTableName(model interface{}) string {
- if t, ok := model.(interface{ TableName() string }); ok {
- return t.TableName()
- }
- return "unknown"
- }
|