gorm.go 3.7 KB

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