Fixed migration of action triggers

This commit is contained in:
edwardro22
2017-08-30 00:52:24 +00:00
parent 44ba428e0f
commit e9a7c027a5
2 changed files with 149 additions and 150 deletions

View File

@@ -2,18 +2,19 @@ package migrator
import (
"fmt"
"gopkg.in/mgo.v2/bson"
"log"
"strings"
"time"
"gopkg.in/mgo.v2/bson"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
type v1ActionTrigger struct {
Id string // for visual identification
ThresholdType string //*min_counter, *max_counter, *min_balance, *max_balance
Id string // for visual identification
ThresholdType string //*min_counter, *max_counter, *min_balance, *max_balance
// stats: *min_asr, *max_asr, *min_acd, *max_acd, *min_tcd, *max_tcd, *min_acc, *max_acc, *min_tcc, *max_tcc
ThresholdValue float64
Recurrent bool // reset eexcuted flag each run
MinSleep time.Duration // Minimum duration between two executions in case of recurrent triggers
@@ -50,10 +51,11 @@ func (m *Migrator) migrateActionTriggers() (err error) {
if err != nil {
return err
}
v1atr := v1atrs
if v1atrs != nil {
atr := v1atr.AsActionTrigger()
atrrs = append(atrrs, atr)
for _, v1atr := range *v1atrs {
atr := v1atr.AsActionTrigger()
atrrs = append(atrrs, atr)
}
}
}
if err := m.dataDB.SetActionTriggers(atrrs[0].ID, atrrs, utils.NonTransactional); err != nil {
@@ -98,14 +100,13 @@ func (m *Migrator) migrateActionTriggers() (err error) {
fmt.Sprintf("error: unsupported: <%s> for migrateActionTriggers method", m.dataDBType))
}
}
func (m *Migrator) getV1ActionTriggerFromDB(key string) (v1Atr *v1ActionTrigger, err error) {
func (m *Migrator) getV1ActionTriggerFromDB(key string) (v1Atr *v1ActionTriggers, err error) {
switch m.dataDBType {
case utils.REDIS:
dataDB := m.dataDB.(*engine.RedisStorage)
if strVal, err := dataDB.Cmd("GET", key).Bytes(); err != nil {
return nil, err
} else {
v1Atr := &v1ActionTrigger{Id: key}
if err := m.mrshlr.Unmarshal(strVal, &v1Atr); err != nil {
return nil, err
}
@@ -115,7 +116,7 @@ func (m *Migrator) getV1ActionTriggerFromDB(key string) (v1Atr *v1ActionTrigger,
dataDB := m.dataDB.(*engine.MongoStorage)
mgoDB := dataDB.DB()
defer mgoDB.Session.Close()
v1Atr := new(v1ActionTrigger)
v1Atr := new(v1ActionTriggers)
if err := mgoDB.C(utils.ACTION_TRIGGER_PREFIX).Find(bson.M{"id": key}).One(v1Atr); err != nil {
return nil, err
}

View File

@@ -18,15 +18,15 @@ package migrator
import (
"flag"
// "fmt"
// "path"
"reflect"
"testing"
"time"
"log"
// "fmt"
// "path"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
"log"
"reflect"
"testing"
"time"
)
var (
@@ -37,8 +37,8 @@ var (
onStorCfg string
dbtype string
mig *Migrator
migrate = flag.String("migrate", "", "Fire up automatic migration <*cost_details|*set_versions>")
version = flag.Bool("version", false, "Prints the application version.")
migrate = flag.String("migrate", "", "Fire up automatic migration <*cost_details|*set_versions>")
version = flag.Bool("version", false, "Prints the application version.")
dataDBType = flag.String("datadb_type", config.CgrConfig().DataDbType, "The type of the DataDb database <redis>")
dataDBHost = flag.String("datadb_host", config.CgrConfig().DataDbHost, "The DataDb host to connect to.")
@@ -68,10 +68,10 @@ var (
oldStorDBUser = flag.String("old_stordb_user", config.CgrConfig().StorDBUser, "The storDb user to sign in as.")
oldStorDBPass = flag.String("old_stordb_passwd", config.CgrConfig().StorDBPass, "The storDb user's password.")
loadHistorySize = flag.Int("load_history_size", config.CgrConfig().LoadHistorySize, "Limit the number of records in the load history")
loadHistorySize = flag.Int("load_history_size", config.CgrConfig().LoadHistorySize, "Limit the number of records in the load history")
oldLoadHistorySize = flag.Int("old_load_history_size", config.CgrConfig().LoadHistorySize, "Limit the number of records in the load history")
dbDataEncoding = flag.String("dbdata_encoding", config.CgrConfig().DBDataEncoding, "The encoding used to store object data in strings")
dbDataEncoding = flag.String("dbdata_encoding", config.CgrConfig().DBDataEncoding, "The encoding used to store object data in strings")
oldDBDataEncoding = flag.String("old_dbdata_encoding", config.CgrConfig().DBDataEncoding, "The encoding used to store object data in strings")
)
@@ -86,28 +86,28 @@ var sTestsITMigrator = []func(t *testing.T){
}
func TestOnStorITRedisConnect(t *testing.T) {
dataDB, err := engine.ConfigureDataStorage(*dataDBType, *dataDBHost, *dataDBPort, *dataDBName, *dataDBUser, *dataDBPass, *dbDataEncoding, config.CgrConfig().CacheConfig, *loadHistorySize)
if err != nil {
log.Fatal(err)
}
oldDataDB, err := engine.ConfigureDataStorage(*oldDataDBType, *oldDataDBHost, *oldDataDBPort, *oldDataDBName, *oldDataDBUser, *oldDataDBPass, *oldDBDataEncoding, config.CgrConfig().CacheConfig, *oldLoadHistorySize)
if err != nil {
log.Fatal(err)
}
storDB, err := engine.ConfigureStorStorage(*storDBType, *storDBHost, *storDBPort, *storDBName, *storDBUser, *storDBPass, *dbDataEncoding,
config.CgrConfig().StorDBMaxOpenConns, config.CgrConfig().StorDBMaxIdleConns, config.CgrConfig().StorDBConnMaxLifetime, config.CgrConfig().StorDBCDRSIndexes)
if err != nil {
log.Fatal(err)
}
oldstorDB, err := engine.ConfigureStorStorage(*oldStorDBType, *oldStorDBHost, *oldStorDBPort, *oldStorDBName, *oldStorDBUser, *oldStorDBPass, *oldDBDataEncoding,
config.CgrConfig().StorDBMaxOpenConns, config.CgrConfig().StorDBMaxIdleConns, config.CgrConfig().StorDBConnMaxLifetime, config.CgrConfig().StorDBCDRSIndexes)
if err != nil {
log.Fatal(err)
}
mig,err = NewMigrator(dataDB, *dataDBType, *dbDataEncoding, storDB, *storDBType,oldDataDB,*oldDataDBType,*oldDBDataEncoding,oldstorDB,*oldStorDBType)
if err != nil {
log.Fatal(err)
}
dataDB, err := engine.ConfigureDataStorage(*dataDBType, *dataDBHost, *dataDBPort, *dataDBName, *dataDBUser, *dataDBPass, *dbDataEncoding, config.CgrConfig().CacheConfig, *loadHistorySize)
if err != nil {
log.Fatal(err)
}
oldDataDB, err := engine.ConfigureDataStorage(*oldDataDBType, *oldDataDBHost, *oldDataDBPort, *oldDataDBName, *oldDataDBUser, *oldDataDBPass, *oldDBDataEncoding, config.CgrConfig().CacheConfig, *oldLoadHistorySize)
if err != nil {
log.Fatal(err)
}
storDB, err := engine.ConfigureStorStorage(*storDBType, *storDBHost, *storDBPort, *storDBName, *storDBUser, *storDBPass, *dbDataEncoding,
config.CgrConfig().StorDBMaxOpenConns, config.CgrConfig().StorDBMaxIdleConns, config.CgrConfig().StorDBConnMaxLifetime, config.CgrConfig().StorDBCDRSIndexes)
if err != nil {
log.Fatal(err)
}
oldstorDB, err := engine.ConfigureStorStorage(*oldStorDBType, *oldStorDBHost, *oldStorDBPort, *oldStorDBName, *oldStorDBUser, *oldStorDBPass, *oldDBDataEncoding,
config.CgrConfig().StorDBMaxOpenConns, config.CgrConfig().StorDBMaxIdleConns, config.CgrConfig().StorDBConnMaxLifetime, config.CgrConfig().StorDBCDRSIndexes)
if err != nil {
log.Fatal(err)
}
mig, err = NewMigrator(dataDB, *dataDBType, *dbDataEncoding, storDB, *storDBType, oldDataDB, *oldDataDBType, *oldDBDataEncoding, oldstorDB, *oldStorDBType)
if err != nil {
log.Fatal(err)
}
}
func TestOnStorITRedis(t *testing.T) {
@@ -155,6 +155,7 @@ func testOnStorITFlush(t *testing.T) {
}
}
}
//1
func testMigratorAccounts(t *testing.T) {
@@ -175,7 +176,7 @@ func testMigratorAccounts(t *testing.T) {
if err != nil {
t.Error("Error when setting v1 acc ", err.Error())
}
_,err = mig.getV1AccountFromDB(v1AccountDBPrefix+v1Acc.Id)
_, err = mig.getV1AccountFromDB(v1AccountDBPrefix + v1Acc.Id)
if err != nil {
t.Error("Error when getting v1 acc ", err.Error())
}
@@ -189,27 +190,27 @@ func testMigratorAccounts(t *testing.T) {
}
if !reflect.DeepEqual(testAccount.BalanceMap["*voice"][0], result.BalanceMap["*voice"][0]) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.BalanceMap["*voice"][0], result.BalanceMap["*voice"][0])
}else if !reflect.DeepEqual(testAccount, result) {
} else if !reflect.DeepEqual(testAccount, result) {
t.Errorf("Expecting: %+v, received: %+v", testAccount, result)
}
/*
case dbtype == utils.MONGO:
err := mig.SetV1onMongoAccount(v1AccountDBPrefix, v1Acc)
if err != nil {
t.Error("Error when marshaling ", err.Error())
}
err = mig.Migrate(utils.MetaAccounts)
if err != nil {
t.Error("Error when migrating accounts ", err.Error())
}
result, err := mig.dataDB.GetAccount(testAccount.ID)
if err != nil {
t.Error("Error when getting account ", err.Error())
}
if !reflect.DeepEqual(testAccount, result) {
t.Errorf("Expecting: %+v, received: %+v", testAccount, result)
}
*/
case dbtype == utils.MONGO:
err := mig.SetV1onMongoAccount(v1AccountDBPrefix, v1Acc)
if err != nil {
t.Error("Error when marshaling ", err.Error())
}
err = mig.Migrate(utils.MetaAccounts)
if err != nil {
t.Error("Error when migrating accounts ", err.Error())
}
result, err := mig.dataDB.GetAccount(testAccount.ID)
if err != nil {
t.Error("Error when getting account ", err.Error())
}
if !reflect.DeepEqual(testAccount, result) {
t.Errorf("Expecting: %+v, received: %+v", testAccount, result)
}
*/
}
}
@@ -229,7 +230,7 @@ func testMigratorActionPlans(t *testing.T) {
if err != nil {
t.Error("Error when setting v1 ActionPlan ", err.Error())
}
_,err = mig.getV1ActionPlansFromDB(setv1id)
_, err = mig.getV1ActionPlansFromDB(setv1id)
if err != nil {
t.Error("Error when setting v1 ActionPlan ", err.Error())
}
@@ -248,32 +249,31 @@ func testMigratorActionPlans(t *testing.T) {
} else if ap.ActionTimings[0].Weight != result.ActionTimings[0].Weight || ap.ActionTimings[0].ActionsID != result.ActionTimings[0].ActionsID {
t.Errorf("Expecting: %+v, received: %+v", ap.ActionTimings[0].Weight, result.ActionTimings[0].Weight)
}
/*
case dbtype == utils.MONGO:
err := mig.SetV1onMongoActionPlan(utils.ACTION_PLAN_PREFIX, v1ap)
if err != nil {
t.Error("Error when setting v1 ActionPlans ", err.Error())
}
err = mig.Migrate("migrateActionPlans")
if err != nil {
t.Error("Error when migrating ActionPlans ", err.Error())
}
result, err := mig.dataDB.GetActionPlan(ap.Id, true, utils.NonTransactional)
if err != nil {
t.Error("Error when getting ActionPlan ", err.Error())
}
if ap.Id != result.Id || !reflect.DeepEqual(ap.AccountIDs, result.AccountIDs) {
t.Errorf("Expecting: %+v, received: %+v", *ap, result)
} else if !reflect.DeepEqual(ap.ActionTimings[0].Timing, result.ActionTimings[0].Timing) {
t.Errorf("Expecting: %+v, received: %+v", ap.ActionTimings[0].Timing, result.ActionTimings[0].Timing)
} else if ap.ActionTimings[0].Weight != result.ActionTimings[0].Weight || ap.ActionTimings[0].ActionsID != result.ActionTimings[0].ActionsID {
t.Errorf("Expecting: %+v, received: %+v", ap.ActionTimings[0].Weight, result.ActionTimings[0].Weight)
}
*/
/*
case dbtype == utils.MONGO:
err := mig.SetV1onMongoActionPlan(utils.ACTION_PLAN_PREFIX, v1ap)
if err != nil {
t.Error("Error when setting v1 ActionPlans ", err.Error())
}
err = mig.Migrate("migrateActionPlans")
if err != nil {
t.Error("Error when migrating ActionPlans ", err.Error())
}
result, err := mig.dataDB.GetActionPlan(ap.Id, true, utils.NonTransactional)
if err != nil {
t.Error("Error when getting ActionPlan ", err.Error())
}
if ap.Id != result.Id || !reflect.DeepEqual(ap.AccountIDs, result.AccountIDs) {
t.Errorf("Expecting: %+v, received: %+v", *ap, result)
} else if !reflect.DeepEqual(ap.ActionTimings[0].Timing, result.ActionTimings[0].Timing) {
t.Errorf("Expecting: %+v, received: %+v", ap.ActionTimings[0].Timing, result.ActionTimings[0].Timing)
} else if ap.ActionTimings[0].Weight != result.ActionTimings[0].Weight || ap.ActionTimings[0].ActionsID != result.ActionTimings[0].ActionsID {
t.Errorf("Expecting: %+v, received: %+v", ap.ActionTimings[0].Weight, result.ActionTimings[0].Weight)
}
*/
}
}
//3
func testMigratorActionTriggers(t *testing.T) {
@@ -289,7 +289,7 @@ func testMigratorActionTriggers(t *testing.T) {
Executed: true,
BalanceExpirationDate: tim,
},
}
}
atrs := engine.ActionTriggers{
&engine.ActionTrigger{
ID: "Test",
@@ -322,7 +322,7 @@ func testMigratorActionTriggers(t *testing.T) {
if err != nil {
t.Error("Error when setting v1 ActionTriggers ", err.Error())
}
_,err = mig.getV1ActionTriggerFromDB(setv1id)
_, err = mig.getV1ActionTriggerFromDB(setv1id)
if err != nil {
t.Error("Error when setting v1 ActionTriggers ", err.Error())
}
@@ -398,32 +398,31 @@ func testMigratorActionTriggers(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", atrs[0].Balance.Blocker, result[0].Balance.Blocker)
}
*/
/*
case dbtype == utils.MONGO:
err := mig.SetV1onMongoActionTrigger(utils.ACTION_TRIGGER_PREFIX, v1atrs)
if err != nil {
t.Error("Error when setting v1 ActionTriggers ", err.Error())
}
err = mig.Migrate("migrateActionTriggers")
if err != nil {
t.Error("Error when migrating ActionTriggers ", err.Error())
}
result, err := mig.dataDB.GetActionTriggers(v1atrs.Id, true, utils.NonTransactional)
if err != nil {
t.Error("Error when getting ActionTriggers ", err.Error())
}
if !reflect.DeepEqual(atrs[0], result[0]) {
t.Errorf("Expecting: %+v, received: %+v", atrs[0], result[0])
}
err = mig.DropV1Colection(utils.ACTION_TRIGGER_PREFIX)
if err != nil {
t.Error("Error when flushing v1 ActionTriggers ", err.Error())
}
*/
/*
case dbtype == utils.MONGO:
err := mig.SetV1onMongoActionTrigger(utils.ACTION_TRIGGER_PREFIX, v1atrs)
if err != nil {
t.Error("Error when setting v1 ActionTriggers ", err.Error())
}
err = mig.Migrate("migrateActionTriggers")
if err != nil {
t.Error("Error when migrating ActionTriggers ", err.Error())
}
result, err := mig.dataDB.GetActionTriggers(v1atrs.Id, true, utils.NonTransactional)
if err != nil {
t.Error("Error when getting ActionTriggers ", err.Error())
}
if !reflect.DeepEqual(atrs[0], result[0]) {
t.Errorf("Expecting: %+v, received: %+v", atrs[0], result[0])
}
err = mig.DropV1Colection(utils.ACTION_TRIGGER_PREFIX)
if err != nil {
t.Error("Error when flushing v1 ActionTriggers ", err.Error())
}
*/
}
}
//4
func testMigratorActions(t *testing.T) {
@@ -440,7 +439,7 @@ func testMigratorActions(t *testing.T) {
if err != nil {
t.Error("Error when setting v1 Actions ", err.Error())
}
_,err = mig.getV1ActionFromDB(setv1id)
_, err = mig.getV1ActionFromDB(setv1id)
if err != nil {
t.Error("Error when getting v1 Actions ", err.Error())
}
@@ -455,32 +454,31 @@ func testMigratorActions(t *testing.T) {
if !reflect.DeepEqual(act, result) {
t.Errorf("Expecting: %+v, received: %+v", act, result)
}
/*
case dbtype == utils.MONGO:
err := mig.SetV1onMongoAction(utils.ACTION_PREFIX, v1act)
if err != nil {
t.Error("Error when setting v1 Actions ", err.Error())
}
err = mig.Migrate("migrateActions")
if err != nil {
t.Error("Error when migrating Actions ", err.Error())
}
result, err := mig.dataDB.GetActions(v1act.Id, true, utils.NonTransactional)
if err != nil {
t.Error("Error when getting Actions ", err.Error())
}
if !reflect.DeepEqual(act[0].Balance.Timings, result[0].Balance.Timings) {
t.Errorf("Expecting: %+v, received: %+v", act[0].Balance.Timings, result[0].Balance.Timings)
}
err = mig.DropV1Colection(utils.ACTION_PREFIX)
if err != nil {
t.Error("Error when flushing v1 Actions ", err.Error())
}
/*
case dbtype == utils.MONGO:
err := mig.SetV1onMongoAction(utils.ACTION_PREFIX, v1act)
if err != nil {
t.Error("Error when setting v1 Actions ", err.Error())
}
err = mig.Migrate("migrateActions")
if err != nil {
t.Error("Error when migrating Actions ", err.Error())
}
result, err := mig.dataDB.GetActions(v1act.Id, true, utils.NonTransactional)
if err != nil {
t.Error("Error when getting Actions ", err.Error())
}
if !reflect.DeepEqual(act[0].Balance.Timings, result[0].Balance.Timings) {
t.Errorf("Expecting: %+v, received: %+v", act[0].Balance.Timings, result[0].Balance.Timings)
}
err = mig.DropV1Colection(utils.ACTION_PREFIX)
if err != nil {
t.Error("Error when flushing v1 Actions ", err.Error())
}
*/
}
}
// 5
func testMigratorSharedGroups(t *testing.T) {
@@ -520,23 +518,23 @@ func testMigratorSharedGroups(t *testing.T) {
if !reflect.DeepEqual(sg, result) {
t.Errorf("Expecting: %+v, received: %+v", sg, result)
}
/*
case dbtype == utils.MONGO:
err := mig.SetV1onMongoSharedGroup(utils.SHARED_GROUP_PREFIX, v1sg)
if err != nil {
t.Error("Error when setting v1 SharedGroup ", err.Error())
}
err = mig.Migrate("migrateSharedGroups")
if err != nil {
t.Error("Error when migrating SharedGroup ", err.Error())
}
result, err := mig.dataDB.GetSharedGroup(v1sg.Id, true, utils.NonTransactional)
if err != nil {
t.Error("Error when getting SharedGroup ", err.Error())
}
if !reflect.DeepEqual(sg, result) {
t.Errorf("Expecting: %+v, received: %+v", sg, result)
}
/*
case dbtype == utils.MONGO:
err := mig.SetV1onMongoSharedGroup(utils.SHARED_GROUP_PREFIX, v1sg)
if err != nil {
t.Error("Error when setting v1 SharedGroup ", err.Error())
}
err = mig.Migrate("migrateSharedGroups")
if err != nil {
t.Error("Error when migrating SharedGroup ", err.Error())
}
result, err := mig.dataDB.GetSharedGroup(v1sg.Id, true, utils.NonTransactional)
if err != nil {
t.Error("Error when getting SharedGroup ", err.Error())
}
if !reflect.DeepEqual(sg, result) {
t.Errorf("Expecting: %+v, received: %+v", sg, result)
}
*/
}
}