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

152 lines
4.6 KiB

package db
import (
"Open_IM/pkg/common/config"
"fmt"
"time"
3 years ago
2 years ago
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type mysqlDB struct {
2 years ago
//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() {
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)
if err != nil {
time.Sleep(time.Duration(30) * time.Second)
2 years ago
db, err1 = gorm.Open(mysql.Open(dsn), nil)
if err1 != nil {
panic(err1.Error() + " open failed " + dsn)
}
}
2 years ago
3 years ago
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName)
3 years ago
err = db.Exec(sql).Error
if err != nil {
panic(err.Error() + " Exec failed " + sql)
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 {
panic(err.Error() + " Open failed " + dsn)
3 years ago
}
2 years ago
sqlDB, err := db.DB()
if err != nil {
panic(err.Error() + " db.DB() failed ")
}
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
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{}) {
db.Migrator().CreateTable(&Friend{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&FriendRequest{}) {
db.Migrator().CreateTable(&FriendRequest{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&Group{}) {
db.Migrator().CreateTable(&Group{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&GroupMember{}) {
db.Migrator().CreateTable(&GroupMember{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&GroupRequest{}) {
db.Migrator().CreateTable(&GroupRequest{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&User{}) {
db.Migrator().CreateTable(&User{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&Black{}) {
db.Migrator().CreateTable(&Black{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&ChatLog{}) {
db.Migrator().CreateTable(&ChatLog{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&Register{}) {
db.Migrator().CreateTable(&Register{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&Conversation{}) {
db.Migrator().CreateTable(&Conversation{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&Department{}) {
db.Migrator().CreateTable(&Department{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&OrganizationUser{}) {
db.Migrator().CreateTable(&OrganizationUser{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&DepartmentMember{}) {
db.Migrator().CreateTable(&DepartmentMember{})
3 years ago
}
2 years ago
if !db.Migrator().HasTable(&AppVersion{}) {
db.Migrator().CreateTable(&AppVersion{})
3 years ago
}
if !db.Migrator().HasTable(&BlackList{}) {
db.Migrator().CreateTable(&BlackList{})
}
if !db.Migrator().HasTable(&IpLimit{}) {
db.Migrator().CreateTable(&IpLimit{})
}
if !db.Migrator().HasTable(&UserIpLimit{}) {
db.Migrator().CreateTable(&UserIpLimit{})
}
2 years ago
if !db.Migrator().HasTable(&RegisterAddFriend{}) {
db.Migrator().CreateTable(&RegisterAddFriend{})
}
if !db.Migrator().HasTable(&Invitation{}) {
db.Migrator().CreateTable(&Invitation{})
}
if !db.Migrator().HasTable(&ClientInitConfig{}) {
db.Migrator().CreateTable(&ClientInitConfig{})
}
2 years ago
if !db.Migrator().HasTable(&UserIpRecord{}) {
db.Migrator().CreateTable(&UserIpRecord{})
}
2 years ago
DB.MysqlDB.db = db
3 years ago
}
2 years ago
func (m *mysqlDB) DefaultGormDB() *gorm.DB {
return DB.MysqlDB.db
}