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.

134 lines
3.7 KiB

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