diff --git a/README.md b/README.md index 6e74388a4..4054468de 100644 --- a/README.md +++ b/README.md @@ -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": { diff --git a/pumps/mgo_helper_test.go b/pumps/mgo_helper_test.go index c439efd42..a85a51517 100644 --- a/pumps/mgo_helper_test.go +++ b/pumps/mgo_helper_test.go @@ -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 { diff --git a/pumps/mongo.go b/pumps/mongo.go index 479b087e9..90b6803f2 100644 --- a/pumps/mongo.go +++ b/pumps/mongo.go @@ -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. @@ -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, @@ -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 +} diff --git a/pumps/mongo_aggregate.go b/pumps/mongo_aggregate.go index 97362447f..951040cfd 100644 --- a/pumps/mongo_aggregate.go +++ b/pumps/mongo_aggregate.go @@ -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, diff --git a/pumps/mongo_aggregate_test.go b/pumps/mongo_aggregate_test.go index 78d8dbd20..5a8a726a6 100644 --- a/pumps/mongo_aggregate_test.go +++ b/pumps/mongo_aggregate_test.go @@ -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) } diff --git a/pumps/mongo_selective.go b/pumps/mongo_selective.go index 1f0d78650..c8380c372 100644 --- a/pumps/mongo_selective.go +++ b/pumps/mongo_selective.go @@ -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, diff --git a/pumps/mongo_selective_test.go b/pumps/mongo_selective_test.go index 5580c1209..70f560435 100644 --- a/pumps/mongo_selective_test.go +++ b/pumps/mongo_selective_test.go @@ -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) } diff --git a/pumps/mongo_test.go b/pumps/mongo_test.go index d0a0f888b..acaa704dc 100644 --- a/pumps/mongo_test.go +++ b/pumps/mongo_test.go @@ -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) { @@ -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) + } + }) + } +}