From d260290dfc244eec4ff591c622d997e98c7e812f Mon Sep 17 00:00:00 2001 From: Matias <83959431+mativm02@users.noreply.github.com> Date: Mon, 29 Jan 2024 09:52:29 -0300 Subject: [PATCH 1/5] Updating default Mongo driver to mongo-go --- pumps/mgo_helper_test.go | 2 +- pumps/mongo.go | 16 ++++++++++------ pumps/mongo_aggregate.go | 7 +------ pumps/mongo_selective.go | 7 +------ pumps/mongo_test.go | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 19 deletions(-) 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..875c17f06 100644 --- a/pumps/mongo.go +++ b/pumps/mongo.go @@ -353,11 +353,6 @@ func (m *MongoPump) ensureIndexes(collectionName string) error { } func (m *MongoPump) connect() { - if m.dbConf.MongoDriverType == "" { - // Default to mgo - m.dbConf.MongoDriverType = persistent.Mgo - } - store, err := persistent.NewPersistentStorage(&persistent.ClientOpts{ ConnectionString: m.dbConf.MongoURL, UseSSL: m.dbConf.MongoUseSSL, @@ -367,7 +362,7 @@ func (m *MongoPump) connect() { SSLPEMKeyfile: m.dbConf.MongoSSLPEMKeyfile, SessionConsistency: m.dbConf.MongoSessionConsistency, ConnectionTimeout: m.timeout, - Type: m.dbConf.MongoDriverType, + Type: getMongoDriverType(m.dbConf.MongoDriverType), DirectConnection: m.dbConf.MongoDirectConnection, }) if err != nil { @@ -547,3 +542,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..c65925b28 100644 --- a/pumps/mongo_aggregate.go +++ b/pumps/mongo_aggregate.go @@ -215,11 +215,6 @@ 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.store, err = persistent.NewPersistentStorage(&persistent.ClientOpts{ ConnectionString: m.dbConf.MongoURL, UseSSL: m.dbConf.MongoUseSSL, @@ -229,7 +224,7 @@ func (m *MongoAggregatePump) connect() { SSLPEMKeyfile: m.dbConf.MongoSSLPEMKeyfile, SessionConsistency: m.dbConf.MongoSessionConsistency, ConnectionTimeout: m.timeout, - Type: m.dbConf.MongoDriverType, + Type: getMongoDriverType(m.dbConf.MongoDriverType), DirectConnection: m.dbConf.MongoDirectConnection, }) if err != nil { diff --git a/pumps/mongo_selective.go b/pumps/mongo_selective.go index 1f0d78650..1aa639f5d 100644 --- a/pumps/mongo_selective.go +++ b/pumps/mongo_selective.go @@ -113,11 +113,6 @@ 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.store, err = persistent.NewPersistentStorage(&persistent.ClientOpts{ ConnectionString: m.dbConf.MongoURL, UseSSL: m.dbConf.MongoUseSSL, @@ -127,7 +122,7 @@ func (m *MongoSelectivePump) connect() { SSLPEMKeyfile: m.dbConf.MongoSSLPEMKeyfile, SessionConsistency: m.dbConf.MongoSessionConsistency, ConnectionTimeout: m.timeout, - Type: m.dbConf.MongoDriverType, + Type: getMongoDriverType(m.dbConf.MongoDriverType), DirectConnection: m.dbConf.MongoDirectConnection, }) if err != nil { diff --git a/pumps/mongo_test.go b/pumps/mongo_test.go index d0a0f888b..ecae6633e 100644 --- a/pumps/mongo_test.go +++ b/pumps/mongo_test.go @@ -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) + } + }) + } +} From 49a1e4541830fd65a3326ae34d5cccfc11155945 Mon Sep 17 00:00:00 2001 From: Matias <83959431+mativm02@users.noreply.github.com> Date: Mon, 29 Jan 2024 10:17:12 -0300 Subject: [PATCH 2/5] fixing TestDefaultDriver test --- pumps/mongo_aggregate_test.go | 2 +- pumps/mongo_selective_test.go | 2 +- pumps/mongo_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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_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 ecae6633e..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) { From 57272b498ea7f55a4cccaa1d999b55aaf0239971 Mon Sep 17 00:00:00 2001 From: Matias <83959431+mativm02@users.noreply.github.com> Date: Mon, 29 Jan 2024 10:27:39 -0300 Subject: [PATCH 3/5] fixing TestDefaultDriver test --- pumps/mongo.go | 4 +++- pumps/mongo_aggregate.go | 4 +++- pumps/mongo_selective.go | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pumps/mongo.go b/pumps/mongo.go index 875c17f06..6169f9e05 100644 --- a/pumps/mongo.go +++ b/pumps/mongo.go @@ -353,6 +353,8 @@ func (m *MongoPump) ensureIndexes(collectionName string) error { } func (m *MongoPump) connect() { + m.dbConf.MongoDriverType = getMongoDriverType(m.dbConf.MongoDriverType) + store, err := persistent.NewPersistentStorage(&persistent.ClientOpts{ ConnectionString: m.dbConf.MongoURL, UseSSL: m.dbConf.MongoUseSSL, @@ -362,7 +364,7 @@ func (m *MongoPump) connect() { SSLPEMKeyfile: m.dbConf.MongoSSLPEMKeyfile, SessionConsistency: m.dbConf.MongoSessionConsistency, ConnectionTimeout: m.timeout, - Type: getMongoDriverType(m.dbConf.MongoDriverType), + Type: m.dbConf.MongoDriverType, DirectConnection: m.dbConf.MongoDirectConnection, }) if err != nil { diff --git a/pumps/mongo_aggregate.go b/pumps/mongo_aggregate.go index c65925b28..951040cfd 100644 --- a/pumps/mongo_aggregate.go +++ b/pumps/mongo_aggregate.go @@ -215,6 +215,8 @@ func (m *MongoAggregatePump) Init(config interface{}) error { func (m *MongoAggregatePump) connect() { var err error + m.dbConf.MongoDriverType = getMongoDriverType(m.dbConf.MongoDriverType) + m.store, err = persistent.NewPersistentStorage(&persistent.ClientOpts{ ConnectionString: m.dbConf.MongoURL, UseSSL: m.dbConf.MongoUseSSL, @@ -224,7 +226,7 @@ func (m *MongoAggregatePump) connect() { SSLPEMKeyfile: m.dbConf.MongoSSLPEMKeyfile, SessionConsistency: m.dbConf.MongoSessionConsistency, ConnectionTimeout: m.timeout, - Type: getMongoDriverType(m.dbConf.MongoDriverType), + Type: m.dbConf.MongoDriverType, DirectConnection: m.dbConf.MongoDirectConnection, }) if err != nil { diff --git a/pumps/mongo_selective.go b/pumps/mongo_selective.go index 1aa639f5d..c8380c372 100644 --- a/pumps/mongo_selective.go +++ b/pumps/mongo_selective.go @@ -113,6 +113,8 @@ func (m *MongoSelectivePump) Init(config interface{}) error { func (m *MongoSelectivePump) connect() { var err error + m.dbConf.MongoDriverType = getMongoDriverType(m.dbConf.MongoDriverType) + m.store, err = persistent.NewPersistentStorage(&persistent.ClientOpts{ ConnectionString: m.dbConf.MongoURL, UseSSL: m.dbConf.MongoUseSSL, @@ -122,7 +124,7 @@ func (m *MongoSelectivePump) connect() { SSLPEMKeyfile: m.dbConf.MongoSSLPEMKeyfile, SessionConsistency: m.dbConf.MongoSessionConsistency, ConnectionTimeout: m.timeout, - Type: getMongoDriverType(m.dbConf.MongoDriverType), + Type: m.dbConf.MongoDriverType, DirectConnection: m.dbConf.MongoDirectConnection, }) if err != nil { From b0de0748b4197760f52291d6f61368cd9bd183f1 Mon Sep 17 00:00:00 2001 From: Matias <83959431+mativm02@users.noreply.github.com> Date: Mon, 29 Jan 2024 13:03:52 -0300 Subject: [PATCH 4/5] updating config comment --- README.md | 4 ++-- pumps/mongo.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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/mongo.go b/pumps/mongo.go index 6169f9e05..40cab91de 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). + // Default to "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. From b2f69ec4e3d7096fc776d9ffcd50c946a24e75a2 Mon Sep 17 00:00:00 2001 From: Matias <83959431+mativm02@users.noreply.github.com> Date: Tue, 30 Jan 2024 09:12:17 -0300 Subject: [PATCH 5/5] modifying config comment --- pumps/mongo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pumps/mongo.go b/pumps/mongo.go index 40cab91de..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 "mongo-go". 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.