From a672c7881ce73e865e3e9952695112134c623cb3 Mon Sep 17 00:00:00 2001 From: lsytj0413 <511121939@qq.com> Date: Mon, 20 Dec 2021 17:31:45 +0800 Subject: [PATCH] fix: ensure the buckets is exists when use boltdb --- dtmsvr/storage/boltdb.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/dtmsvr/storage/boltdb.go b/dtmsvr/storage/boltdb.go index e1e6cc0..ee3f3f0 100644 --- a/dtmsvr/storage/boltdb.go +++ b/dtmsvr/storage/boltdb.go @@ -21,6 +21,20 @@ func boltGet() *bolt.DB { boltOnce.Do(func() { db, err := bolt.Open("./dtm.bolt", 0666, &bolt.Options{Timeout: 1 * time.Second}) dtmimp.E2P(err) + + // NOTE: we must ensure all buckets is exists before we use it + err = db.Update(func(t *bolt.Tx) error { + for _, bucket := range allBuckets { + _, err := t.CreateBucketIfNotExists(bucket) + if err != nil { + return err + } + } + + return nil + }) + dtmimp.E2P(err) + boltDb = db }) return boltDb @@ -29,6 +43,11 @@ func boltGet() *bolt.DB { var bucketGlobal = []byte("global") var bucketBranches = []byte("branches") var bucketIndex = []byte("index") +var allBuckets = [][]byte{ + bucketGlobal, + bucketBranches, + bucketIndex, +} func tGetGlobal(t *bolt.Tx, gid string) *TransGlobalStore { trans := TransGlobalStore{}