Skip to content

Commit

Permalink
refactor: clear records in batches in 'Transaction Deletion Record' (#…
Browse files Browse the repository at this point in the history
…34109)

refactor: clear records in batches
  • Loading branch information
ruthra-kumar authored Feb 21, 2023
1 parent c88444a commit 4a7b1de
Showing 1 changed file with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@


import frappe
from frappe import _
from frappe import _, qb
from frappe.desk.notifications import clear_notifications
from frappe.model.document import Document
from frappe.utils import cint
from frappe.utils import cint, create_batch


class TransactionDeletionRecord(Document):
def __init__(self, *args, **kwargs):
super(TransactionDeletionRecord, self).__init__(*args, **kwargs)
self.batch_size = 5000

def validate(self):
frappe.only_for("System Manager")
self.validate_doctypes_to_be_ignored()
Expand Down Expand Up @@ -155,8 +159,9 @@ def delete_child_tables(self, doctype, company_fieldname):
"DocField", filters={"fieldtype": "Table", "parent": doctype}, pluck="options"
)

for table in child_tables:
frappe.db.delete(table, {"parent": ["in", parent_docs_to_be_deleted]})
for batch in create_batch(parent_docs_to_be_deleted, self.batch_size):
for table in child_tables:
frappe.db.delete(table, {"parent": ["in", batch]})

def delete_docs_linked_with_specified_company(self, doctype, company_fieldname):
frappe.db.delete(doctype, {company_fieldname: self.company})
Expand All @@ -181,13 +186,16 @@ def update_naming_series(self, naming_series, doctype_name):
frappe.db.sql("""update `tabSeries` set current = %s where name=%s""", (last, prefix))

def delete_version_log(self, doctype, company_fieldname):
frappe.db.sql(
"""delete from `tabVersion` where ref_doctype=%s and docname in
(select name from `tab{0}` where `{1}`=%s)""".format(
doctype, company_fieldname
),
(doctype, self.company),
)
dt = qb.DocType(doctype)
names = qb.from_(dt).select(dt.name).where(dt[company_fieldname] == self.company).run(as_list=1)
names = [x[0] for x in names]

if names:
versions = qb.DocType("Version")
for batch in create_batch(names, self.batch_size):
qb.from_(versions).delete().where(
(versions.ref_doctype == doctype) & (versions.docname.isin(batch))
).run()

def delete_communications(self, doctype, company_fieldname):
reference_docs = frappe.get_all(doctype, filters={company_fieldname: self.company})
Expand All @@ -199,7 +207,8 @@ def delete_communications(self, doctype, company_fieldname):
)
communication_names = [c.name for c in communications]

frappe.delete_doc("Communication", communication_names, ignore_permissions=True)
for batch in create_batch(communication_names, self.batch_size):
frappe.delete_doc("Communication", batch, ignore_permissions=True)


@frappe.whitelist()
Expand Down

0 comments on commit 4a7b1de

Please sign in to comment.