lalithadevi commited on
Commit
22b2737
1 Parent(s): e3d1ff1

Update db_operations/db_write.py

Browse files
Files changed (1) hide show
  1. db_operations/db_write.py +16 -2
db_operations/db_write.py CHANGED
@@ -36,6 +36,7 @@ class DBWrite:
36
  self.__error = 1
37
  self.__close_connection()
38
  self.__client = None
 
39
 
40
 
41
  def __insert(self, documents):
@@ -43,13 +44,26 @@ class DBWrite:
43
 
44
  db = self.__client[self.database]
45
  coll = db[self.collection]
 
 
 
 
 
46
  if (self.db_type == "production") or (COLLECT_PREDICTION_DATA==0):
47
  coll.drop()
48
- coll.insert_many(documents=documents)
 
 
 
 
 
 
 
49
  except Exception as insert_err:
50
  self.__error = 1
51
  self.__close_connection()
52
-
 
53
 
54
  def __close_connection(self):
55
  if self.__client is not None:
 
36
  self.__error = 1
37
  self.__close_connection()
38
  self.__client = None
39
+ raise
40
 
41
 
42
  def __insert(self, documents):
 
44
 
45
  db = self.__client[self.database]
46
  coll = db[self.collection]
47
+ # if (self.db_type == "production") or (COLLECT_PREDICTION_DATA==0):
48
+ # coll.drop()
49
+ # coll.insert_many(documents=documents)
50
+
51
+
52
  if (self.db_type == "production") or (COLLECT_PREDICTION_DATA==0):
53
  coll.drop()
54
+ coll.insert_many(documents=documents)
55
+ else:
56
+ for doc in documents:
57
+ filter_query = {"url": doc["url"]}
58
+ update_query = {"$set": doc}
59
+ coll.update_one(filter_query, update_query, upsert=True)
60
+
61
+
62
  except Exception as insert_err:
63
  self.__error = 1
64
  self.__close_connection()
65
+ logger.critical(f'Error while inserting into DB: {insert_error}')
66
+ raise
67
 
68
  def __close_connection(self):
69
  if self.__client is not None: