Change Check fields to have default as 0 (if not specified) and not null

This commit is contained in:
Anand Doshi 2015-07-29 14:52:10 +05:30
parent a0c75a3439
commit baec4f855f
8 changed files with 100 additions and 58 deletions

View file

@ -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")

View file

@ -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,

View file

@ -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

View file

@ -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"):

View file

@ -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('"', '\"') + '"'

View file

@ -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

View file

View file

@ -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"])))