From 2212ae304f3d1f0d3a1f0ad97c2155e488bb34a7 Mon Sep 17 00:00:00 2001 From: Sydney Kibanga Date: Thu, 13 Mar 2025 13:13:07 +0300 Subject: [PATCH] feat: Migrate legacy VFD custom fields to new underscore naming convention --- vfd_providers/hooks.py | 1 + vfd_providers/patches.txt | 3 +- .../patches/migrate_vfd_customer_data.py | 74 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 vfd_providers/patches/migrate_vfd_customer_data.py diff --git a/vfd_providers/hooks.py b/vfd_providers/hooks.py index 181a995..877b179 100644 --- a/vfd_providers/hooks.py +++ b/vfd_providers/hooks.py @@ -70,6 +70,7 @@ # before_install = "vfd_providers.install.before_install" # after_install = "vfd_providers.install.after_install" +after_migrate = "vfd_providers.patches.migrate_vfd_customer_data.delete" # Uninstallation # ------------ diff --git a/vfd_providers/patches.txt b/vfd_providers/patches.txt index 5b8c354..dff05cf 100644 --- a/vfd_providers/patches.txt +++ b/vfd_providers/patches.txt @@ -3,4 +3,5 @@ [post_model_sync] -vfd_providers.patches.custom_fields.vfd_providers_updated_custom_fields \ No newline at end of file +vfd_providers.patches.custom_fields.vfd_providers_updated_custom_fields +vfd_providers.patches.migrate_vfd_customer_data \ No newline at end of file diff --git a/vfd_providers/patches/migrate_vfd_customer_data.py b/vfd_providers/patches/migrate_vfd_customer_data.py new file mode 100644 index 0000000..b87ecfd --- /dev/null +++ b/vfd_providers/patches/migrate_vfd_customer_data.py @@ -0,0 +1,74 @@ +import frappe + + +def execute(): + def field_exists(doctype, fieldname): + return frappe.db.has_column(doctype, fieldname) + + def field_not_empty_filter(fieldname): + return [fieldname, "not in", (None, "")] + + def process_batch(doctype, fields, conditions, limit_page_length): + start = 0 + while True: + records = frappe.get_all( + doctype, + fields=fields, + filters=conditions, + limit_start=start, + limit_page_length=limit_page_length, + order_by="creation desc", + ) + if not records: + break + + for record in records: + frappe.db.set_value( + doctype, + record.name, + "vfd_cust_id_type", + record.vfd_custidtype, + update_modified=False, + ) + frappe.db.set_value( + doctype, + record.name, + "vfd_cust_id", + record.vfd_custid, + update_modified=False, + ) + frappe.db.commit() + start += limit_page_length + + if field_exists("Customer", "vfd_cust_id_type") and field_exists( + "Customer", "vfd_cust_id" + ): + conditions = [ + field_not_empty_filter("vfd_custidtype"), + field_not_empty_filter("vfd_custid"), + ] + process_batch( + "Customer", + fields=[ + "name", + "vfd_cust_id_type", + "vfd_cust_id", + "vfd_custidtype", + "vfd_custid", + ], + conditions=conditions, + limit_page_length=5000, + ) + + +def delete(): + + def delete_custom_field(doctype, fieldname): + custom_field_name = f"{doctype}-{fieldname}" + if frappe.db.exists("Custom Field", custom_field_name): + custom_field_doc = frappe.get_doc("Custom Field", custom_field_name) + custom_field_doc.delete() + frappe.db.commit() + + delete_custom_field("Customer", "vfd_custidtype") + delete_custom_field("Customer", "vfd_custid")