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.

126 lines
3.4 KiB

  1. package initialize
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/initialize/internal"
  5. "gin-vue-admin/model"
  6. "os"
  7. "go.uber.org/zap"
  8. "gorm.io/driver/mysql"
  9. "gorm.io/gorm"
  10. "gorm.io/gorm/logger"
  11. )
  12. //@author: SliverHorn
  13. //@function: Gorm
  14. //@description: 初始化数据库并产生数据库全局变量
  15. //@return: *gorm.DB
  16. func Gorm() *gorm.DB {
  17. switch global.GVA_CONFIG.System.DbType {
  18. case "mysql":
  19. return GormMysql()
  20. default:
  21. return GormMysql()
  22. }
  23. }
  24. // MysqlTables
  25. //@author: SliverHorn
  26. //@function: MysqlTables
  27. //@description: 注册数据库表专用
  28. //@param: db *gorm.DB
  29. func MysqlTables(db *gorm.DB) {
  30. err := db.AutoMigrate(
  31. model.SysUser{},
  32. model.SysAuthority{},
  33. model.SysApi{},
  34. model.SysBaseMenu{},
  35. model.SysBaseMenuParameter{},
  36. model.JwtBlacklist{},
  37. model.SysDictionary{},
  38. model.SysDictionaryDetail{},
  39. model.ExaFileUploadAndDownload{},
  40. model.ExaFile{},
  41. model.ExaFileChunk{},
  42. model.ExaSimpleUploader{},
  43. model.ExaCustomer{},
  44. model.SysOperationRecord{},
  45. model.WorkflowProcess{},
  46. model.WorkflowNode{},
  47. model.WorkflowEdge{},
  48. model.WorkflowStartPoint{},
  49. model.WorkflowEndPoint{},
  50. model.WorkflowMove{},
  51. model.ExaWfLeave{},
  52. )
  53. if err != nil {
  54. global.GVA_LOG.Error("register table failed", zap.Any("err", err))
  55. os.Exit(0)
  56. }
  57. global.GVA_LOG.Info("register table success")
  58. }
  59. //
  60. //@author: SliverHorn
  61. //@function: GormMysql
  62. //@description: 初始化Mysql数据库
  63. //@return: *gorm.DB
  64. func GormMysql() *gorm.DB {
  65. m := global.GVA_CONFIG.Mysql
  66. if m.Dbname == "" {
  67. return nil
  68. }
  69. dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
  70. mysqlConfig := mysql.Config{
  71. DSN: dsn, // DSN data source name
  72. DefaultStringSize: 191, // string 类型字段的默认长度
  73. DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
  74. DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
  75. DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
  76. SkipInitializeWithVersion: false, // 根据版本自动配置
  77. }
  78. if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig(m.LogMode)); err != nil {
  79. //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
  80. //os.Exit(0)
  81. //return nil
  82. return nil
  83. } else {
  84. sqlDB, _ := db.DB()
  85. sqlDB.SetMaxIdleConns(m.MaxIdleConns)
  86. sqlDB.SetMaxOpenConns(m.MaxOpenConns)
  87. return db
  88. }
  89. }
  90. //@author: SliverHorn
  91. //@function: gormConfig
  92. //@description: 根据配置决定是否开启日志
  93. //@param: mod bool
  94. //@return: *gorm.Config
  95. func gormConfig(mod bool) *gorm.Config {
  96. var config = &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}
  97. switch global.GVA_CONFIG.Mysql.LogZap {
  98. case "silent", "Silent":
  99. config.Logger = internal.Default.LogMode(logger.Silent)
  100. case "error", "Error":
  101. config.Logger = internal.Default.LogMode(logger.Error)
  102. case "warn", "Warn":
  103. config.Logger = internal.Default.LogMode(logger.Warn)
  104. case "info", "Info":
  105. config.Logger = internal.Default.LogMode(logger.Info)
  106. case "zap", "Zap":
  107. config.Logger = internal.Default.LogMode(logger.Info)
  108. default:
  109. if mod {
  110. config.Logger = internal.Default.LogMode(logger.Info)
  111. break
  112. }
  113. config.Logger = internal.Default.LogMode(logger.Silent)
  114. }
  115. return config
  116. }