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

168 lines
4.4 KiB

package db
import (
"Open_IM/pkg/common/config"
3 years ago
"fmt"
"sync"
"time"
3 years ago
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
type mysqlDB struct {
sync.RWMutex
dbMap map[string]*gorm.DB
}
3 years ago
func initMysqlDB() {
//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
3 years ago
db, err := gorm.Open("mysql", dsn)
if err != nil {
3 years ago
fmt.Println("0", "Open failed ", err.Error(), dsn)
3 years ago
}
if err != nil {
time.Sleep(time.Duration(30) * time.Second)
db, err1 = gorm.Open("mysql", dsn)
if err1 != nil {
3 years ago
fmt.Println("0", "Open failed ", err1.Error(), dsn)
panic(err1.Error())
}
}
3 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)
3 years ago
err = db.Exec(sql).Error
if err != nil {
3 years ago
fmt.Println("0", "Exec failed ", err.Error(), sql)
panic(err.Error())
3 years ago
}
db.Close()
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)
3 years ago
db, err = gorm.Open("mysql", dsn)
if err != nil {
3 years ago
fmt.Println("0", "Open failed ", err.Error(), dsn)
panic(err.Error())
3 years ago
}
3 years ago
fmt.Println("open db ok ", dsn)
2 years ago
db.AutoMigrate(
&Register{},
&Friend{},
3 years ago
&FriendRequest{},
&Group{},
&GroupMember{},
&GroupRequest{},
3 years ago
&User{},
3 years ago
&Black{}, &ChatLog{}, &Register{}, &Conversation{}, &AppVersion{}, &Department{})
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
3 years ago
if !db.HasTable(&Friend{}) {
3 years ago
fmt.Println("CreateTable Friend")
3 years ago
db.CreateTable(&Friend{})
}
if !db.HasTable(&FriendRequest{}) {
3 years ago
fmt.Println("CreateTable FriendRequest")
3 years ago
db.CreateTable(&FriendRequest{})
}
if !db.HasTable(&Group{}) {
3 years ago
fmt.Println("CreateTable Group")
3 years ago
db.CreateTable(&Group{})
}
if !db.HasTable(&GroupMember{}) {
3 years ago
fmt.Println("CreateTable GroupMember")
3 years ago
db.CreateTable(&GroupMember{})
}
if !db.HasTable(&GroupRequest{}) {
3 years ago
fmt.Println("CreateTable GroupRequest")
3 years ago
db.CreateTable(&GroupRequest{})
}
if !db.HasTable(&User{}) {
3 years ago
fmt.Println("CreateTable User")
3 years ago
db.CreateTable(&User{})
}
if !db.HasTable(&Black{}) {
3 years ago
fmt.Println("CreateTable Black")
3 years ago
db.CreateTable(&Black{})
}
3 years ago
if !db.HasTable(&ChatLog{}) {
fmt.Println("CreateTable ChatLog")
3 years ago
db.CreateTable(&ChatLog{})
}
if !db.HasTable(&Register{}) {
fmt.Println("CreateTable Register")
3 years ago
db.CreateTable(&Register{})
}
3 years ago
if !db.HasTable(&Conversation{}) {
fmt.Println("CreateTable Conversation")
3 years ago
db.CreateTable(&Conversation{})
}
3 years ago
3 years ago
if !db.HasTable(&Department{}) {
3 years ago
fmt.Println("CreateTable Department")
db.CreateTable(&Department{})
}
3 years ago
if !db.HasTable(&OrganizationUser{}) {
3 years ago
fmt.Println("CreateTable OrganizationUser")
db.CreateTable(&OrganizationUser{})
}
3 years ago
if !db.HasTable(&DepartmentMember{}) {
3 years ago
fmt.Println("CreateTable DepartmentMember")
db.CreateTable(&DepartmentMember{})
}
3 years ago
if !db.HasTable(&AppVersion{}) {
fmt.Println("CreateTable DepartmentMember")
db.CreateTable(&AppVersion{})
}
return
3 years ago
}
func (m *mysqlDB) DefaultGormDB() (*gorm.DB, error) {
return m.GormDB(config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
}
func (m *mysqlDB) GormDB(dbAddress, dbName string) (*gorm.DB, error) {
m.Lock()
defer m.Unlock()
k := key(dbAddress, dbName)
if _, ok := m.dbMap[k]; !ok {
if err := m.open(dbAddress, dbName); err != nil {
return nil, err
}
}
return m.dbMap[k], nil
}
func (m *mysqlDB) open(dbAddress, dbName string) error {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, dbAddress, dbName)
db, err := gorm.Open("mysql", dsn)
if err != nil {
return err
}
db.SingularTable(true)
db.DB().SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns)
db.DB().SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns)
db.DB().SetConnMaxLifetime(time.Duration(config.Config.Mysql.DBMaxLifeTime) * time.Second)
if m.dbMap == nil {
m.dbMap = make(map[string]*gorm.DB)
}
k := key(dbAddress, dbName)
m.dbMap[k] = db
return nil
}