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.

121 lines
3.3 KiB

  1. package initialize
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/initialize/internal"
  5. "gin-vue-admin/model"
  6. "go.uber.org/zap"
  7. "gorm.io/driver/mysql"
  8. "gorm.io/gorm"
  9. "gorm.io/gorm/logger"
  10. "os"
  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. dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
  67. mysqlConfig := mysql.Config{
  68. DSN: dsn, // DSN data source name
  69. DefaultStringSize: 191, // string 类型字段的默认长度
  70. DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
  71. DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
  72. DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
  73. SkipInitializeWithVersion: false, // 根据版本自动配置
  74. }
  75. if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig(m.LogMode)); err != nil {
  76. global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
  77. os.Exit(0)
  78. return nil
  79. } else {
  80. sqlDB, _ := db.DB()
  81. sqlDB.SetMaxIdleConns(m.MaxIdleConns)
  82. sqlDB.SetMaxOpenConns(m.MaxOpenConns)
  83. return db
  84. }
  85. }
  86. //@author: SliverHorn
  87. //@function: gormConfig
  88. //@description: 根据配置决定是否开启日志
  89. //@param: mod bool
  90. //@return: *gorm.Config
  91. func gormConfig(mod bool) *gorm.Config {
  92. var config = &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}
  93. switch global.GVA_CONFIG.Mysql.LogZap {
  94. case "silent", "Silent":
  95. config.Logger = internal.Default.LogMode(logger.Silent)
  96. case "error", "Error":
  97. config.Logger = internal.Default.LogMode(logger.Error)
  98. case "warn", "Warn":
  99. config.Logger = internal.Default.LogMode(logger.Warn)
  100. case "info", "Info":
  101. config.Logger = internal.Default.LogMode(logger.Info)
  102. case "zap", "Zap":
  103. config.Logger = internal.Default.LogMode(logger.Info)
  104. default:
  105. if mod {
  106. config.Logger = internal.Default.LogMode(logger.Info)
  107. break
  108. }
  109. config.Logger = internal.Default.LogMode(logger.Silent)
  110. }
  111. return config
  112. }