Skip to main content
Blog eCommerce

Applying a Data Patch on the Declarative Schema in Magento 2.3

Vijayashanthi M
December 5, 2019 |

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



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 //Setup/Patch/Data/.php file, and implements MagentoSetupModelPatchDataPatchInterface.

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


_init("customer_contactdetails", 'id');

Step 2: Create a Datapatch


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.


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.

More posts by Vijayashanthi M