According to the Develop data and schema patches guide for Magento 2 developers, data patches can also implement PatchRevertabieinterface to provide rollback functionality for their changes. The revert() method contains the instructions to undo the data modifications made by the patch. To ensure that the product attribute is removed when the module is uninstalled, the developer should make the data patch implement PatchRevertabieinterface and implement the revert method to remove the product attribute using EavSetupFactory or AttributeRepositoryInterface. Verified References: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/data-patches.html
According to Adobe Commerce (Magento) best practices, when creating modules that add database schema changes or data through Data Patches, it's crucial to consider the reversibility of these changes for module uninstallation. Here's how each option relates to this practice:
Option A: Adding an Uninstall.php file that extends \Magento\Framework\Setup\UninstallInterface is indeed a method to handle module uninstallation in Magento. This interface requires the implementation of an uninstall method where you could write the logic to remove the product attribute. However, this approach is more commonly used for broader setup/teardown operations beyond simple data patches. The official Magento documentation discusses this approach under module uninstallation:
But for data patches specifically, the recommended approach is different.
Option B: Adding instructions in the README.md file for manual removal by merchants or developers is not a best practice for module management in Magento. This approach relies on human action which can be error-prone and inconsistent, especially in a production environment. Magento encourages automated processes for module lifecycle management to ensure reliability and consistency.
Option C: This is the correct and recommended approach according to Magento best practices for data patches. By implementing \Magento\Framework\Setup\Patch\PatchRevertableInterface in your Data Patch class, you ensure that the patch can be reverted. This interface requires you to implement a revert method, which should contain the logic to remove the changes made by the patch, in this case, the product attribute. Here's how it works:
When creating a Data Patch, you extend \Magento\Framework\Setup\Patch\DataPatchInterface. To make it reversible, you also implement \Magento\Framework\Setup\Patch\PatchRevertableInterface.
In the revert method, you would write the code to remove the product attribute that was added by the patch.
This approach ensures that your module's changes can be automatically undone if the module is uninstalled, maintaining the integrity of the Magento installation. Here's a reference from Magento documentation:
Example implementation:
php
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class AddProductAttribute implements DataPatchInterface, PatchRevertableInterface
{
private $eavSetupFactory;
private $moduleDataSetup;
public function __construct(
EavSetupFactory $eavSetupFactory,
ModuleDataSetupInterface $moduleDataSetup
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->moduleDataSetup = $moduleDataSetup;
}
public function apply()
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'custom_attribute',
[
'type' => 'varchar',
'label' => 'Custom Attribute',
'input' => 'text',
'required' => false,
'sort_order' => 100,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'group' => 'General',
]
);
}
public function revert()
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'custom_attribute');
}
public static function getDependencies()
{
return [];
}
public function getAliases()
{
return [];
}
}
This ensures that if the module is uninstalled, the product attribute will be automatically removed, adhering to Magento's modular and reversible development practices.