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

Updated: Aug 20, 2020

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

#crud #magento2 #upgradeschema

5 views0 comments

Recent Posts

See All

Magento 2 CMS block with the same ID already exists

I have seen the below error when I open cms block grid something went wrong with processing the default view and we have restored the filter to its original state. I found the below log in the excepti

How to generate bearer token - Magento 2 REST API

We can generate the admin bearer token using the below command. Please run the below command in the terminal to generate a token. curl -XPOST -H 'Content-Type: application/json' http://www.domain.com/

Magento 2 script tag and available options in layout xml

We can add a script via layout XML file in Magento 2. The below format will stop the HTML parse until js downloaded and executed. <script src="Bilal_Usean/js/sample.min.js" /> The below format will st

Follow Me

  • LinkedIn
  • Twitter
  • Facebook
  • YouTube
  • Instagram

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