The approach I've seen so far to handle setup scripts is the version_compare:
namespace My\Module\Setup;
class UpgradeSchema implements \Magento\Framework\Setup\UpgradeSchemaInterface;
{
public function __construct(
\My\Module\Model\SomethingFactory $somethingFactory,
\My\Module\Model\AnotherThing $anotherThing
) {
$this->somethingFactory = $somethingFactory;
$this->anotherThing = $anotherThing;
}
public function upgrade(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.0', '<')) {
// do stuff with $this->somethingFactory
}
if (version_compare($context->getVersion(), '1.0.1', '<')) {
// do stuff with $this->anotherThing
}
$setup->endSetup();
}
}
At some point in the life of a module, those scripts can become huge and very hard to maintain, and the dependencies injected might change over time, but you'll need to keep all of them in order to guarantee a safe upgrade from each particular version you have published.
Is it known any other possible approach to manage the setup scripts?
Thanks!