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

Loading…
Cancel
Save