28 lines
477 B
Go
28 lines
477 B
Go
|
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
|
||
|
}
|