Install and Upgrade Scripts in Magento 1.x Vs. 2.x


When we start writing Magento modules (in both versions 1.x or 2.x) sooner or later, we will need to change database schema and/or data. Magento provides a way to do this by using Install and Upgrade scripts. Using these scripts, we can

  1. Install or Upgrade the Database Schema
  2. Install or Upgrade data

In a self sustainable way while keep tracking of the changes done to the system.

Whenever a Module’s version is changed, Magento will look for the install/upgrade scripts and will run them in the order. And it will keep the last processed version in the database. The data table and their difference when moving from Magento 1.x to 2.x is described below.

Mageno 1.x

The table is core_resource. If the module is new, this table won’t have a record from the module name (e.g. MyCompany_Catalog) and hence Magento will look for the installer script. Then Magento will try to install all the upgrade scripts up to the version of the current module.

mysql> describe core_resource;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| code         | varchar(50) | NO   | PRI | NULL    |       |
| version      | varchar(50) | YES  |     | NULL    |       |
| data_version | varchar(50) | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+

mysql> select * from core_resource limit 5;
+-------------------------+---------+--------------+
| code                    | version | data_version |
+-------------------------+---------+--------------+
| adminnotification_setup | 1.6.0.0 | 1.6.0.0      |
| admin_setup             | 1.6.1.2 | 1.6.1.2      |
| banner_setup            | 0.8.0   | 0.8.0        |
| ambase_setup            | 1.0.3   | 1.0.3        |
| amogrid_setup           | 1.6.3   | 1.6.3        |
+-------------------------+---------+--------------+

Based on the config.xml configurations it will look for a file structure like this.

/[Namespace]/[Module]/sql/schema_setup
install-0.1.0.php
upgrade-0.1.0-0.2.0.php
upgrade-0.2.0-0.3.0.php
upgrade-0.3.0-0.4.0.php
upgrade-0.4.0-0.5.0.php

Magento 2.x

When comes to Magento 2, the database table looks nearly the same. But the table name is now setup_module, and the schema will look like this.

mysql> describe setup_module;
+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| module         | varchar(50) | NO   | PRI | NULL    |       |
| schema_version | varchar(50) | YES  |     | NULL    |       |
| data_version   | varchar(50) | YES  |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+

mysql> select * from setup_module limit 5;
+-------------------------------------+----------------+--------------+
| module                              | schema_version | data_version |
+-------------------------------------+----------------+--------------+
| Magento_AdminGws                    | 2.0.0          | 2.0.0        |
| Magento_AdminNotification           | 2.0.0          | 2.0.0        |
| Magento_AdvancedCatalog             | 2.0.0          | 2.0.0        |
| Magento_AdvancedCheckout            | 2.0.0          | 2.0.0        |
| Magento_AdvancedPricingImportExport | 2.0.0          | 2.0.0        |
+-------------------------------------+----------------+--------------+

Unlike Magento 1.x, here Magento 2 wil look for setup classes as follows.

InstallData.php (class InstallData implements Magento\Catalog\Setup\InstallDataInterface)
UpgradeData.php (class UpgradeData implements Magento\Catalog\Setup\UpgradeDataInterface)
InstallSchema.php (class InstallSchema implements Magento\Catalog\Setup\InstallSchemaInterface)
UpgradeSchema.php (class UpgradeSchema implements Magento\Catalog\Setup\UpgradeSchemaInterface)

There are no restrictions of using other classes to supportĀ setup or to break up for simplicity but we should make sure we keepĀ the proper implementation. Unlike Magento 1 now all the upgrades go in the two data/schema upgrade classes. They are wrapped inside version_compare php function blocks.

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
	$setup->startSetup();
	if (version_compare($context->getVersion(), '1.0.1') < 0) {
		//Do the Work
	}

	if (version_compare($context->getVersion(), '1.0.2') < 0) {
		//Do the Next version work
	}
	$setup->endSetup();
}

Leave a Reply

  • (will not be published)