When we should write a patch?

I found, many patches are written in ERPNext core repository. While if some functions, has to be developed can be written in core doctype itself. Then, why and when we should write a patch? I just know that, it adds more/required functionalities to the project, without making any changes in core files.

I just a noob, wants to learn. Let’me help with some example for better understanding. I can see, a folder in custom app as well, but when we should write patch for a custom app as well?

PS: What I understand is, patches are written in Python files only, to make some changes in altering DB Tables, in ERPNext or Custom Apps. Please help us to learn, more scope about the patch. This will definetly useful for best practise.

Hi @asharamseervi,

Patches are used for example when you make a change in an existing functionality and want to update instances that are already running ERPNext to make it compatible with that change. Or it can also be used when you need to correct something in a functionality already used in production.

Patches are only meant to be run once on each sites already running ERPNext. They are never run twice and never run in a new site.

For example, one of the latest patch is meant to rename School into Education.
On a new site, the Doctype Education exists from the beginning so you don’t need to do anything, but on an older site, the doctype was named School, so you need to update the database to have it named Education to be compatible with the new code base in ERPNext. Therefore you run a script that renames School to Education when you update your codebase with Education everywhere instead of School.

Hope it’s clear :slight_smile:

6 Likes

Thanks, @chdecultot, It helped me a lot. Even with me, this answer also helps a newbie like me.

To clear some more doubts, I had seen many developers writes patch for custom apps as well. Not to update compatibility, which might be not a good practice as I understood from your answer. So, to write Necessary part of Python code, to extend functionality. Where should we write a code for a custom app, which is not a genric code.

Somewhat, I like to write a code, for extra raw in csv/excel which is imported from ERPNext & upload it back to make changes. Should I write a patch for this? or what? Or write code in the same file name, as exist on ERPNext?

Hi @asharamseervi,

Sure you can write patches for a custom application. It follows the same logic: the patch will be run only once.

It all depends on your technical needs and use case.

As for the code to be used normally in your custom app, you need to create new doctypes, pages and reports and add your code to the Python, JS or HTML files associated to each type of document. Exactly like in ERPNext and Frappe.

Regarding your csv/excel import question, you have several possibilities if you can’t use the Data Import Tool which is associated to all doctypes: You can make a custom script (not a patch) that will export and import from ERPNext and to ERPNext.
Or you can modify the existing Data Import Tool files to match your needs:

It’s hard to tell, because it really depends on your exact use case (one time import, repeated import, what kind of data,…?) .

If your business case can match the business case of others, don’t hesitate to send a PR :slight_smile:

1 Like