fix config migration issue (#4338)

pull/4339/head
Gheis Mohammadi 2 years ago committed by GitHub
parent 9aa484c994
commit f39b6d61eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      cmd/harmony/config_migrations.go

@ -4,6 +4,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"sort" "sort"
"strconv"
"strings"
goversion "github.com/hashicorp/go-version" goversion "github.com/hashicorp/go-version"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
@ -335,8 +337,38 @@ func getNextVersion(x map[string]configMigrationFunc) string {
keys[i] = k keys[i] = k
i++ i++
} }
sort.Strings(keys) // sorting keys (versions)
requiredFunc := x[keys[len(keys)-1]] // each key is in format "x.x.x". Normal sort won't work if the versions
// don't have same number of digits, for example 1.02.10 and 1.2.9
// so, we need a custom sort
sort.Slice(keys, func(i, j int) bool {
v1 := keys[i]
v2 := keys[j]
v1Parts := strings.Split(v1, ".")
v2Parts := strings.Split(v2, ".")
if len(v1Parts) > len(v2Parts) {
return true
} else if len(v1Parts) < len(v2Parts) {
return false
}
for i := 0; i < len(v1Parts); i++ {
n1, err1 := strconv.ParseInt(v1Parts[i], 10, 32)
if err1 != nil {
panic("wrong version format")
}
n2, err2 := strconv.ParseInt(v2Parts[i], 10, 32)
if err2 != nil {
panic("wrong version format")
}
if n1 > n2 {
return true
} else if n1 < n2 {
return false
}
}
return false
})
requiredFunc := x[keys[0]]
tree = requiredFunc(tree) tree = requiredFunc(tree)
return tree.Get("Version").(string) return tree.Get("Version").(string)
} }

Loading…
Cancel
Save