Setup script files in Magento2

As the name defines: Setup script files used to perform some action on your data or table while installing or upgrading up your module.

All the setup scripts files are created under Setup folder which is at path:

app/code/Webkul/Test/Setup

Setup folder contains following files:

1) InstallSchema.php: This file is created when we want to do changes in our table structure or create a new table.
This file is executed on installation of module.

<?php

namespace Webkul\Test\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)
    {
        //add code here for table create or update
    }
}

2) UpgradeSchema.php : This file is used to update table structure or to create a new table.
This file is get executed whenever your module is installing or upgrading.

There is no version restriction in the file as in magento1, so it will get execute on each version upgradation.
This file executed after Install Schema file.

<?php

namespace Webkul\Test\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)
    {
        //Your code for upgrade data base
    }
}

3) InstallData.php: Used to add data in tables which are already created in your magento.
For example all the product attributes are created by Install Data.
This file gets executed when your module’s Install Schema.

<?php

namespace Webkul\Test\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;

class InstallData implements InstallDataInterface
{
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
    	//code for install data
    }
}

4) UpgradeData.php: Used to add data or update data in tables which are already created in your magento.
For example you can update any existing product’s name or you can create a new product, or can update product’s attribute data.
This gets executed when your module is installing or upgrading.
This file executed after Install Data.

<?php
namespace Webkul\Test\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)
    {
    	//your code for Upgrade data
    }
}

5) Recurring.php: This file is used when you want to add some functionality after each module installation.
Lets take an example:
we have three modules Test1, Test2 and Test3.
And we have Recurring.php file in Test1 module.
So, Test1/Setup/Recurring.php get executed after completion of installation of all the above three modules.
Recurruing.php file get executed on each time either module’s version updated or not.

<?php

namespace Webkul\Test\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)
    {
    }
}

6) Uninstall.php : This file is executed when magento module:uninstall get executed.
By this file All the tables and table’s data and files are deleted related to the specific module.
This Uninstall works only on those modules which are composer dependent.

<?php

namespace Webkul\Test\Setup;

use Magento\Framework\Setup\UninstallInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class Unistall implements UninstallInterface
{
    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
    }
}

You can check install schema in detail in blog:
Install Schema In setup script Magento2
You can check install data in detail in blog:
Install Data In setup script Magento2
Another example of Install Data In setup script to create product Magento2

Hope this blog will help in setting up your magento.

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close