MossChat_Backend/db/init.go

28 lines
477 B
Go
Raw Normal View History

2024-06-16 02:08:50 +08:00
package db
import (
"MossChat/cache"
"MossChat/config"
"MossChat/logger"
"MossChat/models"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
func InitDatabase() {
cache.InitRedis()
db, err := gorm.Open(mysql.Open(config.MYSQL_CONFIG), &gorm.Config{})
if err != nil {
logger.Log.Println("无法打开数据库!")
panic(err)
}
err = db.AutoMigrate(&models.User{})
if err != nil {
logger.Log.Println("迁移数据库失败!")
panic(err)
}
DB = db
}