Setup scripts in Magento 2

Setup Scripts In Magento 2

Create a new module with CustomScripts_SetupScripts

Step 1: Create module.xml file.

app/code/CustomScripts/SetupScripts/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="CustomScripts_SetupScripts" setup_version="1.0.0" />
</config>

Step 2: Create registration.php file.

app/code/CustomScripts/SetupScripts/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
	\Magento\Framework\Component\ComponentRegistrar::MODULE,
	'CustomScripts_SetupScripts',
	__DIR__
);

Step 3: Create InstallSchema.php file.

app/code/CustomScripts/SetupScripts/Setup/InstallSchema.php

<?php

namespace CustomScripts\SetupScripts\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('inviqa_example'))
            ->addColumn(
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => 
false, 'primary' => true],
                'Entity ID'
            )
            ->addColumn(
                'name',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => false],
                'Name'
            )
            ->setComment('Inviqa Example');
        $installer->getConnection()->createTable($table);

        $installer->endSetup();
    }
}

Step 4: Create InstallData.php file.

app/code/CustomScripts/SetupScripts/InstallData.php

<?php

namespace CustomScripts\SetupScripts\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)
    {
        $setup->getConnection()->query("INSERT INTO inviqa_example SET name = 'Test 1'");
    }
}

Step 5: Create UpgradeSchema.php file.

app/code/CustomScripts/SetupScripts/Setup/UpgradeSchema.php

<?php

namespace CustomScripts\SetupScripts\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('inviqa_example'),
                'email',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => true,
                    'comment' => 'Email'
                ]
            );

        }

        $setup->endSetup();
    }
}

Step 6: Create UpgradeData.php file.

app/code/CustomScripts/SetupScripts/Setup/UpgradeData.php

<?php

namespace CustomScripts\SetupScripts\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', '<=')) {
            $setup->getConnection()->query("INSERT INTO inviqa_example SET name = 'Test 2', email = 'test@inviqa.com'");
        }

        $setup->endSetup();
    }
}

Step 7: Create Recurring.php file.

app/code/CustomScripts/SetupScripts/Setup/Recurring.php

<?php

namespace CustomScripts\SetupScripts\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();

        $setup->getConnection()->query("INSERT INTO inviqa_example SET name = 'Test 3', email = 'test@inviqa.com'");

        $setup->endSetup();
    }
}

Step 8: Create Uninstall.php file.

app/code/CustomScripts/SetupScripts/Setup/Uninstall.php

<?php

namespace CustomScripts\SetupScripts\Setup;

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

class Recurring implements UninstallSchemaInterface
{
    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $setup->getConnection()->query("DROP table inviqa_example");

        $setup->endSetup();
    }
}