Migrate MongoDB Go driver from v1.17 to v2.2#1405
Conversation
WalkthroughThis change upgrades the MongoDB Go driver from v1.17.4 to v2.2.0, updating all relevant import paths and APIs throughout the codebase. It refactors BSON registry and codec handling to use the new v2 API, adjusts type assertions and option constructors, and modernizes encoding/decoding in tests. Several indirect dependencies are also updated or removed. Changes
Sequence Diagram(s)sequenceDiagram
participant App
participant MongoClient
participant BSONRegistry
App->>MongoClient: Dial(config)
MongoClient->>BSONRegistry: NewRegistryBuilder()
BSONRegistry-->>MongoClient: *bson.Registry
MongoClient->>MongoClient: mongo.Connect(clientOptions with registry)
MongoClient-->>App: *Client
App->>MongoClient: CRUD operations (Create, Update, Delete, etc.)
MongoClient->>BSONRegistry: Encode/Decode custom types (ID, ActorID, VersionVector, etc.)
BSONRegistry-->>MongoClient: Encoded/Decoded values
MongoClient-->>App: Operation result
Estimated code review effort🎯 4 (Complex) | ⏱️ ~35 minutes Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.2.2)Error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/product/migration-guide for migration instructions Note ⚡️ Unit Test Generation - BetaCodeRabbit's unit test generation is now available in Beta! Automatically generate comprehensive unit tests for your code changes, ensuring better test coverage and catching edge cases you might miss. Our AI analyzes your code structure and creates tests that follow best practices and your project's testing patterns. Learn more here, or just try it under ✨ Finishing Touches. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
go.sumis excluded by!**/*.sum
📒 Files selected for processing (11)
go.mod(2 hunks)server/backend/database/memory/database.go(2 hunks)server/backend/database/mongo/client.go(13 hunks)server/backend/database/mongo/encoder.go(1 hunks)server/backend/database/mongo/indexes.go(1 hunks)server/backend/database/mongo/monitor.go(1 hunks)server/backend/database/mongo/registry.go(7 hunks)server/backend/database/mongo/registry_test.go(3 hunks)server/backend/database/testcases/testcases.go(1 hunks)server/rpc/connecthelper/status_test.go(1 hunks)test/helper/helper.go(2 hunks)
🧰 Additional context used
🧠 Learnings (10)
📓 Common learnings
Learnt from: window9u
PR: yorkie-team/yorkie#1166
File: pkg/limit/limiter.go:132-132
Timestamp: 2025-03-07T10:41:57.466Z
Learning: Go 1.23, released in August 2024, added support for `for range` loops with integers. However, the project currently uses Go 1.21, which does not support this syntax.
server/rpc/connecthelper/status_test.go (1)
Learnt from: KMSstudio
PR: #1372
File: cmd/yorkie/signup.go:0-0
Timestamp: 2025-07-09T01:45:42.906Z
Learning: In the Yorkie CLI codebase (cmd/yorkie/), global variables like username, password, rpcAddr, and insecure are declared in login.go at package level and are reused across different commands (like signup.go) within the same main package, avoiding unnecessary duplication.
server/backend/database/mongo/monitor.go (1)
Learnt from: window9u
PR: #1156
File: server/backend/config.go:235-266
Timestamp: 2025-02-20T00:58:21.388Z
Learning: In Yorkie server, configuration parsing methods (e.g., ParseEventWebhookMaxWaitInterval) use os.Exit(1) for error handling as they are executed during server initialization where early termination on invalid configuration is desired.
go.mod (1)
Learnt from: window9u
PR: #1166
File: pkg/limit/limiter.go:132-132
Timestamp: 2025-03-07T10:41:57.466Z
Learning: Go 1.23, released in August 2024, added support for for range loops with integers. However, the project currently uses Go 1.21, which does not support this syntax.
test/helper/helper.go (1)
Learnt from: KMSstudio
PR: #1372
File: cmd/yorkie/signup.go:0-0
Timestamp: 2025-07-09T01:45:42.906Z
Learning: In the Yorkie CLI codebase (cmd/yorkie/), global variables like username, password, rpcAddr, and insecure are declared in login.go at package level and are reused across different commands (like signup.go) within the same main package, avoiding unnecessary duplication.
server/backend/database/mongo/indexes.go (1)
Learnt from: hackerwins
PR: #1036
File: server/rpc/cluster_server.go:56-121
Timestamp: 2024-10-17T06:34:51.208Z
Learning: In server/rpc/cluster_server.go, it's acceptable to return errors directly without additional context in the DetachDocument method.
server/backend/database/mongo/encoder.go (1)
Learnt from: chacha912
PR: #1164
File: test/helper/helper.go:120-125
Timestamp: 2025-02-21T04:54:29.326Z
Learning: In pkg/document/change/id.go, the InitialID variable represents the initial state where nothing has been edited. Consider using a function like GetInitialID() instead of the package-level variable to enforce proper initialization and prevent shared mutable state.
server/backend/database/memory/database.go (3)
Learnt from: chacha912
PR: #1164
File: test/helper/helper.go:120-125
Timestamp: 2025-02-21T04:54:29.326Z
Learning: In pkg/document/change/id.go, the InitialID variable represents the initial state where nothing has been edited. Consider using a function like GetInitialID() instead of the package-level variable to enforce proper initialization and prevent shared mutable state.
Learnt from: chacha912
PR: #1164
File: test/helper/helper.go:120-125
Timestamp: 2025-02-21T04:54:29.325Z
Learning: In the Yorkie codebase, avoid using InitialID directly as a value. Instead, prefer explicit initialization using NewID with appropriate initial values or use a dedicated function like GetInitialID to enforce proper initialization.
Learnt from: KMSstudio
PR: #1372
File: cmd/yorkie/signup.go:0-0
Timestamp: 2025-07-09T01:45:42.906Z
Learning: In the Yorkie CLI codebase (cmd/yorkie/), global variables like username, password, rpcAddr, and insecure are declared in login.go at package level and are reused across different commands (like signup.go) within the same main package, avoiding unnecessary duplication.
server/backend/database/mongo/registry.go (2)
Learnt from: hackerwins
PR: #1164
File: pkg/document/time/version_vector.go:158-183
Timestamp: 2025-02-20T11:31:21.714Z
Learning: In the Yorkie project, the VersionVector.Max method modifies the receiver in-place and returns it, which is an intentional design decision for memory efficiency. The method takes a pointer parameter but only reads from it, maintaining input integrity.
Learnt from: chacha912
PR: #1164
File: pkg/document/time/version_vector.go:158-183
Timestamp: 2025-02-21T04:54:17.948Z
Learning: In the Yorkie project, the VersionVector.Min and VersionVector.Max methods should maintain consistent behavior and documentation. Both methods should modify the receiver in-place for memory efficiency, take pointer parameters, and document their side effects clearly.
server/backend/database/mongo/client.go (4)
Learnt from: hackerwins
PR: #1036
File: system/client.go:97-109
Timestamp: 2024-10-17T03:49:39.301Z
Learning: In the Yorkie codebase, it's a convention to have both a package-level Dial function and a Dial method on the Client struct in the system package, so renaming is preferred against changing it for consistency.
Learnt from: KMSstudio
PR: #1372
File: cmd/yorkie/signup.go:0-0
Timestamp: 2025-07-09T01:45:42.906Z
Learning: In the Yorkie CLI codebase (cmd/yorkie/), global variables like username, password, rpcAddr, and insecure are declared in login.go at package level and are reused across different commands (like signup.go) within the same main package, avoiding unnecessary duplication.
Learnt from: hackerwins
PR: #1164
File: pkg/document/time/version_vector.go:158-183
Timestamp: 2025-02-20T11:31:21.714Z
Learning: In the Yorkie project, the VersionVector.Max method modifies the receiver in-place and returns it, which is an intentional design decision for memory efficiency. The method takes a pointer parameter but only reads from it, maintaining input integrity.
Learnt from: chacha912
PR: #1164
File: pkg/document/time/version_vector.go:158-183
Timestamp: 2025-02-21T04:54:17.948Z
Learning: In the Yorkie project, the VersionVector.Min and VersionVector.Max methods should maintain consistent behavior and documentation. Both methods should modify the receiver in-place for memory efficiency, take pointer parameters, and document their side effects clearly.
🧬 Code Graph Analysis (4)
test/helper/helper.go (1)
server/backend/database/mongo/registry.go (1)
NewRegistryBuilder(37-52)
server/backend/database/mongo/encoder.go (3)
pkg/document/time/actor_id.go (1)
ActorID(57-57)pkg/document/change/id.go (1)
ID(26-48)api/types/id.go (1)
ID(35-35)
server/backend/database/memory/database.go (2)
pkg/document/change/id.go (1)
ID(26-48)api/types/id.go (1)
ID(35-35)
server/backend/database/mongo/client.go (3)
server/backend/database/mongo/registry.go (1)
NewRegistryBuilder(37-52)pkg/document/change/id.go (1)
ID(26-48)api/types/id.go (1)
ID(35-35)
🔇 Additional comments (31)
server/backend/database/mongo/monitor.go (1)
23-23: Import path bumped to v2 – looks correctThe driver’s
eventpackage path is nowgo.mongodb.org/mongo-driver/v2/event, which matches the module major-version bump. No other code changes required here.server/rpc/connecthelper/status_test.go (1)
24-24: Test import updated to v2 – okThe test now references
go.mongodb.org/mongo-driver/v2/mongo. Compiles fine; no further action.server/backend/database/mongo/indexes.go (1)
23-26: All driver imports migrated to v2 – good
bson,mongo, andmongo/optionspaths have been rewritten with/v2. The remainder of the file already uses driver-agnostic APIs, so nothing else to fix.server/backend/database/testcases/testcases.go (1)
29-29: Test helper import path aligned with v2Alias import (
mongodb) correctly points to the v2 driver. No issues spotted.test/helper/helper.go (2)
34-37: LGTM!Import paths correctly updated to MongoDB Go driver v2.
438-438: LGTM!Registry builder API correctly updated to v2 pattern. The
NewRegistryBuilder()now returns a ready-to-use*bson.Registrydirectly.server/backend/database/memory/database.go (2)
27-27: LGTM!Import path correctly updated. In v2, the
primitivepackage is merged into the mainbsonpackage.
1874-1876: LGTM!ObjectID generation correctly updated to use the v2 API.
server/backend/database/mongo/encoder.go (2)
22-22: LGTM!Import path correctly updated to v2.
28-40: LGTM!Function signatures and types correctly updated to use
bson.ObjectIDfrom v2.server/backend/database/mongo/registry_test.go (4)
24-24: LGTM!Import path correctly updated to v2.
33-33: LGTM!Registry builder usage correctly updated to v2 API.
36-100: LGTM!Test cases correctly refactored to use the v2 encoder/decoder pattern. The new approach is more explicit but provides better control over BSON operations.
117-117: LGTM!Correct usage of
ObjectIDAsHexString()for decoding ObjectIDs as hex strings in tests.Also applies to: 136-136
server/backend/database/mongo/registry.go (4)
23-23: LGTM!Registry creation and registration correctly updated to v2 API. The new approach is cleaner without the builder pattern.
Also applies to: 36-52
54-77: Good addition of flexible ID decoding!The new
idDecodercorrectly handles both ObjectID and String types, providing flexibility in how IDs are stored and retrieved.
79-102: LGTM!All encoder functions correctly updated to v2 API with proper error handling using
bson.ValueEncoderError.Also applies to: 104-120, 147-171
122-145: LGTM!Decoder functions correctly updated to v2 API with proper null handling and error types.
Also applies to: 173-201
server/backend/database/mongo/client.go (13)
28-31: LGTM: Import paths correctly updated for MongoDB driver v2The import paths have been properly updated to use the
/v2suffix, which is the correct approach for migrating to MongoDB Go driver v2.2.
66-66: LGTM: Registry setup correctly updated for driver v2The change from
NewRegistryBuilder().Build()toNewRegistryBuilder()is correct, as the function now returns a*bson.Registrydirectly in the v2 API.
82-84: LGTM: mongo.Connect API correctly updated for driver v2The removal of the context parameter from
mongo.Connectis correct for MongoDB driver v2, where the function signature was simplified.
183-183: LGTM: UpdateOne options correctly updated for driver v2The change from
options.Update()tooptions.UpdateOne()is correct for MongoDB driver v2, providing better type safety for single document update operations.
226-226: LGTM: UpdateOne options correctly updated for driver v2The change from
options.Update()tooptions.UpdateOne()is correct for MongoDB driver v2, providing better type safety for single document update operations.
641-641: LGTM: UpdateOne options correctly updated for driver v2The change from
options.Update()tooptions.UpdateOne()is correct for MongoDB driver v2, providing better type safety for single document update operations.
1559-1559: LGTM: DeleteOne options correctly updated for driver v2The
options.DeleteOne()constructor is correctly used for MongoDB driver v2, providing proper type safety for delete operations.
1573-1573: LGTM: UpdateOne options correctly updated for driver v2The change from
options.Update()tooptions.UpdateOne()is correct for MongoDB driver v2, providing better type safety for single document update operations.
271-271: LGTM: ObjectID type assertion correctly updated for driver v2The change from
primitive.ObjectIDtobson.ObjectIDis correct for MongoDB driver v2, where the ObjectID type has been moved to the bson package.
504-504: LGTM: ObjectID type assertion correctly updated for driver v2The change from
primitive.ObjectIDtobson.ObjectIDis correct for MongoDB driver v2, where the ObjectID type has been moved to the bson package.
1709-1709: LGTM: ObjectID type assertion correctly updated for driver v2The change from
primitive.ObjectIDtobson.ObjectIDis correct for MongoDB driver v2, where the ObjectID type has been moved to the bson package.
1634-1636: LGTM: BSON regex usage correctly updated for driver v2The change to use
bson.Regex{Pattern: ...}instead of a plain string is correct for MongoDB driver v2, providing better type safety for regex operations.
1887-1887: LGTM: Collection options parameter correctly updated for driver v2The change to use
options.Lister[options.CollectionOptions]as the variadic parameter type is correct for MongoDB driver v2, utilizing generics for better type safety.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1405 +/- ##
==========================================
- Coverage 37.72% 37.71% -0.02%
==========================================
Files 182 182
Lines 30350 30364 +14
==========================================
+ Hits 11450 11451 +1
- Misses 17990 18001 +11
- Partials 910 912 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
hackerwins
left a comment
There was a problem hiding this comment.
Thanks for your contribution.
What this PR does / why we need it:
Migrate the MongoDB Go driver from
v1.17tov2.2, following the migration guide, to ensure full compatibility with MongoDB 6.0 - 8.0 and to take advantage of new features introduced in v2, such asclient.BulkWrite().Which issue(s) this PR fixes:
Addresses https://github.com/yorkie-team/devops/issues/199
Special notes for your reviewer:
Does this PR introduce a user-facing change?:
Additional documentation:
Checklist:
Summary by CodeRabbit