-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
fix(db): Prevent two connections for single node databases #45013
Merged
ChristophWurst
merged 1 commit into
master
from
fix/db/two-primary-connections-transaction-isolation-level
May 28, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Test\DB; | ||
|
||
use Doctrine\DBAL\Configuration; | ||
use Doctrine\DBAL\Driver; | ||
use Doctrine\DBAL\Driver\Connection as DriverConnection; | ||
use Doctrine\DBAL\Platforms\MySQLPlatform; | ||
use OC\DB\Adapter; | ||
use OC\DB\Connection; | ||
use Test\TestCase; | ||
|
||
/** | ||
* @group DB | ||
*/ | ||
class ConnectionTest extends TestCase { | ||
|
||
public function testSingleNodeConnectsToPrimaryOnly(): void { | ||
$connectionParams = [ | ||
'user' => 'test', | ||
'password' => 'topsecret', | ||
'host' => 'test', | ||
]; | ||
$adapter = $this->createMock(Adapter::class); | ||
$driver = $this->createMock(Driver::class); | ||
$configuration = $this->createMock(Configuration::class); | ||
$connection = $this->getMockBuilder(Connection::class) | ||
->onlyMethods(['connectTo']) | ||
->setConstructorArgs([ | ||
[ | ||
'adapter' => $adapter, | ||
'platform' => new MySQLPlatform(), | ||
'tablePrefix' => 'nctest', | ||
'primary' => $connectionParams, | ||
'replica' => [ | ||
$connectionParams, | ||
], | ||
], | ||
$driver, | ||
$configuration, | ||
]) | ||
->getMock(); | ||
$driverConnection = $this->createMock(DriverConnection::class); | ||
$connection->expects(self::once()) | ||
->method('connectTo') | ||
->with('primary') | ||
->willReturn($driverConnection); | ||
|
||
$connection->ensureConnectedToReplica(); | ||
$connection->ensureConnectedToPrimary(); | ||
$connection->ensureConnectedToReplica(); | ||
} | ||
|
||
public function testClusterConnectsToPrimaryAndReplica(): void { | ||
$connectionParamsPrimary = [ | ||
'user' => 'test', | ||
'password' => 'topsecret', | ||
'host' => 'testprimary', | ||
]; | ||
$connectionParamsReplica = [ | ||
'user' => 'test', | ||
'password' => 'topsecret', | ||
'host' => 'testreplica', | ||
]; | ||
$adapter = $this->createMock(Adapter::class); | ||
$driver = $this->createMock(Driver::class); | ||
$configuration = $this->createMock(Configuration::class); | ||
$connection = $this->getMockBuilder(Connection::class) | ||
->onlyMethods(['connectTo']) | ||
->setConstructorArgs([ | ||
[ | ||
'adapter' => $adapter, | ||
'platform' => new MySQLPlatform(), | ||
'tablePrefix' => 'nctest', | ||
'primary' => $connectionParamsPrimary, | ||
'replica' => [ | ||
$connectionParamsReplica, | ||
], | ||
], | ||
$driver, | ||
$configuration, | ||
]) | ||
->getMock(); | ||
$driverConnection = $this->createMock(DriverConnection::class); | ||
$connection->expects(self::exactly(2)) | ||
->method('connectTo') | ||
->willReturn($driverConnection); | ||
|
||
$connection->ensureConnectedToReplica(); | ||
$connection->ensureConnectedToPrimary(); | ||
$connection->ensureConnectedToReplica(); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You apparently need to mock
detectDatabasePlatform
https://github.com/nextcloud/server/actions/runs/9265386150/job/25487375754?pr=45013
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.
Fixed by passing an actual platform. Thanks!