Skip to content

Commit 0cd3555

Browse files
committed
perf:优化初始化流程
创建失败不更改config文件保持,前端保持在init界面 修改文件 - server/api/sys_initdb.go - server/service/sys_initdb.go
1 parent f6865ba commit 0cd3555

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

server/api/v1/sys_initdb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"gin-vue-admin/model/request"
66
"gin-vue-admin/model/response"
77
"gin-vue-admin/service"
8+
89
"go.uber.org/zap"
910

1011
"github.com/gin-gonic/gin"
@@ -30,7 +31,7 @@ func InitDB(c *gin.Context) {
3031
}
3132
if err := service.InitDB(dbInfo); err != nil {
3233
global.GVA_LOG.Error("自动创建数据库失败!", zap.Any("err", err))
33-
response.FailWithMessage("自动创建数据库失败,请查看后台日志", c)
34+
response.FailWithMessage("自动创建数据库失败,请查看后台日志,检查后在进行初始化", c)
3435
return
3536
}
3637
response.OkWithData("自动创建数据库成功", c)

server/service/sys_initdb.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
"gin-vue-admin/model/request"
1010
"gin-vue-admin/source"
1111
"gin-vue-admin/utils"
12+
"path/filepath"
13+
1214
"github.com/spf13/viper"
1315
"gorm.io/driver/mysql"
1416
"gorm.io/gorm"
15-
"path/filepath"
1617
)
1718

1819
//@author: [songzhibin97](https://github.com/songzhibin97)
@@ -71,13 +72,6 @@ func initDB(InitDBFunctions ...model.InitDBFunc) (err error) {
7172
//@return: error
7273

7374
func InitDB(conf request.InitDB) error {
74-
BaseMysql := config.Mysql{
75-
Path: "",
76-
Dbname: "",
77-
Username: "",
78-
Password: "",
79-
Config: "charset=utf8mb4&parseTime=True&loc=Local",
80-
}
8175

8276
if conf.Host == "" {
8377
conf.Host = "127.0.0.1"
@@ -100,15 +94,11 @@ func InitDB(conf request.InitDB) error {
10094
Config: "charset=utf8mb4&parseTime=True&loc=Local",
10195
}
10296

103-
if err := writeConfig(global.GVA_VP, MysqlConfig); err != nil {
104-
return err
105-
}
106-
m := global.GVA_CONFIG.Mysql
107-
if m.Dbname == "" {
97+
if MysqlConfig.Dbname == "" {
10898
return nil
10999
}
110100

111-
linkDns := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
101+
linkDns := MysqlConfig.Username + ":" + MysqlConfig.Password + "@tcp(" + MysqlConfig.Path + ")/" + MysqlConfig.Dbname + "?" + MysqlConfig.Config
112102
mysqlConfig := mysql.Config{
113103
DSN: linkDns, // DSN data source name
114104
DefaultStringSize: 191, // string 类型字段的默认长度
@@ -118,15 +108,11 @@ func InitDB(conf request.InitDB) error {
118108
SkipInitializeWithVersion: false, // 根据版本自动配置
119109
}
120110
if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
121-
//global.GVA_LOG.Error("MySQL启动异常!", zap.Any("err", err))
122-
//os.Exit(0)
123-
//return nil
124-
_ = writeConfig(global.GVA_VP, BaseMysql)
125111
return nil
126112
} else {
127113
sqlDB, _ := db.DB()
128-
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
129-
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
114+
sqlDB.SetMaxIdleConns(MysqlConfig.MaxIdleConns)
115+
sqlDB.SetMaxOpenConns(MysqlConfig.MaxOpenConns)
130116
global.GVA_DB = db
131117
}
132118

@@ -147,7 +133,7 @@ func InitDB(conf request.InitDB) error {
147133
model.SysOperationRecord{},
148134
)
149135
if err != nil {
150-
_ = writeConfig(global.GVA_VP, BaseMysql)
136+
global.GVA_DB = nil
151137
return err
152138
}
153139
err = initDB(
@@ -163,7 +149,10 @@ func InitDB(conf request.InitDB) error {
163149
source.File,
164150
source.BaseMenu)
165151
if err != nil {
166-
_ = writeConfig(global.GVA_VP, BaseMysql)
152+
global.GVA_DB = nil
153+
return err
154+
}
155+
if err = writeConfig(global.GVA_VP, MysqlConfig); err != nil {
167156
return err
168157
}
169158
global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")

0 commit comments

Comments
 (0)