diff --git a/cmd/cgr-loader/migrator_rc8.go b/cmd/cgr-loader/migrator_rc8.go index c54527aaf..caca132c3 100644 --- a/cmd/cgr-loader/migrator_rc8.go +++ b/cmd/cgr-loader/migrator_rc8.go @@ -380,7 +380,7 @@ func (mig MigratorRC8) migrateActions() error { } func (mig MigratorRC8) migrateDerivedChargers() error { - keys, err := mig.db.Keys(utils.DERIVEDCHARGERS_PREFIX + "*") + keys, err := mig.db.Cmd("KEYS", utils.DERIVEDCHARGERS_PREFIX+"*").List() if err != nil { return err } @@ -389,7 +389,7 @@ func (mig MigratorRC8) migrateDerivedChargers() error { log.Printf("Migrating derived charger: %s...", key) var oldDcs []*utils.DerivedCharger var values []byte - if values, err = mig.db.Get(key); err == nil { + if values, err = mig.db.Cmd("GET", key).Bytes(); err == nil { if err := mig.ms.Unmarshal(values, &oldDcs); err != nil { return err } @@ -406,7 +406,7 @@ func (mig MigratorRC8) migrateDerivedChargers() error { if err != nil { return err } - if err = mig.db.Set(key, result); err != nil { + if err = mig.db.Cmd("SET", key, result).Err; err != nil { return err } } diff --git a/engine/account.go b/engine/account.go index cef47a190..65c4f5ad3 100644 --- a/engine/account.go +++ b/engine/account.go @@ -587,6 +587,17 @@ func (acc *Account) CleanExpiredBalances() { } } +func (acc *Account) allBalancesExpired() bool { + for _, bm := range acc.BalanceMap { + for i := 0; i < len(bm); i++ { + if !bm[i].IsExpired() { + return false + } + } + } + return true +} + // returns the shared groups that this user balance belnongs to func (acc *Account) GetSharedGroups() (groups []string) { for _, balanceChain := range acc.BalanceMap { diff --git a/engine/storage_map.go b/engine/storage_map.go index 47468d21b..3cc637969 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -468,6 +468,18 @@ func (ms *MapStorage) GetAccount(key string) (ub *Account, err error) { } func (ms *MapStorage) SetAccount(ub *Account) (err error) { + // never override existing account with an empty one + // UPDATE: if all balances expired and were cleaned it makes + // sense to write empty balance map + if len(ub.BalanceMap) == 0 { + if ac, err := ms.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() { + ac.ActionTriggers = ub.ActionTriggers + ac.UnitCounters = ub.UnitCounters + ac.AllowNegative = ub.AllowNegative + ac.Disabled = ub.Disabled + ub = ac + } + } result, err := ms.ms.Marshal(ub) ms.dict[utils.ACCOUNT_PREFIX+ub.Id] = result return diff --git a/engine/storage_mongo.go b/engine/storage_mongo.go index b08a43ab8..599799bfc 100644 --- a/engine/storage_mongo.go +++ b/engine/storage_mongo.go @@ -763,6 +763,18 @@ func (ms *MongoStorage) GetAccount(key string) (result *Account, err error) { } func (ms *MongoStorage) SetAccount(acc *Account) error { + // never override existing account with an empty one + // UPDATE: if all balances expired and were cleaned it makes + // sense to write empty balance map + if len(acc.BalanceMap) == 0 { + if ac, err := ms.GetAccount(acc.Id); err == nil && !ac.allBalancesExpired() { + ac.ActionTriggers = acc.ActionTriggers + ac.UnitCounters = acc.UnitCounters + ac.AllowNegative = acc.AllowNegative + ac.Disabled = acc.Disabled + acc = ac + } + } _, err := ms.db.C(colAcc).Upsert(bson.M{"id": acc.Id}, acc) return err } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 9c85221d5..eda468131 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -620,6 +620,18 @@ func (rs *RedisStorage) GetAccount(key string) (ub *Account, err error) { } func (rs *RedisStorage) SetAccount(ub *Account) (err error) { + // never override existing account with an empty one + // UPDATE: if all balances expired and were cleaned it makes + // sense to write empty balance map + if len(ub.BalanceMap) == 0 { + if ac, err := rs.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() { + ac.ActionTriggers = ub.ActionTriggers + ac.UnitCounters = ub.UnitCounters + ac.AllowNegative = ub.AllowNegative + ac.Disabled = ub.Disabled + ub = ac + } + } result, err := rs.ms.Marshal(ub) err = rs.db.Cmd("SET", utils.ACCOUNT_PREFIX+ub.Id, result).Err return