diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 4c5928dfc..d2428777c 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -27,7 +27,7 @@ import ( "github.com/cgrates/cgrates/utils" "github.com/ugorji/go/codec" - "github.com/mongodb/mongo-go-driver/bson" + "go.mongodb.org/mongo-driver/bson" ) type Storage interface { diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 7ef74257c..7d9818bc9 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -33,15 +33,14 @@ import ( "github.com/cgrates/cgrates/utils" "github.com/cgrates/ltcache" - "github.com/mongodb/mongo-go-driver/bson" - "github.com/mongodb/mongo-go-driver/bson/bsoncodec" - "github.com/mongodb/mongo-go-driver/bson/bsonrw" - "github.com/mongodb/mongo-go-driver/bson/bsontype" - "github.com/mongodb/mongo-go-driver/bson/primitive" - "github.com/mongodb/mongo-go-driver/mongo" - "github.com/mongodb/mongo-go-driver/mongo/options" - "github.com/mongodb/mongo-go-driver/x/bsonx" - "github.com/mongodb/mongo-go-driver/x/network/command" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/bsoncodec" + "go.mongodb.org/mongo-driver/bson/bsonrw" + "go.mongodb.org/mongo-driver/bson/bsontype" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/x/bsonx" ) const ( @@ -147,10 +146,12 @@ func NewMongoStorage(host, port, db, user, pass, storageType string, cdrsIndexes url = "mongodb://" + url reg := bson.NewRegistryBuilder().RegisterDecoder(tTime, bsoncodec.ValueDecoderFunc(TimeDecodeValue1)).Build() opt := options.Client(). + ApplyURI(url). SetRegistry(reg). - SetServerSelectionTimeout(ttl) + SetServerSelectionTimeout(ttl). + SetRetryWrites(false) // set this option to false because as default it is on true - client, err := mongo.NewClientWithOptions(url, opt) + client, err := mongo.NewClient(opt) // client, err := mongo.NewClient(url) if err != nil { @@ -259,16 +260,16 @@ func (ms *MongoStorage) GetContext() context.Context { return ms.ctx } -// func isNotFound(err error) bool { -// de, ok := err.(mongo.CommandError) -// if !ok { // if still can't converted to the mongo.CommandError check if error do not contains message -// return strings.Contains(err.Error(), "ns not found") -// } -// return de.Code == 26 || de.Message == "ns not found" -// } +func isNotFound(err error) bool { + de, ok := err.(mongo.CommandError) + if !ok { // if still can't converted to the mongo.CommandError check if error do not contains message + return strings.Contains(err.Error(), "ns not found") + } + return de.Code == 26 || de.Message == "ns not found" +} func (ms *MongoStorage) ensureIndexesForCol(col string) (err error) { // exported for migrator - if err = ms.dropAllIndexesForCol(col); err != nil && !command.IsNotFound(err) { // make sure you do not have indexes + if err = ms.dropAllIndexesForCol(col); err != nil && !isNotFound(err) { // make sure you do not have indexes return } err = nil diff --git a/engine/storage_mongo_stordb.go b/engine/storage_mongo_stordb.go index d652c40a4..207d60b69 100644 --- a/engine/storage_mongo_stordb.go +++ b/engine/storage_mongo_stordb.go @@ -27,10 +27,10 @@ import ( "github.com/cgrates/cgrates/utils" - "github.com/mongodb/mongo-go-driver/bson" - "github.com/mongodb/mongo-go-driver/mongo" - "github.com/mongodb/mongo-go-driver/mongo/options" - "github.com/mongodb/mongo-go-driver/x/bsonx" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/x/bsonx" ) func (ms *MongoStorage) GetTpIds(colName string) (tpids []string, err error) { @@ -1572,13 +1572,10 @@ func (ms *MongoStorage) RemoveVersions(vrs Versions) (err error) { if len(vrs) == 0 { return ms.query(func(sctx mongo.SessionContext) (err error) { dr, err := ms.getCol(ColVer).DeleteOne(sctx, bson.D{}) - if err != nil { - return err - } if dr.DeletedCount == 0 { return utils.ErrNotFound } - return nil + return err }) } return ms.query(func(sctx mongo.SessionContext) (err error) { diff --git a/glide.lock b/glide.lock index 8984f7f93..59151e32a 100644 --- a/glide.lock +++ b/glide.lock @@ -151,25 +151,37 @@ imports: version: 7eeb5667e42c09cb51bf7b7c28aea8c56767da90 - name: github.com/xdg/stringprep version: 73f8eece6fdcd902c185bf651de50f3828bed5ed -- name: github.com/mongodb/mongo-go-driver - version: 4f3b0a882e7d5f83c3d1ab4d0e530a51642fce03 +- name: go.mongodb.org/mongo-driver + version: 9ec4480161a76f5267d56fc836b7f6d357fd9209 subpackages: - bson - bson/bsoncodec - bson/bsonrw - bson/bsontype - bson/primitive + - event - internal - mongo + - mongo/options - mongo/readconcern - mongo/readpref - mongo/writeconcern + - tag + - version - x/bsonx - x/bsonx/bsoncore + - x/mongo/driver + - x/mongo/driver/address + - x/mongo/driver/auth + - x/mongo/driver/auth/internal/gssapi + - x/mongo/driver/connstring + - x/mongo/driver/description + - x/mongo/driver/dns + - x/mongo/driver/operation + - x/mongo/driver/session - x/mongo/driver/topology - x/mongo/driver/uuid - x/mongo/driver/wiremessage - - mongo/options - name: go.opencensus.io version: b4a14686f0a98096416fe1b4cb848e384fb2b22b subpackages: diff --git a/glide.yaml b/glide.yaml index b921b5ac4..4b60ea54d 100644 --- a/glide.yaml +++ b/glide.yaml @@ -38,7 +38,7 @@ import: - package: github.com/cgrates/radigo - package: github.com/cgrates/ltcache - package: github.com/antchfx/xmlquery -- package: github.com/mongodb/mongo-go-driver +- package: go.mongodb.org/mongo-driver subpackages: - bson - bson/bsoncodec diff --git a/migrator/storage_mongo_datadb.go b/migrator/storage_mongo_datadb.go index 4b4b98b2f..0e92dc220 100644 --- a/migrator/storage_mongo_datadb.go +++ b/migrator/storage_mongo_datadb.go @@ -21,9 +21,9 @@ package migrator import ( "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" - "github.com/mongodb/mongo-go-driver/bson" - "github.com/mongodb/mongo-go-driver/mongo" - "github.com/mongodb/mongo-go-driver/mongo/options" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) const ( @@ -72,12 +72,10 @@ func (mgoMig *mongoMigrator) DataManager() *engine.DataManager { //get func (v1ms *mongoMigrator) getv1Account() (v1Acnt *v1Account, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v1AccountDBPrefix).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v1AccountDBPrefix).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -109,12 +107,10 @@ func (v1ms *mongoMigrator) remV1Account(id string) (err error) { //get func (v1ms *mongoMigrator) getv2Account() (v2Acnt *v2Account, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v2AccountsCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v2AccountsCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -147,12 +143,10 @@ func (v1ms *mongoMigrator) remV2Account(id string) (err error) { func (v1ms *mongoMigrator) getV1ActionPlans() (v1aps *v1ActionPlans, err error) { strct := new(AtKeyValue) if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection("actiontimings").Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection("actiontimings").Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -177,12 +171,10 @@ func (v1ms *mongoMigrator) setV1ActionPlans(x *v1ActionPlans) (err error) { func (v1ms *mongoMigrator) getV1Actions() (v1acs *v1Actions, err error) { strct := new(AcKeyValue) if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection("actions").Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection("actions").Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -206,12 +198,10 @@ func (v1ms *mongoMigrator) setV1Actions(x *v1Actions) (err error) { //get func (v1ms *mongoMigrator) getV1ActionTriggers() (v1acts *v1ActionTriggers, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v1ActionTriggersCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v1ActionTriggersCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -240,12 +230,10 @@ func (v1ms *mongoMigrator) setV1ActionTriggers(act *v1ActionTriggers) (err error //get func (v1ms *mongoMigrator) getV1SharedGroup() (v1sg *v1SharedGroup, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(utils.SHARED_GROUP_PREFIX).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(utils.SHARED_GROUP_PREFIX).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -269,12 +257,10 @@ func (v1ms *mongoMigrator) setV1SharedGroup(x *v1SharedGroup) (err error) { //get func (v1ms *mongoMigrator) getV1Stats() (v1st *v1Stat, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(utils.CDR_STATS_PREFIX).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(utils.CDR_STATS_PREFIX).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -298,12 +284,10 @@ func (v1ms *mongoMigrator) setV1Stats(x *v1Stat) (err error) { //get func (v1ms *mongoMigrator) getV2ActionTrigger() (v2at *v2ActionTrigger, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v1ActionTriggersCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v1ActionTriggersCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -327,12 +311,10 @@ func (v1ms *mongoMigrator) setV2ActionTrigger(x *v2ActionTrigger) (err error) { //get func (v1ms *mongoMigrator) getV1AttributeProfile() (v1attrPrf *v1AttributeProfile, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v1AttributeProfilesCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v1AttributeProfilesCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -356,12 +338,10 @@ func (v1ms *mongoMigrator) setV1AttributeProfile(x *v1AttributeProfile) (err err //get func (v1ms *mongoMigrator) getV2ThresholdProfile() (v2T *v2Threshold, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v2ThresholdProfileCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v2ThresholdProfileCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -391,12 +371,10 @@ func (v1ms *mongoMigrator) remV2ThresholdProfile(tenant, id string) (err error) //get func (v1ms *mongoMigrator) getV1Alias() (v1a *v1Alias, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v1AliasCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v1AliasCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -471,12 +449,10 @@ func (v1ms *mongoMigrator) remV1Alias(key string) (err error) { //get func (v1ms *mongoMigrator) getV1User() (v1u *v1UserProfile, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v1UserCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v1UserCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -515,12 +491,10 @@ func (v1ms *mongoMigrator) remV1User(key string) (err error) { //get func (v1ms *mongoMigrator) getV1DerivedChargers() (v1d *v1DerivedChargersWithKey, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v1DerivedChargersCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v1DerivedChargersCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -553,12 +527,10 @@ func (v1ms *mongoMigrator) remV1DerivedChargers(key string) (err error) { //get func (v1ms *mongoMigrator) getV2AttributeProfile() (v2attrPrf *v2AttributeProfile, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v1AttributeProfilesCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v1AttributeProfilesCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -588,12 +560,10 @@ func (v1ms *mongoMigrator) remV2AttributeProfile(tenant, id string) (err error) //get func (v1ms *mongoMigrator) getV3AttributeProfile() (v3attrPrf *v3AttributeProfile, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(v1AttributeProfilesCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v1AttributeProfilesCol).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) diff --git a/migrator/storage_mongo_stordb.go b/migrator/storage_mongo_stordb.go index e02c748c2..6421ecf9f 100644 --- a/migrator/storage_mongo_stordb.go +++ b/migrator/storage_mongo_stordb.go @@ -21,8 +21,8 @@ package migrator import ( "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" - "github.com/mongodb/mongo-go-driver/bson" - "github.com/mongodb/mongo-go-driver/mongo" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" ) func newMongoStorDBMigrator(stor engine.StorDB) (mgoMig *mongoStorDBMigrator) { @@ -51,12 +51,10 @@ func (mgoMig *mongoStorDBMigrator) StorDB() engine.StorDB { //get func (v1ms *mongoStorDBMigrator) getV1CDR() (v1Cdr *v1Cdrs, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(engine.ColCDRs).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(engine.ColCDRs).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext()) @@ -96,12 +94,10 @@ func (v1ms *mongoStorDBMigrator) createV1SMCosts() (err error) { //get func (v1ms *mongoStorDBMigrator) getV2SMCost() (v2Cost *v2SessionsCost, err error) { if v1ms.cursor == nil { - var cursor mongo.Cursor - cursor, err = v1ms.mgoDB.DB().Collection(utils.SessionCostsTBL).Find(v1ms.mgoDB.GetContext(), bson.D{}) + v1ms.cursor, err = v1ms.mgoDB.DB().Collection(utils.SessionCostsTBL).Find(v1ms.mgoDB.GetContext(), bson.D{}) if err != nil { return nil, err } - v1ms.cursor = &cursor } if !(*v1ms.cursor).Next(v1ms.mgoDB.GetContext()) { (*v1ms.cursor).Close(v1ms.mgoDB.GetContext())