@@ -2,6 +2,7 @@ package mysql
22
33import (
44 "bytes"
5+ "cmp"
56 "compress/zlib"
67 "crypto/rand"
78 "crypto/rsa"
@@ -13,11 +14,11 @@ import (
1314 "io"
1415 mrand "math/rand"
1516 "runtime"
17+ "strconv"
1618 "strings"
1719 "time"
1820
1921 "filippo.io/edwards25519"
20- "github.com/Masterminds/semver"
2122 "github.com/go-mysql-org/go-mysql/utils"
2223 "github.com/pingcap/errors"
2324)
@@ -452,21 +453,46 @@ func ErrorEqual(err1, err2 error) bool {
452453 return e1 .Error () == e2 .Error ()
453454}
454455
456+ func compareSubVersion (typ , a , b , aFull , bFull string ) (int , error ) {
457+ if a == "" || b == "" {
458+ return 0 , nil
459+ }
460+
461+ var aNum , bNum int
462+ var err error
463+
464+ if aNum , err = strconv .Atoi (a ); err != nil {
465+ return 0 , fmt .Errorf ("cannot parse %s version %s of %s" , typ , a , aFull )
466+ }
467+ if bNum , err = strconv .Atoi (b ); err != nil {
468+ return 0 , fmt .Errorf ("cannot parse %s version %s of %s" , typ , b , bFull )
469+ }
470+
471+ return cmp .Compare (aNum , bNum ), nil
472+ }
473+
474+ // Compares version triplet strings, ignoring anything past `-` in version.
475+ // A version string like 8.0 will compare as if third triplet were a wildcard.
476+ // A version string like 8 will compare as if second & third triplets were wildcards.
455477func CompareServerVersions (a , b string ) (int , error ) {
456- var (
457- aVer , bVer * semver.Version
458- err error
459- )
478+ aNumbers , _ , _ := strings .Cut (a , "-" )
479+ bNumbers , _ , _ := strings .Cut (b , "-" )
460480
461- if aVer , err = semver .NewVersion (a ); err != nil {
462- return 0 , fmt .Errorf ("cannot parse %q as semver: %w" , a , err )
481+ aMajor , aRest , _ := strings .Cut (aNumbers , "." )
482+ bMajor , bRest , _ := strings .Cut (bNumbers , "." )
483+
484+ if majorCompare , err := compareSubVersion ("major" , aMajor , bMajor , a , b ); err != nil || majorCompare != 0 {
485+ return majorCompare , err
463486 }
464487
465- if bVer , err = semver .NewVersion (b ); err != nil {
466- return 0 , fmt .Errorf ("cannot parse %q as semver: %w" , b , err )
488+ aMinor , aPatch , _ := strings .Cut (aRest , "." )
489+ bMinor , bPatch , _ := strings .Cut (bRest , "." )
490+
491+ if minorCompare , err := compareSubVersion ("minor" , aMinor , bMinor , a , b ); err != nil || minorCompare != 0 {
492+ return minorCompare , err
467493 }
468494
469- return aVer . Compare ( bVer ), nil
495+ return compareSubVersion ( "patch" , aPatch , bPatch , a , b )
470496}
471497
472498var encodeRef = map [byte ]byte {
0 commit comments