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

Explicitly Call Connection::connect in Configuration #479

Merged
merged 1 commit into from
Jul 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace Doctrine\DBAL\Migrations\Configuration;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Migrations\Finder\MigrationDeepFinderInterface;
use Doctrine\DBAL\Migrations\MigrationException;
use Doctrine\DBAL\Migrations\OutputWriter;
Expand Down Expand Up @@ -495,6 +496,7 @@ public function hasVersion($version)
*/
public function hasVersionMigrated(Version $version)
{
$this->connect();
$this->createMigrationTable();

$version = $this->connection->fetchColumn(
Expand All @@ -512,6 +514,7 @@ public function hasVersionMigrated(Version $version)
*/
public function getMigratedVersions()
{
$this->connect();
$this->createMigrationTable();

$ret = $this->connection->fetchAll("SELECT " . $this->migrationsColumnName . " FROM " . $this->migrationsTableName);
Expand Down Expand Up @@ -546,6 +549,7 @@ public function getAvailableVersions()
*/
public function getCurrentVersion()
{
$this->connect();
$this->createMigrationTable();

if (empty($this->migrations)) {
Expand Down Expand Up @@ -662,6 +666,7 @@ public function resolveVersionAlias($alias)
*/
public function getNumberOfExecutedMigrations()
{
$this->connect();
$this->createMigrationTable();

$result = $this->connection->fetchColumn("SELECT COUNT(" . $this->migrationsColumnName . ") FROM " . $this->migrationsTableName);
Expand Down Expand Up @@ -713,6 +718,7 @@ public function createMigrationTable()
return false;
}

$this->connect();
if ($this->connection->getSchemaManager()->tablesExist([$this->migrationsTableName])) {
$this->migrationTableCreated = true;

Expand Down Expand Up @@ -815,6 +821,24 @@ public function generateVersionNumber(\DateTimeInterface $now=null)
return $now->format(self::VERSION_FORMAT);
}

/**
* 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.
*
* @return bool The same value returned from the `connect` method
*/
protected function connect()
{
if ($this->connection instanceof MasterSlaveConnection) {
return $this->connection->connect('master');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisguitarguy This does assume that there is always a connection named master in a master slave connection setup right ?
If so, is it the case or should we make the name of the master connection configurable ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. MasterSlaveConection only accepts master and slave as arguments to connect, so we should be okay.

}

return $this->connection->connect();
}

/**
* @throws MigrationException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Doctrine\DBAL\Migrations\Tests\Configuration;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Migration;
use Doctrine\DBAL\Migrations\MigrationException;
Expand Down Expand Up @@ -171,6 +174,41 @@ public function testGenerateVersionNumberWithoutNowUsesTheCurrentTime()
$this->assertRegExp(sprintf('/^%s\d{2}$/', $now->format('YmdHi')), $version);
}

/**
* 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 testMasterSlaveConnectionAlwaysConnectsToMaster()
{
$sm = $this->getMockForAbstractClass(AbstractSchemaManager::class, [], '', false, true, true, ['tablesExist']);
$sm->expects($this->once())
->method('tablesExist')
->willReturn(true);
$conn = $this->getMockBuilder(MasterSlaveConnection::class)
->disableOriginalConstructor()
->getMock();
$conn->expects($this->atLeastOnce())
->method('connect')
->with('master')
->willReturn(true);
$conn->expects($this->any())
->method('getSchemaManager')
->willReturn($sm);
$conn->expects($this->once())
->method('fetchAll')
->willReturn([
['version' => '2016070600000'],
]);

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

$config->getMigratedVersions();
}

public function methodsThatNeedsVersionsLoadedWithAlreadyMigratedMigrations()
{
return [
Expand Down