You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/mysql.go

196 lines
5.8 KiB

package db
import (
"Open_IM/pkg/common/config"
"fmt"
"sync"
"time"
3 years ago
2 years ago
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type mysqlDB struct {
sync.RWMutex
2 years ago
db *gorm.DB
}
2 years ago
type Writer struct{}
func (w Writer) Printf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
3 years ago
func initMysqlDB() {
2 years ago
fmt.Println("init mysqlDB start")
//When there is no open IM database, connect to the mysql built-in database to create openIM database
3 years ago
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
var err1 error
2 years ago
db, err := gorm.Open(mysql.Open(dsn), nil)
3 years ago
if err != nil {
2 years ago
fmt.Println("Open failed ", err.Error(), dsn)
3 years ago
}
if err != nil {
time.Sleep(time.Duration(30) * time.Second)
2 years ago
db, err1 = gorm.Open(mysql.Open(dsn), nil)
if err1 != nil {
2 years ago
fmt.Println("Open failed ", err1.Error(), dsn)
panic(err1.Error())
}
}
2 years ago
3 years ago
//Check the database and table during initialization
3 years ago
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName)
2 years ago
fmt.Println("exec sql: ", sql, " begin")
3 years ago
err = db.Exec(sql).Error
if err != nil {
2 years ago
fmt.Println("Exec failed ", err.Error(), sql)
panic(err.Error())
3 years ago
}
2 years ago
fmt.Println("exec sql: ", sql, " end")
3 years ago
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
3 years ago
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
2 years ago
newLogger := logger.New(
Writer{},
logger.Config{
2 years ago
SlowThreshold: time.Duration(config.Config.Mysql.SlowThreshold) * time.Millisecond, // Slow SQL threshold
LogLevel: logger.LogLevel(config.Config.Mysql.LogLevel), // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: true, // Disable color
2 years ago
},
)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: newLogger,
})
3 years ago
if err != nil {
2 years ago
fmt.Println("Open failed ", err.Error(), dsn)
panic(err.Error())
3 years ago
}
2 years ago
sqlDB, err := db.DB()
if err != nil {
panic(err.Error())
}
2 years ago
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.DBMaxLifeTime))
sqlDB.SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns)
sqlDB.SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns)
2 years ago
fmt.Println("open mysql ok ", dsn)
2 years ago
db.AutoMigrate(
&Register{},
&Friend{},
3 years ago
&FriendRequest{},
&Group{},
&GroupMember{},
&GroupRequest{},
3 years ago
&User{},
&Black{}, &ChatLog{}, &Register{}, &Conversation{}, &AppVersion{}, &Department{}, &BlackList{}, &IpLimit{}, &UserIpLimit{}, &Invitation{}, &RegisterAddFriend{},
2 years ago
&ClientInitConfig{}, &UserIpRecord{})
3 years ago
db.Set("gorm:table_options", "CHARSET=utf8")
3 years ago
db.Set("gorm:table_options", "collation=utf8_unicode_ci")
3 years ago
2 years ago
if !db.Migrator().HasTable(&Friend{}) {
3 years ago
fmt.Println("CreateTable Friend")
2 years ago
db.Migrator().CreateTable(&Friend{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&FriendRequest{}) {
3 years ago
fmt.Println("CreateTable FriendRequest")
2 years ago
db.Migrator().CreateTable(&FriendRequest{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&Group{}) {
3 years ago
fmt.Println("CreateTable Group")
2 years ago
db.Migrator().CreateTable(&Group{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&GroupMember{}) {
3 years ago
fmt.Println("CreateTable GroupMember")
2 years ago
db.Migrator().CreateTable(&GroupMember{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&GroupRequest{}) {
3 years ago
fmt.Println("CreateTable GroupRequest")
2 years ago
db.Migrator().CreateTable(&GroupRequest{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&User{}) {
3 years ago
fmt.Println("CreateTable User")
2 years ago
db.Migrator().CreateTable(&User{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&Black{}) {
3 years ago
fmt.Println("CreateTable Black")
2 years ago
db.Migrator().CreateTable(&Black{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&ChatLog{}) {
fmt.Println("CreateTable ChatLog")
2 years ago
db.Migrator().CreateTable(&ChatLog{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&Register{}) {
fmt.Println("CreateTable Register")
2 years ago
db.Migrator().CreateTable(&Register{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&Conversation{}) {
fmt.Println("CreateTable Conversation")
2 years ago
db.Migrator().CreateTable(&Conversation{})
3 years ago
}
3 years ago
2 years ago
if !db.Migrator().HasTable(&Department{}) {
3 years ago
fmt.Println("CreateTable Department")
2 years ago
db.Migrator().CreateTable(&Department{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&OrganizationUser{}) {
3 years ago
fmt.Println("CreateTable OrganizationUser")
2 years ago
db.Migrator().CreateTable(&OrganizationUser{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&DepartmentMember{}) {
3 years ago
fmt.Println("CreateTable DepartmentMember")
2 years ago
db.Migrator().CreateTable(&DepartmentMember{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&AppVersion{}) {
3 years ago
fmt.Println("CreateTable DepartmentMember")
2 years ago
db.Migrator().CreateTable(&AppVersion{})
3 years ago
}
if !db.Migrator().HasTable(&BlackList{}) {
fmt.Println("CreateTable BlackList")
db.Migrator().CreateTable(&BlackList{})
}
if !db.Migrator().HasTable(&IpLimit{}) {
fmt.Println("CreateTable IpLimit")
db.Migrator().CreateTable(&IpLimit{})
}
if !db.Migrator().HasTable(&UserIpLimit{}) {
fmt.Println("CreateTable UserIpLimit")
db.Migrator().CreateTable(&UserIpLimit{})
}
2 years ago
if !db.Migrator().HasTable(&RegisterAddFriend{}) {
fmt.Println("CreateTable RegisterAddFriend")
db.Migrator().CreateTable(&RegisterAddFriend{})
}
if !db.Migrator().HasTable(&Invitation{}) {
fmt.Println("CreateTable Invitation")
db.Migrator().CreateTable(&Invitation{})
}
if !db.Migrator().HasTable(&ClientInitConfig{}) {
fmt.Println("CreateTable ClientInitConfig")
db.Migrator().CreateTable(&ClientInitConfig{})
}
2 years ago
if !db.Migrator().HasTable(&UserIpRecord{}) {
fmt.Println("CreateTable Friend")
db.Migrator().CreateTable(&UserIpRecord{})
}
2 years ago
DB.MysqlDB.db = db
return
3 years ago
}
2 years ago
func (m *mysqlDB) DefaultGormDB() *gorm.DB {
return DB.MysqlDB.db
}