From 14a57795d567795a7a8063d8d77bb65a8399d102 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 6 Dec 2024 17:52:54 +0100 Subject: [PATCH 1/2] fix: migration; make it resilient against old, non-conforming data --- ...te_old_item_wise_tax_detail_data_format.py | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py b/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py index 7eb2d5614cff..f129896d9da5 100644 --- a/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py +++ b/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py @@ -26,33 +26,43 @@ def execute(): updated_tax_details = {} needs_update = False - for item, tax_data in json.loads(doc.item_wise_tax_detail).items(): - if isinstance(tax_data, list) and len(tax_data) == 2: - updated_tax_details[item] = ItemWiseTaxDetail( - tax_rate=tax_data[0], - tax_amount=tax_data[1], - # can't be reliably reconstructed since it depends on the tax type - # (actual, net, previous line total, previous line net, etc) - net_amount=0.0, - ) - needs_update = True - # intermediate patch version of the originating PR - elif isinstance(tax_data, list) and len(tax_data) == 3: - updated_tax_details[item] = ItemWiseTaxDetail( - tax_rate=tax_data[0], - tax_amount=tax_data[1], - net_amount=tax_data[2], - ) - needs_update = True - elif isinstance(tax_data, str): - updated_tax_details[item] = ItemWiseTaxDetail( - tax_rate=flt(tax_data), - tax_amount=0.0, - net_amount=0.0, - ) + try: + item_iterator = json.loads(doc.item_wise_tax_detail).items() + except AttributeError as e: + # This is stale data from 2009 found in a database + if json.loads(doc.item_wise_tax_detail) == 1: + # meaning: replace with an empty {} needs_update = True else: - updated_tax_details[item] = tax_data + raise e + else: + for item, tax_data in item_iterator: + if isinstance(tax_data, list) and len(tax_data) == 2: + updated_tax_details[item] = ItemWiseTaxDetail( + tax_rate=tax_data[0], + tax_amount=tax_data[1], + # can't be reliably reconstructed since it depends on the tax type + # (actual, net, previous line total, previous line net, etc) + net_amount=0.0, + ) + needs_update = True + # intermediate patch version of the originating PR + elif isinstance(tax_data, list) and len(tax_data) == 3: + updated_tax_details[item] = ItemWiseTaxDetail( + tax_rate=tax_data[0], + tax_amount=tax_data[1], + net_amount=tax_data[2], + ) + needs_update = True + elif isinstance(tax_data, str): + updated_tax_details[item] = ItemWiseTaxDetail( + tax_rate=flt(tax_data), + tax_amount=0.0, + net_amount=0.0, + ) + needs_update = True + else: + updated_tax_details[item] = tax_data if needs_update: frappe.db.set_value( From d07f9d746a8111069f9b7a6955bf67a909878d96 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 13 Dec 2024 15:58:56 +0530 Subject: [PATCH 2/2] refactor: ignore selective malformed json --- .../v15_0/migrate_old_item_wise_tax_detail_data_format.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py b/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py index f129896d9da5..735420063d44 100644 --- a/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py +++ b/erpnext/patches/v15_0/migrate_old_item_wise_tax_detail_data_format.py @@ -30,9 +30,8 @@ def execute(): item_iterator = json.loads(doc.item_wise_tax_detail).items() except AttributeError as e: # This is stale data from 2009 found in a database - if json.loads(doc.item_wise_tax_detail) == 1: - # meaning: replace with an empty {} - needs_update = True + if isinstance(json.loads(doc.item_wise_tax_detail), int | float): + needs_update = False else: raise e else: