Merge pull request #1811 from adragusin/master

uniformed the error for duplicate key
This commit is contained in:
Dan Christian Bogos
2019-12-10 09:45:21 +01:00
committed by GitHub
7 changed files with 166 additions and 12 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

@@ -931,7 +931,7 @@ func (ms *MongoStorage) RemoveSMCosts(qryFltr *utils.SMCostFilter) error {
})
}
func (ms *MongoStorage) SetCDR(cdr *CDR, allowUpdate bool) (err error) {
func (ms *MongoStorage) SetCDR(cdr *CDR, allowUpdate bool) error {
if cdr.OrderID == 0 {
cdr.OrderID = ms.cnter.Next()
}
@@ -940,11 +940,13 @@ func (ms *MongoStorage) SetCDR(cdr *CDR, allowUpdate bool) (err error) {
_, err = ms.getCol(ColCDRs).UpdateOne(sctx,
bson.M{CGRIDLow: cdr.CGRID, RunIDLow: cdr.RunID},
bson.M{"$set": cdr}, options.Update().SetUpsert(true))
// return err
} else {
_, err = ms.getCol(ColCDRs).InsertOne(sctx, cdr)
return
}
return err
_, 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
})
}

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()

View File

@@ -32,9 +32,8 @@ import (
)
var (
cfg *config.CGRConfig
storDB StorDB
storDB2ndDBname string
cfg *config.CGRConfig
storDB StorDB
)
// subtests to be executed for each confDIR
@@ -71,7 +70,6 @@ func TestStorDBitMySQL(t *testing.T) {
cfg.StorDbCfg().ConnMaxLifetime); err != nil {
t.Fatal(err)
}
storDB2ndDBname = "mysql"
for _, stest := range sTestsStorDBit {
stestFullName := runtime.FuncForPC(reflect.ValueOf(stest).Pointer()).Name()
split := strings.Split(stestFullName, ".")
@@ -91,7 +89,6 @@ func TestStorDBitPostgresSQL(t *testing.T) {
cfg.StorDbCfg().MaxIdleConns, cfg.StorDbCfg().ConnMaxLifetime); err != nil {
t.Fatal(err)
}
storDB2ndDBname = "postgres"
for _, stest := range sTestsStorDBit {
stestFullName := runtime.FuncForPC(reflect.ValueOf(stest).Pointer()).Name()
split := strings.Split(stestFullName, ".")
@@ -110,7 +107,6 @@ func TestStorDBitMongo(t *testing.T) {
utils.StorDB, cfg.StorDbCfg().StringIndexedFields, false); err != nil {
t.Fatal(err)
}
storDB2ndDBname = "todo"
for _, stest := range sTestsStorDBit {
stestFullName := runtime.FuncForPC(reflect.ValueOf(stest).Pointer()).Name()
split := strings.Split(stestFullName, ".")
@@ -1229,7 +1225,11 @@ func testStorDBitCRUDCDRs(t *testing.T) {
t.Error(err)
}
}
for _, cdr := range snd {
if err := storDB.SetCDR(cdr, false); err == nil || err != utils.ErrExists {
t.Error(err) // for mongo will fail because of indexes
}
}
// READ
if rcv, _, err := storDB.GetCDRs(&filter, false); err != nil {
t.Error(err)