Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-10409] Updating default Mongo driver to mongo-go #783

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1436,8 +1436,8 @@ This can also be set at a pump level. For example:

The `driver` setting defines the driver type to use for Mongo Pumps. It can be one of the following values:

- `mongo-go`: Uses the official MongoDB driver. This driver supports Mongo versions greater or equal to v4. You can get more information about this driver [here](https://github.com/mongodb/mongo-go-driver).
- `mgo` (default): Uses the mgo driver. This driver is deprecated. This driver supports Mongo versions lower or equal to v4. You can get more information about this driver [here](https://github.com/go-mgo/mgo)
- `mongo-go` (default): Uses the official MongoDB driver. This driver supports Mongo versions greater or equal to v4. You can get more information about this driver [here](https://github.com/mongodb/mongo-go-driver).
- `mgo`: Uses the mgo driver. This driver is deprecated. This driver supports Mongo versions lower or equal to v4. You can get more information about this driver [here](https://github.com/go-mgo/mgo)

```json
"mongo": {
Expand Down
2 changes: 1 addition & 1 deletion pumps/mgo_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Conn) ConnectDb() {
if c.Store == nil {
var err error
c.Store, err = persistent.NewPersistentStorage(&persistent.ClientOpts{
Type: "mgo",
Type: "mongo-go",
ConnectionString: dbAddr,
})
if err != nil {
Expand Down
16 changes: 11 additions & 5 deletions pumps/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type BaseMongoConf struct {
// Set the consistency mode for the session, it defaults to `Strong`. The valid values are: strong, monotonic, eventual.
MongoSessionConsistency string `json:"mongo_session_consistency" mapstructure:"mongo_session_consistency"`
// MongoDriverType is the type of the driver (library) to use. The valid values are: “mongo-go” and “mgo”.
// Default to “mgo”. Check out this guide to [learn about MongoDB drivers supported by Tyk Pump](https://github.com/TykTechnologies/tyk-pump#driver-type).
// Since v1.9, the default driver is "mongo-go". Check out this guide to [learn about MongoDB drivers supported by Tyk Pump](https://github.com/TykTechnologies/tyk-pump#driver-type).
MongoDriverType string `json:"driver" mapstructure:"driver"`
// MongoDirectConnection informs whether to establish connections only with the specified seed servers,
// or to obtain information for the whole cluster and establish connections with further servers too.
Expand Down Expand Up @@ -353,10 +353,7 @@ func (m *MongoPump) ensureIndexes(collectionName string) error {
}

func (m *MongoPump) connect() {
if m.dbConf.MongoDriverType == "" {
// Default to mgo
m.dbConf.MongoDriverType = persistent.Mgo
}
m.dbConf.MongoDriverType = getMongoDriverType(m.dbConf.MongoDriverType)

store, err := persistent.NewPersistentStorage(&persistent.ClientOpts{
ConnectionString: m.dbConf.MongoURL,
Expand Down Expand Up @@ -547,3 +544,12 @@ func (m *MongoPump) WriteUptimeData(data []interface{}) {
m.log.Error("Problem inserting to mongo collection: ", err)
}
}

func getMongoDriverType(driverType string) string {
if driverType == "" {
// Default to mongo-go
return persistent.OfficialMongo
}

return driverType
}
5 changes: 1 addition & 4 deletions pumps/mongo_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ func (m *MongoAggregatePump) Init(config interface{}) error {
func (m *MongoAggregatePump) connect() {
var err error

if m.dbConf.MongoDriverType == "" {
// Default to mgo
m.dbConf.MongoDriverType = persistent.Mgo
}
m.dbConf.MongoDriverType = getMongoDriverType(m.dbConf.MongoDriverType)

m.store, err = persistent.NewPersistentStorage(&persistent.ClientOpts{
ConnectionString: m.dbConf.MongoURL,
Expand Down
2 changes: 1 addition & 1 deletion pumps/mongo_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,5 +510,5 @@ func TestDefaultDriverAggregate(t *testing.T) {
defaultConf.MongoDriverType = ""
err := newPump.Init(defaultConf)
assert.Nil(t, err)
assert.Equal(t, persistent.Mgo, newPump.dbConf.MongoDriverType)
assert.Equal(t, persistent.OfficialMongo, newPump.dbConf.MongoDriverType)
}
5 changes: 1 addition & 4 deletions pumps/mongo_selective.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ func (m *MongoSelectivePump) Init(config interface{}) error {
func (m *MongoSelectivePump) connect() {
var err error

if m.dbConf.MongoDriverType == "" {
// Default to mgo
m.dbConf.MongoDriverType = persistent.Mgo
}
m.dbConf.MongoDriverType = getMongoDriverType(m.dbConf.MongoDriverType)

m.store, err = persistent.NewPersistentStorage(&persistent.ClientOpts{
ConnectionString: m.dbConf.MongoURL,
Expand Down
2 changes: 1 addition & 1 deletion pumps/mongo_selective_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,5 +389,5 @@ func TestDefaultDriverSelective(t *testing.T) {
defaultConf.MongoDriverType = ""
err := newPump.Init(defaultConf)
assert.Nil(t, err)
assert.Equal(t, persistent.Mgo, newPump.dbConf.MongoDriverType)
assert.Equal(t, persistent.OfficialMongo, newPump.dbConf.MongoDriverType)
}
34 changes: 33 additions & 1 deletion pumps/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func TestDefaultDriver(t *testing.T) {
defaultConf.MongoDriverType = ""
err := newPump.Init(defaultConf)
assert.Nil(t, err)
assert.Equal(t, persistent.Mgo, newPump.dbConf.MongoDriverType)
assert.Equal(t, persistent.OfficialMongo, newPump.dbConf.MongoDriverType)
}

func TestMongoPump_WriteData(t *testing.T) {
Expand Down Expand Up @@ -657,3 +657,35 @@ func TestMongoPump_WriteData(t *testing.T) {
return records
}))
}
func TestGetMongoDriverType(t *testing.T) {
tests := []struct {
name string
driverType string
want string
}{
{
name: "Empty driver type",
driverType: "",
want: persistent.OfficialMongo,
},
{
name: "mongo-go driver type",
driverType: persistent.OfficialMongo,
want: persistent.OfficialMongo,
},
{
name: "mgo driver type",
driverType: persistent.Mgo,
want: persistent.Mgo,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getMongoDriverType(tt.driverType)
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
Loading