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

153 lines
5.5 KiB

package db
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
"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(
&im_mysql_model.Register{},
&im_mysql_model.Friend{},
&im_mysql_model.FriendRequest{},
&im_mysql_model.Group{},
&im_mysql_model.GroupMember{},
&im_mysql_model.GroupRequest{},
&im_mysql_model.User{},
&im_mysql_model.Black{}, &im_mysql_model.ChatLog{}, &im_mysql_model.Register{}, &im_mysql_model.Conversation{}, &im_mysql_model.AppVersion{}, &im_mysql_model.Department{}, &im_mysql_model.BlackList{}, &im_mysql_model.IpLimit{}, &im_mysql_model.UserIpLimit{}, &im_mysql_model.Invitation{}, &im_mysql_model.RegisterAddFriend{},
&im_mysql_model.ClientInitConfig{}, &im_mysql_model.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
if !db.Migrator().HasTable(&im_mysql_model.Friend{}) {
db.Migrator().CreateTable(&im_mysql_model.Friend{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.FriendRequest{}) {
db.Migrator().CreateTable(&im_mysql_model.FriendRequest{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.Group{}) {
db.Migrator().CreateTable(&im_mysql_model.Group{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.GroupMember{}) {
db.Migrator().CreateTable(&im_mysql_model.GroupMember{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.GroupRequest{}) {
db.Migrator().CreateTable(&im_mysql_model.GroupRequest{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.User{}) {
db.Migrator().CreateTable(&im_mysql_model.User{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.Black{}) {
db.Migrator().CreateTable(&im_mysql_model.Black{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.ChatLog{}) {
db.Migrator().CreateTable(&im_mysql_model.ChatLog{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.Register{}) {
db.Migrator().CreateTable(&im_mysql_model.Register{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.Conversation{}) {
db.Migrator().CreateTable(&im_mysql_model.Conversation{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.Department{}) {
db.Migrator().CreateTable(&im_mysql_model.Department{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.OrganizationUser{}) {
db.Migrator().CreateTable(&im_mysql_model.OrganizationUser{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.DepartmentMember{}) {
db.Migrator().CreateTable(&im_mysql_model.DepartmentMember{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.AppVersion{}) {
db.Migrator().CreateTable(&im_mysql_model.AppVersion{})
3 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.BlackList{}) {
db.Migrator().CreateTable(&im_mysql_model.BlackList{})
}
if !db.Migrator().HasTable(&im_mysql_model.IpLimit{}) {
db.Migrator().CreateTable(&im_mysql_model.IpLimit{})
}
if !db.Migrator().HasTable(&im_mysql_model.UserIpLimit{}) {
db.Migrator().CreateTable(&im_mysql_model.UserIpLimit{})
}
if !db.Migrator().HasTable(&im_mysql_model.RegisterAddFriend{}) {
db.Migrator().CreateTable(&im_mysql_model.RegisterAddFriend{})
2 years ago
}
if !db.Migrator().HasTable(&im_mysql_model.Invitation{}) {
db.Migrator().CreateTable(&im_mysql_model.Invitation{})
}
if !db.Migrator().HasTable(&im_mysql_model.ClientInitConfig{}) {
db.Migrator().CreateTable(&im_mysql_model.ClientInitConfig{})
}
if !db.Migrator().HasTable(&im_mysql_model.UserIpRecord{}) {
db.Migrator().CreateTable(&im_mysql_model.UserIpRecord{})
2 years ago
}
2 years ago
DB.MysqlDB.db = db
3 years ago
}
2 years ago
func (m *mysqlDB) DefaultGormDB() *gorm.DB {
return DB.MysqlDB.db
}