refactor: use wildcard to avoid duplicating some queries

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
This commit is contained in:
Akhil Narang 2026-01-08 18:53:41 +05:30
parent 62b4700395
commit 8657690ef5
No known key found for this signature in database
GPG key ID: 9DCC61E211BF645F
5 changed files with 11 additions and 34 deletions

View file

@ -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,),

View file

@ -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,

View file

@ -334,8 +334,7 @@ def get_communication_data(
return frappe.db.multisql(
{
"sqlite": sqlite_query,
"postgres": query,
"mariadb": query,
"*": query,
},
dict(
doctype=doctype,

View file

@ -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""",
},

View file

@ -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",
}
)