Skip to content

Commit 5c2d916

Browse files
committed
build: migrate mongo-driver v1 to v2
Replace go.mongodb.org/mongo-driver v1.17.9 with v2.5.0. primitive.ObjectID replaced with bson.ObjectID, mongo.Connect no longer takes context, options.Update renamed to UpdateOne.
1 parent 4efa41c commit 5c2d916

329 files changed

Lines changed: 105608 additions & 57 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

datastore/mongo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"time"
88

99
log "github.com/go-pkgz/lgr"
10-
"go.mongodb.org/mongo-driver/bson"
11-
"go.mongodb.org/mongo-driver/mongo"
12-
"go.mongodb.org/mongo-driver/mongo/options"
10+
"go.mongodb.org/mongo-driver/v2/bson"
11+
"go.mongodb.org/mongo-driver/v2/mongo"
12+
"go.mongodb.org/mongo-driver/v2/mongo/options"
1313
)
1414

1515
// MongoServer top level mongo ops
@@ -29,7 +29,7 @@ func New(connectionURI, dbName string, delay time.Duration) (*MongoServer, error
2929
return nil, errors.New("env MONGO_URI not defined and --mongo not passed")
3030
}
3131

32-
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(connectionURI))
32+
client, err := mongo.Connect(options.Client().ApplyURI(connectionURI))
3333
if err != nil {
3434
return nil, err
3535
}

datastore/rules.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import (
66
"net/url"
77

88
log "github.com/go-pkgz/lgr"
9-
"go.mongodb.org/mongo-driver/bson"
10-
"go.mongodb.org/mongo-driver/bson/primitive"
11-
"go.mongodb.org/mongo-driver/mongo"
12-
"go.mongodb.org/mongo-driver/mongo/options"
9+
"go.mongodb.org/mongo-driver/v2/bson"
10+
"go.mongodb.org/mongo-driver/v2/mongo"
11+
"go.mongodb.org/mongo-driver/v2/mongo/options"
1312
)
1413

