refactor: clear records in batches in 'Transaction Deletion Record' (#34109)
refactor: clear records in batches
This commit is contained in:
parent
c88444a6c4
commit
4a7b1de2d8
@ -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()
|
||||
@ -155,8 +159,9 @@ class TransactionDeletionRecord(Document):
|
||||
"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})
|
||||
@ -181,13 +186,16 @@ class TransactionDeletionRecord(Document):
|
||||
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})
|
||||
@ -199,7 +207,8 @@ class TransactionDeletionRecord(Document):
|
||||
)
|
||||
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user