From baec4f855f97358bc82fe66ac438b9ac81d869fa Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 29 Jul 2015 14:52:10 +0530 Subject: [PATCH] Change Check fields to have default as 0 (if not specified) and not null --- .../customize_form/test_customize_form.py | 4 +- frappe/data/Framework.sql | 96 +++++++++---------- frappe/database.py | 4 +- frappe/model/base_document.py | 8 ++ frappe/model/db_schema.py | 11 ++- frappe/patches.txt | 1 + frappe/patches/v5_2/__init__.py | 0 .../patches/v5_2/change_checks_to_not_null.py | 34 +++++++ 8 files changed, 100 insertions(+), 58 deletions(-) create mode 100644 frappe/patches/v5_2/__init__.py create mode 100644 frappe/patches/v5_2/change_checks_to_not_null.py diff --git a/frappe/custom/doctype/customize_form/test_customize_form.py b/frappe/custom/doctype/customize_form/test_customize_form.py index 59f25c98a6..28bd3684a4 100644 --- a/frappe/custom/doctype/customize_form/test_customize_form.py +++ b/frappe/custom/doctype/customize_form/test_customize_form.py @@ -109,7 +109,7 @@ class TestCustomizeForm(unittest.TestCase): location_field.reqd = 0 d.run_method("save_customization") self.assertEquals(frappe.db.get_value("Property Setter", - {"doc_type": "User", "property": "reqd", "field_name": "location"}, "value"), '0') + {"doc_type": "User", "property": "reqd", "field_name": "location"}, "value"), None) def test_save_customization_custom_field_property(self): d = self.get_customize_form("User") @@ -156,7 +156,7 @@ class TestCustomizeForm(unittest.TestCase): d.doc_type = "User" d.run_method('reset_to_defaults') - self.assertEquals(d.get("fields", {"fieldname": "location"})[0].in_list_view, None) + self.assertEquals(d.get("fields", {"fieldname": "location"})[0].in_list_view, 0) frappe.local.test_objects["Property Setter"] = [] make_test_records_for_doctype("Property Setter") diff --git a/frappe/data/Framework.sql b/frappe/data/Framework.sql index ce502541fa..4a1cd8bddb 100644 --- a/frappe/data/Framework.sql +++ b/frappe/data/Framework.sql @@ -13,7 +13,7 @@ CREATE TABLE `tabDocField` ( `modified` datetime(6) DEFAULT NULL, `modified_by` varchar(255) DEFAULT NULL, `owner` varchar(255) DEFAULT NULL, - `docstatus` int(1) DEFAULT '0', + `docstatus` int(1) NOT NULL DEFAULT 0, `parent` varchar(255) DEFAULT NULL, `parentfield` varchar(255) DEFAULT NULL, `parenttype` varchar(255) DEFAULT NULL, @@ -24,27 +24,26 @@ CREATE TABLE `tabDocField` ( `fieldtype` varchar(255) DEFAULT NULL, `oldfieldtype` varchar(255) DEFAULT NULL, `options` text, - `search_index` int(1) DEFAULT NULL, - `hidden` int(1) DEFAULT NULL, - `set_only_once` int(1) DEFAULT NULL, - `print_hide` int(1) DEFAULT NULL, - `report_hide` int(1) DEFAULT NULL, - `reqd` int(1) DEFAULT NULL, - `unique` int(1) DEFAULT NULL, - `no_copy` int(1) DEFAULT NULL, - `allow_on_submit` int(1) DEFAULT NULL, + `search_index` int(1) NOT NULL DEFAULT 0, + `hidden` int(1) NOT NULL DEFAULT 0, + `set_only_once` int(1) NOT NULL DEFAULT 0, + `print_hide` int(1) NOT NULL DEFAULT 0, + `report_hide` int(1) NOT NULL DEFAULT 0, + `reqd` int(1) NOT NULL DEFAULT 0, + `unique` int(1) NOT NULL DEFAULT 0, + `no_copy` int(1) NOT NULL DEFAULT 0, + `allow_on_submit` int(1) NOT NULL DEFAULT 0, `trigger` varchar(255) DEFAULT NULL, `depends_on` varchar(255) DEFAULT NULL, `permlevel` int(11) DEFAULT '0', - `ignore_user_permissions` int(1) DEFAULT NULL, + `ignore_user_permissions` int(1) NOT NULL DEFAULT 0, `width` varchar(255) DEFAULT NULL, `print_width` varchar(255) DEFAULT NULL, `default` text, `description` text, - `in_filter` int(1) DEFAULT NULL, - `in_list_view` int(1) DEFAULT NULL, - `no_column` int(1) DEFAULT NULL, - `read_only` int(1) DEFAULT NULL, + `in_filter` int(1) NOT NULL DEFAULT 0, + `in_list_view` int(1) NOT NULL DEFAULT 0, + `read_only` int(1) NOT NULL DEFAULT 0, `precision` varchar(255) DEFAULT NULL, PRIMARY KEY (`name`), KEY `parent` (`parent`), @@ -65,7 +64,7 @@ CREATE TABLE `tabDocPerm` ( `modified` datetime(6) DEFAULT NULL, `modified_by` varchar(255) DEFAULT NULL, `owner` varchar(255) DEFAULT NULL, - `docstatus` int(1) DEFAULT '0', + `docstatus` int(1) DEFAULT 0, `parent` varchar(255) DEFAULT NULL, `parentfield` varchar(255) DEFAULT NULL, `parenttype` varchar(255) DEFAULT NULL, @@ -73,20 +72,19 @@ CREATE TABLE `tabDocPerm` ( `permlevel` int(11) DEFAULT '0', `role` varchar(255) DEFAULT NULL, `match` varchar(255) DEFAULT NULL, - `read` int(1) DEFAULT '1' NULL, - `write` int(1) DEFAULT '1' NULL, - `create` int(1) DEFAULT '1' NULL, - `submit` int(1) DEFAULT NULL, - `cancel` int(1) DEFAULT NULL, - `delete` int(1) DEFAULT '1' NULL, - `amend` int(1) DEFAULT NULL, - `report` int(1) DEFAULT '1' NULL, - `export` int(1) DEFAULT '1' NULL, - `import` int(1) DEFAULT NULL, - `share` int(1) DEFAULT '1' NULL, - `print` int(1) DEFAULT '1' NULL, - `email` int(1) DEFAULT '1' NULL, - `restrict` int(1) DEFAULT NULL, + `read` int(1) NOT NULL DEFAULT 1, + `write` int(1) NOT NULL DEFAULT 1, + `create` int(1) NOT NULL DEFAULT 1, + `submit` int(1) NOT NULL DEFAULT 0, + `cancel` int(1) NOT NULL DEFAULT 0, + `delete` int(1) NOT NULL DEFAULT 1, + `amend` int(1) NOT NULL DEFAULT 0, + `report` int(1) NOT NULL DEFAULT 1, + `export` int(1) NOT NULL DEFAULT 1, + `import` int(1) NOT NULL DEFAULT 0, + `share` int(1) NOT NULL DEFAULT 1, + `print` int(1) NOT NULL DEFAULT 1, + `email` int(1) NOT NULL DEFAULT 1, PRIMARY KEY (`name`), KEY `parent` (`parent`) ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; @@ -102,14 +100,14 @@ CREATE TABLE `tabDocType` ( `modified` datetime(6) DEFAULT NULL, `modified_by` varchar(255) DEFAULT NULL, `owner` varchar(255) DEFAULT NULL, - `docstatus` int(1) DEFAULT '0', + `docstatus` int(1) DEFAULT 0, `parent` varchar(255) DEFAULT NULL, `parentfield` varchar(255) DEFAULT NULL, `parenttype` varchar(255) DEFAULT NULL, `idx` int(8) DEFAULT NULL, `search_fields` varchar(255) DEFAULT NULL, - `issingle` int(1) DEFAULT NULL, - `istable` int(1) DEFAULT NULL, + `issingle` int(1) NOT NULL DEFAULT 0, + `istable` int(1) NOT NULL DEFAULT 0, `version` int(11) DEFAULT NULL, `module` varchar(255) DEFAULT NULL, `plugin` varchar(255) DEFAULT NULL, @@ -120,35 +118,29 @@ CREATE TABLE `tabDocType` ( `sort_order` varchar(255) DEFAULT NULL, `description` text, `colour` varchar(255) DEFAULT NULL, - `read_only` int(1) DEFAULT NULL, - `in_create` int(1) DEFAULT NULL, - `show_in_menu` int(1) DEFAULT NULL, + `read_only` int(1) NOT NULL DEFAULT 0, + `in_create` int(1) NOT NULL DEFAULT 0, `menu_index` int(11) DEFAULT NULL, `parent_node` varchar(255) DEFAULT NULL, `smallicon` varchar(255) DEFAULT NULL, - `allow_print` int(1) DEFAULT NULL, - `allow_email` int(1) DEFAULT NULL, - `allow_copy` int(1) DEFAULT NULL, - `allow_rename` int(1) DEFAULT NULL, - `allow_import` int(1) DEFAULT NULL, - `hide_toolbar` int(1) DEFAULT NULL, - `hide_heading` int(1) DEFAULT NULL, - `use_template` int(1) DEFAULT NULL, + `allow_copy` int(1) NOT NULL DEFAULT 0, + `allow_rename` int(1) NOT NULL DEFAULT 0, + `allow_import` int(1) NOT NULL DEFAULT 0, + `hide_toolbar` int(1) NOT NULL DEFAULT 0, + `hide_heading` int(1) NOT NULL DEFAULT 0, `max_attachments` int(11) DEFAULT NULL, `print_outline` varchar(255) DEFAULT NULL, - `is_transaction_doc` int(1) DEFAULT NULL, - `read_only_onload` int(1) DEFAULT NULL, - `allow_trash` int(1) DEFAULT NULL, - `in_dialog` int(1) DEFAULT NULL, + `read_only_onload` int(1) NOT NULL DEFAULT 0, + `in_dialog` int(1) NOT NULL DEFAULT 0, `document_type` varchar(255) DEFAULT NULL, `icon` varchar(255) DEFAULT NULL, `tag_fields` varchar(255) DEFAULT NULL, `subject` varchar(255) DEFAULT NULL, `_last_update` varchar(32) DEFAULT NULL, `default_print_format` varchar(255) DEFAULT NULL, - `is_submittable` int(1) DEFAULT NULL, + `is_submittable` int(1) NOT NULL DEFAULT 0, `_user_tags` varchar(255) DEFAULT NULL, - `custom` int(1) DEFAULT NULL, + `custom` int(1) NOT NULL DEFAULT 0, PRIMARY KEY (`name`), KEY `parent` (`parent`) ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; @@ -215,7 +207,7 @@ CREATE TABLE `tabFile Data` ( `modified` datetime(6) DEFAULT NULL, `modified_by` varchar(255) DEFAULT NULL, `owner` varchar(255) DEFAULT NULL, - `docstatus` int(1) DEFAULT '0', + `docstatus` int(1) DEFAULT 0, `parent` varchar(255) DEFAULT NULL, `parentfield` varchar(255) DEFAULT NULL, `parenttype` varchar(255) DEFAULT NULL, @@ -243,7 +235,7 @@ CREATE TABLE `tabDefaultValue` ( `modified` datetime(6) DEFAULT NULL, `modified_by` varchar(255) DEFAULT NULL, `owner` varchar(255) DEFAULT NULL, - `docstatus` int(1) DEFAULT '0', + `docstatus` int(1) DEFAULT 0, `parent` varchar(255) DEFAULT NULL, `parentfield` varchar(255) DEFAULT NULL, `parenttype` varchar(255) DEFAULT NULL, diff --git a/frappe/database.py b/frappe/database.py index 6fd3d5712d..d7a04ee5e7 100644 --- a/frappe/database.py +++ b/frappe/database.py @@ -191,11 +191,11 @@ class Database: """ return [r[0] for r in self.sql(query, values, debug=debug)] - def sql_ddl(self, query, values=()): + def sql_ddl(self, query, values=(), debug=False): """Commit and execute a query. DDL (Data Definition Language) queries that alter schema autocommit in MariaDB.""" self.commit() - self.sql(query) + self.sql(query, debug=debug) def check_transaction_status(self, query): """Raises exception if more than 20,000 `INSERT`, `UPDATE` queries are diff --git a/frappe/model/base_document.py b/frappe/model/base_document.py index 8fddfe3a20..729196ed22 100644 --- a/frappe/model/base_document.py +++ b/frappe/model/base_document.py @@ -174,6 +174,14 @@ class BaseDocument(object): for fieldname in self.meta.get_valid_columns(): d[fieldname] = self.get(fieldname) + df = self.meta.get_field(fieldname) + if df: + if df.fieldtype=="Check" and not isinstance(d[fieldname], int): + d[fieldname] = cint(d[fieldname]) + + elif df.fieldtype in ("Datetime", "Date") and d[fieldname]=="": + d[fieldname] = None + if d[fieldname]=="": df = self.meta.get_field(fieldname) if df and df.fieldtype in ("Datetime", "Date"): diff --git a/frappe/model/db_schema.py b/frappe/model/db_schema.py index 196420e472..e511ebd0fd 100644 --- a/frappe/model/db_schema.py +++ b/frappe/model/db_schema.py @@ -226,7 +226,10 @@ class DbTable: if col.fieldname=="name": continue - if not col.default: + if col.fieldtype=="Check": + col_default = cint(col.default) + + elif not col.default: col_default = "null" else: col_default = '"{}"'.format(col.default.replace('"', '\\"')) @@ -262,7 +265,11 @@ class DbColumn: if not column_def: return column_def - if self.default and (self.default not in default_shortcuts) \ + if self.fieldtype=="Check": + default_value = cint(self.default) or 0 + column_def += ' not null default {0}'.format(default_value) + + elif self.default and (self.default not in default_shortcuts) \ and not self.default.startswith(":") and column_def not in ('text', 'longtext'): column_def += ' default "' + self.default.replace('"', '\"') + '"' diff --git a/frappe/patches.txt b/frappe/patches.txt index 535f0c8feb..785e7c5559 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -83,3 +83,4 @@ frappe.patches.v5_0.modify_session frappe.patches.v5_0.expire_old_scheduler_logs execute:frappe.permissions.reset_perms("DocType") execute:frappe.db.sql("delete from `tabProperty Setter` where `property` = 'idx'") +frappe.patches.v5_2.change_checks_to_not_null diff --git a/frappe/patches/v5_2/__init__.py b/frappe/patches/v5_2/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/frappe/patches/v5_2/change_checks_to_not_null.py b/frappe/patches/v5_2/change_checks_to_not_null.py new file mode 100644 index 0000000000..23f5d659b5 --- /dev/null +++ b/frappe/patches/v5_2/change_checks_to_not_null.py @@ -0,0 +1,34 @@ +from __future__ import unicode_literals +import frappe +from frappe.utils import cint +from frappe.model import default_fields + +def execute(): + for table in frappe.db.get_tables(): + doctype = table[3:] + if frappe.db.exists("DocType", doctype): + fieldnames = [df["fieldname"] for df in + frappe.get_all("DocField", fields=["fieldname"], filters={"parent": doctype})] + custom_fieldnames = [df["fieldname"] for df in + frappe.get_all("Custom Field", fields=["fieldname"], filters={"dt": doctype})] + + else: + fieldnames = custom_fieldnames = [] + + for column in frappe.db.sql("""desc `{0}`""".format(table), as_dict=True): + if column["Type"]=="int(1)": + fieldname = column["Field"] + + # only change for defined fields, ignore old fields that don't exist in meta + if not (fieldname in default_fields or fieldname in fieldnames or fieldname in custom_fieldnames): + continue + + # set 0 + frappe.db.sql("""update `{table}` set `{column}`=0 where `{column}` is null"""\ + .format(table=table, column=fieldname)) + frappe.db.commit() + + # change definition + frappe.db.sql_ddl("""alter table `{table}` + modify `{column}` int(1) not null default {default}"""\ + .format(table=table, column=fieldname, default=cint(column["Default"])))