Files
cgrates/migrator/storage_sql.go
2022-02-06 09:52:26 +01:00

91 lines
2.8 KiB
Go

/*
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 <http://www.gnu.org/licenses/>
*/
package migrator
import (
"database/sql"
"fmt"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
_ "github.com/go-sql-driver/mysql"
)
func newMigratorSQL(stor engine.StorDB) (sqlMig *migratorSQL) {
return &migratorSQL{
storDB: &stor,
sqlStorage: stor.(*engine.SQLStorage),
}
}
type migratorSQL struct {
storDB *engine.StorDB
sqlStorage *engine.SQLStorage
rowIter *sql.Rows
}
func (sqlMig *migratorSQL) close() {
sqlMig.sqlStorage.Close()
}
func (sqlMig *migratorSQL) StorDB() engine.StorDB {
return *sqlMig.storDB
}
func (mgSQL *migratorSQL) renameV1SMCosts() (err error) {
qry := "RENAME TABLE sm_costs TO session_costs;"
if mgSQL.StorDB().GetStorageType() == utils.Postgres {
qry = "ALTER TABLE sm_costs RENAME TO session_costs"
}
if _, err := mgSQL.sqlStorage.DB.Exec(qry); err != nil {
return err
}
return
}
func (mgSQL *migratorSQL) createV1SMCosts() (err error) {
qry := fmt.Sprint("CREATE TABLE sm_costs ( id int(11) NOT NULL AUTO_INCREMENT, run_id varchar(64) NOT NULL, origin_host varchar(64) NOT NULL, origin_id varchar(128) NOT NULL, cost_source varchar(64) NOT NULL, `usage` BIGINT NOT NULL, cost_details MEDIUMTEXT, created_at TIMESTAMP NULL,deleted_at TIMESTAMP NULL, PRIMARY KEY (`id`),UNIQUE KEY costid ( run_id),KEY origin_idx (origin_host, origin_id),KEY run_origin_idx (run_id, origin_id),KEY deleted_at_idx (deleted_at));")
if mgSQL.StorDB().GetStorageType() == utils.Postgres {
qry = `
CREATE TABLE sm_costs (
id SERIAL PRIMARY KEY,
run_id VARCHAR(64) NOT NULL,
origin_host VARCHAR(64) NOT NULL,
origin_id VARCHAR(128) NOT NULL,
cost_source VARCHAR(64) NOT NULL,
usage BIGINT NOT NULL,
cost_details jsonb,
created_at TIMESTAMP WITH TIME ZONE,
deleted_at TIMESTAMP WITH TIME ZONE NULL,
UNIQUE ( run_id)
);
`
}
if _, err := mgSQL.sqlStorage.DB.Exec("DROP TABLE IF EXISTS session_costs;"); err != nil {
return err
}
if _, err := mgSQL.sqlStorage.DB.Exec("DROP TABLE IF EXISTS sm_costs;"); err != nil {
return err
}
if _, err := mgSQL.sqlStorage.DB.Exec(qry); err != nil {
return err
}
return
}