• Bilal Malik

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

  1. InstallSchema.php script executed while running “bin/magento setup:upgrade” , we use this features to create table structure

  2. Create CRUD object using Model, ResourceModel and Collection for CRUD(Create Read Update Delete) operation

  3. Use above-created CRUD object to insert data into our custom table, InstallData.php script also executed while run “bin/magento setup:upgrade” , we use this features to insert data into our custom table.

Now we upgrade the table schema while upgrade module.

  1. 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"/>

Create UpgradeSchema Script

Create app/code/Vendor/Module/Setup/UpgradeSchema.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)

        // 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);



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.

#crud #magento2 #upgradeschema


©2020 by Bilal Usean. Proudly created with Wix.com