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.
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.
<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.