Skip to content

Commit 4acbb26

Browse files
committed
feedback
1 parent e8eb1f5 commit 4acbb26

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

flow/connectors/mongo/validate.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import (
88
shared_mongo "github.com/PeerDB-io/peerdb/flow/shared/mongo"
99
)
1010

11-
const MinSupportedVersion = "5.1.0"
12-
1311
func (c *MongoConnector) ValidateCheck(ctx context.Context) error {
14-
cmp, err := shared_mongo.CompareServerVersion(ctx, c.client, MinSupportedVersion)
12+
version, err := c.GetVersion(ctx)
13+
if err != nil {
14+
return err
15+
}
16+
cmp, err := shared_mongo.CompareServerVersions(version, shared_mongo.MinSupportedVersion)
1517
if err != nil {
1618
return err
1719
}
1820
if cmp == -1 {
19-
return fmt.Errorf("require minimum mongo version %s", MinSupportedVersion)
21+
return fmt.Errorf("require minimum mongo version %s", shared_mongo.MinSupportedVersion)
2022
}
2123
return nil
2224
}

flow/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ require (
1212
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/eventhub/armeventhub v1.3.0
1313
github.com/ClickHouse/ch-go v0.66.1
1414
github.com/ClickHouse/clickhouse-go/v2 v2.37.2
15-
github.com/Masterminds/semver v1.5.0
1615
github.com/PeerDB-io/glua64 v1.0.1
1716
github.com/PeerDB-io/gluabit32 v1.0.2
1817
github.com/PeerDB-io/gluajson v1.0.2

flow/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0
8282
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0.53.0/go.mod h1:jUZ5LYlw40WMd07qxcQJD5M40aUxrfwqQX1g7zxYnrQ=
8383
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.53.0 h1:Ron4zCA/yk6U7WOBXhTJcDpsUBG9npumK6xw2auFltQ=
8484
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.53.0/go.mod h1:cSgYe11MCNYunTnRXrKiR/tHc0eoKjICUuWpNZoVCOo=
85-
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
86-
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
8785
github.com/PeerDB-io/glua64 v1.0.1 h1:biXLlFF/L5pnJCwDon7hkWkuQPozC8NjKS3J7Wzi69I=
8886
github.com/PeerDB-io/glua64 v1.0.1/go.mod h1:UHmAhniv61bJPMhQvxkpC7jXbn353dSbQviu83bgQVg=
8987
github.com/PeerDB-io/gluabit32 v1.0.2 h1:AGI1Z7dwDVotakpuOOuyTX4/QGi5HUYsipL/VfodmO4=

flow/shared/mongo/util.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package mongo
2+
3+
import (
4+
"cmp"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
func CompareServerVersions(a, b string) (int, error) {
11+
aMajor, aRest, _ := strings.Cut(a, ".")
12+
bMajor, bRest, _ := strings.Cut(b, ".")
13+
14+
if majorCompare, err := compareSubVersion("major", aMajor, bMajor, a, b); err != nil || majorCompare != 0 {
15+
return majorCompare, err
16+
}
17+
18+
aMinor, aPatch, _ := strings.Cut(aRest, ".")
19+
bMinor, bPatch, _ := strings.Cut(bRest, ".")
20+
21+
if minorCompare, err := compareSubVersion("minor", aMinor, bMinor, a, b); err != nil || minorCompare != 0 {
22+
return minorCompare, err
23+
}
24+
25+
return compareSubVersion("patch", aPatch, bPatch, a, b)
26+
}
27+
28+
func compareSubVersion(typ, a, b, aFull, bFull string) (int, error) {
29+
if a == "" || b == "" {
30+
return 0, nil
31+
}
32+
33+
var aNum, bNum int
34+
var err error
35+
36+
if aNum, err = strconv.Atoi(a); err != nil {
37+
return 0, fmt.Errorf("cannot parse %s version %s of %s", typ, a, aFull)
38+
}
39+
if bNum, err = strconv.Atoi(b); err != nil {
40+
return 0, fmt.Errorf("cannot parse %s version %s of %s", typ, b, bFull)
41+
}
42+
43+
return cmp.Compare(aNum, bNum), nil
44+
}

flow/shared/mongo/validation.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/Masterminds/semver"
87
"go.mongodb.org/mongo-driver/v2/bson"
98
"go.mongodb.org/mongo-driver/v2/mongo"
109
)
1110

11+
const MinSupportedVersion = "5.1.0"
12+
1213
type BuildInfo struct {
1314
Version string `bson:"version"`
1415
}
@@ -30,19 +31,6 @@ func GetBuildInfo(ctx context.Context, client *mongo.Client) (*BuildInfo, error)
3031
return &info, nil
3132
}
3233

33-
func CompareServerVersion(ctx context.Context, client *mongo.Client, versionString string) (int, error) {
34-
buildInfo, err := GetBuildInfo(ctx, client)
35-
if err != nil {
36-
return 0, fmt.Errorf("failed to get mongo version: %w", err)
37-
}
38-
parsedVersion, err := semver.NewVersion(buildInfo.Version)
39-
if err != nil {
40-
return 0, fmt.Errorf("failed to parse mongo version %w", err)
41-
}
42-
compareVersion := semver.MustParse(versionString)
43-
return parsedVersion.Compare(compareVersion), nil
44-
}
45-
4634
func GetReplSetGetStatus(ctx context.Context, client *mongo.Client) (*ReplSetGetStatus, error) {
4735
db := client.Database("admin")
4836
singleResult := db.RunCommand(ctx, bson.D{

0 commit comments

Comments
 (0)