Skip to content

Commit

Permalink
mgo driver: Remove the last remaining use of mgocompat
Browse files Browse the repository at this point in the history
  • Loading branch information
feliixx committed Sep 23, 2023
1 parent 55b1407 commit fe422a8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
14 changes: 0 additions & 14 deletions datagen/mongo_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/mgocompat"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
Expand Down Expand Up @@ -108,16 +107,3 @@ func createClientOptions(conn *Connection) *options.ClientOptions {

return connOpts.SetAuth(credentials)
}

func runMgoCompatCommand(ctx context.Context, session *mongo.Client, db string, cmd any) error {
// With the default registry, index.Collation is kept event when it's empty,
// and it make the command fail
// to fix this, marshal the command to a bson.Raw with the mgocompat registry
// providing the same behavior that the old mgo driver
mgoRegistry := mgocompat.NewRespectNilValuesRegistryBuilder().Build()
_, cmdBytes, err := bson.MarshalValueWithRegistry(mgoRegistry, cmd)
if err != nil {
return fmt.Errorf("fait to generate mgocompat command\n cause: %v", err)
}
return session.Database(db).RunCommand(ctx, cmdBytes).Err()
}
6 changes: 5 additions & 1 deletion datagen/mongo_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ func (w *mongoWriter) createCollection(coll *Collection) error {
// as the collection is empty, no need to create the indexes on the sharded key before creating the collection,
// because it will be created automatically by mongodb. See https://docs.mongodb.com/manual/core/sharding-shard-key/#shard-key-indexes
// for details
err = runMgoCompatCommand(context.Background(), w.session, "admin", coll.ShardConfig)
cmdBytes, err := bson.Marshal(coll.ShardConfig)
if err != nil {
return fmt.Errorf("fait to marshal sharding cmd\n cause: %v", err)
}
err = w.session.Database("admin").RunCommand(context.Background(), cmdBytes).Err()
if err != nil {
return fmt.Errorf("fail to shard collection '%s' in db '%s'\n cause: %v", coll.Name, coll.DB, err)
}
Expand Down

0 comments on commit fe422a8

Please sign in to comment.