diff --git a/frappe/patches.txt b/frappe/patches.txt index 0407f6946e..377b46889b 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -18,7 +18,7 @@ execute:frappe.db.sql("delete from `tabDocField` where parent='0'") frappe.patches.v4_0.change_varchar_length frappe.patches.v6_4.reduce_varchar_length frappe.patches.v5_2.change_checks_to_not_null -frappe.patches.v6_9.int_float_not_null +frappe.patches.v6_9.int_float_not_null #2015-11-25 frappe.patches.v5_0.v4_to_v5 frappe.patches.v5_0.remove_shopping_cart_app diff --git a/frappe/patches/v6_9/int_float_not_null.py b/frappe/patches/v6_9/int_float_not_null.py index c37142189e..97495f9077 100644 --- a/frappe/patches/v6_9/int_float_not_null.py +++ b/frappe/patches/v6_9/int_float_not_null.py @@ -1,9 +1,11 @@ from __future__ import unicode_literals import frappe +from frappe.utils import cint, flt def execute(): for doctype in frappe.get_all("DocType", filters={"issingle": 0}): doctype = doctype.name + meta = frappe.get_meta(doctype) for column in frappe.db.sql("desc `tab{doctype}`".format(doctype=doctype), as_dict=True): fieldname = column["Field"] @@ -14,3 +16,15 @@ def execute(): frappe.db.sql("""update `tab{doctype}` set `{fieldname}`=0 where `{fieldname}` is null"""\ .format(doctype=doctype, fieldname=fieldname)) + + # alter table + if column["Null"]=='YES': + if not meta.get_field(fieldname): + continue + + default = cint(column["Default"]) if column_type.startswith("int") else flt(column["Default"]) + frappe.db.sql_ddl("""alter table `tab{doctype}` + change `{fieldname}` `{fieldname}` {column_type} not null default '{default}'""".format( + doctype=doctype, fieldname=fieldname, column_type=column_type, default=default)) + +