Merge remote-tracking branch 'origin/tuoyun' into tuoyun

pull/141/head
wenxu12345 3 years ago
commit 7970a0b3b4

@ -17,29 +17,23 @@ type mysqlDB struct {
func initMysqlDB() { func initMysqlDB() {
//When there is no open IM database, connect to the mysql built-in database to create openIM database //When there is no open IM database, connect to the mysql built-in database to create openIM database
//dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
// config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], "mysql")
var db *gorm.DB
//db, err := gorm.Open("mysql", dsn)
//if err != nil {
// log.NewError("0", "Open failed ", err.Error(), dsn)
//}
//if err != nil {
// time.Sleep(time.Duration(30) * time.Second)
// db, err1 = gorm.Open("mysql", dsn)
// if err1 != nil {
// log.NewError("0", "Open failed ", err1.Error(), dsn)
// panic(err1.Error())
// }
//}
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName) config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], "mysql")
var db *gorm.DB
var err1 error
db, err := gorm.Open("mysql", dsn) db, err := gorm.Open("mysql", dsn)
if err != nil { if err != nil {
log.NewError("0", "Open failed ", err.Error(), dsn) log.NewError("0", "Open failed ", err.Error(), dsn)
panic(err.Error())
} }
if err != nil {
time.Sleep(time.Duration(30) * time.Second)
db, err1 = gorm.Open("mysql", dsn)
if err1 != nil {
log.NewError("0", "Open failed ", err1.Error(), dsn)
panic(err1.Error())
}
}
//Check the database and table during initialization //Check the database and table during initialization
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName) sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName)
err = db.Exec(sql).Error err = db.Exec(sql).Error
@ -49,6 +43,14 @@ func initMysqlDB() {
} }
db.Close() db.Close()
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
db, err = gorm.Open("mysql", dsn)
if err != nil {
log.NewError("0", "Open failed ", err.Error(), dsn)
panic(err.Error())
}
log.NewInfo("open db ok ", dsn) log.NewInfo("open db ok ", dsn)
db.AutoMigrate(&Friend{}, db.AutoMigrate(&Friend{},
&FriendRequest{}, &FriendRequest{},
@ -94,10 +96,7 @@ func initMysqlDB() {
log.NewInfo("CreateTable Black") log.NewInfo("CreateTable Black")
db.CreateTable(&Black{}) db.CreateTable(&Black{})
} }
if !db.HasTable(&ChatLog{}) {
log.NewInfo("CreateTable Black")
db.CreateTable(&ChatLog{})
}
return return
sqlTable := "CREATE TABLE IF NOT EXISTS `user` (" + sqlTable := "CREATE TABLE IF NOT EXISTS `user` (" +

Loading…
Cancel
Save