package utils

import (
	"cc-officialweb/models"
	beego "github.com/beego/beego/v2/server/web"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

var DB *gorm.DB

func init() {
	s, err := beego.AppConfig.String("dburl")
	if err != nil {
		panic(err)
	}
	DB, err = gorm.Open(mysql.Open(s), &gorm.Config{
		DisableForeignKeyConstraintWhenMigrating: true,
		Logger:                                   logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic(err)
	}
	DB.Set("gorm:table_options", "ENGINE=InnoDB").
		AutoMigrate(&models.Resource{},
			&models.Products{},
			&models.Server{},
			&models.Content{},
			&models.User{},
			&models.News{},
			&models.FileResource{},
			&models.Data{},
			&models.Contact{},
			&models.Link{},
			&models.ProductType{},
			&models.DataMOdel{})
}
