mirror of
https://github.com/frappe/erpnext.git
synced 2026-08-20 09:49:58 +00:00
fix: only rewrite commission_rate rows the column change can't cast
The previous string comparison (str(raw) != str(cleaned)) rewrote every
whole-number row ('20' vs '20.0'), turning a targeted cleanup into a
full-table rewrite on Sales Team. Skip rows already holding a plain numeric
string and only fix NULL / empty / non-numeric / percent-sign values.
This commit is contained in:
@@ -14,13 +14,30 @@ def execute():
|
||||
if not frappe.db.has_column(doctype, "commission_rate"):
|
||||
continue
|
||||
|
||||
# Percent maps to a NOT NULL decimal column, so empty/NULL/non-numeric text must become 0
|
||||
# as well, otherwise the column type change fails under strict SQL mode.
|
||||
# Only rewrite the rows the column change can't cast as-is. Plain numeric strings (the vast
|
||||
# majority, e.g. "20") are left untouched so this stays a targeted cleanup instead of a
|
||||
# full-table rewrite; NULL / empty / non-numeric / percent-sign values become a clean number,
|
||||
# otherwise the Data -> Percent change fails under strict SQL mode (Percent is NOT NULL decimal).
|
||||
rows = frappe.db.get_all(doctype, fields=["name", "commission_rate"])
|
||||
for row in rows:
|
||||
if _is_plain_number(row.commission_rate):
|
||||
continue
|
||||
cleaned = flt(_strip_percent_sign(row.commission_rate))
|
||||
if str(row.commission_rate) != str(cleaned):
|
||||
frappe.db.set_value(doctype, row.name, "commission_rate", cleaned, update_modified=False)
|
||||
frappe.db.set_value(doctype, row.name, "commission_rate", cleaned, update_modified=False)
|
||||
|
||||
|
||||
def _is_plain_number(value) -> bool:
|
||||
"""True if the stored value is already a clean numeric string the column change can cast."""
|
||||
if value is None:
|
||||
return False
|
||||
text = str(value)
|
||||
if text != text.strip() or "%" in text:
|
||||
return False
|
||||
try:
|
||||
float(text)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _strip_percent_sign(value):
|
||||
|
||||
Reference in New Issue
Block a user