move out db migtration to separate function

This commit is contained in:
Akulij 2025-03-20 03:05:18 +07:00
parent 17e63b59fa
commit ccc98b4298

View File

@ -47,19 +47,32 @@ type BotController struct {
updates tgbotapi.UpdatesChannel updates tgbotapi.UpdatesChannel
} }
func DBMigrate() (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
return db, err
}
db.AutoMigrate(&User{})
db.AutoMigrate(&BotContent{})
return db, err
}
func GetBotController() BotController { func GetBotController() BotController {
cfg := config.GetConfig() cfg := config.GetConfig()
fmt.Printf("Token value: '%v'\n", cfg.BotToken) fmt.Printf("Token value: '%v'\n", cfg.BotToken)
fmt.Printf("Admin password: '%v'\n", cfg.AdminPass) fmt.Printf("Admin password: '%v'\n", cfg.AdminPass)
bot, err := tgbotapi.NewBotAPI(cfg.BotToken) bot, err := tgbotapi.NewBotAPI(cfg.BotToken)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) db, err := DBMigrate()
db.AutoMigrate(&User{}) if err != nil {
db.AutoMigrate(&BotContent{}) log.Panic(err)
}
bot.Debug = true bot.Debug = true