refactor(postgres): port quality_procedure on_trash to the query builder

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mihir Kandoi
2026-06-19 21:21:08 +05:30
parent 1cfae33fb0
commit c18ca7af22
2 changed files with 36 additions and 5 deletions

View File

@@ -49,11 +49,8 @@ class QualityProcedure(NestedSet):
def on_trash(self):
# clear from child table (sub procedures)
frappe.db.sql(
"""update `tabQuality Procedure Process`
set `procedure`='' where `procedure`=%s""",
self.name,
)
qpp = frappe.qb.DocType("Quality Procedure Process")
frappe.qb.update(qpp).set(qpp["procedure"], "").where(qpp["procedure"] == self.name).run()
NestedSet.on_trash(self, allow_root_deletion=True)
def check_for_incorrect_child(self):

View File

@@ -65,6 +65,40 @@ class TestQualityProcedure(ERPNextTestSuite):
child_qp.reload()
self.assertEqual(child_qp.parent_quality_procedure, None)
def test_on_trash_clears_referencing_process(self):
# Build a parent group with a sub-procedure. The parent's child table gets a
# `Quality Procedure Process` row whose `procedure` field points at the child.
child_qp = create_procedure(
{
"quality_procedure_name": "Test Child On Trash",
"is_group": 0,
}
)
create_procedure(
{
"quality_procedure_name": "Test Group On Trash",
"is_group": 1,
"processes": [dict(procedure=child_qp.name)],
}
)
# Sanity: a process row in the parent references the child by name.
referencing_rows = frappe.get_all(
"Quality Procedure Process",
filters={"procedure": child_qp.name},
pluck="name",
)
self.assertTrue(referencing_rows)
# Deleting the child runs on_trash() -> the converted UPDATE clears `procedure`.
child_qp.delete()
for row_name in referencing_rows:
self.assertEqual(
frappe.db.get_value("Quality Procedure Process", row_name, "procedure"),
"",
)
def remove_child_from_old_parent(self):
child_qp = create_procedure(
{