perf: commit GL reposting periodically

If you have a huge list of docs to repost then maintaining transaction
throughtout entire GL reposting is not only unnecessary but also creates
performance issues. Periodically commiting the changes prevents lost
progress and reduces memory usage.
This commit is contained in:
Ankush Menat 2022-06-04 18:19:44 +05:30 committed by Ankush Menat
parent 67c26325ee
commit eb53a9727d

View File

@ -1145,7 +1145,7 @@ def repost_gle_for_stock_vouchers(
precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit")) or 2
gle = get_voucherwise_gl_entries(stock_vouchers, posting_date)
for voucher_type, voucher_no in stock_vouchers:
for idx, (voucher_type, voucher_no) in enumerate(stock_vouchers):
existing_gle = gle.get((voucher_type, voucher_no), [])
voucher_obj = frappe.get_doc(voucher_type, voucher_no)
# Some transactions post credit as negative debit, this is handled while posting GLE
@ -1160,6 +1160,11 @@ def repost_gle_for_stock_vouchers(
else:
_delete_gl_entries(voucher_type, voucher_no)
if idx % 20 == 0:
# Commit every 20 documents to avoid losing progress
# and reducing memory usage
frappe.db.commit()
def sort_stock_vouchers_by_posting_date(
stock_vouchers: List[Tuple[str, str]]