Files
Message-Push-Nest/migrate/migrate.go
T

92 lines
2.0 KiB
Go
Raw Normal View History

2024-01-07 21:30:59 +08:00
package migrate
import (
"errors"
"fmt"
2024-01-13 12:04:40 +08:00
"github.com/sirupsen/logrus"
2024-04-29 17:31:13 +08:00
"gorm.io/gorm"
2024-01-07 21:30:59 +08:00
"message-nest/models"
2024-01-17 23:15:47 +08:00
"message-nest/service/settings_service"
2024-01-07 21:30:59 +08:00
)
2024-01-17 23:15:47 +08:00
// 初始化admin账户
2024-01-07 21:30:59 +08:00
func InitAuthTableData() {
initSection := "init"
initAuthKey := "account"
initAccount := "admin"
initAccountPasswd := "123456"
settingO, err := models.GetSettingByKey(initSection, initAuthKey)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
2024-01-13 12:04:40 +08:00
logrus.Error(fmt.Sprintf("查询账号初始化失败!"))
2024-01-07 21:30:59 +08:00
return
}
if settingO.Value == "1" {
// 已经初始化过
return
}
err = models.AddUser(initAccount, initAccountPasswd)
if err != nil {
2024-01-13 12:04:40 +08:00
logrus.Error(fmt.Sprintf("添加初始化admin账号失败!"))
2024-01-07 21:30:59 +08:00
return
} else {
2024-01-13 12:04:40 +08:00
logrus.Error(fmt.Sprintf("初始化admin账号成功!您的账号:%s 密码:%s", initAccount, initAccountPasswd))
2024-01-07 21:30:59 +08:00
}
err = models.AddOneSetting(models.Settings{Section: initSection, Key: initAuthKey, Value: "1"})
if err != nil {
2024-01-13 12:04:40 +08:00
logrus.Error(fmt.Sprintf("标记admin账号初始化状态失败!"))
2024-01-07 21:30:59 +08:00
return
}
}
func Setup() {
db := models.Setup()
2024-04-29 17:31:13 +08:00
//defer func(db *gorm.DB) {
// err := db.Close()
// if err != nil {
//
// }
//}(db)
2024-01-07 21:30:59 +08:00
2024-04-29 17:31:13 +08:00
//if setting.AppSetting.InitData != "enable" {
// return
//}
2024-01-13 13:43:30 +08:00
entry := logrus.WithFields(logrus.Fields{
"prefix": "[Init Data]",
})
tables := []interface{}{
2024-01-07 21:30:59 +08:00
&models.Auth{},
&models.SendTasks{},
&models.SendWays{},
&models.SendTasksLogs{},
&models.SendTasksIns{},
&models.Settings{},
2024-04-11 17:20:36 +08:00
&models.CronMessages{},
2024-01-13 13:43:30 +08:00
}
2024-01-07 21:30:59 +08:00
2024-01-13 13:43:30 +08:00
for _, table := range tables {
2024-04-29 17:31:13 +08:00
//tableName := db.NewScope(table).TableName()
tableName := models.GetSchema(table)
2024-01-13 13:43:30 +08:00
entry.Infof("Migrate table: %s", tableName)
2024-04-29 17:31:13 +08:00
err := db.AutoMigrate(table)
if err != nil {
entry.Infof("Migrate table erorr: %s", err.Error())
}
2024-01-13 13:43:30 +08:00
}
entry.Infof("Init Account data...")
2024-01-07 21:30:59 +08:00
InitAuthTableData()
2024-01-17 23:15:47 +08:00
entry.Infof("Init Custom Site data...")
ss := settings_service.InitSettingService{}
ss.InitSiteConfig()
entry.Infof("Init Cron data...")
ss.InitLogConfig()
2024-01-13 13:43:30 +08:00
entry.Infof("All table data init done.")
2024-01-07 21:30:59 +08:00
}