aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bucket.go6
-rw-r--r--bucket_test.go27
-rw-r--r--db.go9
-rw-r--r--db_test.go3
4 files changed, 38 insertions, 7 deletions
diff --git a/bucket.go b/bucket.go
index 28832fc..470689b 100644
--- a/bucket.go
+++ b/bucket.go
@@ -332,6 +332,12 @@ func (b *Bucket) NextSequence() (uint64, error) {
return 0, ErrTxNotWritable
}
+ // Materialize the root node if it hasn't been already so that the
+ // bucket will be saved during commit.
+ if b.rootNode == nil {
+ _ = b.node(b.root, nil)
+ }
+
// Increment and return the sequence.
b.bucket.sequence++
return b.bucket.sequence, nil
diff --git a/bucket_test.go b/bucket_test.go
index 90e704a..883fa03 100644
--- a/bucket_test.go
+++ b/bucket_test.go
@@ -490,6 +490,33 @@ func TestBucket_NextSequence(t *testing.T) {
})
}
+// Ensure that a bucket will persist an autoincrementing sequence even if its
+// the only thing updated on the bucket.
+// https://github.com/boltdb/bolt/issues/296
+func TestBucket_NextSequence_Persist(t *testing.T) {
+ db := NewTestDB()
+ defer db.Close()
+ db.Update(func(tx *bolt.Tx) error {
+ _, _ = tx.CreateBucket([]byte("widgets"))
+ return nil
+ })
+
+ db.Update(func(tx *bolt.Tx) error {
+ _, _ = tx.Bucket([]byte("widgets")).NextSequence()
+ return nil
+ })
+
+ db.Update(func(tx *bolt.Tx) error {
+ seq, err := tx.Bucket([]byte("widgets")).NextSequence()
+ if err != nil {
+ t.Fatalf("unexpected error: %s", err)
+ } else if seq != 2 {
+ t.Fatalf("unexpected sequence: %d", seq)
+ }
+ return nil
+ })
+}
+
// Ensure that retrieving the next sequence on a read-only bucket returns an error.
func TestBucket_NextSequence_ReadOnly(t *testing.T) {
db := NewTestDB()
diff --git a/db.go b/db.go
index ae135ef..773620d 100644
--- a/db.go
+++ b/db.go
@@ -694,13 +694,8 @@ func _assert(condition bool, msg string, v ...interface{}) {
}
}
-func warn(v ...interface{}) {
- fmt.Fprintln(os.Stderr, v...)
-}
-
-func warnf(msg string, v ...interface{}) {
- fmt.Fprintf(os.Stderr, msg+"\n", v...)
-}
+func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
+func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }
func printstack() {
stack := strings.Join(strings.Split(string(debug.Stack()), "\n")[2:], "\n")
diff --git a/db_test.go b/db_test.go
index 7ba5ce8..4f05ad9 100644
--- a/db_test.go
+++ b/db_test.go
@@ -696,3 +696,6 @@ func fileSize(path string) int64 {
}
return fi.Size()
}
+
+func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
+func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }