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
4.2 KiB

package db
import (
"Open_IM/pkg/common/config"
2 years ago
"gorm.io/gorm/logger"
3 years ago
"fmt"
"sync"
"time"
3 years ago
2 years ago
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
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() {
//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 {
3 years ago
fmt.Println("0", "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 {
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
}
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{
SlowThreshold: 200 * time.Millisecond, // Slow SQL threshold
2 years ago
LogLevel: logger.Error, // Log level
2 years ago
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: true, // Disable color
},
)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: newLogger,
})
3 years ago
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
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
}
2 years ago
DB.MysqlDB.db = db
return
3 years ago
}
2 years ago
func (m *mysqlDB) DefaultGormDB() *gorm.DB {
return DB.MysqlDB.db
}