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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace Vendor\Module\Setup; class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface { public function install( \Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context) { $setup->startSetup(); $table = $setup->getConnection()->newTable( $setup->getTable('custom_table') )->addColumn( 'custom_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null, ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], 'Custom Id' )->addColumn( 'name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, [], 'Custom Name' )->setComment( 'Custom Table' ); $setup->getConnection()->createTable($table); $setup->endSetup(); } |
InstallData class
InstallData class is used for inserting the data during the initial installation.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace Vendor\Module\Setup; class InstallData implements \Magento\Framework\Setup\InstallDataInterface { public function upgrade( \Magento\Framework\Setup\ModuleDataSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context) { $setup->startSetup(); // data installation code $setup->endSetup(); } } |
UpgradeSchema class
UpgradeSchema class is used for modifying the database schema when updating the module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace Vendor\Module\Setup; class UpgradeSchema implements \Magento\Framework\Setup\UpgradeSchemaInterface { public function upgrade( \Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context ) { $setup->startSetup(); if (version_compare($context->getVersion(), '2.3.1') < 0) { // upgrade schema to version 2.3.1 } $setup->endSetup(); } } |
UpgradeData class
UpgradeData class is used for modifying the data when updating the module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace Vendor\Module\Setup; class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface { public function upgrade( \Magento\Framework\Setup\ModuleDataSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context ) { $setup->startSetup(); if (version_compare($context->getVersion(), '2.3.1') < 0) { // upgrade data to version 2.3.1 } $setup->endSetup(); } } |
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.
Versioning
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class UpgradeSchema implements UpgradeSchemaInterface { public function upgrade( \Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context ) { $setup->startSetup(); if (version_compare($context->getVersion(), '1.5.1') < 0) { //code to upgrade to 1.5.1 } if (version_compare($context->getVersion(), '1.5.7') < 0) { //code to upgrade to 1.5.7 } $setup->endSetup(); } } |
Recurring scripts
Recurring scripts are launched each time you run the setup:upgrade command and don’t depend on the version of the module.
app/code/Vendor/Module/Setup/Recurring.php
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace Vendor\Module\Setup; class Recurring implements \Magento\Framework\Setup\InstallSchemaInterface { public function install( \Magento\Framework\Setup\SchemaSetupInterface$setup, \Magento\Framework\Setup\ModuleContextInterface$context ) { echo ‘Running’; } } |
Thank you for reading, hope this information was useful. Please leave your comments and questions on the article in the comment section below.
Hi Author,
Excellent reading with a technical example for Magento 2 Migration.
Informative content.
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.
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..?