Installation And Upgrade Scripts in Magento

A Magento install and upgrade script is a part of module development.

In this article, we will find out how to install and upgrade sql script for module in Magento 2. When you install or upgrade a module, you may need to change the database structure or add some new data for current table. To do this, Magento 2 provide you some classes which you can do all of them.

For example, here – app/code/Magento/Catalog/Setup/

You can see some examples, install and upgrade scripts with proper naming conventions along with their version numbers.

Overview of Install Upgrade SQL script

Install or Upgrade schema

Schema setup scripts change database schema, they create or change needed database tables. If module is installing, Setup\InstallSchema::install() is executed.

If module is installing or upgrading, Setup\UpgradeSchema::upgrade() is executed. Since there are no separate upgrade scripts for specific upgrade versions, your UpgradeSchema class needs to handle all possible version scenarios.

Example

Install or Upgrade data

Data setup scripts contain entries module needs to insert into database. Data setup is executed after Schema setup, they function in a similar fashion.

Attributes that come with Magento by default, 404 and other Cms pages, various default groups and roles, are all examples of data setup.

Recurring

Recurring script is executed after any module setup.The idea is that Module1 can do something after Module2 and Module3 are installed, if needed.

The only example in current Magento 2 is Magento\Indexer\Setup\Recurring class where Magento_Indexer module checks for new defined indexers and adds them to indexer_state table.

Uninstall

The purpose of Uninstall script is to remove all module tables, columns, data from database, like it was never installed in the first place.

All of the class will be located at app/code/Vendor/Module/Setup folder. The module install/upgrade script will run when you run the “upgrade” command.

In this article, we will use the sample module Iyngaran_HelloWorld to create some demo table and data.

Create a Module “Iyngaran_HelloWorld”

First, let’s create our folders.

  • app/code/Iyngaran/HelloWorld/etc/
  • app/code/Iyngaran/HelloWorld/Setup/

In app/code/Iyngaran/HelloWorld/etc/module.xml, enter the following:

In app/code/Iyngaran/HelloWorld/registration.php, enter the following:

To enable the module, On your command line, enter the following:

Create An Install Script

Installation used to be handled by including a file from the scope of a setup class. Now all installation processes are done within the setup class.

Installation classes must be named InstallSchema and must be located in the Setup/InstallSchema.php file of your module.

In app/code/Iyngaran/HelloWorld/Setup/InstallSchema.php, enter the following:

Let’s break down and see, what’s going on in this file.

Your class must implement the Magento\Framework\Setup\InstallSchemaInterface class.

Your class must implement the install() method.

Your method accepts two arguments: Magento\Framework\Setup\SchemaSetupInterface , and Magento\Framework\Setup\ModuleContextInterface.

This is how you create the installation functionality in your class. While you will be passing the SchemaSetupInterface class, you will actually be receiving the Magento\Framework\Module\Setup class.

The ModuleContextInterface has one method, getVersion() . This is the current version of your module. You can use this for version comparisons, which will have more relevance in the next section.

In the example above, I have created a blog post table and added columns, indices, and created the table in this file.

Your module’s installation script will run when you enter the following on the command line:

Create An Upgrade Script

Inevitably, there will be changes to your module. You need to be able to perform changes to your database schema. You can do this by creating an upgrade script.

In app/code/Iyngaran/HelloWorld/Setup/UpgradeSchema.php, enter the following:

Now you can see how the ModuleContextInterface class comes into play. Since all installations and upgrades run through just two classes, you need to be able to create incremental upgrades. You can do this by evaluating the version of your module.

Create Data Install and Upgrade Scripts

Data installations and upgrades follow the same concept as schemas. Data install scripts allow you to insert data into your newly created tables.

In app/code/Iyngaran/HelloWorld/Setup/InstallData.php, enter the following:

This file is an example of leveraging your models in your module to create new content. You can do just about anything from within your data install.

You can also manipulate table rows without the use of models in these scripts. Let’s do that in an upgrade script.

In app/code/Iyngaran/HelloWorld/Setup/UpgradeData.php, enter the following:

In this example, we take the getTableRow() and updateTableRow() methods to update data on our table. This way you can update data without having to call models in order to resolve the data. Neat!

Create A Recurring Script

You can also create recurring scripts inside of your setup. This is a script that runs every time the setup:upgrade command is run on the command line.

It follows the same concept as an installation script, with some slightly different naming.

In app/code/Iyngaran/HelloWorld/Setup/Recurring.php, enter the following:

Obviously this is a simple example, but now every time the command is run, you will get a log to your log file.

That’s it!.

Clear cache and upgrade magento. You will be able to see the changes on your database.

Hope this helps.

Be the first to comment

Leave a Reply

Your email address will not be published.


*


*