gorm.go 3.5 KB

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