Upgrade schema while upgrade module (UpgradeSchema) – setup script – Magento 2

This is a continuation of the previous tutorial, It helps to alter table into the database while installing the module in Magento 2.

As per our previous tutorials, we summarize the below points

Now we upgrade the table schema while upgrade module.

  • UpgradeSchema.php script also executed while run “bin/magento setup:upgrade” and it additionally check version. We use this features to add a column into our existing custom table.

When we use UpgradeSchema

Consider already you launch your module-version-1.0.0 into public marketplace, If you want to alter schema of your module upcoming version-1.0.1, then you should add logic in UpgradeSchema.

First change the module version in app/code/Bilal/Cruddemo/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="Bilal_Cruddemo" setup_version="1.0.1"/>
</config>

Create UpgradeSchema Script

Create app/code/Vendor/Module/Setup/UpgradeSchema.php

<?php
namespace Bilal\Cruddemo\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();

        // checking the existing version with current version, if the difference found, execute the below script
        if (version_compare($context->getVersion(), '1.0.1') < 0) {

            // Get module table
            $tableName = $setup->getTable('crud_test_emp1');

            // Check if the table already exists
            if ($setup->getConnection()->isTableExists($tableName) == true) {

            // Declare data
                $columns = [
                'designation' => [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => false,
                'comment' => 'employee designation',
                ],
                ];

                $connection = $setup->getConnection();

                foreach ($columns as $name => $definition) {
                    $connection->addColumn($tableName, $name, $definition);
                }

            }
        }

        $setup->endSetup();
    }
}

If you keenly observe the UpgradeSchema script, you can find the logic of version comparison

if (version_compare($context->getVersion(), '1.0.1') < 0) {
...........................................................

As we discussed UpgradeSchema.php execute while running setup:upgrade so run the below command in your Magento root

php bin/magento setup:upgrade

If you check your table, you can find our new column.

I hope it helps! Please carry the same source code to follow next tutorial.

About Bilal

I'm a software programmer, I handle Magento2 modifications at any level.

View all posts by Bilal →

Leave a Reply

Your email address will not be published. Required fields are marked *