From 8356a1ec588ce31ce44e49e728ca1ad5eaa8d997 Mon Sep 17 00:00:00 2001 From: merdan Date: Sat, 24 Sep 2022 13:14:56 +0500 Subject: [PATCH] * --- controllers/ImportController.go | 35 +++++++++++++++------------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/controllers/ImportController.go b/controllers/ImportController.go index 69b8b26..84f493f 100644 --- a/controllers/ImportController.go +++ b/controllers/ImportController.go @@ -140,15 +140,12 @@ func StartImport(w http.ResponseWriter, route *http.Request) { func importCategoryProducts(dbName string, db *gorm.DB) { defer mainImportWG.Done() - dbExists := helper.CheckDBExists(os.Getenv("couch_db_source") + dbName) - fmt.Println(dbName) - - if dbExists { + if dbExists := helper.CheckDBExists(os.Getenv("couch_db_source") + dbName); dbExists { totalDocCount := getTotalDocumentCount(dbName) skip := 0 - limit := 500 - + limit := 100 + totalImport := 0 for skip < totalDocCount { var response models.BagistoModelResponse @@ -166,20 +163,22 @@ func importCategoryProducts(dbName string, db *gorm.DB) { continue } - err = json.Unmarshal(body, &response) - - if err != nil { + if err = json.Unmarshal(body, &response); err != nil { log.Println(err.Error()) continue } //itearate 100 row products for _, element := range response.Rows { - err := ImportProduct(element.Doc, db) - if err != nil { - return + + if err := ImportProduct(element.Doc, db); err != nil { + log.Println(err) + } else { + totalImport++ } } + + log.Printf("%s total imported documents count %d \n", dbName, totalImport) } } else { fmt.Println(dbName + "+doesnt exist") @@ -247,16 +246,12 @@ func ImportProduct(product models.Product, db *gorm.DB) error { famAndSellerWG.Wait() //wait until attribute families and sellers are not get from mysql //BEGIN TRANSACTION - // begin a transaction - tx := *db.Begin() - categories, keywords, errCat := getCats(&tx, product.Categories) + categories, keywords, errCat := getCats(&tx, product.Categories) if errCat != nil { log.Println("ERR0001" + errCat.Error()) return errCat - } else { - log.Println(product.Name) } brand, err := gm.FindOrCreateBrand(&tx, product.Brand, categories) @@ -657,11 +652,13 @@ func ImportProduct(product models.Product, db *gorm.DB) error { if errSProduct != nil { log.Println("ERR10" + errSProduct.Error()) - tx.Rollback() + return tx.Rollback().Error return errSProduct + } else { + log.Println(product.Name) + return tx.Commit().Error } - return tx.Commit().Error } func createSellerProduct(flat *gm.ProductFlat, sellerURL string) gm.MarketplaceProduct {