From a3eac846d4c44db79a7f4ed49a147717ec237b7a Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 13 Mar 2019 10:50:47 +0200 Subject: [PATCH] Updated migration for attributes --- migrator/attributes.go | 5 +- migrator/attributes_it_test.go | 86 +++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/migrator/attributes.go b/migrator/attributes.go index 30bf6da4a..a892ed553 100644 --- a/migrator/attributes.go +++ b/migrator/attributes.go @@ -137,9 +137,6 @@ func (m *Migrator) migrateV2Attributes() (err error) { if err := m.dmOut.DataManager().SetAttributeProfile(attrPrf, true); err != nil { return err } - if err := m.dmIN.remV2AttributeProfile(v2Attr.Tenant, v2Attr.ID); err != nil { - return err - } m.stats[utils.Attributes] += 1 } if m.dryRun { @@ -248,7 +245,7 @@ func (v2AttrPrf v2AttributeProfile) AsAttributeProfile() (attrPrf *engine.Attrib for _, attr := range v2AttrPrf.Attributes { filterIDs := make([]string, 0) //append false translate to if FieldName exist do stuff - if attr.Append == false { + if !attr.Append { filterIDs = append(filterIDs, utils.MetaExists+":"+attr.FieldName+":") } //Initial not *any translate to if value of fieldName = initial do stuff diff --git a/migrator/attributes_it_test.go b/migrator/attributes_it_test.go index 7551d96ae..1162b82c8 100755 --- a/migrator/attributes_it_test.go +++ b/migrator/attributes_it_test.go @@ -45,6 +45,8 @@ var sTestsAttrIT = []func(t *testing.T){ testAttrITConnect, testAttrITFlush, testAttrITMigrateAndMove, + testAttrITFlush, + testAttrITMigrateV2, } func TestAttributeITRedis(t *testing.T) { @@ -291,7 +293,7 @@ func testAttrITMigrateAndMove(t *testing.T) { result, err := attrMigrator.dmOut.DataManager().GetAttributeProfile("cgrates.org", "ATTR_1", false, false, utils.NonTransactional) if err != nil { - t.Error("Error when getting Attribute ", err.Error()) + t.Fatal("Error when getting Attribute ", err.Error()) } result.Compile() attrPrf.Compile() @@ -335,3 +337,85 @@ func testAttrITMigrateAndMove(t *testing.T) { } } } + +func testAttrITMigrateV2(t *testing.T) { + if attrAction != utils.Migrate { + return + } + + v2attr := &v2AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_1", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"*string:Accont:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Attributes: []*v2Attribute{ + &v2Attribute{ + FieldName: "FL1", + Initial: "In1", + Substitute: config.NewRSRParsersMustCompile("Al1", true, utils.INFIELD_SEP), + Append: true, + }, + }, + Weight: 20, + } + + attrPrf := &engine.AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_1", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"*string:Accont:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Attributes: []*engine.Attribute{ + { + FilterIDs: []string{"*string:FL1:In1"}, + FieldName: "FL1", + Substitute: config.NewRSRParsersMustCompile("Al1", true, utils.INFIELD_SEP), + }, + }, + Weight: 20, + } + + err := attrMigrator.dmIN.setV2AttributeProfile(v2attr) + if err != nil { + t.Error("Error when setting v1 AttributeProfile ", err.Error()) + } + currentVersion := engine.Versions{utils.Attributes: 2} + err = attrMigrator.dmIN.DataManager().DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for Attributes ", err.Error()) + } + + if vrs, err := attrMigrator.dmIN.DataManager().DataDB().GetVersions(""); err != nil { + t.Error(err) + } else if vrs[utils.Attributes] != 2 { + t.Errorf("Unexpected version returned: %d", vrs[utils.Attributes]) + } + + err, _ = attrMigrator.Migrate([]string{utils.MetaAttributes}) + if err != nil { + t.Error("Error when migrating Attributes ", err.Error()) + } + + if vrs, err := attrMigrator.dmOut.DataManager().DataDB().GetVersions(""); err != nil { + t.Error(err) + } else if vrs[utils.Attributes] != 3 { + t.Errorf("Unexpected version returned: %d", vrs[utils.Attributes]) + } + result, err := attrMigrator.dmOut.DataManager().GetAttributeProfile("cgrates.org", + "ATTR_1", false, false, utils.NonTransactional) + if err != nil { + t.Fatal("Error when getting Attribute ", err.Error()) + } + result.Compile() + attrPrf.Compile() + if !reflect.DeepEqual(result, attrPrf) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(attrPrf), utils.ToJSON(result)) + } +}