Skip to content

Commit

Permalink
Merge branch 'fix/#6168-#6167-force-nextval-selection-on-the-master-s…
Browse files Browse the repository at this point in the history
…erver'

Close #6167
Close #6168
  • Loading branch information
Ocramius committed Jun 21, 2017
2 parents 1e3bf65 + a97c265 commit 07a9b10
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 20 deletions.
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Id/SequenceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function generate(EntityManager $em, $entity)
$conn = $em->getConnection();
$sql = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName);

$this->_nextValue = (int) $conn->fetchColumn($sql);
// Using `query` to force usage of the master server in MasterSlaveConnection
$this->_nextValue = (int) $conn->query($sql)->fetchColumn();
$this->_maxValue = $this->_nextValue + $this->_allocationSize;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/Doctrine/Tests/Mocks/ConnectionMock.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

namespace Doctrine\Tests\Mocks;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement;

/**
* Mock class for Connection.
Expand All @@ -13,6 +15,16 @@ class ConnectionMock extends Connection
*/
private $_fetchOneResult;

/**
* @var \Exception|null
*/
private $_fetchOneException;

/**
* @var Statement|null
*/
private $_queryResult;

/**
* @var DatabasePlatformMock
*/
Expand Down Expand Up @@ -86,9 +98,21 @@ public function lastInsertId($seqName = null)
*/
public function fetchColumn($statement, array $params = [], $colnum = 0, array $types = [])
{
if (null !== $this->_fetchOneException) {
throw $this->_fetchOneException;
}

return $this->_fetchOneResult;
}

/**
* {@inheritdoc}
*/
public function query() : Statement
{
return $this->_queryResult;
}

/**
* {@inheritdoc}
*/
Expand All @@ -112,6 +136,16 @@ public function setFetchOneResult($fetchOneResult)
$this->_fetchOneResult = $fetchOneResult;
}

/**
* @param \Exception|null $exception
*
* @return void
*/
public function setFetchOneException(\Exception $exception = null)
{
$this->_fetchOneException = $exception;
}

/**
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
*
Expand All @@ -132,6 +166,14 @@ public function setLastInsertId($id)
$this->_lastInsertId = $id;
}

/**
* @param Statement $result
*/
public function setQueryResult(Statement $result)
{
$this->_queryResult = $result;
}

/**
* @return array
*/
Expand Down
58 changes: 39 additions & 19 deletions tests/Doctrine/Tests/ORM/Id/SequenceGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,58 @@

namespace Doctrine\Tests\ORM\Id;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\SequenceGenerator;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\StatementArrayMock;
use Doctrine\Tests\OrmTestCase;

/**
* Description of SequenceGeneratorTest
*
* @author robo
*/
class SequenceGeneratorTest extends OrmTestCase
{
private $_em;
private $_seqGen;

protected function setUp()
/**
* @var EntityManager
*/
private $entityManager;

/**
* @var SequenceGenerator
*/
private $sequenceGenerator;

/**
* @var ConnectionMock
*/
private $connection;

protected function setUp() : void
{
$this->_em = $this->_getTestEntityManager();
$this->_seqGen = new SequenceGenerator('seq', 10);
parent::setUp();

$this->entityManager = $this->_getTestEntityManager();
$this->sequenceGenerator = new SequenceGenerator('seq', 10);
$this->connection = $this->entityManager->getConnection();

self::assertInstanceOf(ConnectionMock::class, $this->connection);
}

public function testGeneration()
public function testGeneration() : void
{
for ($i=0; $i < 42; ++$i) {
$this->connection->setFetchOneException(new \BadMethodCallException(
'Fetch* method used. Query method should be used instead, '
. 'as NEXTVAL should be run on a master server in master-slave setup.'
));

for ($i = 0; $i < 42; ++$i) {
if ($i % 10 == 0) {
$this->_em->getConnection()->setFetchOneResult((int)($i / 10) * 10);
$this->connection->setQueryResult(new StatementArrayMock([[(int)($i / 10) * 10]]));
}
$id = $this->_seqGen->generate($this->_em, null);
$this->assertEquals($i, $id);
$this->assertEquals((int)($i / 10) * 10 + 10, $this->_seqGen->getCurrentMaxValue());
$this->assertEquals($i + 1, $this->_seqGen->getNextValue());
}

$id = $this->sequenceGenerator->generate($this->entityManager, null);

self::assertSame($i, $id);
self::assertSame((int)($i / 10) * 10 + 10, $this->sequenceGenerator->getCurrentMaxValue());
self::assertSame($i + 1, $this->sequenceGenerator->getNextValue());
}
}
}

0 comments on commit 07a9b10

Please sign in to comment.