feat: Upgrade to MongoDB Go Driver v2 (Resolves #95)#96
Open
sagargupta2001 wants to merge 3 commits intoKamva:masterfrom
Open
feat: Upgrade to MongoDB Go Driver v2 (Resolves #95)#96sagargupta2001 wants to merge 3 commits intoKamva:masterfrom
sagargupta2001 wants to merge 3 commits intoKamva:masterfrom
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #95
Motivation
The official MongoDB Go driver has released v2 (go.mongodb.org/mongo-driver/v2), introducing breaking changes and new features. Projects upgrading to the v2 driver are currently blocked from using mgm due to type incompatibilities (e.g., *mongo/options.ClientOptions vs the new v2 types). This PR upgrades mgm's core dependency to the v2 driver to unblock downstream users and ensure long-term support.
Because the v2 driver introduces breaking API changes, this PR will likely warrant a major version bump for mgm (e.g., v4).
Summary of Changes
Dependency & Go Version: Bumped the minimum Go version to 1.22 (as the v2 driver requires 1.19+ and relies heavily on generics requiring 1.18+). Upgraded mongo-driver to v2.5.0.
Import Paths: Systematically updated all imports from go.mongodb.org/mongo-driver/... to go.mongodb.org/mongo-driver/v2/....
BSON Primitives: The bson/primitive package was merged into bson in the v2 driver. Updated all references across models and collections (e.g., primitive.ObjectID → bson.ObjectID).
Connection API: Updated connection.go. mongo.NewClient() was removed in v2; refactored setup to use mongo.Connect() directly.
Sessions & Transactions: Replaced the removed mongo.SessionContext with standard context.Context. Handled the shift of mongo.Session to a pointer *mongo.Session, and updated the mongo.WithSession callback and TransactionFunc types accordingly.
Options Pattern (Generics): Adapted to the v2 driver's split of options and introduction of the options.Lister[T] generic pattern. Updated all option references (e.g., *options.UpdateOptions → options.Lister[options.UpdateOneOptions]). Refactored UpsertTrueOption() to utilize the new builder pattern.
Breaking Changes for mgm Users
Downstream users upgrading to this version of mgm will need to:
Update their imports to use the v2 MongoDB driver.
Replace any usage of primitive.ObjectID in their models with bson.ObjectID.
Adjust any custom database option configurations to match the new v2 options.Lister generic types.
Testing Done
[x] Updated local MongoDB to run as a single-node replica set (to support Oplog for transaction tests).
[x] Ran the full mgm test suite locally (go test -v ./...) – all tests pass.
[x] Tested the local mgm build downstream in a separate Go project using the replace directive in go.mod to verify real-world compilation and usage.