diff --git a/cmd/cgr-loader/cgr-loader.go b/cmd/cgr-loader/cgr-loader.go index 21d2bd891..b205abcbd 100644 --- a/cmd/cgr-loader/cgr-loader.go +++ b/cmd/cgr-loader/cgr-loader.go @@ -215,7 +215,7 @@ func main() { if err != nil { log.Fatal(err) } - if err := migrator.NewMigrator(storDB).Migrate(*migrate); err != nil { + if err := migrator.NewMigrator(storDB, *stor_db_type).Migrate(*migrate); err != nil { log.Fatal(err) } log.Print("Done migrating!") diff --git a/migrator/migrator.go b/migrator/migrator.go index c18de1073..fe4c7e242 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -26,12 +26,13 @@ import ( "github.com/cgrates/cgrates/utils" ) -func NewMigrator(storDB engine.Storage) *Migrator { - return &Migrator{storDB: storDB} +func NewMigrator(storDB engine.Storage, storDBType string) *Migrator { + return &Migrator{storDB: storDB, storDBType: storDBType} } type Migrator struct { - storDB engine.Storage + storDB engine.Storage + storDBType string } func (m *Migrator) Migrate(taskID string) (err error) { @@ -76,8 +77,19 @@ func (m *Migrator) migrateCostDetails() (err error) { if vrs[utils.COST_DETAILS] != 1 { // Right now we only support migrating from version 1 return } - storSQL := m.storDB.(*engine.SQLStorage) - rows, err := storSQL.Db.Query("SELECT id, tor, direction, tenant, category, account, subject, destination, cost, cost_details FROM cdrs WHERE run_id!= '*raw' and cost_details NOT NULL") + var storSQL *sql.DB + switch m.storDBType { + case utils.MYSQL: + storSQL = m.storDB.(*engine.MySQLStorage).Db + case utils.POSTGRES: + storSQL = m.storDB.(*engine.PostgresStorage).Db + default: + return utils.NewCGRError(utils.Migrator, + utils.MandatoryIEMissingCaps, + utils.UnsupportedDB, + fmt.Sprintf("unsupported database type: <%s>", m.storDBType)) + } + rows, err := storSQL.Query("SELECT id, tor, direction, tenant, category, account, subject, destination, cost, cost_details FROM cdrs WHERE run_id!= '*raw' and cost_details IS NOT NULL AND deleted_at IS NULL") if err != nil { return utils.NewCGRError(utils.Migrator, utils.ServerErrorCaps, @@ -111,7 +123,7 @@ func (m *Migrator) migrateCostDetails() (err error) { fmt.Sprintf(" Error: <%s> when converting into CallCost CDR with id: <%d>", err.Error(), id)) continue } - if _, err := storSQL.Db.Exec(fmt.Sprintf("UPDATE cdrs SET cost_details='%s' WHERE id=%d", cc.AsJSON(), id)); err != nil { + if _, err := storSQL.Exec(fmt.Sprintf("UPDATE cdrs SET cost_details='%s' WHERE id=%d", cc.AsJSON(), id)); err != nil { utils.Logger.Warning( fmt.Sprintf(" Error: <%s> updating CDR with id <%d> into StorDB", err.Error(), id)) continue diff --git a/utils/consts.go b/utils/consts.go index ad0252d75..c13a987d4 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -328,4 +328,5 @@ const ( NoStorDBConnection = "not connected to StorDB" UndefinedVersion = "undefined version" MetaSetVersions = "*set_versions" + UnsupportedDB = "unsupported database" )