1514
// RulesDAO data-access obj for custom parsing rules, implements Rules
@@ -19,16 +18,16 @@ type RulesDAO struct {
1918

2019
// Rule record, entry in mongo
2120
type Rule struct {
22-
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
23-
Domain string `json:"domain"`
24-
MatchURLs []string `json:"match_url,omitempty" bson:"match_urls,omitempty"`
25-
Content string `json:"content"`
26-
Author string `json:"author,omitempty" bson:"author,omitempty"`
27-
TS string `json:"ts,omitempty" bson:"ts,omitempty"` // ts of original article
28-
Excludes []string `json:"excludes,omitempty" bson:"excludes,omitempty"`
29-
TestURLs []string `json:"test_urls,omitempty" bson:"test_urls"`
30-
User string `json:"user"`
31-
Enabled bool `json:"enabled"`
21+
ID bson.ObjectID `json:"id" bson:"_id,omitempty"`
22+
Domain string `json:"domain"`
23+
MatchURLs []string `json:"match_url,omitempty" bson:"match_urls,omitempty"`
24+
Content string `json:"content"`
25+
Author string `json:"author,omitempty" bson:"author,omitempty"`
26+
TS string `json:"ts,omitempty" bson:"ts,omitempty"` // ts of original article
27+
Excludes []string `json:"excludes,omitempty" bson:"excludes,omitempty"`
28+
TestURLs []string `json:"test_urls,omitempty" bson:"test_urls"`
29+
User string `json:"user"`
30+
Enabled bool `json:"enabled"`
3231
}
3332

3433
// Get rule by url. Checks if found in mongo, matching by domain
@@ -57,26 +56,26 @@ func (r RulesDAO) Get(ctx context.Context, rURL string) (Rule, bool) {
5756
}
5857

5958
// GetByID returns record by id
60-
func (r RulesDAO) GetByID(ctx context.Context, id primitive.ObjectID) (Rule, bool) {
59+
func (r RulesDAO) GetByID(ctx context.Context, id bson.ObjectID) (Rule, bool) {
6160
var rule Rule
6261
err := r.Collection.FindOne(ctx, bson.M{"_id": id}).Decode(&rule)
6362
return rule, err == nil
6463
}
6564

6665
// Save upsert rule
6766
func (r RulesDAO) Save(ctx context.Context, rule Rule) (Rule, error) {
68-
ch, err := r.UpdateOne(ctx, bson.M{"domain": rule.Domain}, bson.M{"$set": rule}, options.Update().SetUpsert(true))
67+
ch, err := r.UpdateOne(ctx, bson.M{"domain": rule.Domain}, bson.M{"$set": rule}, options.UpdateOne().SetUpsert(true))
6968
if err != nil {
7069
log.Printf("[WARN] failed to save, error=%v, article=%v", err, rule)
7170
return rule, err
7271
}
7372
if ch.UpsertedID != nil {
74-
if oid, ok := ch.UpsertedID.(primitive.ObjectID); ok {
73+
if oid, ok := ch.UpsertedID.(bson.ObjectID); ok {
7574
rule.ID = oid
7675
}
7776
}
7877
// if rule was updated, we have no id, so try to find it by domain
79-
if rule.ID == primitive.NilObjectID {
78+
if rule.ID == bson.NilObjectID {
8079
var found Rule
8180
err = r.Collection.FindOne(ctx, bson.M{"domain": rule.Domain}).Decode(&found)
8281
if err == nil {
@@ -87,7 +86,7 @@ func (r RulesDAO) Save(ctx context.Context, rule Rule) (Rule, error) {
8786
}
8887

8988
// Disable marks enabled=false, by id
90-
func (r RulesDAO) Disable(ctx context.Context, id primitive.ObjectID) error {
89+
func (r RulesDAO) Disable(ctx context.Context, id bson.ObjectID) error {
9190
_, err := r.UpdateOne(ctx, bson.M{"_id": id}, bson.M{"$set": bson.M{"enabled": false}})
9291
return err
9392
}

datastore/rules_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/go-pkgz/testutils/containers"
99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
11-
"go.mongodb.org/mongo-driver/bson/primitive"
11+
"go.mongodb.org/mongo-driver/v2/bson"
1212
)
1313

1414
const letterBytes = "abcdefghijklmnopqrstuvwxyz"
@@ -23,7 +23,7 @@ func TestRulesSave(t *testing.T) {
2323
assert.Equal(t, rule.Domain, saved.Domain)
2424
assert.Equal(t, rule.Content, saved.Content)
2525
assert.True(t, saved.Enabled)
26-
assert.NotEqual(t, primitive.NilObjectID, saved.ID)
26+
assert.NotEqual(t, bson.NilObjectID, saved.ID)
2727
})
2828

2929
t.Run("upsert same domain preserves id", func(t *testing.T) {
@@ -130,12 +130,12 @@ func TestRulesGetByID(t *testing.T) {
130130
})
131131

132132
t.Run("non-existing id", func(t *testing.T) {
133-
_, ok := rules.GetByID(context.Background(), primitive.NewObjectID())
133+
_, ok := rules.GetByID(context.Background(), bson.NewObjectID())
134134
assert.False(t, ok)
135135
})
136136

137137
t.Run("nil object id", func(t *testing.T) {
138-
_, ok := rules.GetByID(context.Background(), primitive.NilObjectID)
138+
_, ok := rules.GetByID(context.Background(), bson.NilObjectID)
139139
assert.False(t, ok)
140140
})
141141
}
@@ -158,7 +158,7 @@ func TestRulesDisable(t *testing.T) {
158158
})
159159

160160
t.Run("disable non-existing id does not error", func(t *testing.T) {
161-
err := rules.Disable(context.Background(), primitive.NewObjectID())
161+
err := rules.Disable(context.Background(), bson.NewObjectID())
162162
require.NoError(t, err) // mongo UpdateOne with no match is not an error
163163
})
164164
}
@@ -204,7 +204,7 @@ func TestRulesAll(t *testing.T) {
204204

205205
func TestRuleString(t *testing.T) {
206206
rule := Rule{
207-
ID: primitive.NewObjectID(),
207+
ID: bson.NewObjectID(),
208208
Domain: "example.com",
209209
Content: ".article",
210210
Enabled: true,

extractor/mocks/rules.go

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extractor/readability.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/PuerkitoBio/goquery"
1515
log "github.com/go-pkgz/lgr"
1616
"github.com/mauidude/go-readability"
17-
"go.mongodb.org/mongo-driver/bson/primitive"
17+
"go.mongodb.org/mongo-driver/v2/bson"
1818

1919
"github.com/ukeeper/ukeeper-readability/datastore"
2020
)
@@ -24,9 +24,9 @@ import (
2424
// Rules interface with all methods to access datastore
2525
type Rules interface {
2626
Get(ctx context.Context, rURL string) (datastore.Rule, bool)
27-
GetByID(ctx context.Context, id primitive.ObjectID) (datastore.Rule, bool)
27+
GetByID(ctx context.Context, id bson.ObjectID) (datastore.Rule, bool)
2828
Save(ctx context.Context, rule datastore.Rule) (datastore.Rule, error)
29-
Disable(ctx context.Context, id primitive.ObjectID) error
29+
Disable(ctx context.Context, id bson.ObjectID) error
3030
All(ctx context.Context) []datastore.Rule
3131
}
3232

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/mauidude/go-readability v0.0.0-20220221173116-a9b3620098b7
1414
github.com/stretchr/testify v1.11.1
1515
go.mongodb.org/mongo-driver v1.17.9
16+
go.mongodb.org/mongo-driver/v2 v2.5.0
1617
golang.org/x/net v0.52.0
1718
)
1819

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQ
359359
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
360360
go.mongodb.org/mongo-driver v1.17.9 h1:IexDdCuuNJ3BHrELgBlyaH9p60JXAvdzWR128q+U5tU=
361361
go.mongodb.org/mongo-driver v1.17.9/go.mod h1:LlOhpH5NUEfhxcAwG0UEkMqwYcc4JU18gtCdGudk/tQ=
362+
go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
363+
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
362364
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
363365
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
364366
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=

rest/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/go-pkgz/rest"
1717
"github.com/go-pkgz/rest/logger"
1818
"github.com/go-pkgz/routegroup"
19-
"go.mongodb.org/mongo-driver/bson/primitive"
19+
"go.mongodb.org/mongo-driver/v2/bson"
2020

2121
"github.com/ukeeper/ukeeper-readability/datastore"
2222
"github.com/ukeeper/ukeeper-readability/extractor"
@@ -345,10 +345,10 @@ func (s *Server) authFake(w http.ResponseWriter, _ *http.Request) {
345345
rest.RenderJSON(w, JSON{"pong": t.Format("20060102150405")})
346346
}
347347

348-
func getBid(id string) primitive.ObjectID {
349-
bid, err := primitive.ObjectIDFromHex(id)
348+
func getBid(id string) bson.ObjectID {
349+
bid, err := bson.ObjectIDFromHex(id)
350350
if err != nil {
351-
return primitive.NilObjectID
351+
return bson.NilObjectID
352352
}
353353
return bid
354354
}

rest/server_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/go-pkgz/rest"
2121
"github.com/stretchr/testify/assert"
2222
"github.com/stretchr/testify/require"
23-
"go.mongodb.org/mongo-driver/bson/primitive"
23+
"go.mongodb.org/mongo-driver/v2/bson"
2424

2525
"github.com/ukeeper/ukeeper-readability/datastore"
2626
"github.com/ukeeper/ukeeper-readability/extractor"
@@ -650,7 +650,7 @@ func newRulesStoreMock() *mocks.RulesMock {
650650
}
651651
return datastore.Rule{}, false
652652
},
653-
GetByIDFunc: func(_ context.Context, id primitive.ObjectID) (datastore.Rule, bool) {
653+
GetByIDFunc: func(_ context.Context, id bson.ObjectID) (datastore.Rule, bool) {
654654
mu.Lock()
655655
defer mu.Unlock()
656656
for _, r := range rules {
@@ -666,7 +666,7 @@ func newRulesStoreMock() *mocks.RulesMock {
666666
// upsert by domain
667667
for i, r := range rules {
668668
if r.Domain == rule.Domain {
669-
if rule.ID != primitive.NilObjectID && rule.ID != r.ID {
669+
if rule.ID != bson.NilObjectID && rule.ID != r.ID {
670670
return rule, fmt.Errorf("the (immutable) field '_id' was found to have been altered")
671671
}
672672
rule.ID = r.ID
@@ -675,19 +675,19 @@ func newRulesStoreMock() *mocks.RulesMock {
675675
}
676676
}
677677
// insert new, check for duplicate _id
678-
if rule.ID != primitive.NilObjectID {
678+
if rule.ID != bson.NilObjectID {
679679
for _, r := range rules {
680680
if r.ID == rule.ID {
681681
return rule, fmt.Errorf("E11000 duplicate key error")
682682
}
683683
}
684684
} else {
685-
rule.ID = primitive.NewObjectID()
685+
rule.ID = bson.NewObjectID()
686686
}
687687
rules = append(rules, rule)
688688
return rule, nil
689689
},
690-
DisableFunc: func(_ context.Context, id primitive.ObjectID) error {
690+
DisableFunc: func(_ context.Context, id bson.ObjectID) error {
691691
mu.Lock()
692692
defer mu.Unlock()
693693
for i, r := range rules {

vendor/github.com/klauspost/compress/internal/race/norace.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)