Skip to content

Commit

Permalink
Added set/get versions functions in migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
adragusin committed Jun 30, 2020
1 parent b7b5c09 commit 98d084e
Show file tree
Hide file tree
Showing 41 changed files with 143 additions and 527 deletions.
22 changes: 4 additions & 18 deletions migrator/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package migrator

import (
"errors"
"fmt"
"log"
"strings"
"time"
Expand Down Expand Up @@ -122,17 +121,8 @@ func (m *Migrator) migrateV2Accounts() (v3Acnt *engine.Account, err error) {
func (m *Migrator) migrateAccounts() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions(utils.EmptyString)
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 Actions")
if vrs, err = m.getVersions(utils.Accounts); err != nil {
return
}
migrated := true
migratedFrom := 0
Expand Down Expand Up @@ -202,12 +192,8 @@ func (m *Migrator) migrateAccounts() (err error) {
}

// All done, update version wtih current one
vrs = engine.Versions{utils.Accounts: engine.CurrentDataDBVersions()[utils.Accounts]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating Accounts version into StorDB", err.Error()))
if err = m.setVersions(utils.Accounts); err != nil {
return err
}
return m.ensureIndexesDataDB(engine.ColAcc)
}
Expand Down
22 changes: 4 additions & 18 deletions migrator/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package migrator

import (
"fmt"
"strings"

"github.com/cgrates/cgrates/engine"
Expand Down Expand Up @@ -98,17 +97,8 @@ func (m *Migrator) migrateV1Actions() (acts engine.Actions, err error) {
func (m *Migrator) migrateActions() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions("")
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, err = m.getVersions(utils.Actions); err != nil {
return
}
migrated := true
var acts engine.Actions
Expand Down Expand Up @@ -151,12 +141,8 @@ func (m *Migrator) migrateActions() (err error) {
// remove old actions

// All done, update version wtih current one
vrs = engine.Versions{utils.Actions: engine.CurrentStorDBVersions()[utils.Actions]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating Actions version into dataDB", err.Error()))
if err = m.setVersions(utils.Actions); err != nil {
return err
}

return m.ensureIndexesDataDB(engine.ColAct)
Expand Down
21 changes: 4 additions & 17 deletions migrator/action_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,8 @@ func (m *Migrator) migrateV1ActionPlans() (v2 []*engine.ActionPlan, err error) {
func (m *Migrator) migrateActionPlans() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions("")
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, err = m.getVersions(utils.ActionPlans); err != nil {
return
}
if m.dmIN.DataManager().DataDB().GetStorageType() == utils.REDIS { // if redis rebuild action plans indexes
redisDB, can := m.dmIN.DataManager().DataDB().(*engine.RedisStorage)
Expand Down Expand Up @@ -188,12 +179,8 @@ func (m *Migrator) migrateActionPlans() (err error) {
}

// All done, update version wtih current one
vrs = engine.Versions{utils.ActionPlans: engine.CurrentDataDBVersions()[utils.ActionPlans]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating ActionPlans version into dataDB", err.Error()))
if err = m.setVersions(utils.ActionPlans); err != nil {
return err
}
return m.ensureIndexesDataDB(engine.ColApl)
}
Expand Down
22 changes: 4 additions & 18 deletions migrator/action_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package migrator

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -113,17 +112,8 @@ func (m *Migrator) removeV1ActionTriggers() (err error) {
func (m *Migrator) migrateActionTriggers() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions(utils.EmptyString)
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, err = m.getVersions(utils.ActionTriggers); err != nil {
return
}
migrated := true
migratedFrom := 0
Expand Down Expand Up @@ -177,12 +167,8 @@ func (m *Migrator) migrateActionTriggers() (err error) {
}

// All done, update version wtih current one
vrs = engine.Versions{utils.ActionTriggers: engine.CurrentDataDBVersions()[utils.ActionTriggers]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating ActionTriggers version into DataDB", err.Error()))
if err = m.setVersions(utils.ActionTriggers); err != nil {
return err
}

return m.ensureIndexesDataDB(engine.ColAtr)
Expand Down
16 changes: 4 additions & 12 deletions migrator/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,8 @@ func (m *Migrator) migrateV4ToV5AttributeProfile(v4Attr *v4AttributeProfile) (v5
func (m *Migrator) migrateAttributeProfile() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions(utils.EmptyString)
if err != nil {
//error getting the current verions
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, err = m.getVersions(utils.Attributes); err != nil {
return
}

migrated := true
Expand Down Expand Up @@ -252,10 +246,8 @@ func (m *Migrator) migrateAttributeProfile() (err error) {
return nil
}
// All done, update version with current one
vrs = engine.Versions{utils.Attributes: engine.CurrentDataDBVersions()[utils.Attributes]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator, utils.ServerErrorCaps, err.Error(),
fmt.Sprintf("error: <%s> when updating Attributes version into dataDB", err.Error()))
if err = m.setVersions(utils.Attributes); err != nil {
return err
}
return m.ensureIndexesDataDB(engine.ColAttr)

Expand Down
22 changes: 4 additions & 18 deletions migrator/cdrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package migrator

import (
"encoding/json"
"fmt"
"time"

"github.com/cgrates/cgrates/engine"
Expand All @@ -47,17 +46,8 @@ func (m *Migrator) migrateCurrentCDRs() (err error) {
func (m *Migrator) migrateCDRs() (err error) {
var vrs engine.Versions
current := engine.CurrentStorDBVersions()
vrs, err = m.storDBIn.StorDB().GetVersions(utils.EmptyString)
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 Actions")
if vrs, err = m.getVersions(utils.CDRs); err != nil {
return
}
migrated := true
var v2 *engine.CDR
Expand Down Expand Up @@ -93,12 +83,8 @@ func (m *Migrator) migrateCDRs() (err error) {
m.stats[utils.CDRs]++
}
// All done, update version wtih current one
vrs = engine.Versions{utils.CDRs: engine.CurrentStorDBVersions()[utils.CDRs]}
if err = m.storDBOut.StorDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating CDRs version into StorDB", err.Error()))
if err = m.setVersions(utils.CDRs); err != nil {
return err
}
return m.ensureIndexesStorDB(engine.ColCDRs)
}
Expand Down
13 changes: 2 additions & 11 deletions migrator/chargers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,8 @@ func (m *Migrator) migrateCurrentCharger() (err error) {
func (m *Migrator) migrateChargers() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions("")
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 ChargerProfile model")
if vrs, err = m.getVersions(utils.Chargers); err != nil {
return
}
switch vrs[utils.Chargers] {
case current[utils.Chargers]:
Expand Down
14 changes: 3 additions & 11 deletions migrator/destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,10 @@ func (m *Migrator) migrateCurrentDestinations() (err error) {
func (m *Migrator) migrateDestinations() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions("")
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, err = m.getVersions(utils.Destinations); err != nil {
return
}

switch vrs[utils.Destinations] {
case current[utils.Destinations]:
if m.sameDataDB {
Expand Down
13 changes: 2 additions & 11 deletions migrator/dispatchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,8 @@ func (m *Migrator) migrateCurrentDispatcherHost() (err error) {
func (m *Migrator) migrateDispatchers() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions("")
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 DispatcherProfile model")
if vrs, err = m.getVersions(utils.Dispatchers); err != nil {
return
}
switch vrs[utils.Dispatchers] {
case current[utils.Dispatchers]:
Expand Down
21 changes: 4 additions & 17 deletions migrator/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,8 @@ func (m *Migrator) migrateRequestFilterV3() (fltr *engine.Filter, err error) {
func (m *Migrator) migrateFilters() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions("")
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, err = m.getVersions(utils.RQF); err != nil {
return
}
migrated := true
migratedFrom := 0
Expand Down Expand Up @@ -378,12 +369,8 @@ func (m *Migrator) migrateFilters() (err error) {
}
}

vrs = engine.Versions{utils.RQF: engine.CurrentDataDBVersions()[utils.RQF]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating Filters version into dataDB", err.Error()))
if err = m.setVersions(utils.RQF); err != nil {
return err
}
return m.ensureIndexesDataDB(engine.ColFlt)
}
Expand Down
18 changes: 4 additions & 14 deletions migrator/load_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package migrator

import (
"fmt"

"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)

//
func (m *Migrator) migrateLoadIDs() (err error) {
var vrs engine.Versions
if vrs, err = m.dmIN.DataManager().DataDB().GetVersions(""); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when querying oldDataDB for versions", err.Error()))
if vrs, err = m.getVersions(utils.LoadIDsVrs); err != nil {
return
}
if vrs[utils.LoadIDs] != 1 {
if err = m.dmOut.DataManager().DataDB().RemoveLoadIDsDrv(); err != nil {
return
}
// All done, update version wtih current one
vrs := engine.Versions{utils.LoadIDsVrs: engine.CurrentDataDBVersions()[utils.LoadIDsVrs]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating LoadIDs version into dataDB", err))
if err = m.setVersions(utils.ActionTriggers); err != nil {
return err
}
}

Expand Down
Loading

0 comments on commit 98d084e

Please sign in to comment.