Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix(Database): Ensure read/write settings correctly inherit shared config #18744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from

Conversation

k-pon-jp
Copy link

Description of Changes

Currently, in the Database\Connection::createDrivers() method, if a configuration array is provided for the read or write roles, it does not correctly inherit the values from the shared (default) configuration.

For instance, if a user only specifies the port for the read connection, other essential parameters like host, username, and database are not carried over from the shared config. This leads to a connection failure, contrary to the behavior described in the documentation.

This issue stems from PHP's operator precedence, where the array union operator (+) is evaluated before the null coalescing operator (??).

Proposed Changes

This PR corrects this behavior by adding parentheses to the configuration merging logic.

The line is changed from:
$readConfig = $config['read'] ?? [] + $sharedConfig;

To:
$readConfig = ($config['read'] ?? []) + $sharedConfig;

This small but critical change ensures that the read or write specific configuration is resolved first, and then the shared configuration is correctly applied as a fallback for any keys that were not specified.

With this fix, users can define only the parameters they wish to override in the read and write configurations, making the feature work as intended and documented.

@LordSimal
Copy link
Contributor

LordSimal commented Jun 16, 2025

A test, which makes sure the config is created as expected, would be highly appreciated 👍🏻

Also you can fix the code style issues if you execute composer cs-fix

@LordSimal
Copy link
Contributor

I just checked the codebase and the Connection class is the only place, where we had ?? [] + $ present, so this bug isn't present anywhere else.

@k-pon-jp k-pon-jp force-pushed the fix/connection-config-issue branch from fc95d2e to f86d780 Compare June 16, 2025 14:19
@k-pon-jp
Copy link
Author

Thanks for the review! I've added a test to cover the fix and also ran cs-fix to correct the code style.

@LordSimal LordSimal requested a review from othercorey June 16, 2025 14:32
@othercorey
Copy link
Member

Surprised we didn't use any shared config values in other tests.

@othercorey othercorey added this to the 5.2.5 milestone Jun 16, 2025
@othercorey
Copy link
Member

Windows does not include the mysql driver so the test is failing.

public function testRoleConfigInheritance(): void
{
$config = [
'driver' => 'Cake\Database\Driver\Mysql',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to sqlite to see if it passes on windows and add this skip line at the start of the test:

$this->skipIf(!extension_loaded('pdo_sqlite'), 'Skipping as SQLite extension is missing');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants