88 "gin-vue-admin/model/request"
99 "gin-vue-admin/source"
1010 "github.com/spf13/viper"
11+ "gorm.io/driver/mysql"
12+ "gorm.io/gorm"
1113)
1214
1315//@author: [songzhibin97](https://github.com/songzhibin97)
@@ -62,6 +64,7 @@ func InitDB(conf request.InitDB) error {
6264 if conf .Host == "" {
6365 conf .Host = "127.0.0.1"
6466 }
67+
6568 if conf .Port == "" {
6669 conf .Port = "3306"
6770 }
@@ -76,11 +79,65 @@ func InitDB(conf request.InitDB) error {
7679 "mysql.db-name" : conf .DBName ,
7780 "mysql.username" : conf .UserName ,
7881 "mysql.password" : conf .Password ,
82+ "mysql.config" : "charset=utf8mb4&parseTime=True&loc=Local" ,
7983 }
8084 if err := writeConfig (global .GVA_VP , setting ); err != nil {
8185 return err
8286 }
83- err := initDB (source .Admin ,
87+ m := global .GVA_CONFIG .Mysql
88+ if m .Dbname == "" {
89+ return nil
90+ }
91+
92+ linkDns := m .Username + ":" + m .Password + "@tcp(" + m .Path + ")/" + m .Dbname + "?" + m .Config
93+ mysqlConfig := mysql.Config {
94+ DSN : linkDns , // DSN data source name
95+ DefaultStringSize : 191 , // string 类型字段的默认长度
96+ DisableDatetimePrecision : true , // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
97+ DontSupportRenameIndex : true , // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
98+ DontSupportRenameColumn : true , // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
99+ SkipInitializeWithVersion : false , // 根据版本自动配置
100+ }
101+ if db , err := gorm .Open (mysql .New (mysqlConfig ), & gorm.Config {DisableForeignKeyConstraintWhenMigrating : true }); err != nil {
102+ //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
103+ //os.Exit(0)
104+ //return nil
105+ return nil
106+ } else {
107+ sqlDB , _ := db .DB ()
108+ sqlDB .SetMaxIdleConns (m .MaxIdleConns )
109+ sqlDB .SetMaxOpenConns (m .MaxOpenConns )
110+ global .GVA_DB = db
111+ }
112+
113+ err := global .GVA_DB .AutoMigrate (
114+ model.SysUser {},
115+ model.SysAuthority {},
116+ model.SysApi {},
117+ model.SysBaseMenu {},
118+ model.SysBaseMenuParameter {},
119+ model.JwtBlacklist {},
120+ model.SysDictionary {},
121+ model.SysDictionaryDetail {},
122+ model.ExaFileUploadAndDownload {},
123+ model.ExaFile {},
124+ model.ExaFileChunk {},
125+ model.ExaSimpleUploader {},
126+ model.ExaCustomer {},
127+ model.SysOperationRecord {},
128+ model.WorkflowProcess {},
129+ model.WorkflowNode {},
130+ model.WorkflowEdge {},
131+ model.WorkflowStartPoint {},
132+ model.WorkflowEndPoint {},
133+ model.WorkflowMove {},
134+ model.ExaWfLeave {},
135+ )
136+ if err != nil {
137+ return err
138+ }
139+ err = initDB (
140+ source .Admin ,
84141 source .Api ,
85142 source .AuthorityMenu ,
86143 source .Authority ,
@@ -92,7 +149,7 @@ func InitDB(conf request.InitDB) error {
92149 source .File ,
93150 source .BaseMenu ,
94151 source .Workflow )
95- if err != nil {
152+ if err != nil {
96153 return err
97154 }
98155 return nil
0 commit comments