Skip to content

Upgrade to MongoDB Go Driver v2#97

Open
hadifarnoud wants to merge 3 commits intomasterfrom
claude/update-mongo-orm-deps-dRp2M
Open

Upgrade to MongoDB Go Driver v2#97
hadifarnoud wants to merge 3 commits intomasterfrom
claude/update-mongo-orm-deps-dRp2M

Conversation

@hadifarnoud
Copy link
Copy Markdown
Member

Summary

This PR upgrades the codebase from MongoDB Go Driver v1 to v2, including comprehensive test coverage for hook execution and field operations.

Key Changes

Driver Upgrade

  • Updated all imports from go.mongodb.org/mongo-driver to go.mongodb.org/mongo-driver/v2
  • Updated go.mod to require go.mongodb.org/mongo-driver/v2 v2.1.0
  • Updated Go version requirement from 1.17 to 1.22
  • Updated CI/CD to test against Go 1.22.x and 1.23.x

API Changes for v2 Compatibility

  • Changed mongo.NewClient() to mongo.Connect() in connection.go - v2 combines client creation and connection
  • Updated option signatures from ...*options.XxxOptions to ...options.Lister[options.XxxOptions] across:
    • FindByID, First, Create, Update, Delete, Replace operations
    • NewCollection and CollectionByName functions
    • Coll utility function
  • Updated TransactionFunc signature from mongo.Session, mongo.SessionContext to *mongo.Session, context.Context
  • Changed primitive.ObjectID to bson.ObjectID throughout the codebase

New Test Coverage

  • Added hooks_internal_test.go with comprehensive tests for hook execution:
    • Tests for legacy hooks (without context) with fallback behavior
    • Tests for context-aware hooks with proper context propagation
    • Tests for error handling and short-circuit behavior
    • Tests for models with no hooks
  • Added field_test.go with tests for field operations:
    • DateFields hook tests for CreatedAt and UpdatedAt timestamps
    • IDField tests for ID getters, setters, and ID preparation
    • Tests for hex string conversion and error cases

Code Quality Improvements

  • Fixed import ordering to follow Go conventions (stdlib, then external packages)
  • Updated test assertions to use require instead of assert where appropriate
  • Improved test helper functions for better testability

Notable Implementation Details

  • Hook system maintains backward compatibility by supporting both legacy (no-context) and context-aware hook signatures
  • The PrepareID method handles multiple ID formats: bson.ObjectID, hex strings, and pass-through for other types
  • Timestamp fields are automatically set during create/update operations via hooks

https://claude.ai/code/session_01SBGcfD2L5xdCZxmVVxMMcQ

- Update all import paths from go.mongodb.org/mongo-driver to go.mongodb.org/mongo-driver/v2
- Replace primitive.ObjectID with bson.ObjectID (bson/primitive merged into bson)
- Replace mongo.NewClient + client.Connect with mongo.Connect (NewClient removed in v2)
- Update transaction API: mongo.Session is now *mongo.Session (concrete type),
  mongo.SessionContext replaced with context.Context
- Update options API: use options.Lister[T] generic interface for collection
  method parameters (FindOneOptions, InsertOneOptions, UpdateOneOptions, etc.)
- Replace UpsertTrueOption to use options.UpdateOne().SetUpsert(true) builder
- Bump minimum Go version from 1.17 to 1.22
- Update Travis CI to test against Go 1.22.x and 1.23.x

https://claude.ai/code/session_01SBGcfD2L5xdCZxmVVxMMcQ
- field_test.go: Tests for DateFields.Creating/Saving hooks (timestamp setting),
  IDField.GetID/SetID (including panic on wrong type), PrepareID edge cases
  (empty string, short hex, nil, non-string passthrough)
- hooks_internal_test.go: Tests for hook fallback logic (WithCtx preferred
  over legacy), context propagation, error short-circuiting, no-hook models
- util_test.go: Strengthen UpsertTrueOption test to verify List() output

https://claude.ai/code/session_01SBGcfD2L5xdCZxmVVxMMcQ
Integration tests (collection_test.go):
- FindByID with ObjectID, hex string, empty string
- FindByID/Create/Update/SimpleFind with options.Lister passthrough
- Context cancellation for FindByID, Create, SimpleFind
- Create/Update timestamp verification
- Update with UpsertTrueOption
- SimpleFind on empty collection, with limit/sort options
- SimpleAggregate with empty stages, mixed builder/bson.M stages
- SimpleAggregateCursor with context

Integration tests (transaction_test.go):
- Transaction error propagation
- Multi-operation commit
- TransactionWithClient explicit client

Connection tests (connection_test.go):
- Context deadline matches configured timeout
- NewCtx with custom timeout
- SetDefaultConfig with custom timeout
- CollectionByName with various names

Escape tests (escape_test.go):
- Empty string, single-char ($, .), multiples, unicode passthrough
- Full round-trip property test

Builder edge tests (builder/builder_edge_test.go):
- All-nil params for every aggregate stage builder
- Zero/false values (verifying they are NOT filtered as nil)
- S() with no operators, multiple operators
- Partial nil params for Lookup, Merge, Unwind
- Group with nil params map and nil values in params

Internal util tests (internal/util/util_test.go):
- IsNil: nil, non-nil, nil pointer/slice/map/func/chan, zero values
- AnyNil: all-nil, none-nil, mixed, empty args
- ToSnakeCase: simple, single word, acronyms, empty, all caps
- PanicErr: nil vs error

Util tests (util_test.go):
- CollectionGetter interface (Coll uses custom collection)
- CollName ignores CollectionGetter (uses reflection)
- CollName pluralizations (Category, Person, Status)

https://claude.ai/code/session_01SBGcfD2L5xdCZxmVVxMMcQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants