|
|
@ -22,37 +22,26 @@ func (w Writer) Printf(format string, args ...interface{}) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func initMysqlDB() {
|
|
|
|
func initMysqlDB() {
|
|
|
|
fmt.Println("init mysqlDB start")
|
|
|
|
|
|
|
|
//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",
|
|
|
|
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")
|
|
|
|
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], "mysql")
|
|
|
|
var db *gorm.DB
|
|
|
|
var db *gorm.DB
|
|
|
|
var err1 error
|
|
|
|
var err1 error
|
|
|
|
db, err := gorm.Open(mysql.Open(dsn), nil)
|
|
|
|
db, err := gorm.Open(mysql.Open(dsn), nil)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Println("Open failed ", err.Error(), dsn)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(time.Duration(30) * time.Second)
|
|
|
|
time.Sleep(time.Duration(30) * time.Second)
|
|
|
|
db, err1 = gorm.Open(mysql.Open(dsn), nil)
|
|
|
|
db, err1 = gorm.Open(mysql.Open(dsn), nil)
|
|
|
|
if err1 != nil {
|
|
|
|
if err1 != nil {
|
|
|
|
fmt.Println("Open failed ", err1.Error(), dsn)
|
|
|
|
panic(err1.Error() + " open failed " + 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)
|
|
|
|
fmt.Println("exec sql: ", sql, " begin")
|
|
|
|
|
|
|
|
err = db.Exec(sql).Error
|
|
|
|
err = db.Exec(sql).Error
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Exec failed ", err.Error(), sql)
|
|
|
|
panic(err.Error() + " Exec failed " + sql)
|
|
|
|
panic(err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("exec sql: ", sql, " end")
|
|
|
|
|
|
|
|
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], config.Config.Mysql.DBDatabaseName)
|
|
|
|
|
|
|
|
|
|
|
|
newLogger := logger.New(
|
|
|
|
newLogger := logger.New(
|
|
|
|
Writer{},
|
|
|
|
Writer{},
|
|
|
|
logger.Config{
|
|
|
|
logger.Config{
|
|
|
@ -66,20 +55,18 @@ func initMysqlDB() {
|
|
|
|
Logger: newLogger,
|
|
|
|
Logger: newLogger,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Open failed ", err.Error(), dsn)
|
|
|
|
panic(err.Error() + " Open failed " + dsn)
|
|
|
|
panic(err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sqlDB, err := db.DB()
|
|
|
|
sqlDB, err := db.DB()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
panic(err.Error() + " db.DB() failed ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.DBMaxLifeTime))
|
|
|
|
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.DBMaxLifeTime))
|
|
|
|
sqlDB.SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns)
|
|
|
|
sqlDB.SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns)
|
|
|
|
sqlDB.SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns)
|
|
|
|
sqlDB.SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns)
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Println("open mysql ok ", dsn)
|
|
|
|
|
|
|
|
db.AutoMigrate(
|
|
|
|
db.AutoMigrate(
|
|
|
|
&Register{},
|
|
|
|
&Register{},
|
|
|
|
&Friend{},
|
|
|
|
&Friend{},
|
|
|
@ -94,99 +81,69 @@ func initMysqlDB() {
|
|
|
|
db.Set("gorm:table_options", "collation=utf8_unicode_ci")
|
|
|
|
db.Set("gorm:table_options", "collation=utf8_unicode_ci")
|
|
|
|
|
|
|
|
|
|
|
|
if !db.Migrator().HasTable(&Friend{}) {
|
|
|
|
if !db.Migrator().HasTable(&Friend{}) {
|
|
|
|
fmt.Println("CreateTable Friend")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&Friend{})
|
|
|
|
db.Migrator().CreateTable(&Friend{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !db.Migrator().HasTable(&FriendRequest{}) {
|
|
|
|
if !db.Migrator().HasTable(&FriendRequest{}) {
|
|
|
|
fmt.Println("CreateTable FriendRequest")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&FriendRequest{})
|
|
|
|
db.Migrator().CreateTable(&FriendRequest{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !db.Migrator().HasTable(&Group{}) {
|
|
|
|
if !db.Migrator().HasTable(&Group{}) {
|
|
|
|
fmt.Println("CreateTable Group")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&Group{})
|
|
|
|
db.Migrator().CreateTable(&Group{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !db.Migrator().HasTable(&GroupMember{}) {
|
|
|
|
if !db.Migrator().HasTable(&GroupMember{}) {
|
|
|
|
fmt.Println("CreateTable GroupMember")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&GroupMember{})
|
|
|
|
db.Migrator().CreateTable(&GroupMember{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&GroupRequest{}) {
|
|
|
|
if !db.Migrator().HasTable(&GroupRequest{}) {
|
|
|
|
fmt.Println("CreateTable GroupRequest")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&GroupRequest{})
|
|
|
|
db.Migrator().CreateTable(&GroupRequest{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&User{}) {
|
|
|
|
if !db.Migrator().HasTable(&User{}) {
|
|
|
|
fmt.Println("CreateTable User")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&User{})
|
|
|
|
db.Migrator().CreateTable(&User{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&Black{}) {
|
|
|
|
if !db.Migrator().HasTable(&Black{}) {
|
|
|
|
fmt.Println("CreateTable Black")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&Black{})
|
|
|
|
db.Migrator().CreateTable(&Black{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&ChatLog{}) {
|
|
|
|
if !db.Migrator().HasTable(&ChatLog{}) {
|
|
|
|
fmt.Println("CreateTable ChatLog")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&ChatLog{})
|
|
|
|
db.Migrator().CreateTable(&ChatLog{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&Register{}) {
|
|
|
|
if !db.Migrator().HasTable(&Register{}) {
|
|
|
|
fmt.Println("CreateTable Register")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&Register{})
|
|
|
|
db.Migrator().CreateTable(&Register{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&Conversation{}) {
|
|
|
|
if !db.Migrator().HasTable(&Conversation{}) {
|
|
|
|
fmt.Println("CreateTable Conversation")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&Conversation{})
|
|
|
|
db.Migrator().CreateTable(&Conversation{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !db.Migrator().HasTable(&Department{}) {
|
|
|
|
if !db.Migrator().HasTable(&Department{}) {
|
|
|
|
fmt.Println("CreateTable Department")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&Department{})
|
|
|
|
db.Migrator().CreateTable(&Department{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&OrganizationUser{}) {
|
|
|
|
if !db.Migrator().HasTable(&OrganizationUser{}) {
|
|
|
|
fmt.Println("CreateTable OrganizationUser")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&OrganizationUser{})
|
|
|
|
db.Migrator().CreateTable(&OrganizationUser{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&DepartmentMember{}) {
|
|
|
|
if !db.Migrator().HasTable(&DepartmentMember{}) {
|
|
|
|
fmt.Println("CreateTable DepartmentMember")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&DepartmentMember{})
|
|
|
|
db.Migrator().CreateTable(&DepartmentMember{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&AppVersion{}) {
|
|
|
|
if !db.Migrator().HasTable(&AppVersion{}) {
|
|
|
|
fmt.Println("CreateTable DepartmentMember")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&AppVersion{})
|
|
|
|
db.Migrator().CreateTable(&AppVersion{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&BlackList{}) {
|
|
|
|
if !db.Migrator().HasTable(&BlackList{}) {
|
|
|
|
fmt.Println("CreateTable BlackList")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&BlackList{})
|
|
|
|
db.Migrator().CreateTable(&BlackList{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&IpLimit{}) {
|
|
|
|
if !db.Migrator().HasTable(&IpLimit{}) {
|
|
|
|
fmt.Println("CreateTable IpLimit")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&IpLimit{})
|
|
|
|
db.Migrator().CreateTable(&IpLimit{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&UserIpLimit{}) {
|
|
|
|
if !db.Migrator().HasTable(&UserIpLimit{}) {
|
|
|
|
fmt.Println("CreateTable UserIpLimit")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&UserIpLimit{})
|
|
|
|
db.Migrator().CreateTable(&UserIpLimit{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !db.Migrator().HasTable(&RegisterAddFriend{}) {
|
|
|
|
if !db.Migrator().HasTable(&RegisterAddFriend{}) {
|
|
|
|
fmt.Println("CreateTable RegisterAddFriend")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&RegisterAddFriend{})
|
|
|
|
db.Migrator().CreateTable(&RegisterAddFriend{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !db.Migrator().HasTable(&Invitation{}) {
|
|
|
|
if !db.Migrator().HasTable(&Invitation{}) {
|
|
|
|
fmt.Println("CreateTable Invitation")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&Invitation{})
|
|
|
|
db.Migrator().CreateTable(&Invitation{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !db.Migrator().HasTable(&ClientInitConfig{}) {
|
|
|
|
if !db.Migrator().HasTable(&ClientInitConfig{}) {
|
|
|
|
fmt.Println("CreateTable ClientInitConfig")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&ClientInitConfig{})
|
|
|
|
db.Migrator().CreateTable(&ClientInitConfig{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !db.Migrator().HasTable(&UserIpRecord{}) {
|
|
|
|
if !db.Migrator().HasTable(&UserIpRecord{}) {
|
|
|
|
fmt.Println("CreateTable Friend")
|
|
|
|
|
|
|
|
db.Migrator().CreateTable(&UserIpRecord{})
|
|
|
|
db.Migrator().CreateTable(&UserIpRecord{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DB.MysqlDB.db = db
|
|
|
|
DB.MysqlDB.db = db
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *mysqlDB) DefaultGormDB() *gorm.DB {
|
|
|
|
func (m *mysqlDB) DefaultGormDB() *gorm.DB {
|
|
|
|