From 28561dba1f7eb1a294d6f2d9f6b176fb921da80b Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 12 Oct 2015 13:46:09 +0300 Subject: [PATCH] finshed mongo tp methods test pending --- apier/v1/tpaccountactions.go | 2 +- apier/v1/tpactionplans.go | 2 +- apier/v1/tpactions.go | 2 +- apier/v1/tpactiontriggers.go | 2 +- apier/v1/tpaliases.go | 2 +- apier/v1/tpcdrstats.go | 2 +- apier/v1/tpderivedcharges.go | 2 +- apier/v1/tpdestinationrates.go | 2 +- apier/v1/tpdestinations.go | 2 +- apier/v1/tplcrrules.go | 2 +- apier/v1/tprates.go | 2 +- apier/v1/tpratingplans.go | 2 +- apier/v1/tpratingprofiles.go | 2 +- apier/v1/tpsharedgroups.go | 2 +- apier/v1/tptimings.go | 2 +- apier/v1/tpusers.go | 2 +- apier/v2/tp.go | 2 +- engine/calldesc.go | 2 +- engine/storage_interface.go | 2 +- engine/storage_mongo_tp.go | 92 ++++++++++++++++++++++++++++-- engine/storage_mysql_local_test.go | 8 +-- engine/storage_psql_local_test.go | 8 +-- engine/storage_sql.go | 14 ++--- 23 files changed, 117 insertions(+), 43 deletions(-) diff --git a/apier/v1/tpaccountactions.go b/apier/v1/tpaccountactions.go index 56778e3d8..ab8bf2799 100644 --- a/apier/v1/tpaccountactions.go +++ b/apier/v1/tpaccountactions.go @@ -157,7 +157,7 @@ func (self *ApierV1) RemTPAccountActions(attrs AttrGetTPAccountActions, reply *s if err := aa.SetAccountActionId(attrs.AccountActionsId); err != nil { return err } - if err := self.StorDb.RemTpData(utils.TBL_TP_ACCOUNT_ACTIONS, aa.Tpid, aa.Loadid, aa.Direction, aa.Tenant, aa.Account); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_ACCOUNT_ACTIONS, aa.Tpid, map[string]string{"loadid": aa.Loadid, "direction": aa.Direction, "tenant": aa.Tenant, "account": aa.Account}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpactionplans.go b/apier/v1/tpactionplans.go index 4824c9794..9a09dd4fc 100644 --- a/apier/v1/tpactionplans.go +++ b/apier/v1/tpactionplans.go @@ -98,7 +98,7 @@ func (self *ApierV1) RemTPActionPlan(attrs AttrGetTPActionPlan, reply *string) e if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Id"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_ACTION_PLANS, attrs.TPid, attrs.Id); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_ACTION_PLANS, attrs.TPid, map[string]string{"tag": attrs.Id}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpactions.go b/apier/v1/tpactions.go index 1d53915fe..c09b7908b 100644 --- a/apier/v1/tpactions.go +++ b/apier/v1/tpactions.go @@ -96,7 +96,7 @@ func (self *ApierV1) RemTPActions(attrs AttrGetTPActions, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "ActionsId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_ACTIONS, attrs.TPid, attrs.ActionsId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_ACTIONS, attrs.TPid, map[string]string{"tag": attrs.ActionsId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpactiontriggers.go b/apier/v1/tpactiontriggers.go index 19447a849..7d153c5bb 100644 --- a/apier/v1/tpactiontriggers.go +++ b/apier/v1/tpactiontriggers.go @@ -92,7 +92,7 @@ func (self *ApierV1) RemTPActionTriggers(attrs AttrGetTPActionTriggers, reply *s if missing := utils.MissingStructFields(&attrs, []string{"TPid", "ActionTriggersId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_ACTION_TRIGGERS, attrs.TPid, attrs.ActionTriggersId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_ACTION_TRIGGERS, attrs.TPid, map[string]string{"tag": attrs.ActionTriggersId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpaliases.go b/apier/v1/tpaliases.go index cdb4b9296..1fc1b2276 100644 --- a/apier/v1/tpaliases.go +++ b/apier/v1/tpaliases.go @@ -87,7 +87,7 @@ func (self *ApierV1) RemTPAlias(attrs AttrGetTPAlias, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "AliasId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_ALIASES, attrs.TPid); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_ALIASES, attrs.TPid, map[string]string{"tag": attrs.AliasId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpcdrstats.go b/apier/v1/tpcdrstats.go index 9990cc139..7893602c9 100644 --- a/apier/v1/tpcdrstats.go +++ b/apier/v1/tpcdrstats.go @@ -94,7 +94,7 @@ func (self *ApierV1) RemTPCdrStats(attrs AttrGetTPCdrStats, reply *string) error if missing := utils.MissingStructFields(&attrs, []string{"TPid", "CdrStatsId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_SHARED_GROUPS, attrs.TPid, attrs.CdrStatsId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_SHARED_GROUPS, attrs.TPid, map[string]string{"tag": attrs.CdrStatsId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpderivedcharges.go b/apier/v1/tpderivedcharges.go index 85b608581..e6d0c825b 100644 --- a/apier/v1/tpderivedcharges.go +++ b/apier/v1/tpderivedcharges.go @@ -103,7 +103,7 @@ func (self *ApierV1) RemTPDerivedChargers(attrs AttrGetTPDerivedChargers, reply if err := tmpDc.SetDerivedChargersId(attrs.DerivedChargersId); err != nil { return err } - if err := self.StorDb.RemTpData(utils.TBL_TP_DERIVED_CHARGERS, attrs.TPid, tmpDc.Loadid, tmpDc.Direction, tmpDc.Tenant, tmpDc.Category, tmpDc.Account, tmpDc.Subject); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_DERIVED_CHARGERS, attrs.TPid, map[string]string{"loadid": tmpDc.Loadid, "direction": tmpDc.Direction, "tenant": tmpDc.Tenant, "category": tmpDc.Category, "account": tmpDc.Account, "subject": tmpDc.Subject}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpdestinationrates.go b/apier/v1/tpdestinationrates.go index 1fa003b40..e79af1504 100644 --- a/apier/v1/tpdestinationrates.go +++ b/apier/v1/tpdestinationrates.go @@ -88,7 +88,7 @@ func (self *ApierV1) RemTPDestinationRate(attrs AttrGetTPDestinationRate, reply if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationRateId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_DESTINATION_RATES, attrs.TPid, attrs.DestinationRateId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_DESTINATION_RATES, attrs.TPid, map[string]string{"tag": attrs.DestinationRateId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpdestinations.go b/apier/v1/tpdestinations.go index a6f68fbeb..93aff6c3e 100644 --- a/apier/v1/tpdestinations.go +++ b/apier/v1/tpdestinations.go @@ -88,7 +88,7 @@ func (self *ApierV1) RemTPDestination(attrs AttrGetTPDestination, reply *string) if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_DESTINATIONS, attrs.TPid, attrs.DestinationId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_DESTINATIONS, attrs.TPid, map[string]string{"tag": attrs.DestinationId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tplcrrules.go b/apier/v1/tplcrrules.go index 444339f2f..708e95329 100644 --- a/apier/v1/tplcrrules.go +++ b/apier/v1/tplcrrules.go @@ -89,7 +89,7 @@ func (self *ApierV1) RemTPLcrRule(attrs AttrGetTPLcrRules, reply *string) error if missing := utils.MissingStructFields(&attrs, []string{"TPid", "LcrRulesId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_LCRS, attrs.TPid, attrs.LcrRuleId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_LCRS, attrs.TPid, map[string]string{"tag": attrs.LcrRuleId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tprates.go b/apier/v1/tprates.go index 64de7d1d4..5d87db447 100644 --- a/apier/v1/tprates.go +++ b/apier/v1/tprates.go @@ -87,7 +87,7 @@ func (self *ApierV1) RemTPRate(attrs AttrGetTPRate, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "RateId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_RATES, attrs.TPid, attrs.RateId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_RATES, attrs.TPid, map[string]string{"tag": attrs.RateId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpratingplans.go b/apier/v1/tpratingplans.go index 6bcdd0196..ff4e9831d 100644 --- a/apier/v1/tpratingplans.go +++ b/apier/v1/tpratingplans.go @@ -88,7 +88,7 @@ func (self *ApierV1) RemTPRatingPlan(attrs AttrGetTPRatingPlan, reply *string) e if missing := utils.MissingStructFields(&attrs, []string{"TPid", "RatingPlanId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_RATING_PLANS, attrs.TPid, attrs.RatingPlanId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_RATING_PLANS, attrs.TPid, map[string]string{"tag": attrs.RatingPlanId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpratingprofiles.go b/apier/v1/tpratingprofiles.go index c2cb75dba..7ea411f7d 100644 --- a/apier/v1/tpratingprofiles.go +++ b/apier/v1/tpratingprofiles.go @@ -161,7 +161,7 @@ func (self *ApierV1) RemTPRatingProfile(attrs AttrGetTPRatingProfile, reply *str if err := tmpRpf.SetRatingProfileId(attrs.RatingProfileId); err != nil { return err } - if err := self.StorDb.RemTpData(utils.TBL_TP_RATE_PROFILES, attrs.TPid, tmpRpf.Loadid, tmpRpf.Direction, tmpRpf.Tenant, tmpRpf.Category, tmpRpf.Subject); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_RATE_PROFILES, attrs.TPid, map[string]string{"loadid": tmpRpf.Loadid, "direction": tmpRpf.Direction, "tenant": tmpRpf.Tenant, "category": tmpRpf.Category, "subject": tmpRpf.Subject}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpsharedgroups.go b/apier/v1/tpsharedgroups.go index 83880d614..881c7f1c9 100644 --- a/apier/v1/tpsharedgroups.go +++ b/apier/v1/tpsharedgroups.go @@ -94,7 +94,7 @@ func (self *ApierV1) RemTPSharedGroups(attrs AttrGetTPSharedGroups, reply *strin if missing := utils.MissingStructFields(&attrs, []string{"TPid", "SharedGroupsId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_SHARED_GROUPS, attrs.TPid, attrs.SharedGroupsId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_SHARED_GROUPS, attrs.TPid, map[string]string{"tag": attrs.SharedGroupsId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tptimings.go b/apier/v1/tptimings.go index dcd47421f..ad148674c 100644 --- a/apier/v1/tptimings.go +++ b/apier/v1/tptimings.go @@ -85,7 +85,7 @@ func (self *ApierV1) RemTPTiming(attrs AttrGetTPTiming, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "TimingId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_TIMINGS, attrs.TPid, attrs.TimingId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_TIMINGS, attrs.TPid, map[string]string{"tag": attrs.TimingId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v1/tpusers.go b/apier/v1/tpusers.go index 79d9ea2d2..44d07ae16 100644 --- a/apier/v1/tpusers.go +++ b/apier/v1/tpusers.go @@ -90,7 +90,7 @@ func (self *ApierV1) RemTPUser(attrs AttrGetTPUser, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "UserId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTpData(utils.TBL_TP_USERS, attrs.TPid); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_USERS, attrs.TPid, map[string]string{"tag": attrs.UserId}); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/apier/v2/tp.go b/apier/v2/tp.go index 492eb85d5..75880df0e 100644 --- a/apier/v2/tp.go +++ b/apier/v2/tp.go @@ -33,7 +33,7 @@ func (self *ApierV2) RemTP(attrs AttrRemTp, reply *string) error { if len(attrs.TPid) == 0 { return utils.NewErrMandatoryIeMissing("TPid") } - if err := self.StorDb.RemTpData("", attrs.TPid); err != nil { + if err := self.StorDb.RemTpData("", attrs.TPid, nil); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" diff --git a/engine/calldesc.go b/engine/calldesc.go index 7fa0f5cce..eba37b91d 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -37,7 +37,7 @@ const ( RECURSION_MAX_DEPTH = 3 MIN_PREFIX_MATCH = 1 FALLBACK_SUBJECT = utils.ANY - DEBUG = true + DEBUG = false ) func init() { diff --git a/engine/storage_interface.go b/engine/storage_interface.go index ab5857c64..f18a652f3 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -137,7 +137,7 @@ type LoadReader interface { } type LoadWriter interface { - RemTpData(string, string, ...string) error + RemTpData(string, string, map[string]string) error SetTpTimings([]TpTiming) error SetTpDestinations([]TpDestination) error SetTpRates([]TpRate) error diff --git a/engine/storage_mongo_tp.go b/engine/storage_mongo_tp.go index ca2c90eba..4f345d5f1 100644 --- a/engine/storage_mongo_tp.go +++ b/engine/storage_mongo_tp.go @@ -1,6 +1,8 @@ package engine import ( + "strings" + "github.com/cgrates/cgrates/utils" "gopkg.in/mgo.v2/bson" ) @@ -26,10 +28,71 @@ const ( colTpCrs = "tp_cdrstats" ) -func (ms *MongoStorage) GetTpIds() ([]string, error) { return nil, nil } -func (ms *MongoStorage) GetTpTableIds(string, string, utils.TPDistinctIds, map[string]string, *utils.Paginator) ([]string, error) { - return nil, nil +func (ms *MongoStorage) GetTpIds() ([]string, error) { + tpidMap := make(map[string]bool) + cols, err := ms.db.CollectionNames() + if err != nil { + return nil, err + } + for _, col := range cols { + if strings.HasPrefix(col, "tp_") { + tpids := make([]string, 0) + if err := ms.db.C(col).Find(nil).Select(bson.M{"tpid": 1}).Distinct("tpid", &tpids); err != nil { + return nil, err + } + for _, tpid := range tpids { + tpidMap[tpid] = true + } + } + } + var tpids []string + for tpid := range tpidMap { + tpids = append(tpids, tpid) + } + return tpids, nil } +func (ms *MongoStorage) GetTpTableIds(tpid, table string, distinct utils.TPDistinctIds, filter map[string]string, pag *utils.Paginator) ([]string, error) { + selectors := bson.M{} + for _, d := range distinct { + selectors[d] = 1 + } + filter["tpid"] = tpid + q := ms.db.C(table).Find(filter) + if pag != nil { + if pag.Limit != nil { + q = q.Limit(*pag.Limit) + } + if pag.Offset != nil { + q = q.Skip(*pag.Offset) + } + } + + iter := q.Select(selectors).Iter() + distinctIds := make(map[string]bool) + item := make(map[string]string) + for iter.Next(item) { + id := "" + last := len(distinct) - 1 + for i, d := range distinct { + if distinctValue, ok := item[d]; ok { + id += distinctValue + } + if i < last { + id += utils.CONCATENATED_KEY_SEP + } + } + distinctIds[id] = true + } + if err := iter.Close(); err != nil { + return nil, err + } + var results []string + for id := range distinctIds { + results = append(results, id) + } + return results, nil +} + func (ms *MongoStorage) GetTpTimings(tpid, tag string) ([]TpTiming, error) { filter := bson.M{ "tpid": tpid, @@ -41,6 +104,7 @@ func (ms *MongoStorage) GetTpTimings(tpid, tag string) ([]TpTiming, error) { err := ms.db.C(colTpTmg).Find(filter).All(&results) return results, err } + func (ms *MongoStorage) GetTpDestinations(tpid, tag string) ([]TpDestination, error) { filter := bson.M{ "tpid": tpid, @@ -52,6 +116,7 @@ func (ms *MongoStorage) GetTpDestinations(tpid, tag string) ([]TpDestination, er err := ms.db.C(colTpDst).Find(filter).All(&results) return results, err } + func (ms *MongoStorage) GetTpRates(tpid, tag string) ([]TpRate, error) { filter := bson.M{ "tpid": tpid, @@ -63,6 +128,7 @@ func (ms *MongoStorage) GetTpRates(tpid, tag string) ([]TpRate, error) { err := ms.db.C(colTpRts).Find(filter).All(&results) return results, err } + func (ms *MongoStorage) GetTpDestinationRates(tpid, tag string, pag *utils.Paginator) ([]TpDestinationRate, error) { filter := bson.M{ "tpid": tpid, @@ -290,9 +356,23 @@ func (ms *MongoStorage) GetTpAccountActions(tp *TpAccountAction) ([]TpAccountAct return results, err } -func (ms *MongoStorage) RemTpData(string, string, ...string) error { - - return nil +func (ms *MongoStorage) RemTpData(table, tpid string, args map[string]string) error { + if len(table) == 0 { // Remove tpid out of all tables + cols, err := ms.db.CollectionNames() + if err != nil { + return err + } + for _, col := range cols { + if strings.HasPrefix(col, "tp_") { + if err := ms.db.C(col).Remove(bson.M{"tpid": tpid}); err != nil { + return err + } + } + } + } + // Remove from a single table + args["tpid"] = tpid + return ms.db.C(table).Remove(args) } func (ms *MongoStorage) SetTpTimings(tps []TpTiming) error { diff --git a/engine/storage_mysql_local_test.go b/engine/storage_mysql_local_test.go index 4f36bf03f..2bcabc1fe 100644 --- a/engine/storage_mysql_local_test.go +++ b/engine/storage_mysql_local_test.go @@ -372,7 +372,7 @@ func TestMySQLRemoveTPData(t *testing.T) { t.Error("Could not store TPTiming") } // Remove Timings - if err := mysqlDb.RemTpData(utils.TBL_TP_TIMINGS, utils.TEST_SQL, tm.TimingId); err != nil { + if err := mysqlDb.RemTpData(utils.TBL_TP_TIMINGS, utils.TEST_SQL, map[string]string{"tag": tm.TimingId}); err != nil { t.Error(err.Error()) } if tmgs, err := mysqlDb.GetTpTimings(utils.TEST_SQL, tm.TimingId); err != nil { @@ -393,7 +393,7 @@ func TestMySQLRemoveTPData(t *testing.T) { t.Error("Could not store TPRatingProfile") } // Remove RatingProfile - if err := mysqlDb.RemTpData(utils.TBL_TP_RATE_PROFILES, rp.TPid, rp.LoadId, rp.Direction, rp.Tenant, rp.Category, rp.Subject); err != nil { + if err := mysqlDb.RemTpData(utils.TBL_TP_RATE_PROFILES, rp.TPid, map[string]string{"loadid": rp.LoadId, "direction": rp.Direction, "tenant": rp.Tenant, "category": rp.Category, "subject": rp.Subject}); err != nil { t.Error(err.Error()) } if rps, err := mysqlDb.GetTpRatingProfiles(&mrp[0]); err != nil { @@ -414,7 +414,7 @@ func TestMySQLRemoveTPData(t *testing.T) { t.Error("Could not create TPAccountActions") } // Remove AccountActions - if err := mysqlDb.RemTpData(utils.TBL_TP_ACCOUNT_ACTIONS, aa.TPid, aa.LoadId, aa.Direction, aa.Tenant, aa.Account); err != nil { + if err := mysqlDb.RemTpData(utils.TBL_TP_ACCOUNT_ACTIONS, aa.TPid, map[string]string{"loadis": aa.LoadId, "direction": aa.Direction, "tenant": aa.Tenant, "account": aa.Account}); err != nil { t.Error(err.Error()) } if aas, err := mysqlDb.GetTpAccountActions(maa); err != nil { @@ -450,7 +450,7 @@ func TestMySQLRemoveTPData(t *testing.T) { t.Error("Could not create TPAccountActions") } // Remove TariffPlan completely - if err := mysqlDb.RemTpData("", utils.TEST_SQL); err != nil { + if err := mysqlDb.RemTpData("", utils.TEST_SQL, nil); err != nil { t.Error(err.Error()) } // Make sure we have removed it diff --git a/engine/storage_psql_local_test.go b/engine/storage_psql_local_test.go index 1926bd74c..0388111ed 100644 --- a/engine/storage_psql_local_test.go +++ b/engine/storage_psql_local_test.go @@ -367,7 +367,7 @@ func TestPSQLRemoveTPData(t *testing.T) { t.Error("Could not store TPTiming") } // Remove Timings - if err := psqlDb.RemTpData(utils.TBL_TP_TIMINGS, utils.TEST_SQL, tm.TimingId); err != nil { + if err := psqlDb.RemTpData(utils.TBL_TP_TIMINGS, utils.TEST_SQL, map[string]string{"tag": tm.TimingId}); err != nil { t.Error(err.Error()) } if tmgs, err := psqlDb.GetTpTimings(utils.TEST_SQL, tm.TimingId); err != nil { @@ -388,7 +388,7 @@ func TestPSQLRemoveTPData(t *testing.T) { t.Error("Could not store TPRatingProfile") } // Remove RatingProfile - if err := psqlDb.RemTpData(utils.TBL_TP_RATE_PROFILES, rp.TPid, rp.LoadId, rp.Direction, rp.Tenant, rp.Category, rp.Subject); err != nil { + if err := psqlDb.RemTpData(utils.TBL_TP_RATE_PROFILES, rp.TPid, map[string]string{"loadid": rp.LoadId, "direction": rp.Direction, "tenant": rp.Tenant, "category": rp.Category, "subject": rp.Subject}); err != nil { t.Error(err.Error()) } if rps, err := psqlDb.GetTpRatingProfiles(&mrp[0]); err != nil { @@ -409,7 +409,7 @@ func TestPSQLRemoveTPData(t *testing.T) { t.Error("Could not create TPAccountActions") } // Remove AccountActions - if err := psqlDb.RemTpData(utils.TBL_TP_ACCOUNT_ACTIONS, aa.TPid, aa.LoadId, aa.Direction, aa.Tenant, aa.Account); err != nil { + if err := psqlDb.RemTpData(utils.TBL_TP_ACCOUNT_ACTIONS, aa.TPid, map[string]string{"loadid": aa.LoadId, "direction": aa.Direction, "tenant": aa.Tenant, "account": aa.Account}); err != nil { t.Error(err.Error()) } if aas, err := psqlDb.GetTpAccountActions(maa); err != nil { @@ -445,7 +445,7 @@ func TestPSQLRemoveTPData(t *testing.T) { t.Error("Could not create TPAccountActions") } // Remove TariffPlan completely - if err := psqlDb.RemTpData("", utils.TEST_SQL); err != nil { + if err := psqlDb.RemTpData("", utils.TEST_SQL, nil); err != nil { t.Error(err.Error()) } // Make sure we have removed it diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 179f8b799..50bac9f5e 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -158,7 +158,7 @@ func (self *SQLStorage) GetTpTableIds(tpid, table string, distinct utils.TPDisti return ids, nil } -func (self *SQLStorage) RemTpData(table, tpid string, args ...string) error { +func (self *SQLStorage) RemTpData(table, tpid string, args map[string]string) error { tx := self.db.Begin() if len(table) == 0 { // Remove tpid out of all tables for _, tblName := range []string{utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_RATES, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_RATING_PLANS, utils.TBL_TP_RATE_PROFILES, @@ -173,15 +173,9 @@ func (self *SQLStorage) RemTpData(table, tpid string, args ...string) error { } // Remove from a single table tx = tx.Table(table).Where("tpid = ?", tpid) - switch table { - default: - tx = tx.Where("tag = ?", args[0]) - case utils.TBL_TP_RATE_PROFILES: - tx = tx.Where("loadid = ?", args[0]).Where("direction = ?", args[1]).Where("tenant = ?", args[2]).Where("category = ?", args[3]).Where("subject = ?", args[4]) - case utils.TBL_TP_ACCOUNT_ACTIONS: - tx = tx.Where("loadid = ?", args[0]).Where("direction = ?", args[1]).Where("tenant = ?", args[2]).Where("account = ?", args[3]) - case utils.TBL_TP_DERIVED_CHARGERS: - tx = tx.Where("loadid = ?", args[0]).Where("direction = ?", args[1]).Where("tenant = ?", args[2]).Where("category = ?", args[3]).Where("account = ?", args[4]).Where("subject = ?", args[5]) + // Compose filters + for key, value := range args { + tx = tx.Where(key+" = ?", value) } if err := tx.Delete(nil).Error; err != nil { tx.Rollback()