gorm.go 3.6 KB

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