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.

122 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. //
  55. //@author: SliverHorn
  56. //@function: GormMysql
  57. //@description: 初始化Mysql数据库
  58. //@return: *gorm.DB
  59. func GormMysql() *gorm.DB {
  60. m := global.GVA_CONFIG.Mysql
  61. if m.Dbname == "" {
  62. return nil
  63. }
  64. dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
  65. mysqlConfig := mysql.Config{
  66. DSN: dsn, // DSN data source name
  67. DefaultStringSize: 191, // string 类型字段的默认长度
  68. DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
  69. DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
  70. DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
  71. SkipInitializeWithVersion: false, // 根据版本自动配置
  72. }
  73. if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig(m.LogMode)); err != nil {
  74. //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
  75. //os.Exit(0)
  76. //return nil
  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. var config = &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}
  92. switch global.GVA_CONFIG.Mysql.LogZap {
  93. case "silent", "Silent":
  94. config.Logger = internal.Default.LogMode(logger.Silent)
  95. case "error", "Error":
  96. config.Logger = internal.Default.LogMode(logger.Error)
  97. case "warn", "Warn":
  98. config.Logger = internal.Default.LogMode(logger.Warn)
  99. case "info", "Info":
  100. config.Logger = internal.Default.LogMode(logger.Info)
  101. case "zap", "Zap":
  102. config.Logger = internal.Default.LogMode(logger.Info)
  103. default:
  104. if mod {
  105. config.Logger = internal.Default.LogMode(logger.Info)
  106. break
  107. }
  108. config.Logger = internal.Default.LogMode(logger.Silent)
  109. }
  110. return config
  111. }