Skip to content

Commit

Permalink
fix: retry count per doc instead of global (#26159)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush committed Apr 25, 2024
1 parent 8ff2516 commit 1e4c182
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions frappe/model/base_document.py
Expand Up @@ -569,8 +569,8 @@ def db_insert(self, ignore_if_duplicate=False):
if frappe.db.is_primary_key_violation(e):
if self.meta.autoname == "hash":
# hash collision? try again
frappe.flags.retry_count = (frappe.flags.retry_count or 0) + 1
if frappe.flags.retry_count > 5 and not frappe.flags.in_test:
self.flags.retry_count = (self.flags.retry_count or 0) + 1
if self.flags.retry_count > 5:
raise
self.name = None
self.db_insert()
Expand Down
11 changes: 11 additions & 0 deletions frappe/tests/test_naming.py
Expand Up @@ -20,6 +20,8 @@
parse_naming_series,
revert_series_if_last,
)
from frappe.query_builder.utils import db_type_is
from frappe.tests.test_query_builder import run_only_if
from frappe.tests.utils import FrappeTestCase, patch_hooks
from frappe.utils import now_datetime, nowdate, nowtime

Expand Down Expand Up @@ -379,6 +381,15 @@ def test_naming_with_empty_field(self):
name = parse_naming_series(series, doc=webhook)
self.assertTrue(name.startswith("KOOH---"), f"incorrect name generated {name}")

@run_only_if(db_type_is.MARIADB)
def test_hash_collision(self):
doctype = new_doctype(autoname="hash").insert().name
name = frappe.generate_hash()
for _ in range(10):
frappe.flags.in_import = True
frappe.new_doc(doctype).update({"name": name}).insert()
frappe.flags.pop("in_import", None)

def test_custom_parser(self):
# check naming with custom parser
todo = frappe.new_doc("ToDo")
Expand Down

0 comments on commit 1e4c182

Please sign in to comment.