migration.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package model
  2. import (
  3. "fmt"
  4. "os"
  5. "gorm.io/driver/mysql"
  6. "gorm.io/gorm"
  7. "gorm.io/gorm/schema"
  8. )
  9. var DB *gorm.DB
  10. func Construct(isMigrate bool) {
  11. var err error
  12. DB, err = gorm.Open(mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
  13. os.Getenv("DB_USERNAME"),
  14. os.Getenv("DB_PASSWORD"),
  15. os.Getenv("DB_HOST"),
  16. os.Getenv("DB_PORT"),
  17. os.Getenv("DB_DATABASE"))),
  18. &gorm.Config{
  19. NamingStrategy: schema.NamingStrategy{
  20. SingularTable: true,
  21. TablePrefix: os.Getenv("DB_PREFIX"),
  22. },
  23. DisableForeignKeyConstraintWhenMigrating: true,
  24. SkipDefaultTransaction: true,
  25. QueryFields: true,
  26. })
  27. if err != nil {
  28. panic(err)
  29. }
  30. // debug模式输出所有sql语句
  31. // if os.Getenv("APP_DEBUG") == "true" {
  32. DB = DB.Debug()
  33. // }
  34. sqlDB, err := DB.DB()
  35. if err != nil {
  36. panic(err)
  37. }
  38. sqlDB.SetMaxIdleConns(10)
  39. sqlDB.SetMaxOpenConns(100)
  40. // 开始迁移
  41. if isMigrate {
  42. Migrate()
  43. }
  44. }
  45. func Migrate() {
  46. // 问卷
  47. DB.Set("gorm:table_options", "ENGINE=InnoDB default charset = utf8mb4 COLLATE=utf8mb4_0900_as_cs").AutoMigrate(&Survey{})
  48. // 问卷授权
  49. DB.Set("gorm:table_options", "ENGINE=InnoDB default charset = utf8mb4 COLLATE=utf8mb4_0900_as_cs").AutoMigrate(&SurveyMechanism{})
  50. // 问卷结果
  51. DB.Set("gorm:table_options", "ENGINE=InnoDB default charset = utf8mb4 COLLATE=utf8mb4_0900_as_cs").AutoMigrate(&SurveyResult{})
  52. DB.Set("gorm:table_options", "ENGINE=InnoDB default charset = utf8mb4 COLLATE=utf8mb4_0900_as_cs").AutoMigrate(&QuestionnaireSubject{})
  53. DB.Set("gorm:table_options", "ENGINE=InnoDB default charset = utf8mb4 COLLATE=utf8mb4_0900_as_cs").AutoMigrate(&QuestionnaireSurvey{})
  54. DB.Set("gorm:table_options", "ENGINE=InnoDB default charset = utf8mb4 COLLATE=utf8mb4_0900_as_cs").AutoMigrate(&QuestionnaireSurveyQuestionnaireSubject{})
  55. DB.Set("gorm:table_options", "ENGINE=InnoDB default charset = utf8mb4 COLLATE=utf8mb4_0900_as_cs").AutoMigrate(&QuestionnaireTemplate{})
  56. DB.Set("gorm:table_options", "ENGINE=InnoDB default charset = utf8mb4 COLLATE=utf8mb4_0900_as_cs").AutoMigrate(&QuestionnaireTemplateSubject{})
  57. }
  58. // 分页查询的Scope
  59. func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB {
  60. return func(db *gorm.DB) *gorm.DB {
  61. if page == 0 {
  62. page = 1
  63. }
  64. switch {
  65. case pageSize > 100:
  66. pageSize = 100
  67. case pageSize <= 0:
  68. pageSize = 10
  69. }
  70. offset := (page - 1) * pageSize
  71. return db.Offset(offset).Limit(pageSize)
  72. }
  73. }
  74. // 分页查询的Scope
  75. func Limit(limit int) func(db *gorm.DB) *gorm.DB {
  76. return func(db *gorm.DB) *gorm.DB {
  77. switch {
  78. case limit > 100:
  79. limit = 100
  80. case limit <= 0:
  81. limit = 10
  82. }
  83. return db.Limit(limit)
  84. }
  85. }
  86. // 指定机构的Scope
  87. func MechanismQuery(mechanismId string) func(db *gorm.DB) *gorm.DB {
  88. return func(db *gorm.DB) *gorm.DB {
  89. if mechanismId != "" {
  90. return db.Where("mechanism_id = ?", mechanismId)
  91. }
  92. return db
  93. }
  94. }