-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
base: 5.x
Are you sure you want to change the base?
Conversation
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 |
I just checked the codebase and the Connection class is the only place, where we had |
fc95d2e
to
f86d780
Compare
Thanks for the review! I've added a test to cover the fix and also ran cs-fix to correct the code style. |
Surprised we didn't use any shared config values in other tests. |
Windows does not include the mysql driver so the test is failing. |
public function testRoleConfigInheritance(): void | ||
{ | ||
$config = [ | ||
'driver' => 'Cake\Database\Driver\Mysql', |
There was a problem hiding this comment.
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');
Description of Changes
Currently, in the
Database\Connection::createDrivers()
method, if a configuration array is provided for theread
orwrite
roles, it does not correctly inherit the values from the shared (default) configuration.For instance, if a user only specifies the
port
for theread
connection, other essential parameters likehost
,username
, anddatabase
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
orwrite
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
andwrite
configurations, making the feature work as intended and documented.