channeldb: don't use KeyN in latest migration

In this commit, we fix a bug in the latest migration that could cause
the migration to end in a panic. Additionally, we modify the migration
to exit early if the bucket wasn't found, as in this case, no migration
is required.

Fixes #1874.
This commit is contained in:
Olaoluwa Osuntokun 2018-09-12 22:06:11 -07:00
parent e23f889639
commit 034198ffb7
No known key found for this signature in database
GPG Key ID: CE58F7F8E20FD9A2

@ -473,12 +473,9 @@ func migratePruneEdgeUpdateIndex(tx *bolt.Tx) error {
if edges == nil {
return nil
}
edgeUpdateIndex, err := edges.CreateBucketIfNotExists(
edgeUpdateIndexBucket,
)
if err != nil {
return fmt.Errorf("unable to create/fetch edge update " +
"index bucket")
edgeUpdateIndex := edges.Bucket(edgeUpdateIndexBucket)
if edgeUpdateIndex == nil {
return nil
}
// Retrieve some buckets that will be needed later on. These should
@ -518,7 +515,6 @@ func migratePruneEdgeUpdateIndex(tx *bolt.Tx) error {
// With the existing edge policies gathered, we'll recreate the index
// and populate it with the correct entries.
oldNumEntries := edgeUpdateIndex.Stats().KeyN
if err := edges.DeleteBucket(edgeUpdateIndexBucket); err != nil {
return fmt.Errorf("unable to remove existing edge update "+
"index: %v", err)
@ -558,10 +554,6 @@ func migratePruneEdgeUpdateIndex(tx *bolt.Tx) error {
}
}
newNumEntries := edgeUpdateIndex.Stats().KeyN
log.Infof("Pruned %d stale entries from the edge update index",
oldNumEntries-newNumEntries)
log.Info("Migration to properly prune edge update index complete!")
return nil