Browse Source

Merge pull request #527 from songzhibin97/gva_gormv2_dev

perf:优化初始化流程
main
奇淼(piexlmax 3 years ago
committed by GitHub
parent
commit
377b6721d1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      server/api/v1/sys_initdb.go
  2. 33
      server/service/sys_initdb.go

3
server/api/v1/sys_initdb.go

@ -5,6 +5,7 @@ import (
"gin-vue-admin/model/request" "gin-vue-admin/model/request"
"gin-vue-admin/model/response" "gin-vue-admin/model/response"
"gin-vue-admin/service" "gin-vue-admin/service"
"go.uber.org/zap" "go.uber.org/zap"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -30,7 +31,7 @@ func InitDB(c *gin.Context) {
} }
if err := service.InitDB(dbInfo); err != nil { if err := service.InitDB(dbInfo); err != nil {
global.GVA_LOG.Error("自动创建数据库失败!", zap.Any("err", err)) global.GVA_LOG.Error("自动创建数据库失败!", zap.Any("err", err))
response.FailWithMessage("自动创建数据库失败,请查看后台日志", c)
response.FailWithMessage("自动创建数据库失败,请查看后台日志,检查后在进行初始化", c)
return return
} }
response.OkWithData("自动创建数据库成功", c) response.OkWithData("自动创建数据库成功", c)

33
server/service/sys_initdb.go

@ -9,10 +9,11 @@ import (
"gin-vue-admin/model/request" "gin-vue-admin/model/request"
"gin-vue-admin/source" "gin-vue-admin/source"
"gin-vue-admin/utils" "gin-vue-admin/utils"
"path/filepath"
"github.com/spf13/viper" "github.com/spf13/viper"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/gorm" "gorm.io/gorm"
"path/filepath"
) )
//@author: [songzhibin97](https://github.com/songzhibin97) //@author: [songzhibin97](https://github.com/songzhibin97)
@ -71,13 +72,6 @@ func initDB(InitDBFunctions ...model.InitDBFunc) (err error) {
//@return: error //@return: error
func InitDB(conf request.InitDB) error { func InitDB(conf request.InitDB) error {
BaseMysql := config.Mysql{
Path: "",
Dbname: "",
Username: "",
Password: "",
Config: "charset=utf8mb4&parseTime=True&loc=Local",
}
if conf.Host == "" { if conf.Host == "" {
conf.Host = "127.0.0.1" conf.Host = "127.0.0.1"
@ -100,15 +94,11 @@ func InitDB(conf request.InitDB) error {
Config: "charset=utf8mb4&parseTime=True&loc=Local", Config: "charset=utf8mb4&parseTime=True&loc=Local",
} }
if err := writeConfig(global.GVA_VP, MysqlConfig); err != nil {
return err
}
m := global.GVA_CONFIG.Mysql
if m.Dbname == "" {
if MysqlConfig.Dbname == "" {
return nil return nil
} }
linkDns := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
linkDns := MysqlConfig.Username + ":" + MysqlConfig.Password + "@tcp(" + MysqlConfig.Path + ")/" + MysqlConfig.Dbname + "?" + MysqlConfig.Config
mysqlConfig := mysql.Config{ mysqlConfig := mysql.Config{
DSN: linkDns, // DSN data source name DSN: linkDns, // DSN data source name
DefaultStringSize: 191, // string 类型字段的默认长度 DefaultStringSize: 191, // string 类型字段的默认长度
@ -118,15 +108,11 @@ func InitDB(conf request.InitDB) error {
SkipInitializeWithVersion: false, // 根据版本自动配置 SkipInitializeWithVersion: false, // 根据版本自动配置
} }
if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil { if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
//global.GVA_LOG.Error("MySQL启动异常!", zap.Any("err", err))
//os.Exit(0)
//return nil
_ = writeConfig(global.GVA_VP, BaseMysql)
return nil return nil
} else { } else {
sqlDB, _ := db.DB() sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
sqlDB.SetMaxIdleConns(MysqlConfig.MaxIdleConns)
sqlDB.SetMaxOpenConns(MysqlConfig.MaxOpenConns)
global.GVA_DB = db global.GVA_DB = db
} }
@ -147,7 +133,7 @@ func InitDB(conf request.InitDB) error {
model.SysOperationRecord{}, model.SysOperationRecord{},
) )
if err != nil { if err != nil {
_ = writeConfig(global.GVA_VP, BaseMysql)
global.GVA_DB = nil
return err return err
} }
err = initDB( err = initDB(
@ -163,7 +149,10 @@ func InitDB(conf request.InitDB) error {
source.File, source.File,
source.BaseMenu) source.BaseMenu)
if err != nil { if err != nil {
_ = writeConfig(global.GVA_VP, BaseMysql)
global.GVA_DB = nil
return err
}
if err = writeConfig(global.GVA_VP, MysqlConfig); err != nil {
return err return err
} }
global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..") global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")

Loading…
Cancel
Save