Magento 2 replaced Magento 1's Setup Upgrade scripts with a bin/magento setup:upgrade
command.
When a module's installed, this command looks for a InstallSchema
and InstallData
class in a module's Setup
folder, and runs both class's install
method.
When a module is upgraded, this command looks for an UpgradeSchema
and UpgradeData
class in a module's Setup
folder, and runs these class's upgrade
method. Unlike Magento 1, there is no longer any built-in logic to run through a set of scripts named with version numbers.
The pattern used by Magento 2's core in Upgrade scripts looks like this
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$connection = $installer->getConnection();
if (version_compare($context->getVersion(), '2.0.1') < 0) {
//... upgrade scripts here ...
}
if (version_compare($context->getVersion(), '2.0.2') < 0) {
//... upgrade scripts here ...
}
if (version_compare($context->getVersion(), '2.0.3') < 0) {
//... upgrade scripts here ...
}
if (version_compare($context->getVersion(), '2.0.4') < 0) {
//... upgrade scripts here ...
}
}
i.e. The Upgrade classes need to include programatic logic for version checks.
Looking over how Magento's coded these in the core though -- it's not clear how this system works. Is $context->getVersion()
the version of am module the user is upgrading from? Or to? If you're jumping module versions (from say, 2.0.2
to 2.0.5
) does setup:upgrade
call this upgrade
once, or does it run through the scripts multiple times (one for the 2.0.3
upgrade, again for 2.0.4
upgrade, and again for 2.0.5
?
This doesn't (seem to be) documented on the dev docs site -- and setup code is pretty tricky to test by trial and error. Does anyone know how these work, and if Magento's implementation is "non-contiguous version upgrade" safe? If you're tempted to answer of course to the later without testing -- welcome newcomer!