db.go 785 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package utils
  2. import (
  3. "cc-officialweb/models"
  4. beego "github.com/beego/beego/v2/server/web"
  5. "gorm.io/driver/mysql"
  6. "gorm.io/gorm"
  7. "gorm.io/gorm/logger"
  8. )
  9. var DB *gorm.DB
  10. func init() {
  11. s, err := beego.AppConfig.String("dburl")
  12. if err != nil {
  13. panic(err)
  14. }
  15. DB, err = gorm.Open(mysql.Open(s), &gorm.Config{
  16. DisableForeignKeyConstraintWhenMigrating: true,
  17. Logger: logger.Default.LogMode(logger.Info),
  18. })
  19. if err != nil {
  20. panic(err)
  21. }
  22. DB.Set("gorm:table_options", "ENGINE=InnoDB").
  23. AutoMigrate(&models.Resource{},
  24. &models.Products{},
  25. &models.Server{},
  26. &models.Content{},
  27. &models.User{},
  28. &models.News{},
  29. &models.FileResource{},
  30. &models.Data{},
  31. &models.Contact{},
  32. &models.Link{},
  33. &models.DataMOdel{})
  34. }