// Ensure a closed database returns an error while running a transaction block func TestDB_Update_Closed(t *testing.T) { var db bolt.DB err := db.Update(func(tx *bolt.Tx) error { tx.CreateBucket([]byte("widgets")) return nil }) equals(t, err, bolt.ErrDatabaseNotOpen) }
func deleteKey(db *bolt.DB, name string) error { return db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(dbBucket)) if b == nil { return fmt.Errorf("unknown bucket: %v", dbBucket) } return b.Delete([]byte(name)) }) }
func setMigrated(db *bolt.DB, name string) error { return db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(dbBucket)) if b == nil { return fmt.Errorf("unknown bucket: %v", dbBucket) } return b.Put([]byte("isMigrated:"+name), []byte{1}) }) }
func isMigrated(db *bolt.DB, name string) (bool, error) { found := false err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(dbBucket)) if b == nil { return fmt.Errorf("unknown bucket: %v", dbBucket) } if dat := b.Get([]byte("isMigrated:" + name)); dat != nil { found = true } return nil }) return found, err }
func decode(db *bolt.DB, name string, dst interface{}) error { var data []byte err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(dbBucket)) if b == nil { return fmt.Errorf("unknown bucket: %v", dbBucket) } data = b.Get([]byte(name)) return nil }) if err != nil { return err } gr, err := gzip.NewReader(bytes.NewReader(data)) if err != nil { return err } defer gr.Close() return gob.NewDecoder(gr).Decode(dst) }
// Ensure that opening a transaction while the DB is closed returns an error. func TestDB_BeginRW_Closed(t *testing.T) { var db bolt.DB tx, err := db.Begin(true) equals(t, err, bolt.ErrDatabaseNotOpen) assert(t, tx == nil, "") }
// Ensure that a database cannot open a transaction when it's not open. func TestDB_Begin_DatabaseNotOpen(t *testing.T) { var db bolt.DB tx, err := db.Begin(false) assert(t, tx == nil, "") equals(t, err, bolt.ErrDatabaseNotOpen) }
func deleteKey(db *bolt.DB, name string) error { return db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(dbBucket)) return b.Delete([]byte(name)) }) }