123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package global
- import (
- "Ic_ouath/configs"
- "Ic_ouath/models"
- "fmt"
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
- "gorm.io/gorm/logger"
- "log"
- "runtime"
- )
- var (
- DBLink *gorm.DB
- )
- func SetupDBLink() error {
- var err error
- dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?%s",
- configs.Config.GetString("database.userName"),
- configs.Config.GetString("database.password"),
- configs.Config.GetString("database.host"),
- configs.Config.GetString("database.db"),
- configs.Config.GetString("database.otherParams"),
- )
- //判断当前系统是windows还是linux
- //runtime获取的是当前Go语言框架的参数,不是实际运行的操作系统的参数
- sysType := runtime.GOOS
- if sysType == "windows" {
- DBLink, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
- DisableForeignKeyConstraintWhenMigrating: true,
- Logger: logger.Default.LogMode(logger.Info),
- })
- log.Print("当前系统为windows")
- } else if sysType == "linux" {
- DBLink, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
- }
- if err != nil {
- return err
- }
- //DBLink.SingularTable(true)
- //DBLink.DB().SetMaxIdleConns(DatabaseSetting.MaxIdleConn)
- //DBLink.DB().SetMaxOpenConns(DatabaseSetting.MaxOpenConn)
- DBLink.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&models.User{})
- return nil
- }
|