uniformed the error for duplicate key

This commit is contained in:
adragusin
2019-12-09 16:41:24 +02:00
parent 74ed5e9958
commit 4cf69dd552
6 changed files with 155 additions and 0 deletions

View File

@@ -55,6 +55,27 @@ func TestCDRsITInternal(t *testing.T) {
}
}
func TestCDRsITMongo(t *testing.T) {
cdrsConfDIR = "cdrsv1mongo"
for _, stest := range sTestsCDRsIT {
t.Run(cdrsConfDIR, stest)
}
}
func TestCDRsITMySql(t *testing.T) {
cdrsConfDIR = "cdrsv1mysql"
for _, stest := range sTestsCDRsIT {
t.Run(cdrsConfDIR, stest)
}
}
func TestCDRsITPostgres(t *testing.T) {
cdrsConfDIR = "cdrsv1postgres"
for _, stest := range sTestsCDRsIT {
t.Run(cdrsConfDIR, stest)
}
}
func testV1CDRsInitConfig(t *testing.T) {
var err error
cdrsCfgPath = path.Join(*dataDir, "conf", "samples", cdrsConfDIR)

View File

@@ -0,0 +1,44 @@
{
// CGRateS Configuration file
//
// Used in apier/v1/cdrs_it_test
"data_db": {
"db_type": "mongo",
"db_name": "10",
"db_port": 27017,
},
"stor_db": {
"db_type": "mongo",
"db_name": "cgrates",
"db_port": 27017,
},
"rals": {
"enabled": true
},
"scheduler": {
"enabled": true
},
"cdrs": {
"enabled": true,
"rals_conns": [
{"address": "127.0.0.1:2012", "transport":"*json"},
],
},
"apier": {
"scheduler_conns": [ // connections to SchedulerS for reloads
{"address": "*internal"},
],
},
}

View File

@@ -0,0 +1,41 @@
{
// CGRateS Configuration file
//
// Used in apier/v1/cdrs_it_test
"data_db": { // database used to store runtime data (eg: accounts, cdr stats)
"db_type": "redis", // data_db type: <redis|mongo>
"db_port": 6379, // data_db port to reach the database
"db_name": "10", // data_db database name to connect to
},
"stor_db": {
"db_password": "CGRateS.org",
},
"rals": {
"enabled": true
},
"scheduler": {
"enabled": true
},
"cdrs": {
"enabled": true,
"rals_conns": [
{"address": "127.0.0.1:2012", "transport":"*json"},
],
},
"apier": {
"scheduler_conns": [ // connections to SchedulerS for reloads
{"address": "*internal"},
],
},
}

View File

@@ -0,0 +1,43 @@
{
// CGRateS Configuration file
//
// Used in apier/v1/cdrs_it_test
"data_db": { // database used to store runtime data (eg: accounts, cdr stats)
"db_type": "redis", // data_db type: <redis|mongo>
"db_port": 6379, // data_db port to reach the database
"db_name": "10", // data_db database name to connect to
},
"stor_db": {
"db_type": "postgres", // stor database type to use: <mysql|postgres>
"db_port": 5432, // the port to reach the stordb
"db_password": "CGRateS.org",
},
"rals": {
"enabled": true
},
"scheduler": {
"enabled": true
},
"cdrs": {
"enabled": true,
"rals_conns": [
{"address": "127.0.0.1:2012", "transport":"*json"},
],
},
"apier": {
"scheduler_conns": [ // connections to SchedulerS for reloads
{"address": "*internal"},
],
},
}

View File

@@ -943,6 +943,9 @@ func (ms *MongoStorage) SetCDR(cdr *CDR, allowUpdate bool) (err error) {
// return err
} else {
_, err = ms.getCol(ColCDRs).InsertOne(sctx, cdr)
if err != nil && strings.Contains(err.Error(), "E11000") { // Mongo returns E11000 when key is duplicated
err = utils.ErrExists
}
}
return err
})

View File

@@ -879,6 +879,9 @@ func (self *SQLStorage) SetCDR(cdr *CDR, allowUpdate bool) error {
if saved.Error != nil {
tx.Rollback()
if !allowUpdate {
if strings.Contains(saved.Error.Error(), "1062") || strings.Contains(saved.Error.Error(), "duplicate key") { // returns 1062/pq when key is duplicated
return utils.ErrExists
}
return saved.Error
}
tx = self.db.Begin()