From 408e55d7091c73abf4b8091100bf4f5156f1a2da Mon Sep 17 00:00:00 2001 From: Ali <010109ali@gmail.com> Date: Tue, 3 Mar 2026 17:40:05 +0400 Subject: [PATCH] e-taxes settings is single doctype now --- invoice_az/migrate_utils.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/invoice_az/migrate_utils.py b/invoice_az/migrate_utils.py index f9a48ae..80ec690 100644 --- a/invoice_az/migrate_utils.py +++ b/invoice_az/migrate_utils.py @@ -169,15 +169,28 @@ def restore_etaxes_from_file(): ) restored_scalars += 1 - # Child tables → DELETE existing + INSERT from backup + # Child tables → update parent for existing rows + insert missing ones restored_children = 0 + old_name = backup.get("old_name", "") for fieldname, rows in backup.get("children", {}).items(): table = CHILD_TABLES.get(fieldname) if not table or not _sql_table_exists(table) or not rows: continue - frappe.db.sql(f"DELETE FROM `{table}` WHERE parent = 'E-Taxes Settings'") + # First: update parent/parenttype/parentfield for ALL rows that came from old name + if old_name and old_name != "E-Taxes Settings": + frappe.db.sql( + f"UPDATE `{table}` SET parent = 'E-Taxes Settings', " + f"parenttype = 'E-Taxes Settings', parentfield = %s " + f"WHERE parent = %s", + (fieldname, old_name), + ) + updated = frappe.db.sql("SELECT ROW_COUNT() as cnt")[0][0] + if updated: + print(f" {table}: updated parent for {updated} existing rows") + restored_children += updated + # Then: insert any rows that don't exist yet for row in rows: row["parent"] = "E-Taxes Settings" row["parenttype"] = "E-Taxes Settings" @@ -186,10 +199,11 @@ def restore_etaxes_from_file(): placeholders = ", ".join(["%s"] * len(row)) try: frappe.db.sql( - f"INSERT INTO `{table}` ({columns}) VALUES ({placeholders})", + f"INSERT IGNORE INTO `{table}` ({columns}) VALUES ({placeholders})", list(row.values()), ) - restored_children += 1 + if frappe.db.sql("SELECT ROW_COUNT() as cnt")[0][0]: + restored_children += 1 except Exception as e: print(f" Warning: failed to insert row in {table}: {e}")