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. "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. // Code generated by gin-vue-admin Begin; DO NOT EDIT.
  46. // Code generated by gin-vue-admin End; DO NOT EDIT.
  47. )
  48. if err != nil {
  49. global.GVA_LOG.Error("register table failed", zap.Any("err", err))
  50. os.Exit(0)
  51. }
  52. global.GVA_LOG.Info("register table success")
  53. }
  54. //@author: SliverHorn
  55. //@function: GormMysql
  56. //@description: 初始化Mysql数据库
  57. //@return: *gorm.DB
  58. func GormMysql() *gorm.DB {
  59. m := global.GVA_CONFIG.Mysql
  60. if m.Dbname == "" {
  61. return nil
  62. }
  63. dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
  64. mysqlConfig := mysql.Config{
  65. DSN: dsn, // DSN data source name
  66. DefaultStringSize: 191, // string 类型字段的默认长度
  67. DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
  68. DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
  69. DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
  70. SkipInitializeWithVersion: false, // 根据版本自动配置
  71. }
  72. if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig(m.LogMode)); err != nil {
  73. //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
  74. //os.Exit(0)
  75. //return nil
  76. return nil
  77. } else {
  78. sqlDB, _ := db.DB()
  79. sqlDB.SetMaxIdleConns(m.MaxIdleConns)
  80. sqlDB.SetMaxOpenConns(m.MaxOpenConns)
  81. return db
  82. }
  83. }
  84. //@author: SliverHorn
  85. //@function: gormConfig
  86. //@description: 根据配置决定是否开启日志
  87. //@param: mod bool
  88. //@return: *gorm.Config
  89. func gormConfig(mod bool) *gorm.Config {
  90. var config = &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}
  91. switch global.GVA_CONFIG.Mysql.LogZap {
  92. case "silent", "Silent":
  93. config.Logger = internal.Default.LogMode(logger.Silent)
  94. case "error", "Error":
  95. config.Logger = internal.Default.LogMode(logger.Error)
  96. case "warn", "Warn":
  97. config.Logger = internal.Default.LogMode(logger.Warn)
  98. case "info", "Info":
  99. config.Logger = internal.Default.LogMode(logger.Info)
  100. case "zap", "Zap":
  101. config.Logger = internal.Default.LogMode(logger.Info)
  102. default:
  103. if mod {
  104. config.Logger = internal.Default.LogMode(logger.Info)
  105. break
  106. }
  107. config.Logger = internal.Default.LogMode(logger.Silent)
  108. }
  109. return config
  110. }