refactor(minor): Use ORM instead of raw delete queries

Modified query building for "IN" statements, as well as older condition
builders to use frappe.db.delete
This commit is contained in:
Gavin D'souza 2021-07-27 02:12:19 +05:30
parent 3ca7fa7796
commit d63affc732
4 changed files with 20 additions and 29 deletions

View file

@ -154,29 +154,23 @@ def clear_default(key=None, value=None, parent=None, name=None, parenttype=None)
:param name: Default ID.
:param parenttype: Clear defaults table for a particular type e.g. **User**.
"""
conditions = []
values = []
filters = {}
if name:
conditions.append("name=%s")
values.append(name)
filters.update({"name": name})
else:
if key:
conditions.append("defkey=%s")
values.append(key)
filters.update({"defkey": key})
if value:
conditions.append("defvalue=%s")
values.append(value)
filters.update({"defvalue": value})
if parent:
conditions.append("parent=%s")
values.append(parent)
filters.update({"parent": parent})
if parenttype:
conditions.append("parenttype=%s")
values.append(parenttype)
filters.update({"parenttype": parenttype})
if parent:
clear_defaults_cache(parent)
@ -184,11 +178,10 @@ def clear_default(key=None, value=None, parent=None, name=None, parenttype=None)
clear_defaults_cache("__default")
clear_defaults_cache("__global")
if not conditions:
if not filters:
raise Exception("[clear_default] No key specified.")
frappe.db.sql("""delete from tabDefaultValue where {0}""".format(" and ".join(conditions)),
tuple(values))
frappe.db.delete("DefaultValue", filters)
_clear_cache(parent)

View file

@ -39,11 +39,7 @@ class ToDo(Document):
self.update_in_reference()
def on_trash(self):
# unlink todo from linked comments
frappe.db.delete("Communication Link", {
"link_doctype": self.doctype,
"link_name": self.name
})
self.delete_communication_links()
self.update_in_reference()
def add_assign_comment(self, text, comment_type):
@ -52,6 +48,13 @@ class ToDo(Document):
frappe.get_doc(self.reference_type, self.reference_name).add_comment(comment_type, text)
def delete_communication_links(self):
# unlink todo from linked comments
return frappe.db.delete("Communication Link", {
"link_doctype": self.doctype,
"link_name": self.name
})
def update_in_reference(self):
if not (self.reference_type and self.reference_name):
return

View file

@ -173,14 +173,8 @@ def clear_outbox(days=None):
WHERE `priority`=0 AND `modified` < (NOW() - INTERVAL '{0}' DAY)""".format(days))
if email_queues:
# TODO: email_queues IN frappe.db.sql
frappe.db.sql("""DELETE FROM `tabEmail Queue` WHERE `name` IN ({0})""".format(
','.join(['%s']*len(email_queues)
)), tuple(email_queues))
frappe.db.sql("""DELETE FROM `tabEmail Queue Recipient` WHERE `parent` IN ({0})""".format(
','.join(['%s']*len(email_queues)
)), tuple(email_queues))
frappe.db.delete("Email Queue", {"name": ("in", email_queues)})
frappe.db.delete("Email Queue Recipient", {"parent": ("in", email_queues)})
def set_expiry_for_email_queue():
''' Mark emails as expire that has not sent for 7 days.

View file

@ -2,7 +2,8 @@ import frappe
def execute():
#if current = 0, simply delete the key as it'll be recreated on first entry
frappe.db.sql('delete from `tabSeries` where current = 0')
frappe.db.delete("Series", {"current": 0})
duplicate_keys = frappe.db.sql('''
SELECT name, max(current) as current
from