From 3abd00f3bb2b26beaeeea7bee03775f7368ceb08 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 5 Sep 2022 15:32:48 +0530 Subject: [PATCH 1/3] fix: Migrate old lead notes as per the new format --- erpnext/patches.txt | 1 + ...isting_lead_notes_as_per_the_new_format.py | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 4729add16b3..f48b2a1bb0a 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -307,6 +307,7 @@ erpnext.patches.v13_0.job_card_status_on_hold erpnext.patches.v14_0.copy_is_subcontracted_value_to_is_old_subcontracting_flow erpnext.patches.v14_0.migrate_gl_to_payment_ledger erpnext.patches.v14_0.crm_ux_cleanup +erpnext.patches.v14_0.migrate_existing_lead_notes_as_per_the_new_format erpnext.patches.v14_0.remove_india_localisation # 14-07-2022 erpnext.patches.v13_0.fix_number_and_frequency_for_monthly_depreciation erpnext.patches.v14_0.remove_hr_and_payroll_modules # 20-07-2022 diff --git a/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py b/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py new file mode 100644 index 00000000000..e97651e5575 --- /dev/null +++ b/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py @@ -0,0 +1,21 @@ +import frappe +from frappe.utils import cstr, strip_html + + +def execute(): + for doctype in ("Lead", "Prospect"): + if not frappe.db.has_column(doctype, "notes"): + continue + + dt = frappe.qb.DocType(doctype) + records = ( + frappe.qb.from_(dt) + .select(dt.name, dt.notes, dt.modified_by, dt.modified) + .where(dt.notes.isnotnull() & dt.notes != "") + ).run() + + for d in records: + if strip_html(cstr(d.notes)).strip(): + doc = frappe.get_doc(doctype, d.name) + doc.append("notes", {"note": d.notes, "added_by": d.modified_by, "added_on": d.modified}) + doc.save() From 2a100abef19d4db9566b7b760b1c029e1609f3e1 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 5 Sep 2022 18:16:34 +0530 Subject: [PATCH 2/3] perf: lesser SQL queries and no validation Co-authored-by: Sagar Vora --- .../v14_0/migrate_existing_lead_notes_as_per_the_new_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py b/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py index e97651e5575..6ba5a9ff214 100644 --- a/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py +++ b/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py @@ -18,4 +18,4 @@ def execute(): if strip_html(cstr(d.notes)).strip(): doc = frappe.get_doc(doctype, d.name) doc.append("notes", {"note": d.notes, "added_by": d.modified_by, "added_on": d.modified}) - doc.save() + doc.update_child_table("notes") From 4b1345202210680e4bcb7bc4f299cc0db3829543 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 5 Sep 2022 18:27:03 +0530 Subject: [PATCH 3/3] fix: drop old notes column from lead and prospect --- .../migrate_existing_lead_notes_as_per_the_new_format.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py b/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py index 6ba5a9ff214..032aeccc23d 100644 --- a/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py +++ b/erpnext/patches/v14_0/migrate_existing_lead_notes_as_per_the_new_format.py @@ -3,7 +3,7 @@ from frappe.utils import cstr, strip_html def execute(): - for doctype in ("Lead", "Prospect"): + for doctype in ("Lead", "Prospect", "Opportunity"): if not frappe.db.has_column(doctype, "notes"): continue @@ -19,3 +19,5 @@ def execute(): doc = frappe.get_doc(doctype, d.name) doc.append("notes", {"note": d.notes, "added_by": d.modified_by, "added_on": d.modified}) doc.update_child_table("notes") + + frappe.db.sql_ddl(f"alter table `tab{doctype}` drop column `notes`")