Skip to content
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

[11.x] Fix database reconnecting logic #53693

Merged
merged 2 commits into from
Nov 29, 2024
Merged

Conversation

stancl
Copy link
Contributor

@stancl stancl commented Nov 28, 2024

Fixes #53692.

Sending this as a draft for convenience so that this can move forward once I get confirmation that my idea of the fix is correct.

Before this is merged, a test should be added verifying that connections are now repaired by the reconnect() call.

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@stancl
Copy link
Contributor Author

stancl commented Nov 28, 2024

The only tests that seem to relate to the reconnecting logic are in DatabaseConnectionTest and all seem to have transaction-specific logic. Additionally, custom reconnectors are set for the created connections and pretty much everything in that test case uses mocks.

To test that the default reconnector re-creates the connection in DatabaseManager as well as heals the Connection itself, I think avoiding mocks would be preferable but I'm not sure how you tend to test these sorts of things.

The test I'd like to add would be essentially something like this (just copied this from a codebase where I'm using Pest):

config(['database.connections.testbench' => [
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'username' => 'root',
    'password' => 'invalid-credentials',
    'database' => 'forge',
    'prefix' => '',
]]);

$connection = DB::connection('testbench');

expect($connection->getRawPdo())->not()->toBeNull();

DB::purge('testbench');

expect($connection->getRawPdo())->toBeNull();

// normally triggered by a query but we're not establishing a connection to a real server here
$connection->reconnectIfMissingConnection();

// fails if I revert my patch
expect($connection->getRawPdo())->not()->toBeNull();

DB::purge('testbench');

@stancl stancl marked this pull request as ready for review November 28, 2024 23:18
@stancl
Copy link
Contributor Author

stancl commented Nov 28, 2024

Marking as ready for review, please let me know how I should add the test or feel free to just push it.

@taylorotwell taylorotwell merged commit 8848cc8 into laravel:11.x Nov 29, 2024
38 checks passed
@stancl
Copy link
Contributor Author

stancl commented Nov 29, 2024

@taylorotwell Do you want to add the regression test in some form?

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

Successfully merging this pull request may close these issues.

Database reconnecting logic broken when using DB::purge()
2 participants