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)

        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



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?



Split it to set of classes.

Like UpdaterTo100, UpdaterTo101, ...

  • Any examples? How do I tell Magento to read those files? – Rafael Kassner Sep 27 '16 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.