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.

118 lines
3.5 KiB

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