From f7abf3b1cff7affc429355c277417037daea0a57 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 12 Jun 2023 21:37:31 +0530 Subject: [PATCH 1/2] fix: handle fixture syncing for missing doctypes --- frappe/utils/fixtures.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/frappe/utils/fixtures.py b/frappe/utils/fixtures.py index 821b1a1187..81e0edbca5 100644 --- a/frappe/utils/fixtures.py +++ b/frappe/utils/fixtures.py @@ -17,15 +17,31 @@ def sync_fixtures(app=None): frappe.flags.in_fixtures = True for app in apps: - fixtures_path = frappe.get_app_path(app, "fixtures") - if os.path.exists(fixtures_path): - import_doc(fixtures_path) - + import_fixtures(app) import_custom_scripts(app) frappe.flags.in_fixtures = False +def import_fixtures(app): + fixtures_path = frappe.get_app_path(app, "fixtures") + if not os.path.exists(fixtures_path): + return + + fixture_files = os.listdir(fixtures_path) + + for fname in fixture_files: + if not fname.endswith(".json"): + continue + + file_path = frappe.get_app_path(app, "fixtures", fname) + try: + import_doc(file_path) + except (ImportError, frappe.DoesNotExistError) as e: + # fixture syncing for missing doctypes + print(f"Skipping fixture syncing from the file {fname}. Reason: {e}") + + def import_custom_scripts(app): """Import custom scripts from `[app]/fixtures/custom_scripts`""" if os.path.exists(frappe.get_app_path(app, "fixtures", "custom_scripts")): From 1bab900e6f2f0757a2870e13a967fc800bff74c3 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 12 Jun 2023 22:09:08 +0530 Subject: [PATCH 2/2] fix: skip custom script syncing for missing doctypes --- frappe/utils/fixtures.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/frappe/utils/fixtures.py b/frappe/utils/fixtures.py index 81e0edbca5..241f17e62b 100644 --- a/frappe/utils/fixtures.py +++ b/frappe/utils/fixtures.py @@ -44,18 +44,34 @@ def import_fixtures(app): def import_custom_scripts(app): """Import custom scripts from `[app]/fixtures/custom_scripts`""" - if os.path.exists(frappe.get_app_path(app, "fixtures", "custom_scripts")): - for fname in os.listdir(frappe.get_app_path(app, "fixtures", "custom_scripts")): - if fname.endswith(".js"): - with open(frappe.get_app_path(app, "fixtures", "custom_scripts") + os.path.sep + fname) as f: - doctype = fname.rsplit(".", 1)[0] - script = f.read() - if frappe.db.exists("Client Script", {"dt": doctype}): - custom_script = frappe.get_doc("Client Script", {"dt": doctype}) - custom_script.script = script - custom_script.save() - else: - frappe.get_doc({"doctype": "Client Script", "dt": doctype, "script": script}).insert() + scripts_folder = frappe.get_app_path(app, "fixtures", "custom_scripts") + if not os.path.exists(scripts_folder): + return + + for fname in os.listdir(scripts_folder): + if not fname.endswith(".js"): + continue + + doctype = fname.rsplit(".", 1)[0] + if not frappe.db.exists("DocType", doctype): + print( + f"Skipping custom script fixture syncing for the missing doctype {doctype} from the file {fname}" + ) + continue + + # not using get_app_path here as it scrubs the fname (will not work for dt name with > 1 word) + file_path = scripts_folder + os.path.sep + fname + + with open(file_path) as f: + script = f.read() + if frappe.db.exists("Client Script", {"dt": doctype}): + client_script = frappe.get_doc("Client Script", {"dt": doctype}) + client_script.script = script + client_script.save() + else: + client_script = frappe.new_doc("Client Script") + client_script.update({"__newname": doctype, "dt": doctype, "script": script}) + client_script.insert() def export_fixtures(app=None):