optimize gorm initial logic in conf

pull/477/head
Michael Li 10 months ago
parent 9d4ee0eeb7
commit ee40280e6c
No known key found for this signature in database

@ -33,7 +33,7 @@ func MustGormDB() *gorm.DB {
return _gormdb
}
func newGormDB() (*gorm.DB, error) {
func newGormDB() (db *gorm.DB, err error) {
newLogger := logger.New(
logrus.StandardLogger(), // io writer日志输出的目标前缀和日志包含的内容
logger.Config{
@ -58,15 +58,9 @@ func newGormDB() (*gorm.DB, error) {
SetMaxIdleConns(MysqlSetting.MaxIdleConns).
SetMaxOpenConns(MysqlSetting.MaxOpenConns)
var (
db *gorm.DB
err error
)
if cfg.If("MySQL") {
logrus.Debugln("use MySQL as db")
if db, err = gorm.Open(mysql.Open(MysqlSetting.Dsn()), config); err == nil {
db.Use(plugin)
}
db, err = gorm.Open(mysql.Open(MysqlSetting.Dsn()), config)
} else if cfg.If("Postgres") {
logrus.Debugln("use PostgreSQL as db")
db, err = gorm.Open(postgres.Open(PostgresSetting.Dsn()), config)
@ -75,10 +69,10 @@ func newGormDB() (*gorm.DB, error) {
db, err = gormOpenSqlite3(config)
} else {
logrus.Debugln("use default of MySQL as db")
if db, err = gorm.Open(mysql.Open(MysqlSetting.Dsn()), config); err == nil {
db.Use(plugin)
}
db, err = gorm.Open(mysql.Open(MysqlSetting.Dsn()), config)
}
return db, err
if err == nil {
err = db.Use(plugin)
}
return
}

Loading…
Cancel
Save