Skip to content

Commit eef26fe

Browse files
ili-adMatt Howardruthra-kumar
authored
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 <[email protected]> Co-authored-by: ruthra kumar <[email protected]>
1 parent 043d208 commit eef26fe

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

erpnext/accounts/doctype/repost_accounting_ledger/repost_accounting_ledger.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,22 @@ def start_repost(account_repost_doc=str) -> None:
215215

216216

217217
def get_allowed_types_from_settings(child_doc: bool = False):
218-
repost_docs = [
219-
x.document_type
220-
for x in frappe.db.get_all(
221-
"Repost Allowed Types", filters={"allowed": True}, fields=["distinct(document_type)"]
222-
)
223-
]
218+
# Avoid DISTINCT(...) here: Frappe applies a default ORDER BY which breaks on Postgres
219+
# when used with SELECT DISTINCT.
220+
repost_docs = frappe.db.get_all(
221+
"Repost Allowed Types",
222+
filters={"allowed": True},
223+
pluck="document_type",
224+
)
225+
226+
# De-dupe while preserving order (first occurrence wins)
227+
repost_docs = list(dict.fromkeys(repost_docs))
224228
result = repost_docs
225229

226230
if repost_docs and child_doc:
227231
result.extend(get_child_docs(repost_docs))
232+
# Keep uniqueness after extending
233+
result = list(dict.fromkeys(result))
228234

229235
return result
230236

@@ -291,8 +297,11 @@ def get_repost_allowed_types(doctype, txt, searchfield, start, page_len, filters
291297
if txt:
292298
filters.update({"document_type": ("like", f"%{txt}%")})
293299

294-
if allowed_types := frappe.db.get_all(
295-
"Repost Allowed Types", filters=filters, fields=["distinct(document_type)"], as_list=1
296-
):
297-
return allowed_types
298-
return []
300+
allowed_types = frappe.db.get_all(
301+
"Repost Allowed Types",
302+
filters=filters,
303+
pluck="document_type",
304+
)
305+
306+
allowed_types = list(dict.fromkeys(allowed_types))
307+
return [[dt] for dt in allowed_types]

erpnext/patches/v15_0/update_pick_list_fields.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@ def execute():
88

99

1010
def update_delivery_note():
11-
DN = frappe.qb.DocType("Delivery Note")
12-
DNI = frappe.qb.DocType("Delivery Note Item")
13-
14-
frappe.qb.update(DNI).join(DN).on(DN.name == DNI.parent).set(DNI.against_pick_list, DN.pick_list).where(
15-
IfNull(DN.pick_list, "") != ""
16-
).run()
11+
# Postgres doesn't support UPDATE ... JOIN. Use UPDATE ... FROM instead.
12+
frappe.db.multisql(
13+
{
14+
"mariadb": """
15+
UPDATE `tabDelivery Note Item` dni
16+
JOIN `tabDelivery Note` dn ON dn.`name` = dni.`parent`
17+
SET dni.`against_pick_list` = dn.`pick_list`
18+
WHERE COALESCE(dn.`pick_list`, '') <> ''
19+
""",
20+
"postgres": """
21+
UPDATE "tabDelivery Note Item" dni
22+
SET against_pick_list = dn.pick_list
23+
FROM "tabDelivery Note" dn
24+
WHERE dn.name = dni.parent
25+
AND COALESCE(dn.pick_list, '') <> ''
26+
""",
27+
}
28+
)
1729

1830

1931
def update_pick_list_items():

0 commit comments

Comments
 (0)