Patches do not get executed when installing app

Hi,
We are developing a custom app. The custom app has some patches defined in patches.txt.

Now, when we install the custom app on the site, say.
bench --site [sitename] install-app custom-app
The patches do not get executed even though the patchLog table has all the patch entries.

However, when we do bench migrate on an existing site
bench --site [sitename2] migrate
The patches get executed successfully.
Is this the normal behaviour in frappe? How do we get the patches executed when installing app?

Yes this is normal. They do get installed during migrate.

You can maybe try putting your patch method in the after_install in hooks.py. Here’s an example from erpnext codebase:

1 Like

Thanks. So during install-app the patches do not get executed is normal?

Correct. The reason is that patches are typically used in the upgrade process to change the schema of already existing doctypes. If you’re installing a new app with new doctypes, you typically wouldn’t patch but just define everything the way you want it in the first place.

Can I ask what you are trying to accomplish with patches at installation time?

1 Like

Thank you. Loading some doctypes with default data. I think after_install would be the correct approach.

Ah, understood. Another approach for default data would be to use “fixtures”. They’re not very well documented, unfortunately, but there’s lots of discussion in the forum.

1 Like

Please note that if your use case is to have initial data that can be then modified by the users, fixtures won’t work as they will sync and overwrite the data on every migrate.

To have initial data, but not static data, the only way I’ve found is to use both a patch and after_install hook, to cover existing and new installations.

2 Likes