Magento2 – Purpose of InstallSchema and UpgradeSchema

databaseinstall-scriptmagento2setup-script

If UpgradeSchema/UpgradeData are executed when installing and upgrading a module what's the point of having InstallSchema/InstallData (besides just having a well-named file that is only executed once). We could handle the install part in the Upgrade file as well – we could check !$context->getVersion().

This raises another question about the correct workflow for the lifetime of a module.

Do we create the Install* script only once and then never change it? I've seen third party modules changing this file between versions. In that case I assume they also track the same changes in Upgrade* (hence duplicating code).

Am I missing something here?
Thanks!

UPDATE: I understand the role of each file and the differences between Schema and Data. I'm interested to find out about best practices. My understanding is we're allowed to update Install* as long as the module is in development but once it's made public we're not allowed to change this file ever again. From that point on we should only use versioned Upgrade* scripts. Is this correct?

Best Answer

I got this question in a training I was giving, and we tried out if we need InstallSchema / InstallData at all. It turned out we don't need it, as the Upgrade classes are executed on installation too. So, in my opinion, I would not use the Install classes at all as the distinction is quite arbitrary and it only confuses in most cases.

Related Topic