db.go 811 B

1234567891011121314151617181920212223242526272829303132333435363738
  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.ProductType{},
  34. &models.DataMOdel{})
  35. }