From 84a21970273384fbb62d8717a197ee8af4a80aea Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Thu, 10 Feb 2022 11:57:03 +0800 Subject: [PATCH 1/2] db --- pkg/common/db/mysql.go | 45 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/pkg/common/db/mysql.go b/pkg/common/db/mysql.go index bb5823860..ba38f57e9 100644 --- a/pkg/common/db/mysql.go +++ b/pkg/common/db/mysql.go @@ -17,31 +17,25 @@ type mysqlDB struct { func initMysqlDB() { //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", - 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) if err != nil { 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 - 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+"test1") err = db.Exec(sql).Error if err != nil { log.NewError("0", "Exec failed ", err.Error(), sql) @@ -49,6 +43,14 @@ func initMysqlDB() { } 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) db.AutoMigrate(&Friend{}, &FriendRequest{}, @@ -94,10 +96,7 @@ func initMysqlDB() { log.NewInfo("CreateTable Black") db.CreateTable(&Black{}) } - if !db.HasTable(&ChatLog{}) { - log.NewInfo("CreateTable Black") - db.CreateTable(&ChatLog{}) - } + return sqlTable := "CREATE TABLE IF NOT EXISTS `user` (" + From e02ca2f7e363ee2d4f5602a59313f08306aa9eb1 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Thu, 10 Feb 2022 12:01:00 +0800 Subject: [PATCH 2/2] db --- pkg/common/db/mysql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/common/db/mysql.go b/pkg/common/db/mysql.go index ba38f57e9..712ba5b2d 100644 --- a/pkg/common/db/mysql.go +++ b/pkg/common/db/mysql.go @@ -35,7 +35,7 @@ func initMysqlDB() { } //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+"test1") + 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 if err != nil { log.NewError("0", "Exec failed ", err.Error(), sql)