This is very common requirement to create our own custom module in Magento 2 for fulfilling some specific requirements. Sometimes, it is also required to manage database or files/folders to achieve the defined objective.

 

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

 

To handle these type of requirements, Magento 2 has provided Setup Scripts which make a custom module very efficient. There are 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 install function under 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 install function under 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 upgrade function under UpgradeSchema class. As the name suggests, the function upgrade is called. It helps in writing the scripts to be executed based on the version of 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_feed table if current version of module is less than or equal to 1.1.0.

 

 

UPGRADEDATA:

UpgradeData.php: This file contains upgrade function under UpgradeData class. As the name suggests, the function upgrade is called after UpgradeSchema is called. It helps in writing the scripts to be executed for managing data based on the version of current module. The file is called everytime whenver 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 bin/magento setup:upgrade; CLI command is executed. The Recurring Script is executed whenever version of custom module is upgraded. It does not depend on version comparison condition. Here, is the script of this file:

 

<?php

 

namespace Inviqa\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 Recurring class does not have its own interface though it uses InstallSchemaInterface and the recurring code is written into install function.

 

 

UNINSTALL:

Uninstall.php - This file contains uninstall function under 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 to manage the complications by coding in single script file. So, Magento 2 Developer should implement the best coding practices to make it more understandable.