From 86f29aeaa34db8456161e530da0e4ed7bf500911 Mon Sep 17 00:00:00 2001 From: abhishek Date: Tue, 12 Oct 2021 03:21:08 +0530 Subject: [PATCH] fix: missing logical cases - handle first time imports - update hash and timestamps --- frappe/modules/import_file.py | 40 +++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/frappe/modules/import_file.py b/frappe/modules/import_file.py index 230a59df6e..f4179fb811 100644 --- a/frappe/modules/import_file.py +++ b/frappe/modules/import_file.py @@ -7,8 +7,9 @@ import os import frappe from frappe.model.base_document import get_controller from frappe.modules import get_module_path, scrub_dt_dn -from frappe.utils import get_datetime_str from frappe.query_builder import DocType +from frappe.utils import get_datetime_str, now + def caclulate_hash(path: str) -> str: """Calculate md5 hash of the file in binary mode @@ -77,22 +78,26 @@ def import_file_by_path(path, force=False, data_import=False, pre_process=None, docs = [docs] for doc in docs: - if not force: + + # modified timestamp in db, none if doctype's first import + db_modified_timestamp = frappe.db.get_value(doc["doctype"], doc["name"], "modified") + is_db_timestamp_latest = db_modified_timestamp and doc.get("modified") <= get_datetime_str(db_modified_timestamp) + + if not force or db_modified_timestamp: try: stored_hash = frappe.db.get_value(doc["doctype"], doc["name"], "migration_hash") except Exception: frappe.flags.dt += [doc["doctype"]] stored_hash = None - # fallback if stored_hash doesn't exist - if not stored_hash and is_timestamp_changed(doc): + # if hash exists and is equal no need to update + if stored_hash and stored_hash == calculated_hash: return False - if calculated_hash == stored_hash: + # if hash doesn't exist, check if db timestamp is same as json timestamp, add hash if from doctype + if is_db_timestamp_latest and doc["doctype"] != "DocType": return False - original_modified = doc.get("modified") - import_doc( docdict=doc, force=force, @@ -103,15 +108,24 @@ def import_file_by_path(path, force=False, data_import=False, pre_process=None, path=path, ) - # not using db.set_value to avoid making changes in tabSingles if doc["doctype"] == "DocType": - doctype_table = frappe.qb.DocType("DocType") - frappe.qb.update(doctype_table).set( + doctype_table = DocType("DocType") + frappe.qb.update( + doctype_table + ).set( doctype_table.migration_hash, calculated_hash - ).where(doctype_table.name == doc["name"]).run() + ).where( + doctype_table.name == doc["name"] + ).run() - if original_modified: - update_modified(original_modified, doc) + new_modified_timestamp = doc.get("modified") + + # if db timestamp is newer, hash must have changed, must update db timestamp + if is_db_timestamp_latest and doc["doctype"] == "DocType": + new_modified_timestamp = now() + + if new_modified_timestamp: + update_modified(new_modified_timestamp, doc) return True