From f66b23b96d4b2a52179c95771f02f3e04c6e2565 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 28 Mar 2024 11:14:05 +0530 Subject: [PATCH] fix: handle nested event calls Treat disable_transaction_control as a stack incr/decr when moving in and out of context. --- frappe/database/database.py | 14 +++++++++----- frappe/model/document.py | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/frappe/database/database.py b/frappe/database/database.py index 03f4cacff4..3ce3ccfbbc 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -7,6 +7,7 @@ import random import re import string import traceback +import warnings from collections.abc import Iterable, Sequence from contextlib import contextmanager, suppress from time import time @@ -47,6 +48,9 @@ MULTI_WORD_PATTERN = re.compile(r'([`"])(tab([A-Z]\w+)( [A-Z]\w+)+)\1') SQL_ITERATOR_BATCH_SIZE = 100 +TRANSACTION_DISABLED_MSG = "Commit/rollback are disabled during certain events. This command will be ignored." + + class Database: """ Open a database connection with the given parmeters, if use_default is True, use the @@ -97,7 +101,7 @@ class Database: # Setting this to true will disable full rollback and commit # You can still use savepoint with partial rollback. - self.disable_transaction_control = False + self._disable_transaction_control = 0 # self.db_type: str # self.last_query (lazy) attribute of last sql query executed @@ -1032,8 +1036,8 @@ class Database: def commit(self): """Commit current transaction. Calls SQL `COMMIT`.""" - if self.disable_transaction_control: - self.logger.error("Commit issued during disabled transaction state ignored") + if self._disable_transaction_control: + warnings.warn(message=TRANSACTION_DISABLED_MSG, stacklevel=2) return self.before_rollback.reset() @@ -1050,7 +1054,7 @@ class Database: """`ROLLBACK` current transaction. Optionally rollback to a known save_point.""" if save_point: self.sql(f"rollback to savepoint {save_point}") - elif not self.disable_transaction_control: + elif not self._disable_transaction_control: self.before_commit.reset() self.after_commit.reset() @@ -1061,7 +1065,7 @@ class Database: self.after_rollback.run() else: - self.logger.error("Rollback issued during disabled transaction state ignored") + warnings.warn(message=TRANSACTION_DISABLED_MSG, stacklevel=2) def savepoint(self, save_point): """Savepoints work as a nested transaction. diff --git a/frappe/model/document.py b/frappe/model/document.py index c9ae34a354..602749f5f6 100644 --- a/frappe/model/document.py +++ b/frappe/model/document.py @@ -1301,10 +1301,10 @@ class Document(BaseDocument): add_to_return_value(self, fn(self, *args, **kwargs)) for f in hooks: try: - frappe.db.disable_transaction_control = True + frappe.db._disable_transaction_control += 1 add_to_return_value(self, f(self, method, *args, **kwargs)) finally: - frappe.db.disable_transaction_control = False + frappe.db._disable_transaction_control -= 1 return self.__dict__.pop("_return_value", None)