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

app/code/DCKAP/Datapatch/etc/db_schema.xml

Run the command:

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

i.app/code/vendor/module/Model/Contactdetails

ii.app/code/vendor/module/Model/ResourceModel/Contactdetails

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.

app/code/DCKAP/Datapatch/Setup/Patch/Data/AddData.php

Step 3: Run the Commands

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.

Be the first to Know

Subscribe to our blog to get the latest articles directly to your inbox.

READY TO DRIVE SALES?

Ready to drive online revenue

Get In Touch

Leave a Reply

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