refactor: cleaner handling of length and precision
This commit is contained in:
parent
c230e86e2c
commit
9bd8dc8900
1 changed files with 11 additions and 8 deletions
|
|
@ -10,6 +10,10 @@ SPECIAL_CHAR_PATTERN = re.compile(r"[\W]", flags=re.UNICODE)
|
|||
|
||||
VARCHAR_CAST_PATTERN = re.compile(r"varchar\(([\d]+)\)")
|
||||
|
||||
CONFIGURABLE_DECIMAL_TYPES = ("Currency", "Float", "Percent")
|
||||
DEFAULT_DECIMAL_LENGTH = 21
|
||||
DEFAULT_DECIMAL_PRECISION = 9
|
||||
|
||||
|
||||
class InvalidColumnName(frappe.ValidationError):
|
||||
pass
|
||||
|
|
@ -429,10 +433,13 @@ def get_definition(fieldtype, precision=None, length=None, *, options=None):
|
|||
size = d[1] if d[1] else None
|
||||
|
||||
if size:
|
||||
# This check needs to exist for backward compatibility.
|
||||
# Till V13, default size used for float, currency and percent are (18, 6).
|
||||
if fieldtype in ["Float", "Currency", "Percent"] and cint(precision) > 6:
|
||||
size = f"21,{cint(precision)}"
|
||||
if fieldtype in CONFIGURABLE_DECIMAL_TYPES:
|
||||
width = length if length else DEFAULT_DECIMAL_LENGTH
|
||||
precision_is_set = precision not in (None, "")
|
||||
precision = precision if precision_is_set else DEFAULT_DECIMAL_PRECISION
|
||||
if cint(precision) > cint(width):
|
||||
precision = width
|
||||
size = f"{cint(width)},{cint(precision)}"
|
||||
|
||||
if length:
|
||||
if coltype == "varchar":
|
||||
|
|
@ -442,10 +449,6 @@ def get_definition(fieldtype, precision=None, length=None, *, options=None):
|
|||
# NOTE: this will only be applicable for mariadb as frappe implements int
|
||||
# in postgres as bigint (as seen in type_map)
|
||||
size = length
|
||||
elif coltype == "decimal":
|
||||
max_possible_precision = min(9, length)
|
||||
precision = max_possible_precision if precision in (None, "") else cint(precision)
|
||||
size = f"{length},{precision}"
|
||||
|
||||
if size is not None:
|
||||
coltype = f"{coltype}({size})"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue