WHAT IS SETUP SCRIPT IN MAGENTO 2

This is a very common requirement for creating our own custom modules in Magento 2 to fulfill specific requirements. Sometimes, it is also required to manage the database or files/folders to achieve the defined objective.

Sometimes, it is also required that the existing module be upgraded, and some more scripts have to be added to make changes in the database or files/folders.

Magento 2 has provided Setup Scripts to handle these requirements, which make a custom module very efficient. There are the following 6 types of Setup Scripts:

InstallSchema
InstallData
UpgradeSchema
UpgradeData
Recurring
Uninstall

Suppose a new custom module is created under app/code/Bizspice/SetupExample/ folder. So, all files of Setup Scripts will be created under app/code/Bizspice/SetupExample/Setup folder.

Installschema:

InstallSchema.php – This file contains the install function under the InstallSchema class. As the name suggests, the function install is called only once when the custom module is installed. Generally, Database operations and File Handling functions are written in this function. Here is the script of this file:

<?php
namespace Bizspice\SetupExample\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class InstallSchema implements InstallSchemaInterface
{
     public function install(SchemaSetupInterface $setup, ModuleContextInterface $context){
         $installer = $setup;
         $installer->startSetup(); 
         $table = $installer->getConnection()->newTable(
         $installer->getTable('bs_feed_feed'))
         ->addColumn(
         'feed_id',             
         \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
         null,
         ['unsigned' => false,
         'nullable' => false,
         'identity' => true,
         'primary' => true],
         'Feed Id')
         ->addColumn(
         'name',
         \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
         255,
         ['unsigned' => false,
         'nullable' => false],
         'Name'
         );
         $installer->getConnection()->createTable($table);
         $installer->endSetup();
    }
}

Here, you can see a table bs_feed_feed has been created with 2 columns.

Installdata:

InstallData.php – This file contains the install function under the InstallData class. As the name suggests, the function install is called only once after InstallSchema is called. In this function, generally, SQL script is written to insert the data, add column(s) into existing tables, etc. Here is the script of this file:

<?php
namespace Bizspice\SetupExample\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
  public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context){
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
     $connection = $resource->getConnection();
     $bs_feed_feed = $resource->getTableName('bs_feed_feed');
     $setup->getConnection()->query("INSERT INTO ".$bs_feed_feed." SET name = 'Feed 1'");
 }
} 

Here, in this file, a record has been inserted into bs_feed_feed table.

Upgradeshema:

UpgradeSchema.php: This file contains the upgrade function under the UpgradeSchema class. As the name suggests, the function upgrade is called. It helps write the scripts to be executed based on the version of the current module. The file is called every time whenever bin/magento setup:upgrade; CLI command is executed. Here is the script of this file:

<?php
namespace Bizspice\SetupExample\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
   public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context){
      $setup->startSetup();
      if (version_compare($context->getVersion(), '1.1.0', '<='))
      {
        $setup->getConnection()->addColumn(
        $setup->getTable('bs_feed_feed'),
        'xml',
        [
        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        'length' => '64K'
        'nullable' => true,
        'comment' => 'XML Content of feed'
        ]
        );
      }
      $setup->endSetup();
   }
} 

Here, a new column has been added into bs_feed_feedtable if current version of module is less than or equal to 1.1.0.

Upgradedata:

UpgradeData.php: This file contains the upgrade function under the UpgradeData class. As the name suggests, the function upgrade is called after UpgradeSchema is called. It helps write the scripts to be executed for managing data based on the version of the current module. The file is called everytime the bin/magento setup:upgrade; CLI command is executed. Here is the script of this file:

<?php
namespace Bizspice\SetupExample\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context ){
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.1.0', '<='))
        {
          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); 
          $connection = $resource->getConnection();
          $bs_feed_feed = $resource->getTableName('bs_feed_feed'); 
          $setup->getConnection()->query("INSERT INTO ".$bs_feed_feed." SET name = 'Feed 1', xml = 'xml data here'");
        }
        $setup->endSetup();
     } 
}

Recurring:

This feature has been introduced in Magento 2, and it runs whenever the bin/magento setup:upgrade; CLI command is executed. The Recurring Script is executed whenever the version of the custom module is upgraded. It does not depend on version comparison conditions. Here is the script of this file:

<?php
namespace Bizpsice\SetupExample\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class Recurring implements InstallSchemaInterface 
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context){
       $setup->startSetup();
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
       $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
       $connection = $resource->getConnection();
       $bs_feed_feed = $resource->getTableName('bs_feed_feed');  
       $setup->getConnection()->query("INSERT INTO ".$bs_feed_feed." SET name = 'Feed 2', xml = 'xml data here'");
       $setup->endSetup();
    }
}

Here, you will see that the Recurring class does not have its own interface though it uses InstallSchemaInterface, and the recurring code is written into the install function.

Uninstall:

Uninstall.php – This file contains the uninstall function under the Uninstall class. As the name suggests, the function uninstall is called on uninstalling the custom module. In this function, generally, SQL script is written to drop the tables and delete the files/folders. Here is the script of this file:

<?php
namespace Bizspice\Feeds\Setup;
use Magento\Framework\Setup\UninstallInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class Uninstall implements UninstallInterface
{ 
    protected $_setup;
    public function __construct(SchemaSetupInterface $setup,
    ModuleContextInterface $context
    ){
       $this->_setup = $setup;
     }
     public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context){
     $installer = $this->_setup;
     $installer->startSetup();     
     if ($setup->tableExists('bs_feed_feed')){
     $setup->getConnection()->dropTable('bs_feed_feed'); 
     $installer->endSetup();
    }
}

Here, in this class, you can see that code has been written to drop the bs_feed_feed table.

As compared to Magento 1, it has been simplified to manage Setup Scripts in Magento 2. Though, it has made Magento 2 Developer more responsible for managing the complications by coding in a single script file. So, Magento 2 Developer should implement the best coding practices to make it more understandable.