gorm.go 3.6 KB

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