fix: Add for update in nest set model updates

This commit is contained in:
Deepesh Garg 2021-07-18 17:46:16 +05:30
parent c58a633335
commit 33f8bfcaf2

View file

@ -57,13 +57,14 @@ def update_add_node(doc, parent, parent_field):
# get the last sibling of the parent
if parent:
left, right = frappe.db.sql("select lft, rgt from `tab{0}` where name=%s"
left, right = frappe.db.sql("select lft, rgt from `tab{0}` where name=%s for update"
.format(doctype), parent)[0]
validate_loop(doc.doctype, doc.name, left, right)
else: # root
right = frappe.db.sql("""
SELECT COALESCE(MAX(rgt), 0) + 1 FROM `tab{0}`
WHERE COALESCE(`{1}`, '') = ''
FOR UPDATE
""".format(doctype, parent_field))[0][0]
right = right or 1
@ -89,7 +90,7 @@ def update_move_node(doc, parent_field):
if parent:
new_parent = frappe.db.sql("""select lft, rgt from `tab{0}`
where name = %s""".format(doc.doctype), parent, as_dict=1)[0]
where name = %s for update""".format(doc.doctype), parent, as_dict=1)[0]
validate_loop(doc.doctype, doc.name, new_parent.lft, new_parent.rgt)
@ -108,7 +109,7 @@ def update_move_node(doc, parent_field):
if parent:
new_parent = frappe.db.sql("""select lft, rgt from `tab%s`
where name = %s""" % (doc.doctype, '%s'), parent, as_dict=1)[0]
where name = %s for update""" % (doc.doctype, '%s'), parent, as_dict=1)[0]
# set parent lft, rgt
@ -128,7 +129,7 @@ def update_move_node(doc, parent_field):
new_diff = new_parent.rgt - doc.lft
else:
# new root
max_rgt = frappe.db.sql("""select max(rgt) from `tab{0}`""".format(doc.doctype))[0][0]
max_rgt = frappe.db.sql("""select max(rgt) from `tab{0}` for update""".format(doc.doctype))[0][0]
new_diff = max_rgt + 1 - doc.lft
# bring back from dark side