How to Apply Schema Patch in Magento 2?

| |
Comments: 0
How to Apply Schema Patch in Magento 2?

What is Schema Patch in Magento 2?

Schema patch is a class that contains custom schema modification instructions which means you can add, update, or delete a column from the custom table or EAV table. These modifications can be complex such as performing data migration, adding functions, renaming columns and tables. In this blog we will learn about how to use declarative schema and how to apply schema patches.

Manually Schema Patch is defined in a //Setup/Patch/Schema/.php file and implements \Magento\Framework\Setup\Patch\SchemaPatchInterface.

We can also create schema patch using below command:

php bin/magento setup:db-declaration:generate-patch –type=schema Vendor_ModuleName FileName

By using the above command, let’s create schema patch in Magento 2.

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Mageants\Blog\Setup\Patch\Schema;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
/**
* Patch is mechanism, that allows to do atomic upgrade data changes
*/
class PatchDemo implements SchemaPatchInterface
{
/**
* @var ModuleDataSetupInterface $moduleDataSetup
*/
private $moduleDataSetup;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
*/
public function __construct(ModuleDataSetupInterface $moduleDataSetup)
{
$this->moduleDataSetup = $moduleDataSetup;
}
/**
* Do Upgrade
*
* @return void
*/
public function apply()
{
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [
];
}
}

In above example our vendor is Mageants,Module Name is Blog and file name is PatchDemo.php

Using the above command we will create a schema patch file with default methods.

-apply() function is used to implement the main code logic where we create product attribute code inside the apply() function.

-getDependencies() function contains the class name of dependent patches. This functionality notifies Magento to execute the “patches”.

-getVersion() function will return a version of the patch.

-getAliases() function defines aliases for the patch class. Sometimes, When you want to change the class name then it could be possible using the getAliases() function.

So, How to Apply Schema Patch in Magento 2 ?

Now let’s discuss how to apply schema patch using declarative schema,

Step 1:

Create db_schema.xml file At MageAnts->Blog->etc->db_schema.xml.

	<?xml version="1.0"?>
	<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
	   <table name="demotable" resource="default" engine="innodb" comment="Demo Table1">
	   	<column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" identity="true" comment="ID"/>
	   	<column xsi:type="varchar" name="name" comment="Name"/>
	   	<constraint xsi:type="primary" referenceId="PRIMARY">
	       	<column name="id"/>
	   	</constraint>
	   	<index referenceId="MY_MODULE_ID" indexType="btree">
	       	<column name="id"/>
	   	</index>
	   </table>
	</schema>

In the above example I will create a demotable table.

Step 2:

Now Create AddColumn.php file At Mageants->Blog->Setup->Patch->Schema->AddColumn.php

/**
* Copyright © 2019 Magenest. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Mageants\Blog\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(‘demotable’),
‘surname’,
[
‘type’ => Table::TYPE_TEXT,
‘length’ => 255,
‘nullable’ => true,
‘comment’ => ‘SurName’,
]
);
$this->moduleDataSetup->endSetup();
}
}

Using above code we add another column surname at demotable.

Conclusion:

Using the above tutorial, you can learn about how to use schema patch in Magento 2. In case of any issue regarding the above example then you can contact us.

Recommended Magento Tutorials for You:

How to Install Magento 2 Using Composer?

How to add a custom tab to the customer account in Magento 2?

Leave a Reply

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