[Solution] Dropping a table from within the framework

If you are trying to delete a DocType with a patch this is the way to go:

  1. Remove all files in Git
  2. Remove it from tabDocType
  3. Drop the table

Writing the patch

Create a folder (e.g. “v1_0_0”) in your patch subfolder within your app and add an empty __init__.py file. Then create your patch file, in this example it is called “delete_eso_manual.py”.

SQL can be executed with frappe.db.sql.

In order to make schema changes there is frappe.db.sql_ddl which is a wrapper function https://github.com/frappe/frappe/blob/develop/frappe/database.py#L246

This would be an example patch file:

import frappe


def execute():

    print("Deleting ESO Manual")
    frappe.db.sql("""Delete from `tabDocType` where name = 'ESO Manual' """)
    frappe.db.sql_ddl("""DROP TABLE IF EXISTS `tabESO Manual`""")

Next run that patch in bench to test it, in this example the command would be:

bench execute newmatik.patches.v1_0_0.delete_eso_manual.execute

If all worked well commit to Git and the patches will be run on all setups with the app.

6 Likes