From 289a8c067a52f6f281f40dfbfb4e792ad41796d8 Mon Sep 17 00:00:00 2001 From: Anevo Date: Wed, 23 May 2018 08:27:01 -0400 Subject: [PATCH] Added remove function for Thresholds profile and updated thresholds_it_test in migrator --- migrator/migrator_datadb.go | 1 + migrator/storage_mongo_datadb.go | 6 ++ migrator/storage_redis.go | 6 ++ migrator/thresholds.go | 3 + migrator/thresholds_it_test.go | 101 +++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) diff --git a/migrator/migrator_datadb.go b/migrator/migrator_datadb.go index 14bf124bd..e1c5e51f3 100644 --- a/migrator/migrator_datadb.go +++ b/migrator/migrator_datadb.go @@ -43,5 +43,6 @@ type MigratorDataDB interface { setV1AttributeProfile(x *v1AttributeProfile) (err error) getV2ThresholdProfile() (v2T *v2Threshold, err error) setV2ThresholdProfile(x *v2Threshold) (err error) + remV2ThresholdProfile(tenant, id string) (err error) DataManager() *engine.DataManager } diff --git a/migrator/storage_mongo_datadb.go b/migrator/storage_mongo_datadb.go index 61a7e56ad..6b3e31f23 100644 --- a/migrator/storage_mongo_datadb.go +++ b/migrator/storage_mongo_datadb.go @@ -22,6 +22,7 @@ import ( "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" "github.com/cgrates/mgo" + "github.com/cgrates/mgo/bson" ) const ( @@ -283,3 +284,8 @@ func (v1ms *mongoMigrator) setV2ThresholdProfile(x *v2Threshold) (err error) { } return } + +//rem +func (v1ms *mongoMigrator) remV2ThresholdProfile(tenant, id string) (err error) { + return v1ms.mgoDB.DB().C(v2ThresholdProfileCol).Remove(bson.M{"tenant": tenant, "id": id}) +} diff --git a/migrator/storage_redis.go b/migrator/storage_redis.go index b4923c267..06e859649 100644 --- a/migrator/storage_redis.go +++ b/migrator/storage_redis.go @@ -455,3 +455,9 @@ func (v1rs *redisMigrator) setV2ThresholdProfile(x *v2Threshold) (err error) { } return } + +//rem +func (v1rs *redisMigrator) remV2ThresholdProfile(tenant, id string) (err error) { + key := utils.ThresholdProfilePrefix + utils.ConcatenatedKey(tenant, id) + return v1rs.rds.Cmd("DEL", key).Err +} diff --git a/migrator/thresholds.go b/migrator/thresholds.go index 97786c812..b0a95a4f8 100644 --- a/migrator/thresholds.go +++ b/migrator/thresholds.go @@ -147,6 +147,9 @@ func (m *Migrator) migrateV2Thresholds() (err error) { if v2T != nil { th := v2T.V2toV3Threshold() if m.dryRun != true { + if err = m.dmIN.remV2ThresholdProfile(v2T.Tenant, v2T.ID); err != nil { + return err + } if err = m.dmOut.DataManager().SetThresholdProfile(th, true); err != nil { return err } diff --git a/migrator/thresholds_it_test.go b/migrator/thresholds_it_test.go index b45c5662f..155d9f48a 100644 --- a/migrator/thresholds_it_test.go +++ b/migrator/thresholds_it_test.go @@ -203,6 +203,71 @@ func testTrsITMigrateAndMove(t *testing.T) { ActivationInterval: &utils.ActivationInterval{v1trs.ExpirationDate, v1trs.ActivationDate}, MinSleep: v1trs.MinSleep, } + + v2trs := &v2Threshold{ + Tenant: "cgrates.org", + ID: "th_rec", + FilterIDs: []string{}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + Recurrent: true, + MinHits: 0, + MinSleep: time.Duration(5 * time.Minute), + Blocker: false, + Weight: 20.0, + ActionIDs: []string{}, + Async: false, + } + + tresProf2 := &engine.ThresholdProfile{ + Tenant: "cgrates.org", + ID: "th_rec", + FilterIDs: []string{}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + MaxHits: -1, + MinHits: 0, + MinSleep: time.Duration(5 * time.Minute), + Blocker: false, + Weight: 20.0, + ActionIDs: []string{}, + Async: false, + } + + v2trs_nonrec := &v2Threshold{ + Tenant: "cgrates.org", + ID: "th_nonrec", + FilterIDs: []string{}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + Recurrent: false, + MinHits: 0, + MinSleep: time.Duration(5 * time.Minute), + Blocker: false, + Weight: 20.0, + ActionIDs: []string{}, + Async: false, + } + + tresProf3 := &engine.ThresholdProfile{ + Tenant: "cgrates.org", + ID: "th_nonrec", + FilterIDs: []string{}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), + }, + MaxHits: 1, + MinHits: 0, + MinSleep: time.Duration(5 * time.Minute), + Blocker: false, + Weight: 20.0, + ActionIDs: []string{}, + Async: false, + } + switch trsThresholds { case utils.Migrate: err := trsMigrator.dmIN.setV2ActionTrigger(v1trs) @@ -233,6 +298,42 @@ func testTrsITMigrateAndMove(t *testing.T) { } else if !reflect.DeepEqual(tresProf.MinSleep, result.MinSleep) { t.Errorf("Expecting: %+v, received: %+v", tresProf.MinSleep, result.MinSleep) } + //Migrate V2Threshold to NewThreshold + err = trsMigrator.dmIN.setV2ThresholdProfile(v2trs) + if err != nil { + t.Error("Error when setting v1 Thresholds ", err.Error()) + } + err = trsMigrator.dmIN.setV2ThresholdProfile(v2trs_nonrec) + if err != nil { + t.Error("Error when setting v1 Thresholds ", err.Error()) + } + + currentVersion = engine.Versions{utils.StatS: 2, utils.Thresholds: 2, utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 2, utils.ActionPlans: 2, utils.SharedGroups: 2} + err = trsMigrator.dmOut.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for Thresholds ", err.Error()) + } + err, _ = trsMigrator.Migrate([]string{utils.MetaThresholds}) + if err != nil { + t.Error("Error when migrating Thresholds ", err.Error()) + } + + result, err = trsMigrator.dmOut.DataManager().GetThresholdProfile(tresProf2.Tenant, tresProf2.ID, false, utils.NonTransactional) + if err != nil { + t.Error("Error when getting Thresholds ", err.Error()) + } + if !reflect.DeepEqual(tresProf2, result) { + t.Errorf("Expectong: %+v, received: %+v", utils.ToJSON(tresProf2), utils.ToJSON(result)) + } + + result, err = trsMigrator.dmOut.DataManager().GetThresholdProfile(tresProf3.Tenant, tresProf3.ID, false, utils.NonTransactional) + if err != nil { + t.Error("Error when getting Thresholds ", err.Error()) + } + if !reflect.DeepEqual(tresProf3, result) { + t.Errorf("Expectong: %+v, received: %+v", utils.ToJSON(tresProf3), utils.ToJSON(result)) + } + case utils.Move: if err := trsMigrator.dmIN.DataManager().SetThresholdProfile(tresProf, false); err != nil { t.Error("Error when setting Thresholds ", err.Error())