From 55392b64e7bbadcacee52959fd2b5d4d9614ef02 Mon Sep 17 00:00:00 2001 From: Conor Date: Wed, 29 Dec 2021 17:10:06 -0600 Subject: [PATCH] fix: Revert changes made to fix collation differences Due to collation differences in MariaDB and Postgres, tests gave inconsistent results. This was to be handled in tests alone instead of the application. It was unnecessary. Collation changes should be made at DBMS config level only. Accomodating for those in the application will unnecessarily degrade performance for everyone. Other changes: * use pluck in user_type * revert ordering in nestedset * revert parsing in order_field * use preferred APIs & styling Co-authored-by: gavin --- frappe/core/doctype/user_type/user_type.py | 21 ++++++---------- frappe/desk/doctype/dashboard/dashboard.py | 29 ++++++++++++++-------- frappe/model/db_query.py | 7 ++++-- frappe/utils/nestedset.py | 13 ++++------ 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/frappe/core/doctype/user_type/user_type.py b/frappe/core/doctype/user_type/user_type.py index 5775d499bd..661ac932e7 100644 --- a/frappe/core/doctype/user_type/user_type.py +++ b/frappe/core/doctype/user_type/user_type.py @@ -36,20 +36,15 @@ class UserType(Document): if not self.user_doctypes: return - DocType = frappe.qb.DocType("DocType") + modules = frappe.get_all("DocType", + filters={"name": ("in", [d.document_type for d in self.user_doctypes])}, + distinct=True, + pluck="module", + ) - document_types = [d.document_type for d in self.user_doctypes] or [''] - modules = (frappe.qb.from_(DocType) - .select(DocType.module) - .where(DocType.name.isin(document_types)) - .groupby(DocType.module) - .distinct()).run() - - self.set('user_type_modules', []) - for row in modules: - self.append('user_type_modules', { - 'module': row[0] - }) + self.set("user_type_modules", []) + for module in modules: + self.append("user_type_modules", {"module": module}) def validate_document_type_limit(self): limit = frappe.conf.get('user_type_doctype_limit', {}).get(frappe.scrub(self.name)) diff --git a/frappe/desk/doctype/dashboard/dashboard.py b/frappe/desk/doctype/dashboard/dashboard.py index 55044cda4b..ac62796dc2 100644 --- a/frappe/desk/doctype/dashboard/dashboard.py +++ b/frappe/desk/doctype/dashboard/dashboard.py @@ -1,24 +1,33 @@ -# -*- coding: utf-8 -*- -# Copyright (c) 2019, Frappe Technologies and contributors +# Copyright (c) 2022, Frappe Technologies and contributors # License: MIT. See LICENSE -from frappe.model.document import Document -from frappe.modules.export_file import export_to_files -from frappe.config import get_modules_from_all_apps_for_user +import json + import frappe from frappe import _ -import json +from frappe.config import get_modules_from_all_apps_for_user +from frappe.model.document import Document +from frappe.modules.export_file import export_to_files +from frappe.query_builder import DocType + class Dashboard(Document): def on_update(self): if self.is_default: # make all other dashboards non-default - DashBoard = frappe.qb.DocType("Dashboard") - - frappe.qb.update(DashBoard).set(DashBoard.is_default, 0).where(DashBoard.name != self.name).run() + DashBoard = DocType("Dashboard") + + frappe.qb.update(DashBoard).set( + DashBoard.is_default, 0 + ).where( + DashBoard.name != self.name + ).run() if frappe.conf.developer_mode and self.is_standard: - export_to_files(record_list=[['Dashboard', self.name, self.module + ' Dashboard']], record_module=self.module) + export_to_files( + record_list=[["Dashboard", self.name, f"{self.module} Dashboard"]], + record_module=self.module + ) def validate(self): if not frappe.conf.developer_mode and self.is_standard: diff --git a/frappe/model/db_query.py b/frappe/model/db_query.py index d296c24362..1bb42f9c97 100644 --- a/frappe/model/db_query.py +++ b/frappe/model/db_query.py @@ -213,8 +213,11 @@ class DatabaseQuery(object): order_field = order_field.replace(r, "") if order_field not in args.fields: - order_fieldm = order_field.replace("`", '"') - args.fields += f", MAX({order_fieldm}) as `{order_fieldm}`" + order_fieldm = order_field.replace("`", "") + if "." in order_fieldm: + args.fields += ", MAX(" + order_fieldm.split(".")[1] + ") as `" + order_fieldm + "`" + else: + args.fields += ", MAX(" + order_fieldm + ") as `" + order_fieldm + "`" args.order_by = args.order_by.replace(order_field, "`" + order_fieldm + "`") return args diff --git a/frappe/utils/nestedset.py b/frappe/utils/nestedset.py index 1fc2d7779a..34322092dc 100644 --- a/frappe/utils/nestedset.py +++ b/frappe/utils/nestedset.py @@ -1,4 +1,4 @@ -# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: MIT. See LICENSE # Tree (Hierarchical) Nested Set Model (nsm) @@ -16,9 +16,6 @@ import frappe from frappe import _ from frappe.model.document import Document from frappe.query_builder import DocType, Order -from pypika import CustomFunction - -Replace = CustomFunction('REPLACE', ['input', 'substr_to_replace', 'substr_to_replace_with']) class NestedSetRecursionError(frappe.ValidationError): pass class NestedSetMultipleRootsError(frappe.ValidationError): pass @@ -91,7 +88,7 @@ def update_move_node(doc, parent_field): if parent: new_parent = frappe.db.sql("""select lft, rgt from `tab{0}` - where name = %s for update""".format(doc.doctype), parent, as_dict=1)[0] + where name = %s for update""".format(doc.doctype), parent, as_dict=1)[0] validate_loop(doc.doctype, doc.name, new_parent.lft, new_parent.rgt) @@ -110,7 +107,7 @@ def update_move_node(doc, parent_field): if parent: new_parent = frappe.db.sql("""select lft, rgt from `tab%s` - where name = %s for update""" % (doc.doctype, '%s'), parent, as_dict=1)[0] + where name = %s for update""" % (doc.doctype, '%s'), parent, as_dict=1)[0] # set parent lft, rgt frappe.db.sql("""update `tab{0}` set rgt = rgt + %s @@ -156,7 +153,7 @@ def rebuild_tree(doctype, parent_field): .where( (column == "") | (column.isnull()) ) - .orderby(Replace(table.name, '_', ''), order=Order.asc) + .orderby(table.name, order=Order.asc) .select(table.name) ).run() @@ -179,7 +176,7 @@ def rebuild_node(doctype, parent, left, parent_field): column = getattr(table, parent_field) result = ( - frappe.qb.from_(table).where(column == parent).select(table.name).orderby(Replace(table.name, '_', ''), order=Order.asc) + frappe.qb.from_(table).where(column == parent).select(table.name) ).run() for r in result: