Skip to content

Commit

Permalink
update UpdateDB()
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Sep 8, 2020
1 parent 86407fd commit 37a4d52
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,34 @@ func SetDefaultName(dbname string) {

// 初始化Gorm
func UpdateDB(dbname string) error {
var (
orm *gorm.DB
err error
)

v, _ := Gorm.Load(dbname)
// first: open new gorm client
err = retry.Retry(func() error {
orm, err = openORM(dbname)
if err != nil {
xlog.Errorf("UpdateDB(%s) error:%+v", dbname, err)
return err
}
return nil
}, 5, 3*time.Second)
if err != nil {
return err
}

orm, err := openORM(dbname)
// second: load gorm client
v, _ := Gorm.Load(dbname)

// third: delete old gorm client and store the new gorm client
Gorm.Delete(dbname)
Gorm.Store(dbname, orm)

// fourth: if old client is not nil, delete and close connection
if v != nil {
err = v.(*gorm.DB).Close()
if err != nil {
return err
}
v.(*gorm.DB).Close()
}
return nil
}
Expand Down

0 comments on commit 37a4d52

Please sign in to comment.