Declarative Schema

Magento 2.3 brings a new feature called the Declarative Schema, which allows a developer to facilitate the Magento database installation (Installschema) and upgrading (Upgradeschema) processes, without performing any additional work, i.e. writing version updates for each new installation/upgrading process in etc/module.xml

Create a Table Using the Declarative Schema


<?xml version="1.0"?>
<schema xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
	<table name="customer_contactdetails" resource="default" engine="innodb" comment="Customer Details">
    	<column xsi:type="smallint" name="id" padding="6" unsigned="false" nullable="false" identity="true" comment="ID"/>
    	<column xsi:type="varchar" name="customer_name" nullable="false" length="25" comment="Name"/>
    	<column xsi:type="varchar" name="customer_email" nullable="false" length="25" comment="Email"/>
    	<column xsi:type="varchar" name="contact_no" nullable="false" length="255" comment="Contact No" />
    	<constraint xsi:type="primary" referenceId="PRIMARY">
        	<column name="id"/>

Run the command:

php bin/magento setup:db-declaration:generate-whitelist --module-name=DCKAP_Datapatch

Create a custom module, as shown in the file structure below.


Data Patch - Custom Module

Data Patch

A data patch is a class that contains data modification instructions. Through the Data Patch Versioning, all the InstallData and UpgradeData will be replaced.

It is defined in a <Namespace>/<Module_Name>/Setup/Patch/Data/<Patch_Name>.php file, and implements \Magento\Setup\Model\Patch\DataPatchInterface.

Step 1: Create a Model and Resource Model for Creating a Table as Usual

namespace DCKAP\Datapatch\Model;
use Magento\Framework\Model\AbstractModel;
class Contactdetails extends AbstractModel{
	protected function _construct()

namespace DCKAP\Datapatch\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Contactdetails extends AbstractDb{
	public function _construct()
		$this->_init("customer_contactdetails", 'id');

Step 2: Create a Datapatch

DataPatchInterface must implement three functions: apply(), getDependencies() and getAliases()

PatchVersionInterface must implement one function: getVersion()

apply() function is used to define your core logic for installing/upgrading data to the database.

getAliases() function defines aliases for the patch class. In this case, the class name could possibly change, and if it does, we should supply the old class-name here, so that it doesn’t get executed a second time.

getDependencies() function contain the class name of dependent patches. This functionality notifies Magento to execute the “patches”. We first define it here, before the patch script.

getVersion() function can return a version number of the patch. If the version number of the module is higher than the version we specify in our patch, then it will not get executed. If it is equal to or lower than the version here, it will be executed.


namespace DCKAP\Datapatch\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use DCKAP\Datapatch\Model\ContactdetailsFactory;
Use DCKAP\Datapatch\Model\ResourceModel\Contactdetails;
class AddData implements DataPatchInterface, PatchVersionInterface
	private $contactDetailsFactory;
	private $contactDetailsResource;
	private $moduleDataSetup;
	public function __construct(
		ContactdetailsFactory $contactDetailsFactory,
		Contactdetails $contactDetailsResource,
		ModuleDataSetupInterface $moduleDataSetup
		$this->contactDetailsFactory = $contactDetailsFactory;
		$this->contactDetailsResource = $contactDetailsResource;
	public function apply()
		//Install data row into contact_details table
	public static function getDependencies()
		return [];
	public static function getVersion()
		return '1.0.1';
	public function getAliases()

		return [];

Step 3: Run the Commands

php bin/magento setup:upgrade

php bin/magento cache:flush

After running the above commands, the data will be added to the customer_contactdetails table.

Data Patch - Run Command

The patch is run only once. You can find the patch id and name in the patch_list table.

Data Patch - Run Command

How to Re-execute the Patch?

  • Remove the value from the patch_list
  • Run bin/magento setup:upgrade

That’s it! We are done now. Hope this blog was helpful to you on learning how to apply the Data Patch on the Declarative Schema in Magento 2.3. If you have any queries, please feel free to leave a comment below.


  • Vijayashanthi M

    Vijayashanthi M is a passionate coder with an experience of 2.5 years in PHP, Yii 2, and Magento 2. She is always on the lookout for interesting things on the internet, and is an avid learner of new technologies. Her hobbies include dancing and listening to music.


Ready to drive online revenue

Get In Touch

Leave a Reply

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