refactor!: Better Integer handling

- Remove length: it makes no difference, it's for ZEROFILL only.
- Switch to tinyint for checkboxes in mysql and smallint on postgres.
- Use `int` instead of `bigint` by default.
This commit is contained in:
Ankush Menat 2024-04-06 10:45:50 +05:30
parent b493bfe7c2
commit eef9c2c8cc
5 changed files with 97 additions and 97 deletions

View file

@ -161,11 +161,11 @@ class MariaDBDatabase(MariaDBConnectionUtil, MariaDBExceptionUtil, Database):
self.db_type = "mariadb"
self.type_map = {
"Currency": ("decimal", "21,9"),
"Int": ("int", "11"),
"Int": ("int", None),
"Long Int": ("bigint", "20"),
"Float": ("decimal", "21,9"),
"Percent": ("decimal", "21,9"),
"Check": ("int", "1"),
"Check": ("tinyint", None),
"Small Text": ("text", ""),
"Long Text": ("longtext", ""),
"Code": ("longtext", ""),
@ -288,7 +288,7 @@ class MariaDBDatabase(MariaDBConnectionUtil, MariaDBExceptionUtil, Database):
`name` VARCHAR(255) NOT NULL,
`fieldname` VARCHAR(140) NOT NULL,
`password` TEXT NOT NULL,
`encrypted` INT(1) NOT NULL DEFAULT 0,
`encrypted` TINYINT NOT NULL DEFAULT 0,
PRIMARY KEY (`doctype`, `name`, `fieldname`)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci"""
)
@ -303,7 +303,7 @@ class MariaDBDatabase(MariaDBConnectionUtil, MariaDBExceptionUtil, Database):
content text,
fulltext(content),
route varchar({self.VARCHAR_LEN}),
published int(1) not null default 0,
published TINYINT not null default 0,
unique `doctype_name` (doctype, name))
COLLATE=utf8mb4_unicode_ci
ENGINE=MyISAM

View file

@ -13,61 +13,61 @@ CREATE TABLE `tabDocField` (
`modified` datetime(6) DEFAULT NULL,
`modified_by` varchar(255) DEFAULT NULL,
`owner` varchar(255) DEFAULT NULL,
`docstatus` int(1) NOT NULL DEFAULT 0,
`docstatus` tinyint NOT NULL DEFAULT 0,
`parent` varchar(255) DEFAULT NULL,
`parentfield` varchar(255) DEFAULT NULL,
`parenttype` varchar(255) DEFAULT NULL,
`idx` int(8) NOT NULL DEFAULT 0,
`idx` int NOT NULL DEFAULT 0,
`fieldname` varchar(255) DEFAULT NULL,
`label` varchar(255) DEFAULT NULL,
`oldfieldname` varchar(255) DEFAULT NULL,
`fieldtype` varchar(255) DEFAULT NULL,
`oldfieldtype` varchar(255) DEFAULT NULL,
`options` text,
`search_index` int(1) NOT NULL DEFAULT 0,
`show_dashboard` int(1) NOT NULL DEFAULT 0,
`hidden` int(1) NOT NULL DEFAULT 0,
`set_only_once` int(1) NOT NULL DEFAULT 0,
`allow_in_quick_entry` 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,
`bold` int(1) NOT NULL DEFAULT 0,
`in_global_search` int(1) NOT NULL DEFAULT 0,
`collapsible` 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,
`show_preview_popup` int(1) NOT NULL DEFAULT 0,
`search_index` tinyint NOT NULL DEFAULT 0,
`show_dashboard` tinyint NOT NULL DEFAULT 0,
`hidden` tinyint NOT NULL DEFAULT 0,
`set_only_once` tinyint NOT NULL DEFAULT 0,
`allow_in_quick_entry` tinyint NOT NULL DEFAULT 0,
`print_hide` tinyint NOT NULL DEFAULT 0,
`report_hide` tinyint NOT NULL DEFAULT 0,
`reqd` tinyint NOT NULL DEFAULT 0,
`bold` tinyint NOT NULL DEFAULT 0,
`in_global_search` tinyint NOT NULL DEFAULT 0,
`collapsible` tinyint NOT NULL DEFAULT 0,
`unique` tinyint NOT NULL DEFAULT 0,
`no_copy` tinyint NOT NULL DEFAULT 0,
`allow_on_submit` tinyint NOT NULL DEFAULT 0,
`show_preview_popup` tinyint NOT NULL DEFAULT 0,
`trigger` varchar(255) DEFAULT NULL,
`collapsible_depends_on` text,
`mandatory_depends_on` text,
`read_only_depends_on` text,
`depends_on` text,
`permlevel` int(11) NOT NULL DEFAULT 0,
`ignore_user_permissions` int(1) NOT NULL DEFAULT 0,
`permlevel` int NOT NULL DEFAULT 0,
`ignore_user_permissions` tinyint NOT NULL DEFAULT 0,
`width` varchar(255) DEFAULT NULL,
`print_width` varchar(255) DEFAULT NULL,
`columns` int(11) NOT NULL DEFAULT 0,
`columns` int NOT NULL DEFAULT 0,
`default` text,
`description` text,
`in_list_view` int(1) NOT NULL DEFAULT 0,
`fetch_if_empty` int(1) NOT NULL DEFAULT 0,
`in_filter` int(1) NOT NULL DEFAULT 0,
`remember_last_selected_value` int(1) NOT NULL DEFAULT 0,
`ignore_xss_filter` int(1) NOT NULL DEFAULT 0,
`print_hide_if_no_value` int(1) NOT NULL DEFAULT 0,
`allow_bulk_edit` int(1) NOT NULL DEFAULT 0,
`in_standard_filter` int(1) NOT NULL DEFAULT 0,
`in_preview` int(1) NOT NULL DEFAULT 0,
`read_only` int(1) NOT NULL DEFAULT 0,
`in_list_view` tinyint NOT NULL DEFAULT 0,
`fetch_if_empty` tinyint NOT NULL DEFAULT 0,
`in_filter` tinyint NOT NULL DEFAULT 0,
`remember_last_selected_value` tinyint NOT NULL DEFAULT 0,
`ignore_xss_filter` tinyint NOT NULL DEFAULT 0,
`print_hide_if_no_value` tinyint NOT NULL DEFAULT 0,
`allow_bulk_edit` tinyint NOT NULL DEFAULT 0,
`in_standard_filter` tinyint NOT NULL DEFAULT 0,
`in_preview` tinyint NOT NULL DEFAULT 0,
`read_only` tinyint NOT NULL DEFAULT 0,
`precision` varchar(255) DEFAULT NULL,
`max_height` varchar(10) DEFAULT NULL,
`length` int(11) NOT NULL DEFAULT 0,
`translatable` int(1) NOT NULL DEFAULT 0,
`hide_border` int(1) NOT NULL DEFAULT 0,
`hide_days` int(1) NOT NULL DEFAULT 0,
`hide_seconds` int(1) NOT NULL DEFAULT 0,
`length` int NOT NULL DEFAULT 0,
`translatable` tinyint NOT NULL DEFAULT 0,
`hide_border` tinyint NOT NULL DEFAULT 0,
`hide_days` tinyint NOT NULL DEFAULT 0,
`hide_seconds` tinyint NOT NULL DEFAULT 0,
PRIMARY KEY (`name`),
KEY `parent` (`parent`),
KEY `label` (`label`),
@ -87,27 +87,27 @@ CREATE TABLE `tabDocPerm` (
`modified` datetime(6) DEFAULT NULL,
`modified_by` varchar(255) DEFAULT NULL,
`owner` varchar(255) DEFAULT NULL,
`docstatus` int(1) NOT NULL DEFAULT 0,
`docstatus` tinyint NOT NULL DEFAULT 0,
`parent` varchar(255) DEFAULT NULL,
`parentfield` varchar(255) DEFAULT NULL,
`parenttype` varchar(255) DEFAULT NULL,
`idx` int(8) NOT NULL DEFAULT 0,
`permlevel` int(11) DEFAULT '0',
`idx` int NOT NULL DEFAULT 0,
`permlevel` int DEFAULT '0',
`role` varchar(255) DEFAULT NULL,
`match` varchar(255) 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,
`read` tinyint NOT NULL DEFAULT 1,
`write` tinyint NOT NULL DEFAULT 1,
`create` tinyint NOT NULL DEFAULT 1,
`submit` tinyint NOT NULL DEFAULT 0,
`cancel` tinyint NOT NULL DEFAULT 0,
`delete` tinyint NOT NULL DEFAULT 1,
`amend` tinyint NOT NULL DEFAULT 0,
`report` tinyint NOT NULL DEFAULT 1,
`export` tinyint NOT NULL DEFAULT 1,
`import` tinyint NOT NULL DEFAULT 0,
`share` tinyint NOT NULL DEFAULT 1,
`print` tinyint NOT NULL DEFAULT 1,
`email` tinyint NOT NULL DEFAULT 1,
PRIMARY KEY (`name`),
KEY `parent` (`parent`)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
@ -123,11 +123,11 @@ CREATE TABLE `tabDocType Action` (
`modified` datetime(6) DEFAULT NULL,
`modified_by` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`owner` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`docstatus` int(1) NOT NULL DEFAULT 0,
`docstatus` tinyint NOT NULL DEFAULT 0,
`parent` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`parentfield` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`parenttype` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`idx` int(8) NOT NULL DEFAULT 0,
`idx` int NOT NULL DEFAULT 0,
`label` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`group` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`action_type` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
@ -147,11 +147,11 @@ CREATE TABLE `tabDocType Link` (
`modified` datetime(6) DEFAULT NULL,
`modified_by` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`owner` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`docstatus` int(1) NOT NULL DEFAULT 0,
`docstatus` tinyint NOT NULL DEFAULT 0,
`parent` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`parentfield` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`parenttype` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`idx` int(8) NOT NULL DEFAULT 0,
`idx` int NOT NULL DEFAULT 0,
`group` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`link_doctype` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`link_fieldname` varchar(140) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
@ -170,15 +170,15 @@ CREATE TABLE `tabDocType` (
`modified` datetime(6) DEFAULT NULL,
`modified_by` varchar(255) DEFAULT NULL,
`owner` varchar(255) DEFAULT NULL,
`docstatus` int(1) NOT NULL DEFAULT 0,
`idx` int(8) NOT NULL DEFAULT 0,
`docstatus` tinyint NOT NULL DEFAULT 0,
`idx` int NOT NULL DEFAULT 0,
`search_fields` varchar(255) DEFAULT NULL,
`issingle` int(1) NOT NULL DEFAULT 0,
`is_virtual` int(1) NOT NULL DEFAULT 0,
`is_tree` int(1) NOT NULL DEFAULT 0,
`istable` int(1) NOT NULL DEFAULT 0,
`editable_grid` int(1) NOT NULL DEFAULT 1,
`track_changes` int(1) NOT NULL DEFAULT 0,
`issingle` tinyint NOT NULL DEFAULT 0,
`is_virtual` tinyint NOT NULL DEFAULT 0,
`is_tree` tinyint NOT NULL DEFAULT 0,
`istable` tinyint NOT NULL DEFAULT 0,
`editable_grid` tinyint NOT NULL DEFAULT 1,
`track_changes` tinyint NOT NULL DEFAULT 0,
`module` varchar(255) DEFAULT NULL,
`restrict_to_domain` varchar(255) DEFAULT NULL,
`app` varchar(255) DEFAULT NULL,
@ -191,17 +191,17 @@ CREATE TABLE `tabDocType` (
`sort_order` varchar(255) DEFAULT NULL,
`description` text,
`colour` varchar(255) DEFAULT NULL,
`read_only` int(1) NOT NULL DEFAULT 0,
`in_create` int(1) NOT NULL DEFAULT 0,
`menu_index` int(11) DEFAULT NULL,
`read_only` tinyint NOT NULL DEFAULT 0,
`in_create` tinyint NOT NULL DEFAULT 0,
`menu_index` int DEFAULT NULL,
`parent_node` varchar(255) DEFAULT NULL,
`smallicon` varchar(255) 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,
`track_seen` int(1) NOT NULL DEFAULT 0,
`max_attachments` int(11) NOT NULL DEFAULT 0,
`allow_copy` tinyint NOT NULL DEFAULT 0,
`allow_rename` tinyint NOT NULL DEFAULT 0,
`allow_import` tinyint NOT NULL DEFAULT 0,
`hide_toolbar` tinyint NOT NULL DEFAULT 0,
`track_seen` tinyint NOT NULL DEFAULT 0,
`max_attachments` int NOT NULL DEFAULT 0,
`print_outline` varchar(255) DEFAULT NULL,
`document_type` varchar(255) DEFAULT NULL,
`icon` varchar(255) DEFAULT NULL,
@ -211,22 +211,22 @@ CREATE TABLE `tabDocType` (
`_last_update` varchar(32) DEFAULT NULL,
`engine` varchar(20) DEFAULT 'InnoDB',
`default_print_format` varchar(255) DEFAULT NULL,
`is_submittable` int(1) NOT NULL DEFAULT 0,
`show_name_in_global_search` int(1) NOT NULL DEFAULT 0,
`is_submittable` tinyint NOT NULL DEFAULT 0,
`show_name_in_global_search` tinyint NOT NULL DEFAULT 0,
`_user_tags` varchar(255) DEFAULT NULL,
`custom` int(1) NOT NULL DEFAULT 0,
`beta` int(1) NOT NULL DEFAULT 0,
`has_web_view` int(1) NOT NULL DEFAULT 0,
`allow_guest_to_view` int(1) NOT NULL DEFAULT 0,
`custom` tinyint NOT NULL DEFAULT 0,
`beta` tinyint NOT NULL DEFAULT 0,
`has_web_view` tinyint NOT NULL DEFAULT 0,
`allow_guest_to_view` tinyint NOT NULL DEFAULT 0,
`route` varchar(255) DEFAULT NULL,
`is_published_field` varchar(255) DEFAULT NULL,
`website_search_field` varchar(255) DEFAULT NULL,
`email_append_to` int(1) NOT NULL DEFAULT 0,
`email_append_to` tinyint NOT NULL DEFAULT 0,
`subject_field` varchar(255) DEFAULT NULL,
`sender_field` varchar(255) DEFAULT NULL,
`show_title_field_in_link` int(1) NOT NULL DEFAULT 0,
`show_title_field_in_link` tinyint NOT NULL DEFAULT 0,
`migration_hash` varchar(255) DEFAULT NULL,
`translated_doctype` int(1) NOT NULL DEFAULT 0,
`translated_doctype` tinyint NOT NULL DEFAULT 0,
PRIMARY KEY (`name`)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
@ -237,7 +237,7 @@ CREATE TABLE `tabDocType` (
DROP TABLE IF EXISTS `tabSeries`;
CREATE TABLE `tabSeries` (
`name` varchar(100),
`current` int(10) NOT NULL DEFAULT 0,
`current` int NOT NULL DEFAULT 0,
PRIMARY KEY(`name`)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
@ -280,7 +280,7 @@ CREATE TABLE `__Auth` (
`name` VARCHAR(255) NOT NULL,
`fieldname` VARCHAR(140) NOT NULL,
`password` TEXT NOT NULL,
`encrypted` INT(1) NOT NULL DEFAULT 0,
`encrypted` tinyint NOT NULL DEFAULT 0,
PRIMARY KEY (`doctype`, `name`, `fieldname`)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
@ -295,16 +295,16 @@ CREATE TABLE `tabFile` (
`modified` datetime(6) DEFAULT NULL,
`modified_by` varchar(255) DEFAULT NULL,
`owner` varchar(255) DEFAULT NULL,
`docstatus` int(1) NOT NULL DEFAULT 0,
`docstatus` tinyint NOT NULL DEFAULT 0,
`parent` varchar(255) DEFAULT NULL,
`parentfield` varchar(255) DEFAULT NULL,
`parenttype` varchar(255) DEFAULT NULL,
`idx` int(8) NOT NULL DEFAULT 0,
`idx` int NOT NULL DEFAULT 0,
`file_name` varchar(255) DEFAULT NULL,
`file_url` varchar(255) DEFAULT NULL,
`module` varchar(255) DEFAULT NULL,
`attached_to_name` varchar(255) DEFAULT NULL,
`file_size` int(11) NOT NULL DEFAULT 0,
`file_size` int NOT NULL DEFAULT 0,
`attached_to_doctype` varchar(255) DEFAULT NULL,
PRIMARY KEY (`name`),
KEY `parent` (`parent`),
@ -323,11 +323,11 @@ CREATE TABLE `tabDefaultValue` (
`modified` datetime(6) DEFAULT NULL,
`modified_by` varchar(255) DEFAULT NULL,
`owner` varchar(255) DEFAULT NULL,
`docstatus` int(1) NOT NULL DEFAULT 0,
`docstatus` tinyint NOT NULL DEFAULT 0,
`parent` varchar(255) DEFAULT NULL,
`parentfield` varchar(255) DEFAULT NULL,
`parenttype` varchar(255) DEFAULT NULL,
`idx` int(8) NOT NULL DEFAULT 0,
`idx` int NOT NULL DEFAULT 0,
`defvalue` text,
`defkey` varchar(255) DEFAULT NULL,
PRIMARY KEY (`name`),

View file

@ -59,8 +59,8 @@ class MariaDBTable(DBTable):
modified datetime(6),
modified_by varchar({varchar_len}),
owner varchar({varchar_len}),
docstatus int(1) not null default '0',
idx int(8) not null default '0',
docstatus tinyint not null default '0',
idx int not null default '0',
{additional_definitions})
ENGINE={engine}
ROW_FORMAT=DYNAMIC

View file

@ -125,7 +125,7 @@ class PostgresDatabase(PostgresExceptionUtil, Database):
self.db_type = "postgres"
self.type_map = {
"Currency": ("decimal", "21,9"),
"Int": ("bigint", None),
"Int": ("int", None),
"Long Int": ("bigint", None),
"Float": ("decimal", "21,9"),
"Percent": ("decimal", "21,9"),

View file

@ -35,7 +35,7 @@ class TestDBUpdate(FrappeTestCase):
)
default = field_def.default if field_def.default is not None else fallback_default
self.assertEqual(fieldtype, table_column.type)
self.assertIn(fieldtype, table_column.type, msg=f"Types not matching for {fieldname}")
self.assertIn(cstr(table_column.default) or "NULL", [cstr(default), f"'{default}'"])
def test_index_and_unique_constraints(self):
@ -167,7 +167,7 @@ class TestDBUpdate(FrappeTestCase):
def get_fieldtype_from_def(field_def):
fieldtuple = frappe.db.type_map.get(field_def.fieldtype, ("", 0))
fieldtype = fieldtuple[0]
if fieldtype in ("varchar", "datetime", "int"):
if fieldtype in ("varchar", "datetime"):
fieldtype += f"({field_def.length or fieldtuple[1]})"
return fieldtype