How To

How to Apply Schema Patch in Magento 2?

Today I will be discussing How to Apply Schema Patch in Magento 2.

Schema patch is a class in Magento 2 that contains schema modification instructions so you can add, update, or delete columns in any custom table or EAV table.

Here, we are adding a new column in the existing table (“custom_table”) using schema patch.

Steps to Apply Schema Patch in Magento 2:

Step 1:  We need to create an “AddColumn.php” file inside our extension at the following path.

app\code\Vendor\Extension\Setup\Patch\Schema\

Then add the code as follows

<?php
namespace Vendor\Extension\Setup\Patch\Schema;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class AddColumn implements SchemaPatchInterface
{
   private $moduleDataSetup;

   public function __construct(
       ModuleDataSetupInterface $moduleDataSetup
   ) {
       $this->moduleDataSetup = $moduleDataSetup;
   }

   public static function getDependencies()
   {
       return [];
   }

   public function getAliases()
   {
       return [];
   }

   public function apply()
   {
       $this->moduleDataSetup->startSetup();

       $this->moduleDataSetup->getConnection()->addColumn(
           $this->moduleDataSetup->getTable('custom_table'),
           'name',
           [
               'type' => Table::TYPE_TEXT,
               'length' => 255,
               'nullable' => true,
               'comment'  => 'Name',
           ]
       );

       $this->moduleDataSetup->endSetup();
   }
}

Step 2: After adding this file run the Magento upgrade command

php bin/magento setup:upgrade

Conclusion:

This way you can Apply Schema PAtch in Magento 2. Alternatively, learn How to Use Data Patch in Magento 2. In case you have any doubt let me know through the comment section. Stay in touch with us for more.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago