Skip to content

Commit 3b2c807

Browse files
committed
channeldb: fix bug in migration from 0.4 to 0.5
In this commit, we fix a bug in the latest database migration when migrating from 0.4 to 0.5. There's an issue in bolt db where if one deletes a bucket that has a key with a nil value, it thinks that's a sub bucket and attempts a bucket deletion. This will fail as it's not actually a sub-bucket. We get around this by using a cursor to manually delete items in the bucket. Fixes #1907.
1 parent 48d016b commit 3b2c807

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

channeldb/migrations.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,8 @@ func migratePruneEdgeUpdateIndex(tx *bolt.Tx) error {
479479
}
480480

481481
// Retrieve some buckets that will be needed later on. These should
482-
// already exist given the assumption that the buckets above do as well.
482+
// already exist given the assumption that the buckets above do as
483+
// well.
483484
edgeIndex, err := edges.CreateBucketIfNotExists(edgeIndexBucket)
484485
if edgeIndex == nil {
485486
return fmt.Errorf("unable to create/fetch edge index " +
@@ -515,16 +516,15 @@ func migratePruneEdgeUpdateIndex(tx *bolt.Tx) error {
515516

516517
// With the existing edge policies gathered, we'll recreate the index
517518
// and populate it with the correct entries.
518-
if err := edges.DeleteBucket(edgeUpdateIndexBucket); err != nil {
519-
return fmt.Errorf("unable to remove existing edge update "+
520-
"index: %v", err)
521-
}
522-
edgeUpdateIndex, err = edges.CreateBucketIfNotExists(
523-
edgeUpdateIndexBucket,
524-
)
525-
if err != nil {
526-
return fmt.Errorf("unable to recreate edge update index: %v",
527-
err)
519+
//
520+
// NOTE: In bolt DB, calling Delete() on an iterator will actually move
521+
// it to the NEXT item. As a result, we call First() here again to go
522+
// back to the front after the item has been deleted.
523+
updateCursor := edgeUpdateIndex.Cursor()
524+
for k, _ := updateCursor.First(); k != nil; k, _ = updateCursor.First() {
525+
if err := updateCursor.Delete(); err != nil {
526+
return err
527+
}
528528
}
529529

530530
// For each edge key, we'll retrieve the policy, deserialize it, and

0 commit comments

Comments
 (0)