From 8657690ef5043394cd117a8f5affc99cd9356cc3 Mon Sep 17 00:00:00 2001 From: Akhil Narang Date: Thu, 8 Jan 2026 18:53:41 +0530 Subject: [PATCH] refactor: use wildcard to avoid duplicating some queries Signed-off-by: Akhil Narang --- .../copy_communication_date_to_link.py | 10 +++++----- .../system_health_report.py | 19 ++----------------- frappe/desk/form/load.py | 3 +-- frappe/model/utils/user_settings.py | 5 +---- frappe/utils/make_random.py | 8 ++------ 5 files changed, 11 insertions(+), 34 deletions(-) diff --git a/frappe/core/doctype/communication_link/patches/copy_communication_date_to_link.py b/frappe/core/doctype/communication_link/patches/copy_communication_date_to_link.py index 563acd8f8d..2331ea6eb1 100644 --- a/frappe/core/doctype/communication_link/patches/copy_communication_date_to_link.py +++ b/frappe/core/doctype/communication_link/patches/copy_communication_date_to_link.py @@ -16,18 +16,18 @@ def execute(): and c.communication_date is not null limit %s """, - "postgres": """ - UPDATE "tabCommunication Link" + "*": """ + UPDATE `tabCommunication Link` SET communication_date = sub.communication_date FROM ( SELECT cl.name, c.communication_date - FROM "tabCommunication Link" cl - JOIN "tabCommunication" c ON cl.parent = c.name + FROM `tabCommunication Link` cl + JOIN `tabCommunication` c ON cl.parent = c.name WHERE cl.communication_date IS NULL AND c.communication_date IS NOT NULL LIMIT %s ) AS sub - WHERE "tabCommunication Link".name = sub.name + WHERE `tabCommunication Link`.name = sub.name """, }, (batch_size,), diff --git a/frappe/desk/doctype/system_health_report/system_health_report.py b/frappe/desk/doctype/system_health_report/system_health_report.py index a9fccc4d48..3c70c16bdc 100644 --- a/frappe/desk/doctype/system_health_report/system_health_report.py +++ b/frappe/desk/doctype/system_health_report/system_health_report.py @@ -203,7 +203,7 @@ class SystemHealthReport(Document): # Exclude "maybe" curently executing job upper_threshold = add_to_date(None, minutes=-30, as_datetime=True) - mariadb_query = """ + query = """ SELECT scheduled_job_type, AVG(CASE WHEN status != 'Complete' THEN 1 ELSE 0 END) * 100 AS failure_rate FROM `tabScheduled Job Log` @@ -231,25 +231,10 @@ class SystemHealthReport(Document): LIMIT 5 """ - sqlite_query = """ - SELECT scheduled_job_type, - AVG(CASE WHEN status != 'Complete' THEN 1 ELSE 0 END) * 100 AS failure_rate - FROM `tabScheduled Job Log` - WHERE - creation > %(lower_threshold)s - AND modified > %(lower_threshold)s - AND creation < %(upper_threshold)s - GROUP BY scheduled_job_type - HAVING failure_rate > 0 - ORDER BY failure_rate DESC - LIMIT 5 - """ - failing_jobs = frappe.db.multisql( { - "mariadb": mariadb_query, "postgres": postgres_query, - "sqlite": sqlite_query, + "*": query, }, {"lower_threshold": lower_threshold, "upper_threshold": upper_threshold}, as_dict=True, diff --git a/frappe/desk/form/load.py b/frappe/desk/form/load.py index a0e6999b87..87f1954093 100644 --- a/frappe/desk/form/load.py +++ b/frappe/desk/form/load.py @@ -334,8 +334,7 @@ def get_communication_data( return frappe.db.multisql( { "sqlite": sqlite_query, - "postgres": query, - "mariadb": query, + "*": query, }, dict( doctype=doctype, diff --git a/frappe/model/utils/user_settings.py b/frappe/model/utils/user_settings.py index cd81b1e3ac..68766a8d05 100644 --- a/frappe/model/utils/user_settings.py +++ b/frappe/model/utils/user_settings.py @@ -54,10 +54,7 @@ def sync_user_settings(): "mariadb": """INSERT INTO `__UserSettings`(`user`, `doctype`, `data`) VALUES (%s, %s, %s) ON DUPLICATE key UPDATE `data`=%s""", - "postgres": """INSERT INTO `__UserSettings` (`user`, `doctype`, `data`) - VALUES (%s, %s, %s) - ON CONFLICT ("user", "doctype") DO UPDATE SET `data`=%s""", - "sqlite": """INSERT INTO `__UserSettings` (`user`, `doctype`, `data`) + "*": """INSERT INTO `__UserSettings` (`user`, `doctype`, `data`) VALUES (%s, %s, %s) ON CONFLICT (`user`, `doctype`) DO UPDATE SET `data`=%s""", }, diff --git a/frappe/utils/make_random.py b/frappe/utils/make_random.py index 6d9750ee71..48b3e7fae4 100644 --- a/frappe/utils/make_random.py +++ b/frappe/utils/make_random.py @@ -41,12 +41,8 @@ def get_random(doctype: str, filters: dict | None = None, doc: bool = False): out = frappe.db.multisql( { - "mariadb": f"""select name from `tab{doctype}` {condition} - order by RAND() limit 1 offset 0""", - "postgres": f"""select name from `tab{doctype}` {condition} - order by RANDOM() limit 1 offset 0""", - "sqlite": f"""select name from `tab{doctype}` {condition} - order by RANDOM() limit 1 offset 0""", + "mariadb": f"select name from `tab{doctype}` {condition} order by RAND() limit 1 offset 0", + "*": f"select name from `tab{doctype}` {condition} order by RANDOM() limit 1 offset 0", } )