seitime-frappe/frappe/patches/v14_0/remove_db_aggregation.py
Gavin D'souza 43fc713dd7 refactor: Remove Aggregation methods from DB API
* Make it DRY & make it "better"
* Add to patches.txt so the patch runs :')
* Style fixes - tabs > spaces for consistency, removed unnecessary
  blocks
2021-11-29 13:53:27 +05:30

32 lines
940 B
Python

import re
import frappe
from frappe.query_builder import DocType
def execute():
"""Replace temporarily available Database Aggregate APIs on frappe (develop)
APIs changed:
* frappe.db.max => frappe.qb.max
* frappe.db.min => frappe.qb.min
* frappe.db.sum => frappe.qb.sum
* frappe.db.avg => frappe.qb.avg
"""
ServerScript = DocType("Server Script")
server_scripts = frappe.qb.from_(ServerScript).where(
ServerScript.script.like("%frappe.db.max(%")
| ServerScript.script.like("%frappe.db.min(%")
| ServerScript.script.like("%frappe.db.sum(%")
| ServerScript.script.like("%frappe.db.avg(%")
).select(
"name", "script"
).run(as_dict=True)
for server_script in server_scripts:
name, script = server_script["name"], server_script["script"]
for agg in ["avg", "max", "min", "sum"]:
script = re.sub(f"frappe.db.{agg}(", f"frappe.qb.{agg}(", script)
frappe.db.update("Server Script", name, "script", script)