@@ -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
7374func 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