Magento – Magento 2: What are `resources.xml` Files Used For

-setupdatabasemagento2migration

In Magento 2, one of the XML configuration files a module may have is resources.xml file. For example, the sales module has one

#File: vendor/magento/module-sales/etc/resources.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/resources.xsd">
    <resource name="sales_setup" extends="core_setup" />
    <resource name="sales" extends="core" />
</config>

Does anyone know what these files are used for in Magento 2? They appear to be the old Magento 1 top level <resources/> node. However, since Magento no longer uses Setup Resource Model/Migrations (instead Magento 2 uses classes in Setup/InstallSchema.php, Setup/InstallData.php, Setup/UpgradeSchema.php, Setup/UpgradeData.php for install/migration-like scripts), its not 100% clear what the sales_setup resource is for.

For the non setup resource, is this just another way to add a database connection class with different credentials/connection string information? Or something else?

Best Answer

I think the resources is relating to the actual database connection. M1 in the past had something like this:

   <resources>
        <backup_setup>
            <setup>
                <module>Mage_Backup</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </backup_setup>
        <backup_write>
            <connection>
                <use>core_write</use>
            </connection>
        </backup_write>
        <backup_read>
            <connection>
                <use>core_read</use>
            </connection>
        </backup_read>
    </resources>

so I would say the resources file is the successor to what the connection was.

Not sure that the _setup node provides much value above the other line as we have this code in

lib/internal/Magento/Framework/App/ResourceConnection/Config.php

public function getConnectionName($resourceName)
{
    $connectionName = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION;

    $resourceName = preg_replace("/_setup$/", '', $resourceName);

and also in setup/src/Magento/Setup/Module/Setup/ResourceConfig.php

class ResourceConfig implements \Magento\Framework\App\ResourceConnection\ConfigInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConnectionName($resourceName)
    {
        return \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION;
    }
}

I had a quick look through some of the enterprise modules to see if that sheds more light on the use of the resources.xml file (as multiple dbs is apparently an M2 Enterprise feature) but the only use that I have come across so far also only uses extends from core.

My hunch is that one would need to create additional db connections in app/etc/env.php which then allows you to use those values to override the ones shipped in the default resource.xml files (ie just the 1 default connection).