Patches
The Patches used to data related changes, you might want to run one off scripts to change existing data to match expectations as per new code. We call these scripts patch in frappe.
To write a patch, you must write an execute
method in a python script and add it to patches.txt
of your app.
The directory structure followed in Agarwals is as below :
agarwals
└── patches
└── v1_0
└── mail_log_patches.py
The patch can then be added to patches.txt
by its dotted path.
agarwals.patches.v1_0.mail_log_patches
For Execute the patches in backend using python the python will be created and call the execute() method
import frappe
from agarwals.utils.error_handler import log_error
def execute():
#your patch code here
Post-Model sync patches
Often your patch might not require access to database schema before DocType models are synced with database. In such cases it's better to keep the patch in [post_model_sync]
section of patches.txt
.
patches.txt
supports INI-like file format where two sections specify when a patch should run - before or after doctype schema migration. Post model sync patches do not require reloading any doctypes as all doctypes are reloaded before executing them. Here is example of such patches.txt file:
[pre_model_sync]
app.module.patch1
app.module.patch2
[post_model_sync]
app.module.patch3
app.module.patch4
RUN Pathces
The Patch would run while migrate the bench.
bench migrate
Patch execution order
Patches run in the order they are defined. All lines in patches.txt
have to be unique. If a patch has been run before, it won't run again. If you want to run a patch again, add a comment that will make the line appear as new.
For Example,
agarwals.pathces.v1_0.mail_log_patches #2019-09-08
OR We can delete the data in `tabPatch Log` using sql syntax and pass the patch path and run the patch.
DELETE FROM database_name.`tabPatch Log`
WHERE patch = 'agarwals.patches.v1_0.mail_log_patches';