Skip to content

Migrate MongoDB Go driver from v1.17 to v2.2#1405

Merged
hackerwins merged 2 commits into
mainfrom
mongodb-upgrade
Jul 24, 2025
Merged

Migrate MongoDB Go driver from v1.17 to v2.2#1405
hackerwins merged 2 commits into
mainfrom
mongodb-upgrade

Conversation

@krapie

@krapie krapie commented Jul 24, 2025

Copy link
Copy Markdown
Member

What this PR does / why we need it:

Migrate the MongoDB Go driver from v1.17 to v2.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 as client.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:

  • Added relevant tests or not required
  • Addressed and resolved all CodeRabbit review comments
  • Didn't break anything

Summary by CodeRabbit

  • Chores
    • Upgraded the MongoDB Go driver to version 2, updating all related dependencies and import paths.
    • Updated internal handling for BSON encoding/decoding and registry management to align with the new driver API.
    • Removed an unused dependency and updated others for compatibility.
  • Tests
    • Updated test code to use the new MongoDB driver and BSON APIs for encoding and decoding.

@krapie krapie requested a review from hackerwins July 24, 2025 01:31
@krapie krapie self-assigned this Jul 24, 2025
@krapie krapie added the enhancement 🌟 New feature or request label Jul 24, 2025
@coderabbitai

coderabbitai Bot commented Jul 24, 2025

Copy link
Copy Markdown

Walkthrough

This 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

File(s) Change Summary
go.mod Upgraded MongoDB Go driver to v2.2.0, updated snappy, and removed montanaflynn/stats dependency.
server/backend/database/memory/database.go Updated BSON import and changed newID() to use bson.NewObjectID().Hex().
server/backend/database/mongo/client.go Updated MongoDB imports, option constructors, type assertions, and collection options to v2 API.
server/backend/database/mongo/encoder.go Changed BSON import and updated function signatures/types to use bson.ObjectID.
server/backend/database/mongo/indexes.go
server/backend/database/mongo/monitor.go
Updated MongoDB driver import paths to v2.
server/backend/database/mongo/registry.go Refactored BSON registry/codec setup to use v2 API, updated encoder/decoder signatures, added new decoder for types.ID.
server/backend/database/mongo/registry_test.go Refactored BSON encoding/decoding tests to use v2 encoder/decoder APIs and registry usage.
server/backend/database/testcases/testcases.go
server/rpc/connecthelper/status_test.go
Updated MongoDB driver import path to v2.
test/helper/helper.go Updated MongoDB driver imports and client setup to v2 API.

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
Loading

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
The command is terminated due to an 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 - Beta

CodeRabbit'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 Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch mongodb-upgrade

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca8283e and 591b33c.

⛔ Files ignored due to path filters (1)
  • go.sum is 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 correct

The driver’s event package path is now go.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 – ok

The 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, and mongo/options paths 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 v2

Alias 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.Registry directly.

server/backend/database/memory/database.go (2)

27-27: LGTM!

Import path correctly updated. In v2, the primitive package is merged into the main bson package.


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.ObjectID from 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 idDecoder correctly 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 v2

The import paths have been properly updated to use the /v2 suffix, which is the correct approach for migrating to MongoDB Go driver v2.2.


66-66: LGTM: Registry setup correctly updated for driver v2

The change from NewRegistryBuilder().Build() to NewRegistryBuilder() is correct, as the function now returns a *bson.Registry directly in the v2 API.


82-84: LGTM: mongo.Connect API correctly updated for driver v2

The removal of the context parameter from mongo.Connect is correct for MongoDB driver v2, where the function signature was simplified.


183-183: LGTM: UpdateOne options correctly updated for driver v2

The change from options.Update() to options.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 v2

The change from options.Update() to options.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 v2

The change from options.Update() to options.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 v2

The 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 v2

The change from options.Update() to options.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 v2

The change from primitive.ObjectID to bson.ObjectID is 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 v2

The change from primitive.ObjectID to bson.ObjectID is 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 v2

The change from primitive.ObjectID to bson.ObjectID is 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 v2

The 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 v2

The change to use options.Lister[options.CollectionOptions] as the variadic parameter type is correct for MongoDB driver v2, utilizing generics for better type safety.

Comment thread go.mod
@codecov

codecov Bot commented Jul 24, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 58.06452% with 26 lines in your changes missing coverage. Please review.

Project coverage is 37.71%. Comparing base (ca8283e) to head (591b33c).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
server/backend/database/mongo/registry.go 53.48% 18 Missing and 2 partials ⚠️
server/backend/database/mongo/client.go 61.53% 5 Missing ⚠️
test/helper/helper.go 0.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hackerwins hackerwins left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

@hackerwins hackerwins merged commit 4c04758 into main Jul 24, 2025
6 checks passed
@hackerwins hackerwins deleted the mongodb-upgrade branch July 24, 2025 01:46
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Yorkie Project Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement 🌟 New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants