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.
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
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!
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…
The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…