From 59c945bb84cdeea04eb77c1925d906f54f09d8db Mon Sep 17 00:00:00 2001 From: Himanshu Date: Tue, 21 May 2019 00:00:57 +0530 Subject: [PATCH] fix(communication): Make optimizations to the patch * use batches to move Timeline and Links to Timeline Links doctype --- .../move_timeline_links_to_dynamic_links.py | 60 +++++++++---------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/frappe/patches/v12_0/move_timeline_links_to_dynamic_links.py b/frappe/patches/v12_0/move_timeline_links_to_dynamic_links.py index 873988c7f3..ce544d658c 100644 --- a/frappe/patches/v12_0/move_timeline_links_to_dynamic_links.py +++ b/frappe/patches/v12_0/move_timeline_links_to_dynamic_links.py @@ -6,41 +6,39 @@ def execute(): frappe.reload_doc('core', 'doctype', 'communication') communications = frappe.db.sql(""" - SELECT - `tabCommunication`.name, `tabCommunication`.creation, `tabCommunication`.modified, - `tabCommunication`.modified_by,`tabCommunication`.timeline_doctype, `tabCommunication`.timeline_name, - `tabCommunication`.link_doctype, `tabCommunication`.link_name - FROM `tabCommunication` - WHERE `tabCommunication`.communication_medium='Email' - """, as_dict=True) + SELECT + `tabCommunication`.name, `tabCommunication`.creation, `tabCommunication`.modified, + `tabCommunication`.modified_by,`tabCommunication`.timeline_doctype, `tabCommunication`.timeline_name, + `tabCommunication`.link_doctype, `tabCommunication`.link_name + FROM `tabCommunication` + WHERE `tabCommunication`.communication_medium='Email' + """, as_dict=True) + + name = 10000000000001234567 + values = [] for count, communication in enumerate(communications): counter = 1 if communication.timeline_doctype and communication.timeline_name: - values = [ - counter, frappe.generate_hash(length=10), 'timeline_links', 'Communication', communication.name, - communication.timeline_doctype, communication.timeline_name, communication.creation, communication.modified, - communication.modified_by - ] - execute_query(values) + name += 1 + values.append("""({0}, "{1}", "timeline_links", "Communication", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}")""".format( + counter, str(name), communication.name, communication.timeline_doctype, + communication.timeline_name, communication.creation, communication.modified, communication.modified_by + )) counter += 1 - if communication.link_doctype and communication.link_name: - values = [ - counter, frappe.generate_hash(length=10), 'timeline_links', 'Communication', communication.name, - communication.link_doctype, communication.link_name, communication.creation, communication.modified, - communication.modified_by - ] - execute_query(values) + name += 1 + values.append("""({0}, "{1}", "timeline_links", "Communication", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}")""".format( + counter, str(name), communication.name, communication.link_doctype, + communication.link_name, communication.creation, communication.modified, communication.modified_by + )) -def execute_query(values): - try: - frappe.db.sql(""" - INSERT INTO `tabDynamic Link` - (`idx`, `name`, `parentfield`, `parenttype`, `parent`, `link_doctype`, `link_name`, `creation`, - `modified`, `modified_by`) - VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}) - """.format(values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9])) - except Exception as e: - values[1] = frappe.generate_hash(length=10) - execute_query(values) + if count % 10000 == 0 or count == len(communications) - 1: + frappe.db.sql(""" + INSERT INTO `tabDynamic Link` + (`idx`, `name`, `parentfield`, `parenttype`, `parent`, `link_doctype`, `link_name`, `creation`, + `modified`, `modified_by`) + VALUES {0} + """.format(", ".join([d for d in values]))) + + values = []