|
|
@@ -1,12 +1,39 @@ |
|
|
|
import frappe |
|
|
|
from frappe.query_builder import DocType |
|
|
|
|
|
|
|
|
|
|
|
def execute(): |
|
|
|
rating_fields = frappe.get_all("DocField", fields=["parent", "fieldname"], filters={"fieldtype": "Rating"}) |
|
|
|
RATING_FIELD_TYPE = "decimal(3,2)" |
|
|
|
rating_fields = frappe.get_all( |
|
|
|
"DocField", fields=["parent", "fieldname"], filters={"fieldtype": "Rating"} |
|
|
|
) |
|
|
|
|
|
|
|
custom_rating_fields = frappe.get_all( |
|
|
|
"Custom Field", fields=["dt", "fieldname"], filters={"fieldtype": "Rating"} |
|
|
|
) |
|
|
|
|
|
|
|
for _field in rating_fields + custom_rating_fields: |
|
|
|
doctype_name = _field.get("parent") or _field.get("dt") |
|
|
|
doctype = DocType(doctype_name) |
|
|
|
field = _field.fieldname |
|
|
|
|
|
|
|
# TODO: Add postgres support (for the check) |
|
|
|
if ( |
|
|
|
frappe.conf.db_type == "mariadb" |
|
|
|
and frappe.db.get_column_type(doctype_name, field) == RATING_FIELD_TYPE |
|
|
|
): |
|
|
|
continue |
|
|
|
|
|
|
|
# commit any changes so far for upcoming DDL |
|
|
|
frappe.db.commit() |
|
|
|
|
|
|
|
# alter column types for rating fieldtype |
|
|
|
frappe.db.change_column_type(doctype_name, column=field, type=RATING_FIELD_TYPE, nullable=True) |
|
|
|
|
|
|
|
custom_rating_fields = frappe.get_all("Custom Field", fields=["dt", "fieldname"], filters={"fieldtype": "Rating"}) |
|
|
|
# update data: int => decimal |
|
|
|
frappe.qb.update(doctype).set( |
|
|
|
doctype[field], doctype[field] / 5 |
|
|
|
).run() |
|
|
|
|
|
|
|
for field in rating_fields + custom_rating_fields: |
|
|
|
doctype_name = field.get("parent") or field.get("dt") |
|
|
|
doctype = frappe.qb.DocType(doctype_name) |
|
|
|
field = field.fieldname |
|
|
|
(frappe.qb.update(doctype_name).set(doctype[field], doctype[field]/5)).run() |
|
|
|
# commit to flush updated rows |
|
|
|
frappe.db.commit() |