Skip to content

Commit

Permalink
Updating default Mongo driver to mongo-go
Browse files Browse the repository at this point in the history
  • Loading branch information
mativm02 committed Jan 29, 2024
1 parent d9d64dc commit d260290
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
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: 10 additions & 6 deletions pumps/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
7 changes: 1 addition & 6 deletions pumps/mongo_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions pumps/mongo_selective.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions pumps/mongo_test.go
Original file line number Diff line number Diff line change
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)
}
})
}
}

0 comments on commit d260290

Please sign in to comment.