Bug: PGSQL ERPNext financial_statements.py

Hi,

I found a bug with pgsql V18 !

Target : erpnext/accounts/report/financial_statements.py L553

Problem: QueryBuilder .force_index() generates FORCE INDEX SQL which is

MySQL/MariaDB only — PostgreSQL raises SyntaxError at “INDEX”.

Fix : Guard the call with frappe.db.db_type != “postgres”.

MariaDB behaviour is preserved; PostgreSQL silently skips it.

import sys

import os

# ---------------------------------------------------------------------------

# Patch: fix ERPNext financial_statements.py force_index() → no-op on PG

# ---------------------------------------------------------------------------

# Target : erpnext/accounts/report/financial_statements.py L553

# Problem: QueryBuilder .force_index() generates FORCE INDEX SQL which is

# MySQL/MariaDB only — PostgreSQL raises SyntaxError at “INDEX”.

# Fix : Guard the call with frappe.db.db_type != “postgres”.

# MariaDB behaviour is preserved; PostgreSQL silently skips it.

# ---------------------------------------------------------------------------

_TARGET = “erpnext/accounts/report/financial_statements.py”

# From repr() read: L553 uses \t\t indentation (double tab, inside if block)

_OLD = ‘\t\tquery = query.force_index(“posting_date_company_index”)\n’

# Replacement: PostgreSQL-safe guard

_NEW = (

'\\t\\t# PATCH: force_index() is MySQL/MariaDB only — skip on PostgreSQL\\n'

'\\t\\tif frappe.db.db_type != "postgres":\\n'

'\\t\\t\\tquery = query.force_index("posting_date_company_index")\\n'

)

def apply(bench_path: str) → None:

"""Apply the patch to financial_statements.py."""

erpnext_root = os.path.join(bench_path, "apps", "erpnext")

fpath = os.path.join(erpnext_root, \_TARGET)



try:

    with open(fpath, "r", encoding="utf-8") as fh:

        content = fh.read()

except FileNotFoundError:

    print(f"\[ERROR\] File not found: {fpath}", file=sys.stderr)

    sys.exit(1)



\# Guard: already patched

if 'db_type != "postgres"' in content and "force_index" in content:

    print(f"\[SKIP\] {\_TARGET} — already patched.")

    return



\# Guard: pattern must exist

if \_OLD not in content:

    print(

        f"\[ERROR\] Pattern not found in {\_TARGET}.\\n"

        "        File may have changed upstream.",

        file=sys.stderr,

    )

    sys.exit(1)



patched = content.replace(\_OLD, \_NEW, 1)



with open(fpath, "w", encoding="utf-8") as fh:

    fh.write(patched)



print(f"\[OK\] Patched: {fpath}")

print("     force_index() now guarded: skip on PostgreSQL.")

if _name_ == “_main_”:

bench_path = (

    sys.argv\[1\] if len(sys.argv) > 1 else "/home/frappe/frappe-bench"

)

apply(bench_path)