Skip to content

Commit

Permalink
Merge pull request #1096 from greg0ire/address-master-slave-rename
Browse files Browse the repository at this point in the history
Address MasterSlaveConnection rename
  • Loading branch information
greg0ire committed Dec 5, 2020
2 parents e00f803 + dbbd7b3 commit af91502
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
14 changes: 10 additions & 4 deletions lib/Doctrine/Migrations/Configuration/Configuration.php
Expand Up @@ -10,6 +10,7 @@
use Doctrine\Common\EventArgs;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\Migrations\Configuration\Exception\MigrationsNamespaceRequired;
use Doctrine\Migrations\Configuration\Exception\ParameterIncompatibleWithFinder;
use Doctrine\Migrations\DependencyFactory;
Expand Down Expand Up @@ -351,13 +352,18 @@ public function generateVersionNumber(?DateTimeInterface $now = null) : string

/**
* Explicitely opens the database connection. This is done to play nice
* with DBAL's MasterSlaveConnection. Which, in some cases, connects to a
* follower when fetching the executed migrations. If a follower is lagging
* significantly behind that means the migrations system may see unexecuted
* migrations that were actually executed earlier.
* with DBAL's PrimaryReadReplicaConnection. Which, in some cases, connects
* to a follower when fetching the executed migrations. If a follower is
* lagging significantly behind that means the migrations system may see
* unexecuted migrations that were actually executed earlier.
*/
public function connect() : bool
{
if ($this->connection instanceof PrimaryReadReplicaConnection) {
return $this->connection->ensureConnectedToPrimary();
}

// Drop this if block when bumping to doctrine/dbal 2.11.0
if ($this->connection instanceof MasterSlaveConnection) {
return $this->connection->connect('master');
}
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Expand Up @@ -17,6 +17,8 @@ parameters:
- '~^Parameter #1 \$files of method Doctrine\\Migrations\\Finder\\Finder::loadMigrationClasses\(\) expects array<string>, array<int, string\|false> given\.\z~'
- '~^Class Doctrine\\Migrations\\Tests\\DoesNotExistAtAll not found\.\z~'
- '~^Call to method getVersion\(\) of deprecated class PackageVersions\\Versions\.$~'
# Drop when bumping to doctrine/dbal 2.11+
- '~^.*PrimaryReadReplicaConnection.*$~'

includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
Expand Down
Expand Up @@ -8,6 +8,7 @@
use DateTimeZone;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
use Doctrine\Migrations\Configuration\Configuration;
Expand All @@ -24,6 +25,7 @@
use Symfony\Component\Stopwatch\Stopwatch as SymfonyStopwatch;
use function array_keys;
use function call_user_func_array;
use function class_exists;
use function sprintf;
use function str_replace;

Expand Down Expand Up @@ -176,9 +178,40 @@ public function testMasterSlaveConnectionAlwaysConnectsToMaster() : void
{
$connection = $this->createMock(MasterSlaveConnection::class);

if (class_exists(PrimaryReadReplicaConnection::class)) {
$connection->expects(self::once())
->method('ensureConnectedToPrimary')
->willReturn(true);
} else {
$connection->expects(self::once())
->method('connect')
->with('master')
->willReturn(true);
}

$configuration = new Configuration($connection);
$configuration->setMigrationsNamespace(str_replace('\Version1Test', '', Version1Test::class));
$configuration->setMigrationsDirectory(__DIR__ . '/../Stub/Configuration/AutoloadVersions');

self::assertTrue($configuration->connect());
}

/**
* Connection is tested via the `getMigratedVersions` method which is the
* simplest to set up for.
*
* @see https://github.com/doctrine/migrations/issues/336
*/
public function testPrimaryReadReplicaConnectionAlwaysConnectsToMaster() : void
{
if (! class_exists(PrimaryReadReplicaConnection::class)) {
self::markTestSkipped('This test requires doctrine/dbal 2.11+');
}

$connection = $this->createMock(PrimaryReadReplicaConnection::class);

$connection->expects(self::once())
->method('connect')
->with('master')
->method('ensureConnectedToPrimary')
->willReturn(true);

$configuration = new Configuration($connection);
Expand Down

0 comments on commit af91502

Please sign in to comment.