Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vfd_providers/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ------------
Expand Down
3 changes: 2 additions & 1 deletion vfd_providers/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

[post_model_sync]

vfd_providers.patches.custom_fields.vfd_providers_updated_custom_fields
vfd_providers.patches.custom_fields.vfd_providers_updated_custom_fields
vfd_providers.patches.migrate_vfd_customer_data
74 changes: 74 additions & 0 deletions vfd_providers/patches/migrate_vfd_customer_data.py
Original file line number Diff line number Diff line change
@@ -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")