Get Free Audit

Working with Setup Migration Scripts in Magento 2

Aug 16, 2018

2437 Andrey Litvin

Working with Setup Migration Scripts in Magento 2

Magento 2 uses schema/data migrations to provide data persistence and database updates. These migrations contain instructions for:

  • creating the required tables and filling them during the initial installation;
  • transforming the database scheme and information in it for each of the possible versions of the application.

Magento 2 setup scripts are located in the app/code/Vendor/Module/Setup folder.

InstallSchema and InstallData classes are responsible for the first module installation and UpgradeSchema and UpgradeData scripts will be used when upgrading the module’s version.

InstallSchema class

InstallSchema class is used for creating the necessary table module during the primary installation.

InstallData class

InstallData class is used for inserting the data during the initial installation.

UpgradeSchema class

UpgradeSchema class is used for modifying the database schema when updating the module.

Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

UpgradeData class

UpgradeData class is used for modifying the data when updating the module.

Running setup scripts

You can run migration scripts with the following CLI command: $ php bin/magento setup:upgrade

If Magento sees any new module, it will instantiate objects from the Vendor\Module\Setup\InstallSchema and Vendor\Module\Setup\InstallData classes.

If the module’s version has changed, then Vendor\Module\Setup\UpgradeSchema and Vendor\Module\Setup\UpgradeData will be instantiated. After that, the corresponding upgrade methods will be launched.


Unlike Magento 1, Magento 2 does not contain built-in tools for migrations versioning and each developer must check the current/required version of the module themselves.

Recurring scripts

Recurring scripts are launched each time you run the setup:upgrade command and don’t depend on the version of the module.


Thank you for reading, hope this information was useful. Please leave your comments and questions on the article in the comment section below.

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey


  1. Hi, Kumar

    Take a look here as an example: Magento\Indexer\Setup\Recurring
    Each time the setup:upgrade is performed, the Magento_Indexer module checks if new indexers have been added and if yes, it adds them to the indexer_state table.

  2. What is the use of Recurring script.. why we need to run migration classes each and every time when Setup:Upgrade…
    Seems unnecessary..?? Is there any reason behind it..?

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *