Patch Fails: erpnext.patches.v12_0.remove_denied_leaves_from_leave_ledger

We did bench update and we get error messages.
It seems we have a bad record.
the user have a leave application record and then the user is disabled
before the update action.

Version: 12
OS Centos7

logs
yarn install v1.17.3
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.96s.
Patching sites...
Migrating erpnext.net-swift.com
Executing execute:frappe.reload_doc('core', 'doctype', 'communication_link') #2019-10-02 in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.109s
Executing execute:frappe.reload_doc('core', 'doctype', 'communication') #2019-10-02 in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 1.724s
Executing execute:frappe.reload_doc('desk', 'doctype', 'notification_log') in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.469s
Executing frappe.patches.v12_0.set_default_incoming_email_port in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 2.429s
Executing frappe.patches.v12_0.update_global_search in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 7.482s
Executing execute:frappe.reload_doc('desk', 'doctype', 'notification_settings') in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.473s
Executing frappe.patches.v12_0.setup_tags in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 4.348s
Executing frappe.patches.v12_0.update_auto_repeat_status_and_not_submittable in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.143s
Executing frappe.patches.v12_0.copy_to_parent_for_tags in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.055s
Executing erpnext.patches.v12_0.rename_bank_account_field_in_journal_entry_account in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 1.389s
Executing erpnext.patches.v12_0.create_default_energy_point_rules in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 1.047s
Executing erpnext.patches.v12_0.set_produced_qty_field_in_sales_order_for_work_order in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 6.387s
Executing erpnext.patches.v12_0.set_default_shopify_app_type in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 1.071s
Executing erpnext.patches.v12_0.set_cwip_and_delete_asset_settings in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 2.863s
Executing erpnext.patches.v12_0.set_expense_account_in_landed_cost_voucher_taxes in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.72s
Executing erpnext.patches.v12_0.replace_accounting_with_accounts_in_home_settings in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.042s
Executing erpnext.patches.v12_0.set_payment_entry_status in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.967s
Executing erpnext.patches.v12_0.update_owner_fields_in_acc_dimension_custom_fields in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.016s
Executing erpnext.patches.v12_0.set_default_for_add_taxes_from_item_tax_template in erpnext.net-swift.com (_c6d2cec0a2672282)
Success: Done in 0.127s
Executing erpnext.patches.v12_0.remove_denied_leaves_from_leave_ledger in erpnext.net-swift.com (_c6d2cec0a2672282)
Syntax error in query:
 Delete
                FROM `tabLeave Ledger Entry`
                WHERE
                        transaction_type = 'Leave Application'
                        AND transaction_name in ('HR-LAP-2019-00001',)
Traceback (most recent call last):
  File "/usr/lib64/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/opt/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 97, in <module>
    main()
  File "/opt/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 18, in main
    click.Group(commands=commands)(prog_name='bench')
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/opt/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 25, in _func
    ret = f(frappe._dict(ctx.obj), *args, **kwargs)
  File "/opt/frappe-bench/apps/frappe/frappe/commands/site.py", line 233, in migrate
    migrate(context.verbose, rebuild_website=rebuild_website, skip_failing=skip_failing)
  File "/opt/frappe-bench/apps/frappe/frappe/migrate.py", line 48, in migrate
    frappe.modules.patch_handler.run_all(skip_failing)
  File "/opt/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 41, in run_all
    run_patch(patch)
  File "/opt/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 30, in run_patch
    if not run_single(patchmodule = patch):
  File "/opt/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 71, in run_single
    return execute_patch(patchmodule, method, methodargs)
  File "/opt/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 91, in execute_patch
    frappe.get_attr(patchmodule.split()[0] + ".execute")()
  File "/opt/frappe-bench/apps/erpnext/erpnext/patches/v12_0/remove_denied_leaves_from_leave_ledger.py", line 16, in execute
    delete_denied_leaves_from_leave_ledger_entry(leave_application_list)
  File "/opt/frappe-bench/apps/erpnext/erpnext/patches/v12_0/remove_denied_leaves_from_leave_ledger.py", line 26, in delete_denied_leaves_from_leave_ledger_entry
    AND transaction_name in {0} '''.format(tuple(leave_application_list))) #nosec
  File "/opt/frappe-bench/apps/frappe/frappe/database/database.py", line 171, in sql
    self._cursor.execute(query)
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/pymysql/connections.py", line 517, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/pymysql/connections.py", line 732, in _read_query_result
    result.read()
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/pymysql/connections.py", line 1075, in read
    first_packet = self.connection._read_packet()
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/pymysql/connections.py", line 684, in _read_packet
    packet.check_error()
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/opt/frappe-bench/env/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 5")

After check with the database
We realized in table tabLeave Application we have a record with name HR-LAP-2019-00001
based on the action of script remove_denied_leaves_from_leave_ledger.py did, it read information from tabLeave Application with record that is not approved and try to delete the record.
The problem is in table tabLeave Ledger Entry , we don’t have the record.

Can we just delete the record in table tabLeave Application with name HR-LAP-2019-00001 as workaround?

Yes you can remove this Record if it’s not significant. and keep a copy of this record , in case you need to insert it after update.

Maybe you can find more records like this.

Thanks for the response so quickly , it is even better than some other commercial software. :smile: