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

Fixed #6167 - Forced SELECT NEXTVAL to be run on master #6168

Closed
Closed
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
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);
// Use query to force master in MasterSlaveConnection
$this->_nextValue = (int) $conn->query($sql)->fetchColumn();
$this->_maxValue = $this->_nextValue + $this->_allocationSize;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Doctrine/Tests/Mocks/ConnectionMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\Mocks;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement;

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

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

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

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

Choose a reason for hiding this comment

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

This makes sense, but I'll probably have to change it to use the mocking framework to do this (on merge)

throw $this->_fetchOneException;
}

return $this->_fetchOneResult;
}

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

/**
* {@inheritdoc}
*/
Expand All @@ -112,6 +135,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 +165,14 @@ public function setLastInsertId($id)
$this->_lastInsertId = $id;
}

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

/**
* @return array
*/
Expand Down
10 changes: 7 additions & 3 deletions tests/Doctrine/Tests/ORM/Id/SequenceGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Tests\ORM\Id;

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

/**
Expand All @@ -23,17 +24,20 @@ protected function setUp()

public function testGeneration()
{
$this->_em->getConnection()->setFetchOneException(
new \RuntimeException('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);
$nextId = [[(int)($i / 10) * 10]];
$this->_em->getConnection()->setQueryResult(new StatementArrayMock($nextId));
}
$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());
}


}
}