Skip to content

[11.x] Fix database reconnecting logic#53693

Merged
taylorotwell merged 2 commits into
laravel:11.xfrom
stancl:patch-3
Nov 29, 2024
Merged

[11.x] Fix database reconnecting logic#53693
taylorotwell merged 2 commits into
laravel:11.xfrom
stancl:patch-3

Conversation

@stancl

@stancl stancl commented Nov 28, 2024

Copy link
Copy Markdown
Contributor

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.

@github-actions

Copy link
Copy Markdown

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

stancl commented Nov 28, 2024

Copy link
Copy Markdown
Contributor Author

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

stancl commented Nov 28, 2024

Copy link
Copy Markdown
Contributor Author

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
@stancl

stancl commented Nov 29, 2024

Copy link
Copy Markdown
Contributor Author

@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