diff --git a/src/db.py b/src/db.py index c43c03d..324deae 100644 --- a/src/db.py +++ b/src/db.py @@ -401,18 +401,18 @@ def fetch_all(): return results def restore_db(data): - tables_with_uuid = ["item", "itemlist"] tables = ["item", "itemlist", "listcontent", "history"] connection = connect_db() cursor = connection.cursor() - item_uuid = {} + item_uuid_dict = {} + list_uuid_dict = {} for elem in data["item"]: # check if item already exists query = f'SELECT * FROM item WHERE itemid={str(elem["itemid"])} and skuid={str(elem["skuid"])}' cursor.execute(query) result = cursor.rowcount # complete item uuid dict - item_uuid[elem["uuid"]] = (elem["itemid"], elem["skuid"]) + item_uuid_dict[elem["uuid"]] = (elem["itemid"], elem["skuid"]) if result == 0: # if item doesn't already exist, insert it in database attributes = str(elem["attributes"]).split(',') if len(str(elem["attributes"]))>0 else "[]::text[]" @@ -425,16 +425,50 @@ def restore_db(data): result = cursor.rowcount if result == 0: # find related item - (rel_itemid, rel_skuid) = item_uuid[elem["uuid"]] + (rel_itemid, rel_skuid) = item_uuid_dict[elem["uuid"]] query = f'SELECT uuid FROM item WHERE itemid={rel_itemid} and skuid={rel_skuid}' cursor.execute(query) uuid = cursor.fetchall()[0][0] # insert history entry query = f'INSERT INTO history (uuid, price, currency, quantity, discount_percentage, h_timestamp) VALUES ({uuid}, \'{elem["price"]}\', \'{elem["currency"]}\', {elem["quantity"]}, {elem["discount_percentage"]}, \'{elem["h_timestamp"]}\')' cursor.execute(query) - + for elem in data["itemlist"]: + # check if itemlist already exists + query = f'SELECT * FROM itemlist WHERE name=\'{elem["name"]}\'' + cursor.execute(query) + result = cursor.rowcount + # complete list uuid dict + list_uuid_dict[elem["uuid"]] = elem["name"] + if result == 0: + # if itemlist doesn't already exist, insert it in database + description = f'\'{elem["description"]}\'' if len(elem["description"])>0 else "NULL" + query = f'INSERT INTO itemlist (uuid, name, description) VALUES (nextval(\'uuid_list_sequence\'), \'{elem["name"]}\', {description})' + cursor.execute(query) cursor.close() connection.commit() + for elem in data["listcontent"]: + cursor = connection.cursor() + try: + # find related item + (rel_itemid, rel_skuid) = item_uuid_dict[elem["item_uuid"]] + query = f'SELECT uuid FROM item WHERE itemid={rel_itemid} and skuid={rel_skuid}' + cursor.execute(query) + item_uuid = cursor.fetchall()[0][0] + # find related itemlist + rel_name = list_uuid_dict[elem["list_uuid"]] + query = f'SELECT uuid FROM itemlist WHERE name=\'{rel_name}\'' + cursor.execute(query) + list_uuid = cursor.fetchall()[0][0] + # try inserting item in itemlist + query = f'INSERT INTO listcontent (item_uuid, list_uuid) VALUES ({item_uuid}, {list_uuid})' + cursor.execute(query) + cursor.close() + connection.commit() + except psycopg2.errors.UniqueViolation: + # item already in itemlist + cursor.close() + connection.rollback() + connection.commit() connection.close() def export_csv():