From 465f0635eb5c56eacbb4f0e34fdc1390b9a2a48f Mon Sep 17 00:00:00 2001 From: TeoV Date: Thu, 1 Feb 2018 13:20:55 +0200 Subject: [PATCH] Add SessionsCosts in migrator and versions --- engine/version.go | 4 ++- engine/version_test.go | 12 ++++++++- migrator/migrator.go | 2 ++ migrator/migrator_it_test.go | 32 ++++++++++++++++++++++++ migrator/sessions_costs.go | 48 ++++++++++++++++++++++++++++++++++++ utils/consts.go | 2 ++ 6 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 migrator/sessions_costs.go diff --git a/engine/version.go b/engine/version.go index 53b2d10e2..ab6e49077 100644 --- a/engine/version.go +++ b/engine/version.go @@ -90,7 +90,8 @@ func (vers Versions) Compare(curent Versions, storType string) string { utils.SharedGroups: "cgr-migrator -migrate=*shared_groups", } stor := map[string]string{ - utils.COST_DETAILS: "cgr-migrator -migrate=*cost_details", + utils.COST_DETAILS: "cgr-migrator -migrate=*cost_details", + utils.SessionsCosts: "cgr-migrator -migrate=*sessions_costs", } switch storType { case utils.MONGO: @@ -141,6 +142,7 @@ func CurrentDataDBVersions() Versions { func CurrentStorDBVersions() Versions { return Versions{ utils.COST_DETAILS: 2, + utils.SessionsCosts: 2, utils.TpRatingPlans: 1, utils.TpFilters: 1, utils.TpDestinationRates: 1, diff --git a/engine/version_test.go b/engine/version_test.go index e2644d352..5400cfafb 100644 --- a/engine/version_test.go +++ b/engine/version_test.go @@ -29,7 +29,8 @@ func TestVersionCompare(t *testing.T) { z := Versions{utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 2, utils.ActionPlans: 1, utils.SharedGroups: 2, utils.COST_DETAILS: 2} q := Versions{utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 2, utils.ActionPlans: 2, utils.SharedGroups: 1, utils.COST_DETAILS: 2} c := Versions{utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 2, utils.ActionPlans: 2, utils.SharedGroups: 2, utils.COST_DETAILS: 1} - + a := Versions{utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 2, utils.ActionPlans: 2, utils.SharedGroups: 2, utils.COST_DETAILS: 2, utils.SessionsCosts: 1} + b := Versions{utils.Accounts: 2, utils.Actions: 2, utils.ActionTriggers: 2, utils.ActionPlans: 2, utils.SharedGroups: 2, utils.COST_DETAILS: 2, utils.SessionsCosts: 2} message1 := y.Compare(x, utils.MONGO) if message1 != "cgr-migrator -migrate=*accounts" { t.Errorf("Error failed to compare to curent version expected: %s received: %s", "cgr-migrator -migrate=*accounts", message1) @@ -46,4 +47,13 @@ func TestVersionCompare(t *testing.T) { if message4 != "cgr-migrator -migrate=*cost_details" { t.Errorf("Error failed to compare to curent version expected: %s received: %s", "cgr-migrator -migrate=*cost_details", message4) } + message5 := a.Compare(b, utils.MYSQL) + if message5 != "cgr-migrator -migrate=*sessions_costs" { + t.Errorf("Error failed to compare to curent version expected: %s received: %s", "cgr-migrator -migrate=*sessions_costs", message5) + } + message6 := a.Compare(b, utils.POSTGRES) + if message6 != "cgr-migrator -migrate=*sessions_costs" { + t.Errorf("Error failed to compare to curent version expected: %s received: %s", "cgr-migrator -migrate=*sessions_costs", message6) + } + } diff --git a/migrator/migrator.go b/migrator/migrator.go index 42c941a6a..cabb4f21a 100755 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -115,6 +115,8 @@ func (m *Migrator) Migrate(taskIDs []string) (err error, stats map[string]int) { } else { log.Print("Cannot dryRun SetVersions!") } + case utils.MetaSessionsCosts: + err = m.migrateSessionsCosts() case utils.MetaCostDetails: err = m.migrateCostDetails() case utils.MetaAccounts: diff --git a/migrator/migrator_it_test.go b/migrator/migrator_it_test.go index 904b93024..ee9d00f03 100644 --- a/migrator/migrator_it_test.go +++ b/migrator/migrator_it_test.go @@ -50,6 +50,7 @@ var sTestsITMigrator = []func(t *testing.T){ testMigratorActions, testMigratorSharedGroups, testMigratorStats, + testMigratorSessionsCosts, testFlush, testMigratorAlias, //FIXME testMigratorReverseAlias, @@ -1112,6 +1113,37 @@ func testMigratorStats(t *testing.T) { } } +func testMigratorSessionsCosts(t *testing.T) { + switch action { + case Move: + currentVersion := engine.CurrentStorDBVersions() + currentVersion[utils.SessionsCosts] = 1 + err := mig.dmOut.DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for SessionsCosts ", err.Error()) + } + err, _ = mig.Migrate([]string{utils.MetaSessionsCosts}) + if err == nil { + t.Error("Expecting error , received: nil ") + } + if vrs, err := mig.dmOut.DataDB().GetVersions(utils.SessionsCosts); err != nil { + t.Error(err) + } else if vrs[utils.SessionsCosts] != 1 { + t.Errorf("Expecting: 1, received: %+v", vrs[utils.SessionsCosts]) + } + currentVersion = engine.CurrentStorDBVersions() + err = mig.dmOut.DataDB().SetVersions(currentVersion, false) + if err != nil { + t.Error("Error when setting version for SessionsCosts ", err.Error()) + } + if vrs, err := mig.dmOut.DataDB().GetVersions(utils.SessionsCosts); err != nil { + t.Error(err) + } else if vrs[utils.SessionsCosts] != 2 { + t.Errorf("Expecting: 2, received: %+v", vrs[utils.SessionsCosts]) + } + } +} + func testMigratorThreshold(t *testing.T) { tim := time.Date(2012, time.February, 27, 23, 59, 59, 0, time.UTC) tenant := config.CgrConfig().DefaultTenant diff --git a/migrator/sessions_costs.go b/migrator/sessions_costs.go new file mode 100644 index 000000000..d949f5ce4 --- /dev/null +++ b/migrator/sessions_costs.go @@ -0,0 +1,48 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package migrator + +import ( + "errors" + "fmt" + + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func (m *Migrator) migrateSessionsCosts() (err error) { + var vrs engine.Versions + vrs, err = m.dmOut.DataDB().GetVersions(utils.TBLVersions) + if err != nil { + return utils.NewCGRError(utils.Migrator, + utils.ServerErrorCaps, + err.Error(), + fmt.Sprintf("error: <%s> when querying oldDataDB for versions", err.Error())) + } else if len(vrs) == 0 { + return utils.NewCGRError(utils.Migrator, + utils.MandatoryIEMissingCaps, + utils.UndefinedVersion, + "version number is not defined for ActionTriggers model") + } + if vrs[utils.SessionsCosts] == 1 { + msg := "Migration needed: please backup cgrates data and run from data/storage the scripts for MySql and Postgres" + return errors.New(msg) + } + return nil +} diff --git a/utils/consts.go b/utils/consts.go index 7994007fd..49a96ec75 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -368,6 +368,7 @@ const ( SchedulerNotRunningCaps = "SCHEDULLER_NOT_RUNNING" MetaScheduler = "*scheduler" MetaCostDetails = "*cost_details" + MetaSessionsCosts = "*sessions_costs" MetaAccounts = "*accounts" MetaActionPlans = "*action_plans" MetaActionTriggers = "*action_triggers" @@ -439,6 +440,7 @@ const ( Disabled = "Disabled" Action = "Action" MetaNow = "*now" + SessionsCosts = "SessionsCosts" TpRatingPlans = "TpRatingPlans" TpFilters = "TpFilters" TpDestinationRates = "TpDestinationRates"