With version 2.3, aside from InstallData and UpdateData classes, Magento introduced data patches, a new way for modules to apply data changes. In this blog, we will show you how to write and apply data patches in Magento 2.

1. Create a data patch

A data patch class is stored under <Vendor>/<Module_Name>/Setup/Patch/Data/<Patch_Name>.php  and implements \Magento\Framework\Setup\Patch\DataPatchInterface.

In this example, we will create the file as Magenest/DataPatch/Setup/Patch/Data/AddEnableColorAttribute.php with the following content:

<?php
namespace Magenest\DataPatch\Setup\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddEnableColorAttribute implements DataPatchInterface
{
   /** @var ModuleDataSetupInterface */
   private $moduleDataSetup;

   /** @var EavSetupFactory */
   private $eavSetupFactory;

   /**
    * @param ModuleDataSetupInterface $moduleDataSetup
    * @param EavSetupFactory $eavSetupFactory
    */
   public function __construct(
       ModuleDataSetupInterface $moduleDataSetup,
       EavSetupFactory $eavSetupFactory
   ) {
       $this->moduleDataSetup = $moduleDataSetup;
       $this->eavSetupFactory = $eavSetupFactory;
   }

   /**
    * {@inheritdoc}
    */
   public function apply()
   {
       /** @var EavSetup $eavSetup */
       $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

       $eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'enable_color', [
           'type' => 'int',
           'backend' => '',
           'frontend' => '',
           'label' => 'Enable Color',
           'input' => 'select',
           'class' => '',
           'source' => \Magento\Catalog\Model\Product\Attribute\Source\Boolean::class,
           'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
           'visible' => true,
           'required' => true,
           'user_defined' => false,
           'default' => '',
           'searchable' => false,
           'filterable' => false,
           'comparable' => false,
           'visible_on_front' => false,
           'used_in_product_listing' => true,
           'unique' => false,
       ]);
   }

   /**
    * {@inheritdoc}
    */
   public static function getDependencies()
   {
       return [];
   }

   /**
    * {@inheritdoc}
    */
   public function getAliases()
   {
       return [];
   }

   /**
   * {@inheritdoc}
   */
   public static function getVersion()
   {
      return '2.0.0';
   }
}

In this class, we will create an EAV attribute for product entities named 'enable_color' using EavSetupFactory. There are several core functions inside a data patch class:

  • apply(): The main execution function. Data changes are written here.
  • getDependencies(): A patch may have several dependencies. When we place our dependencies, the function returns an array of class names and Magento will execute the patches we defined here first. However, some of your patches can be independent and can be installed in any order. In the AddCodeAttribution.php file, we didn't provide any dependencies, so we returned an empty array.
  • getAliases(): The place to identify the aliases for the patch. If a patch has changed its name and you don’t want it executed twice, its aliases can be returned in this function as an array. Our patch has no aliases so we will return an empty array.
  • getVersion(): The function returns a string with a version number. If the module version number in our database is higher than the version we specified here in our file, the patch will not execute. If it is equal or lower than the version here, it will perform. Here we return 2.0.0, this means the data patch will be executed only if the module’s data version is 2.0.0 or older.

2. Apply a data patch

To apply the data patch, lets run bin/magento setup:upgrade. Our patch will be executed and the attribute will be created. To see the new attribute, clear cache with bin/magento cache:clean and edit any products from backend. Here’s the result:

Apply data patch in Magento 2: New attribute created

The patch will only be applied once. When the patch is successfully executed, Magento will insert a record into the patch_list table in the database with the patch_name is the full name path of our patch class.

Apply data patch in Magento 2: Record patch

Removing the record from the patch_list table will allow the patch to be executed again when running bin/magento setup:upgrade, so this method can be quite helpful when creating and removing patch scripts.

READ MORE. Magento 2.3.4 Release - Magenest Blog

The above is a simple example for you to apply the data patch in magento version 2.3. We hope it will help you, if you have a comment, please comment below so we can better complement this article and the following articles. Thank you all and good luck.