Declarative Schema Data Patch Magento 2.3

Previous blog I have introduced about the declarative schema in Magento 2.3. In this blog, I am going to elaborate on the Data Patch. It is a new concept in the Declarative Schema approach. A data patch is a class that contains the data modification instruction.

The patch is defined in Vendor\Module\Setup\Patch\Data\Patch_Name.php file and implements \Magento\Framework\Setup\Patch\DataPatchInterface.

The declarative schema approach removes the version from the setup_module table, leaving only the Composer version. Therefore, you can create all-new patches and modules without specifying a setup_module version.

Create EAV Attribute using Declarative Schema in Magento 2.3

I stuck at the moments of creating product attributes using declarative schema. So I came to know that, however declarative schema script doesn’t apply for creating product attributes. For that, we need to implements patches that will create attributes.

We need to create a class that implements DataPatchInterface and instantiate the EavSetupFactory class within the constructor.

The naming conventiion for the patch file is, if we are trying to add a single attribute named custom_size, we name our class AddCustomSizeAttribute.

The following are the steps to create an eav attribute using a data patch in the declarative schema.

  • Create Setup\Patch\Data within you module. Create file AddCustomSizeAttribute.php inside the Data folder. Write down the following code in the file.

  • The DataPatchInterface needs to be implements the functions : apply, getDependencies and getAliases.

  • The apply function is where we will create our attributes. We just create an instance of the EavSetupFactory, passing in our moduleDataSetup object, and add our attribute

  • The getDependencies function expects an array of strings containing class names of dependencies. This tells Magento to execute the patches we define here first, before our setup script.
    This is how Magento controls the order of how patch scripts are executed.In this situation, we won’t have any dependencies, so we’ll just return an empty array.

  • The getAliases return the the path names array.Some patches with time can change their names. Changing name should not affect installation process, that’s why if we will change the name of the patch we will add alias here

  • The getVersion function return version number. If the version number of the module is higher than the version we specify here in code, the patch will not execute. If it is equal to or lower than the version here, it will execute.

Now when we run php bin/magento setup:upgrade to apply the updates, our data patch executes and the attribute is created. For all patches which are successfully executed, Magento inserts a record into the patch_list database table with the value of the patch_name field being the value of our patch class.

Removing the value from the patch_list table will cause the patch to re-execute when running php bin/magento setup:upgrade again, so this approach can be extremely useful when first creating and debugging patch scripts. If you find this blog useful then comment and share this blog.

By | 2019-07-30T17:54:47+00:00 July 30th, 2019|Magento 2|

Leave A Comment