3

I'd like to know if I can override a CLI, specifically the bin/magento setup:upgrade.

I want to override the class Magento\Setup\Validator\DbValidator, the method checkDatabasePrivileges.

1 Answer 1

1

There are two ways to go about this, one is to replace the Magento\Setup\Validator\DbValidator system wide with your customized version. The other is to only change it for the bin/magento setup:upgrade command.

I assume you prefer the send one. To archieve this, first create a class in your custom module which extends Magento\Setup\Validator\DbValidator and override the checkDatabasePrivileges method to change the implementation to suit your needs. Then you need to tell Magento to use this class instead of the default Magento\Setup\Validator\DbValidator. For this create a etc/di.xml file in your custom module with the following contents :

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Setup\Model\Installer">
    <arguments>
        <argument name="dbValidator" xsi:type="object">Custom\Module\Path\To\Class</argument>
    </arguments>
</type>

Replace Custom\Module\Path\To\Class with the custom class you created and run. bin/magento setup:di:compile

Your class should now be used instead of the default one.

The setup:upgrade command uses the Magento\Setup\Model\Installer class which uses the Magento\Setup\Validator\DbValidator. We are telling Magento that everytime the Magento\Setup\Model\Installer class is used in the system, we want it to use our custom class instead of the original Magento\Setup\Validator\DbValidator.

3
  • It didn't work because is overriding the class in vendor folder, so doesn't affect my setup:upgrade that uses the same class in /setup/src/Magento/Setup/Validator/DBValidator.php, I'd like to know how to override the /setup/src/... class. Commented Mar 1, 2018 at 18:54
  • It doesn't matter where the file is located, the override that we did in the di.xml works based on the class name. There must be another thing wrong then. If you want you can try <preference for="Magento\Setup\Validator\DbValidator" type="Custom\Module\Path\To\Class" /> to replace the class system wide. Also confirm that the module were you are adding the di.xml is enabled and working, the di.xml is located directly under etc as opposed to etc/frontend.
    – user4864
    Commented Mar 2, 2018 at 8:56
  • How do you do it where DbValidator is overridden with composer?
    – CarComp
    Commented Jan 10, 2020 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.