瀏覽代碼

perf: ~65% faster `create_custom_fields` (#17875)

Co-authored-by: Sagar Vora <sagar@resilient.tech>
version-14
mergify[bot] 2 年之前
committed by GitHub
父節點
當前提交
c27e6bf299
沒有發現已知的金鑰在資料庫的簽署中 GPG Key ID: 4AEE18F83AFDEB23
共有 1 個文件被更改,包括 46 次插入38 次删除
  1. +46
    -38
      frappe/custom/doctype/custom_field/custom_field.py

+ 46
- 38
frappe/custom/doctype/custom_field/custom_field.py 查看文件

@@ -80,17 +80,15 @@ class CustomField(Document):
check_fieldname_conflicts(self)

def on_update(self):
if not frappe.flags.in_setup_wizard:
frappe.clear_cache(doctype=self.dt)

# validate field
if not self.flags.ignore_validate:
# validate field
from frappe.core.doctype.doctype.doctype import validate_fields_for_doctype

validate_fields_for_doctype(self.dt)

# update the schema
if not frappe.db.get_value("DocType", self.dt, "issingle") and not frappe.flags.in_setup_wizard:
# clear cache and update the schema
if not frappe.flags.in_create_custom_fields:
frappe.clear_cache(doctype=self.dt)
frappe.db.updatedb(self.dt)

def on_trash(self):
@@ -169,38 +167,48 @@ def create_custom_fields(custom_fields, ignore_validate=False, update=True):

:param custom_fields: example `{'Sales Invoice': [dict(fieldname='test')]}`"""

if not ignore_validate and frappe.flags.in_setup_wizard:
ignore_validate = True

for doctypes, fields in custom_fields.items():
if isinstance(fields, dict):
# only one field
fields = [fields]

if isinstance(doctypes, str):
# only one doctype
doctypes = (doctypes,)

for doctype in doctypes:
for df in fields:
field = frappe.db.get_value("Custom Field", {"dt": doctype, "fieldname": df["fieldname"]})
if not field:
try:
df = df.copy()
df["owner"] = "Administrator"
create_custom_field(doctype, df, ignore_validate=ignore_validate)

except frappe.exceptions.DuplicateEntryError:
pass

elif update:
custom_field = frappe.get_doc("Custom Field", field)
custom_field.flags.ignore_validate = ignore_validate
custom_field.update(df)
custom_field.save()

frappe.clear_cache(doctype=doctype)
frappe.db.updatedb(doctype)
try:
frappe.flags.in_create_custom_fields = True
doctypes_to_update = set()

if frappe.flags.in_setup_wizard:
ignore_validate = True

for doctypes, fields in custom_fields.items():
if isinstance(fields, dict):
# only one field
fields = [fields]

if isinstance(doctypes, str):
# only one doctype
doctypes = (doctypes,)

for doctype in doctypes:
doctypes_to_update.add(doctype)

for df in fields:
field = frappe.db.get_value("Custom Field", {"dt": doctype, "fieldname": df["fieldname"]})
if not field:
try:
df = df.copy()
df["owner"] = "Administrator"
create_custom_field(doctype, df, ignore_validate=ignore_validate)

except frappe.exceptions.DuplicateEntryError:
pass

elif update:
custom_field = frappe.get_doc("Custom Field", field)
custom_field.flags.ignore_validate = ignore_validate
custom_field.update(df)
custom_field.save()

for doctype in doctypes_to_update:
frappe.clear_cache(doctype=doctype)
frappe.db.updatedb(doctype)

finally:
frappe.flags.in_create_custom_fields = False


@frappe.whitelist()


Loading…
取消
儲存