fix(postgres): fix v15 migration failures on Postgres (#51481)

* fix(postgres): avoid DISTINCT(...) in repost allowed types query

* fix(postgres): rewrite update pick list patch to avoid UPDATE JOIN

* chore: linting changes

---------

Co-authored-by: Matt Howard <github.severity519@passmail.net>
Co-authored-by: ruthra kumar <ruthra@erpnext.com>
This commit is contained in:
ili.ad
2026-01-16 06:05:16 -05:00
committed by GitHub
parent 043d208580
commit eef26fea9a
2 changed files with 38 additions and 17 deletions

View File

@@ -215,16 +215,22 @@ def start_repost(account_repost_doc=str) -> None:
def get_allowed_types_from_settings(child_doc: bool = False): def get_allowed_types_from_settings(child_doc: bool = False):
repost_docs = [ # Avoid DISTINCT(...) here: Frappe applies a default ORDER BY which breaks on Postgres
x.document_type # when used with SELECT DISTINCT.
for x in frappe.db.get_all( repost_docs = frappe.db.get_all(
"Repost Allowed Types", filters={"allowed": True}, fields=["distinct(document_type)"] "Repost Allowed Types",
) filters={"allowed": True},
] pluck="document_type",
)
# De-dupe while preserving order (first occurrence wins)
repost_docs = list(dict.fromkeys(repost_docs))
result = repost_docs result = repost_docs
if repost_docs and child_doc: if repost_docs and child_doc:
result.extend(get_child_docs(repost_docs)) result.extend(get_child_docs(repost_docs))
# Keep uniqueness after extending
result = list(dict.fromkeys(result))
return result return result
@@ -291,8 +297,11 @@ def get_repost_allowed_types(doctype, txt, searchfield, start, page_len, filters
if txt: if txt:
filters.update({"document_type": ("like", f"%{txt}%")}) filters.update({"document_type": ("like", f"%{txt}%")})
if allowed_types := frappe.db.get_all( allowed_types = frappe.db.get_all(
"Repost Allowed Types", filters=filters, fields=["distinct(document_type)"], as_list=1 "Repost Allowed Types",
): filters=filters,
return allowed_types pluck="document_type",
return [] )
allowed_types = list(dict.fromkeys(allowed_types))
return [[dt] for dt in allowed_types]

View File

@@ -8,12 +8,24 @@ def execute():
def update_delivery_note(): def update_delivery_note():
DN = frappe.qb.DocType("Delivery Note") # Postgres doesn't support UPDATE ... JOIN. Use UPDATE ... FROM instead.
DNI = frappe.qb.DocType("Delivery Note Item") frappe.db.multisql(
{
frappe.qb.update(DNI).join(DN).on(DN.name == DNI.parent).set(DNI.against_pick_list, DN.pick_list).where( "mariadb": """
IfNull(DN.pick_list, "") != "" UPDATE `tabDelivery Note Item` dni
).run() JOIN `tabDelivery Note` dn ON dn.`name` = dni.`parent`
SET dni.`against_pick_list` = dn.`pick_list`
WHERE COALESCE(dn.`pick_list`, '') <> ''
""",
"postgres": """
UPDATE "tabDelivery Note Item" dni
SET against_pick_list = dn.pick_list
FROM "tabDelivery Note" dn
WHERE dn.name = dni.parent
AND COALESCE(dn.pick_list, '') <> ''
""",
}
)
def update_pick_list_items(): def update_pick_list_items():